import { API_BASE_URL } from "./config"; const TOKEN_KEY = "classlens_token"; export function getToken(): string | null { return localStorage.getItem(TOKEN_KEY); } export function setToken(token: string) { localStorage.setItem(TOKEN_KEY, token); } export function clearToken() { localStorage.removeItem(TOKEN_KEY); } function authHeaders(): Record { const token = getToken(); return token ? { Authorization: `Bearer ${token}` } : {}; } async function handleResponse(res: Response): Promise { if (res.status === 401) { clearToken(); window.location.reload(); throw new Error("Unauthorized"); } if (!res.ok) { const text = await res.text(); let msg = text; try { const j = JSON.parse(text); msg = j.detail || j.message || text; } catch {} throw new Error(msg); } return res.json(); } export async function apiGet(path: string): Promise { const res = await fetch(`${API_BASE_URL}${path}`, { headers: { ...authHeaders() }, }); return handleResponse(res); } export async function apiPost(path: string, body?: unknown): Promise { const res = await fetch(`${API_BASE_URL}${path}`, { method: "POST", headers: { "Content-Type": "application/json", ...authHeaders() }, body: body ? JSON.stringify(body) : undefined, }); return handleResponse(res); } export async function apiPut(path: string, body: unknown): Promise { const res = await fetch(`${API_BASE_URL}${path}`, { method: "PUT", headers: { "Content-Type": "application/json", ...authHeaders() }, body: JSON.stringify(body), }); return handleResponse(res); } export async function apiDelete(path: string): Promise { const res = await fetch(`${API_BASE_URL}${path}`, { method: "DELETE", headers: { ...authHeaders() }, }); if (res.status === 401) { clearToken(); window.location.reload(); throw new Error("Unauthorized"); } if (!res.ok) { const text = await res.text(); throw new Error(text); } } export async function apiUpload(path: string, formData: FormData): Promise { const res = await fetch(`${API_BASE_URL}${path}`, { method: "POST", headers: { ...authHeaders() }, body: formData, }); return handleResponse(res); }