/** * AutoForge API Client * * Wraps fetch() with JWT injection, error handling, and base URL management. */ const API = (() => { const BASE_URL = ''; /** * Make an authenticated API request. */ async function request(method, path, body = null) { const headers = { 'Content-Type': 'application/json' }; const token = localStorage.getItem('autoforge_token'); if (token) { headers['Authorization'] = `Bearer ${token}`; } const options = { method, headers }; if (body) { options.body = JSON.stringify(body); } const response = await fetch(`${BASE_URL}${path}`, options); if (response.status === 401) { localStorage.removeItem('autoforge_token'); localStorage.removeItem('autoforge_user'); window.location.href = '/login.html'; throw new Error('Session expired'); } const data = await response.json(); if (!response.ok) { const message = data.detail || `Request failed (${response.status})`; throw new Error(message); } return data; } return { get: (path) => request('GET', path), post: (path, body) => request('POST', path, body), put: (path, body) => request('PUT', path, body), delete: (path) => request('DELETE', path), }; })();