const TOKEN_KEY = 'ayos_token'; function getToken(): string | null { return localStorage.getItem(TOKEN_KEY); } async function request( method: string, path: string, body?: unknown, ): Promise { const headers: Record = { 'Content-Type': 'application/json', }; const token = getToken(); if (token) { headers['Authorization'] = `Bearer ${token}`; } const response = await fetch(path, { method, headers, body: body != null ? JSON.stringify(body) : undefined, }); if (!response.ok) { let message = `Request failed: ${response.status}`; try { const errorData = await response.json(); if (errorData.error) { message = errorData.error; } else if (errorData.message) { message = errorData.message; } } catch { // ignore JSON parse errors } throw new Error(message); } if (response.status === 204) { return undefined as T; } return response.json() as Promise; } export const apiClient = { get(path: string): Promise { return request('GET', path); }, post(path: string, body?: unknown): Promise { return request('POST', path, body); }, put(path: string, body?: unknown): Promise { return request('PUT', path, body); }, delete(path: string): Promise { return request('DELETE', path); }, };