import { API_BASE } from "./config"; export class ApiClientError extends Error { status: number; code?: string; constructor(status: number, message: string, code?: string) { super(message); this.name = "ApiClientError"; this.status = status; this.code = code; } } async function request( method: string, url: string, body?: unknown, ): Promise { const headers: Record = {}; const token = typeof window !== "undefined" ? localStorage.getItem("token") : null; if (token) { headers["Authorization"] = `Bearer ${token}`; } if (body !== undefined) { headers["Content-Type"] = "application/json"; } const res = await fetch(`${API_BASE}${url}`, { method, headers, body: body !== undefined ? JSON.stringify(body) : undefined, }); if (!res.ok) { let message = `HTTP ${res.status}`; let code: string | undefined; try { const json = await res.json(); message = json.message || json.error || json.detail || message; code = json.code; } catch {} throw new ApiClientError(res.status, message, code); } if (res.status === 204) return undefined as T; const json = await res.json(); return json as T; } export const apiClient = { get: (url: string) => request("GET", url), post: (url: string, body?: unknown) => request("POST", url, body), patch: (url: string, body?: unknown) => request("PATCH", url, body), put: (url: string, body?: unknown) => request("PUT", url, body), delete: (url: string) => request("DELETE", url), }; export const API_BASE_URL = API_BASE;