This commit is contained in:
2026-09-01 16:53:14 +02:00
parent 43d14552f1
commit f2b56377a4
2 changed files with 35 additions and 1 deletions
+13 -1
View File
@@ -129,7 +129,19 @@
const $ = (id) => document.getElementById(id);
const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));
const api = (path) => window.BT ? BT.api(path) : fetch(path).then((r) => r.json());
const api = async (path, options = {}) => {
if (window.BT) return BT.api(path, options);
const opts = {...options};
if (opts.body !== undefined && !(opts.body instanceof FormData)) {
opts.headers = {"Content-Type": "application/json", ...(opts.headers || {})};
}
const response = await fetch(path, opts);
const text = await response.text();
let payload = {};
try { payload = text ? JSON.parse(text) : {}; } catch (_) { payload = {raw: text}; }
if (!response.ok) throw new Error(payload.error || payload.detail || `HTTP ${response.status}`);
return payload;
};
const esc = (value) => window.BT ? BT.escape(value) : String(value ?? "").replace(/[&<>"']/g, (ch) => ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[ch]));
const uid = (prefix) => `${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;