const BASE_URL = "/api"; async function request( method: string, endpoint: string, data?: unknown, ): Promise { const options: RequestInit = { method, headers: { "Content-Type": "application/json" }, }; if (data !== undefined) { options.body = JSON.stringify(data); } const response = await fetch(`${BASE_URL}${endpoint}`, options); if (!response.ok) { const text = await response.text().catch(() => response.statusText); throw new Error(text || `${method} ${endpoint} failed (${response.status})`); } if (response.status === 204) { return undefined as T; } return response.json() as Promise; } export const api = { get: (endpoint: string) => request("GET", endpoint), post: (endpoint: string, data?: unknown) => request("POST", endpoint, data), put: (endpoint: string, data?: unknown) => request("PUT", endpoint, data), delete: (endpoint: string) => request("DELETE", endpoint), };