Files
bookshelf/static/js/api.js
night f29678ebf1 Initial commit
Photo-based book cataloger with AI identification.
Room → Cabinet → Shelf → Book hierarchy; FastAPI + SQLite backend;
vanilla JS SPA; OpenAI-compatible plugin system for boundary
detection, text recognition, and archive search.
2026-03-09 14:11:11 +03:00

24 lines
889 B
JavaScript

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