PicoBot/webui/src/lib/api.js
2026-07-15 10:40:56 +08:00

27 lines
864 B
JavaScript

export async function api(path, options = {}) {
const response = await fetch(path, {
...options,
headers: { "Content-Type": "application/json", ...(options.headers || {}) }
});
const data = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(data.error || `${response.status} ${response.statusText}`);
return data;
}
export function formatTime(value) {
if (!value) return "—";
const timestamp = typeof value === "number" && value < 1e12 ? value * 1000 : value;
const date = new Date(timestamp);
return Number.isNaN(date.getTime()) ? "—" : date.toLocaleString();
}
export function clientId() {
const key = "picobot_web_client_id";
let id = localStorage.getItem(key);
if (!id) {
id = `web_${crypto.randomUUID().replaceAll("-", "").slice(0, 24)}`;
localStorage.setItem(key, id);
}
return id;
}