const BASE_URL = import.meta.env.VITE_API_BASE || '/api' async function request(method, path, body = null) { const headers = { 'Content-Type': 'application/json' } const token = localStorage.getItem('token') if (token) headers['Authorization'] = `Bearer ${token}` const res = await fetch(`${BASE_URL}${path}`, { method, headers, body: body ? JSON.stringify(body) : null, }) if (res.status === 401) { localStorage.removeItem('token') const basePath = import.meta.env.VITE_BASE_PATH || '/leopost-full' window.location.href = basePath ? `${basePath}/login` : '/login' return } if (!res.ok) { const error = await res.json().catch(() => ({ detail: 'Request failed' })) // Pass through structured errors (upgrade_required etc.) const detail = error.detail if (typeof detail === 'object' && detail !== null) { const err = new Error(detail.message || 'Request failed') err.data = detail throw err } throw new Error(detail || 'Request failed') } if (res.status === 204) return null return res.json() } export const api = { get: (path) => request('GET', path), post: (path, body) => request('POST', path, body), put: (path, body) => request('PUT', path, body), delete: (path) => request('DELETE', path), } export { BASE_URL }