const API_BASE_URL = process.env['NEXT_PUBLIC_API_URL'] || 'http://localhost:3001'; export class ApiError extends Error { constructor( public status: number, message: string, ) { super(message); this.name = 'ApiError'; } } type RequestOptions = Omit & { body?: unknown; }; async function request(endpoint: string, options: RequestOptions = {}): Promise { const { body, headers, ...rest } = options; const res = await fetch(`${API_BASE_URL}${endpoint}`, { ...rest, headers: { 'Content-Type': 'application/json', ...headers, }, body: body ? JSON.stringify(body) : undefined, }); if (!res.ok) { const error = await res.json().catch(() => ({ message: res.statusText })); throw new ApiError(res.status, error.message || 'Request failed'); } return res.json(); } function authHeaders(token: string): HeadersInit { return { Authorization: `Bearer ${token}` }; } export const apiClient = { get: (endpoint: string, headers?: HeadersInit) => request(endpoint, { method: 'GET', headers }), post: (endpoint: string, body?: unknown, headers?: HeadersInit) => request(endpoint, { method: 'POST', body, headers }), patch: (endpoint: string, body?: unknown, headers?: HeadersInit) => request(endpoint, { method: 'PATCH', body, headers }), authGet: (endpoint: string, token: string) => request(endpoint, { method: 'GET', headers: authHeaders(token) }), authPost: (endpoint: string, token: string, body?: unknown) => request(endpoint, { method: 'POST', body, headers: authHeaders(token) }), authPatch: (endpoint: string, token: string, body?: unknown) => request(endpoint, { method: 'PATCH', body, headers: authHeaders(token) }), };