/* * api.js * Single fetch wrapper used for all server communication. * Throws an Error with the server's detail message on non-2xx responses. * * Provides: req(method, url, body?, isForm?) * Depends on: nothing */ // ── API ────────────────────────────────────────────────────────────────────── async function req(method, url, body = null, isForm = false) { const opts = {method}; if (body) { if (isForm) { opts.body = body; } else { opts.headers = {'Content-Type':'application/json'}; opts.body = JSON.stringify(body); } } const r = await fetch(url, opts); if (!r.ok) { const e = await r.json().catch(() => ({detail:'Request failed'})); throw new Error(e.detail || 'Request failed'); } return r.json(); }