// Small fetch wrapper: JSON in, JSON out, errors carry the server's message. export async function request(path, { method = 'GET', body, params } = {}) { const url = new URL(path, location.origin); for (const [k, v] of Object.entries(params || {})) if (v !== undefined && v !== null && v !== '') url.searchParams.set(k, v); const res = await fetch(url, { method, headers: { Accept: 'application/json', ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}) }, body: body !== undefined ? JSON.stringify(body) : undefined, }); if (!res.ok) { let detail = res.statusText; try { const j = await res.json(); detail = typeof j.detail === 'string' ? j.detail : JSON.stringify(j.detail ?? j); } catch {} const err = new Error(detail || `HTTP ${res.status}`); err.status = res.status; throw err; } return res.status === 204 ? null : res.json(); } export const get = (path, params) => request(path, { params }); export const post = (path, body) => request(path, { method: 'POST', body }); export const put = (path, body) => request(path, { method: 'PUT', body }); export const patch = (path, body) => request(path, { method: 'PATCH', body }); export const del = (path, params) => request(path, { method: 'DELETE', params });