douyinWEB/frontend/src/lib/api.js
eric a360eec2ba Document slots/covers/PB ops UX and sync feature pass to main.
Ship zombie reconcile, S3 covers, session end/drill-down, rooms ops, and refresh README for production.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-08 20:07:15 -05:00

63 lines
1.6 KiB
JavaScript

export async function fetchJson(url, init) {
const res = await fetch(url, {
...init,
headers: {
"Content-Type": "application/json",
...(init?.headers || {}),
},
});
const text = await res.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = { raw: text };
}
if (!res.ok) {
const detail = data?.detail || data?.message || text || res.statusText;
throw new Error(typeof detail === "string" ? detail : JSON.stringify(detail));
}
return data;
}
export function fmtDuration(seconds) {
const total = Math.max(0, Math.floor(Number(seconds) || 0));
const h = Math.floor(total / 3600);
const m = Math.floor((total % 3600) / 60);
const s = total % 60;
if (h > 0) return `${h}小时${m}分${s}秒`;
if (m > 0) return `${m}分${s}秒`;
return `${s}秒`;
}
export function fmtTs(ts) {
if (!ts) return "—";
try {
return new Date(ts * 1000).toLocaleString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
});
} catch {
return String(ts);
}
}
/** Prefer API proxy so private S3 still loads; bust cache safely. */
export function sessionCoverSrc(session, bust = true) {
if (!session) return "";
const base =
session.cover_proxy_url ||
(session.id ? `/api/sessions/${session.id}/cover` : "") ||
session.cover_url ||
"";
if (!base) return "";
if (!bust) return base;
const ts = session.started_at || session.updated_at || Date.now();
return base.includes("?") ? `${base}&ts=${ts}` : `${base}?ts=${ts}`;
}