// Talking to the server: every call is relative, so the /receipt/ prefix nginx adds keeps working. function detailText(detail) { if (!Array.isArray(detail)) return detail; // Pydantic errors: say where, minus the "body" prefix and union tags. const tags = ["body", "once", "recurring", "sun", "calendar", "watch", "webhook", "note", "qr", "image", "random_image", "agenda", "headlines", "divider"]; return detail.map((d) => { const where = (d.loc || []) .filter((p) => !tags.includes(p) && !/[[(]/.test(String(p))) .map((p) => (typeof p === "number" ? `#${p + 1}` : p.replaceAll("_", " "))); const msg = String(d.msg || "").replace(/^Value error, /, ""); return where.length ? `${where.join(" › ")}: ${msg}` : msg; }).join("; "); } export async function call(path, init = {}) { init.headers = Object.assign({"X-Requested-With": "receipt"}, init.headers || {}); let res; try { res = await fetch(path, init); } catch (e) { return {ok: false, status: 0, text: "Network error: " + e.message, body: {}}; } let body = {}; try { body = await res.json(); } catch (e) { /* nginx pages aren't JSON */ } if (!("ok" in body) && !("detail" in body)) { // Not from this server: nginx's offline page, or its login redirect. const offline = [502, 503, 504].includes(res.status); return {ok: false, status: res.status, body, text: offline ? "The printer machine is offline." : "Signed out? Reload the page and sign in."}; } return {ok: res.ok && body.ok !== false, status: res.status, body, text: body.output || detailText(body.detail) || res.statusText}; } export const get = (path) => call(path); const withJson = (method) => (path, data) => call(path, { method, headers: {"Content-Type": "application/json"}, body: data === undefined ? undefined : JSON.stringify(data), }); export const post = withJson("POST"); export const put = withJson("PUT"); export const del = withJson("DELETE"); // POST something that queues a print, then long-poll the job until it finishes. // onUpdate(text) reports progress; resolves {ok, text}. export async function printJob(request, onUpdate = () => {}) { onUpdate("Sending…"); let r = await request(); let job = null; while (r.ok && r.body.job && !["done", "failed"].includes(r.body.job.state)) { job = r.body.job; onUpdate(job.state === "queued" ? "Queued…" : "Printing…"); r = await call(`api/jobs/${job.id}?wait=20`); } if (r.ok && r.body.skipped) return {ok: true, text: r.body.skipped}; if (!r.ok || !r.body.job) { return {ok: false, text: r.text + (job && r.status === 0 ? " (the job may still print)" : "")}; } return {ok: r.body.job.state === "done", text: r.body.job.message}; } // Lists the editor and settings share; refreshed whenever they change. export const cache = {sources: null, assets: null, settings: null}; export async function sources(refresh = false) { if (refresh || !cache.sources) { const r = await get("api/sources"); cache.sources = r.ok ? r.body.sources : []; } return cache.sources; } export async function assets(refresh = false) { if (refresh || !cache.assets) { const r = await get("api/assets"); cache.assets = r.ok ? r.body.assets : []; } return cache.assets; } export async function settings(refresh = false) { if (refresh || !cache.settings) { const r = await get("api/settings"); cache.settings = r.ok ? r.body : {settings: {}, timezones: []}; } return cache.settings; }