/** * Schulkalender API client. Re-uses the same /api/school/* proxy + the JWT * helper from stundenplan so we don't fork the auth flow. */ import { getStundenplanToken } from '@/lib/stundenplan/api' import type { PublicEvent, SchoolCalendarConfig, UpsertSchoolCalendarConfig, } from '@/app/schulkalender/types' async function apiFetch(endpoint: string, options: RequestInit = {}): Promise { const headers: Record = { 'Content-Type': 'application/json', ...(options.headers as Record | undefined), } const token = getStundenplanToken() if (token) headers['Authorization'] = `Bearer ${token}` const res = await fetch(`/api/school${endpoint}`, { ...options, headers }) if (!res.ok) { const err = await res.json().catch(() => ({ error: 'Unknown error' })) throw new Error(err.error || err.detail || `HTTP ${res.status}`) } if (res.status === 204) return undefined as T return res.json() } export const calendarApi = { listHolidays: (region: string, from: string, to: string) => apiFetch(`/calendar/holidays?region=${encodeURIComponent(region)}&from=${from}&to=${to}`), getConfig: () => apiFetch('/calendar/config'), upsertConfig: (data: UpsertSchoolCalendarConfig) => apiFetch('/calendar/config', { method: 'PUT', body: JSON.stringify(data) }), }