const API_BASE = (import.meta.env.VITE_AUTH_API_URL || '').replace(/\/$/, '') + '/api'; async function request(path: string, options?: RequestInit): Promise { const token = localStorage.getItem('token'); const headers: Record = { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}` } : {}), }; const res = await fetch(`${API_BASE}${path}`, { ...options, headers }); if (res.status === 401) { localStorage.removeItem('token'); window.location.href = '/login'; throw new Error('Unauthorized'); } if (!res.ok) { const body = await res.text(); throw new Error(body || `HTTP ${res.status}`); } if (res.status === 204 || res.headers.get('content-length') === '0') { return undefined as T; } return res.json(); } export const authApi = { get: (path: string) => request(path), post: (path: string, body?: unknown) => request(path, { method: 'POST', body: JSON.stringify(body) }), };