const API_BASE = import.meta.env.VITE_API_URL ?? ""; async function request(path: string, init?: RequestInit): Promise { const headers = new Headers(init?.headers); if (!(init?.body instanceof FormData) && !headers.has("Content-Type")) { headers.set("Content-Type", "application/json"); } const response = await fetch(`${API_BASE}${path}`, { ...init, credentials: "include", headers }); if (!response.ok) { const body = await response.json().catch(() => ({ error: "Request failed" })); throw new Error(body.error ?? "Request failed"); } if (response.status === 204) { return undefined as T; } return response.json() as Promise; } export const api = { get: (path: string) => request(path), post: (path: string, body?: unknown) => request(path, { method: "POST", body: body instanceof FormData ? body : body ? JSON.stringify(body) : undefined }), put: (path: string, body: unknown) => request(path, { method: "PUT", body: JSON.stringify(body) }), delete: (path: string) => request(path, { method: "DELETE" }) };