import { SitePagesContent } from '@/lib/site-pages' import { FormData, PolicyDefaults, PrivacyPolicy } from '@/types/privacy-policy' const API_BASE_URL = '/api' /** Fehler einer API-Antwort inkl. der Feld-Validierungsmeldungen des Servers. */ export class ApiError extends Error { details: string[] constructor(message: string, details: string[] = []) { super(message) this.name = 'ApiError' this.details = details } } export class ApiClient { private async request(endpoint: string, options?: RequestInit) { const response = await fetch(`${API_BASE_URL}${endpoint}`, { headers: { 'Content-Type': 'application/json', ...options?.headers, }, ...options, }) if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Netzwerkfehler' })) throw new ApiError( error.error || 'Die Anfrage ist fehlgeschlagen', Array.isArray(error.details) ? error.details : [] ) } return response.json() } async getAllPolicies(admin = false): Promise { const query = admin ? '?admin=true' : '' return this.request(`/privacy-policies${query}`) } async getPolicy(id: number): Promise { return this.request(`/privacy-policies/${id}`) } async createPolicy(data: FormData): Promise { return this.request('/privacy-policies', { method: 'POST', body: JSON.stringify(data), }) } async updatePolicy(id: number, data: FormData): Promise { return this.request(`/privacy-policies/${id}`, { method: 'PUT', body: JSON.stringify(data), }) } async deletePolicy(id: number): Promise<{ message: string }> { return this.request(`/privacy-policies/${id}`, { method: 'DELETE', }) } /** Liest die zentral gepflegten Standardtexte. */ async getPolicyDefaults(): Promise { return this.request('/policy-defaults') } /** Speichert die zentral gepflegten Standardtexte (nur Admin). */ async updatePolicyDefaults(data: Omit): Promise { return this.request('/policy-defaults', { method: 'PUT', body: JSON.stringify(data), }) } /** Liest die Inhalte von Impressum, Datenschutzerklärung und Barrierefreiheit. */ async getSitePages(): Promise { return this.request('/site-pages') } /** Speichert die Inhalte der rechtlichen Seiten (nur Admin). */ async updateSitePages(data: SitePagesContent): Promise { return this.request('/site-pages', { method: 'PUT', body: JSON.stringify(data), }) } } export const apiClient = new ApiClient()