const BASE_URL = import.meta.env.VITE_API_URL ?? "http://localhost:3002"; function getToken() { return localStorage.getItem("access_token"); } async function request(path: string, options: RequestInit = {}): Promise { const token = getToken(); const res = await fetch(`${BASE_URL}${path}`, { ...options, headers: { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}), ...(options.headers ?? {}), }, }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error((err as { message?: string }).message ?? `HTTP ${res.status}`); } return res.json() as Promise; } export const api = { get: (path: string) => request(path), post: (path: string, body: unknown) => request(path, { method: "POST", body: JSON.stringify(body) }), patch: (path: string, body: unknown) => request(path, { method: "PATCH", body: JSON.stringify(body) }), delete: (path: string) => request(path, { method: "DELETE" }), };