/* api.js — thin REST client over fetch(). Throws on non-2xx so callers can * try/catch. Returns parsed JSON. */ const Api = (() => { async function _req(method, url, body) { const opts = { method, headers: {} }; if (body !== undefined) { opts.headers["content-type"] = "application/json"; opts.body = JSON.stringify(body); } const res = await fetch(url, opts); if (!res.ok) { let msg = res.statusText; try { msg = (await res.json()).error || msg; } catch {} const err = new Error(`${res.status} ${msg}`); err.status = res.status; throw err; } if (res.status === 204) return null; const ct = res.headers.get("content-type") || ""; return ct.includes("application/json") ? res.json() : res.text(); } return { // Projects listProjects: () => _req("GET", "/api/projects"), getProject: (id) => _req("GET", `/api/projects/${id}`), createProject: (name, data) => _req("POST", "/api/projects", { name, data }), updateProject: (id, payload) => _req("PUT", `/api/projects/${id}`, payload), deleteProject: (id) => _req("DELETE", `/api/projects/${id}`), // Snapshots listSnapshots: (projectId) => _req("GET", `/api/projects/${projectId}/snapshots`), getSnapshot: (sid) => _req("GET", `/api/snapshots/${sid}`), restoreSnapshot: (sid) => _req("POST", `/api/snapshots/${sid}/restore`), // Presets listPresets: () => _req("GET", "/api/presets"), createPreset: (name, params) => _req("POST", "/api/presets", { name, params }), updatePreset: (id, payload) => _req("PUT", `/api/presets/${id}`, payload), deletePreset: (id) => _req("DELETE", `/api/presets/${id}`), }; })();