Files
wenil d8cb0dc06d Initial commit: Busbar Designer
Web tool for designing nickel/copper busbars over cylindrical-cell battery
packs (21700, 18650) in hex holders. Flask + build123d backend exports
STEP/DXF/SVG; vanilla JS frontend with live preview, multi-project SQLite
persistence, snapshot history.

Deploy scripts in deploy/ (proxmox-lxc.sh, install.sh, update.sh).
2026-05-24 18:59:50 +03:00

46 lines
1.8 KiB
JavaScript

/* 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}`),
};
})();