async function apiFetch(path, options = {}) { const res = await fetch(path, { headers: { 'Content-Type': 'application/json' }, ...options, }); if (res.status === 401) { window.location.href = '/auth/login?returnTo=' + encodeURIComponent(window.location.pathname); return new Promise(() => {}); } if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.message || body.error || `Request failed: ${res.status}`); } if (res.status === 204) return null; return res.json(); } const api = { get: (path) => apiFetch(path), post: (path, data) => apiFetch(path, { method: 'POST', body: JSON.stringify(data) }), patch: (path, data) => apiFetch(path, { method: 'PATCH', body: JSON.stringify(data) }), put: (path, data) => apiFetch(path, { method: 'PUT', body: JSON.stringify(data) }), del: (path) => apiFetch(path, { method: 'DELETE' }), };