const BASE = import.meta.env.VITE_BACKEND_URL ?? "http://localhost:8787"; export async function apiFetch(path: string, init?: RequestInit): Promise { // Only declare a JSON content-type when we actually send a body. Body-less // POSTs (resume, close) would otherwise make Fastify reject the empty body // with FST_ERR_CTP_EMPTY_JSON_BODY (400). const headers: Record = { ...(init?.headers as Record | undefined), }; if (init?.body != null && !("content-type" in headers) && !("Content-Type" in headers)) { headers["content-type"] = "application/json"; } const res = await fetch(`${BASE}${path}`, { ...init, headers }); if (!res.ok) throw new Error(`${res.status} ${await res.text().catch(() => "")}`); return res.status === 204 ? (undefined as T) : ((await res.json()) as T); } export { BASE };