Null-DRM-Official/apps/streamd/internal/ui/static/js/api.js
404errordeveloper 066e018749
All checks were successful
release / build-and-upload (push) Successful in 6m18s
modules-release / build-and-upload (push) Successful in 5m18s
streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links
- Embed dashboard screenshots in README; Linux streamd+agent setup guide
- C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH
- Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
2026-10-09 20:57:43 +02:00

134 lines
5 KiB
JavaScript

// API client. Carries the CSRF token on mutations and routes a 401 back to
// the sign-in screen rather than leaving a half-dead page.
let csrfToken = "";
const onUnauthorized = [];
export function setCSRF(token) {
csrfToken = token || "";
}
export function getCSRF() {
return csrfToken;
}
/** Register a callback for when the session turns out to be gone. */
export function onAuthLost(fn) {
onUnauthorized.push(fn);
}
export class ApiError extends Error {
constructor(message, status, body) {
super(message);
this.name = "ApiError";
this.status = status;
this.body = body;
}
}
async function request(method, path, body, opts = {}) {
const headers = {};
let payload;
if (body !== undefined && body !== null) {
headers["Content-Type"] = "application/json";
payload = JSON.stringify(body);
}
// The session cookie is HttpOnly, so the double-submit token is what proves
// this request came from the dashboard and not from another origin.
if (method !== "GET" && method !== "HEAD" && csrfToken) {
headers["X-CSRF-Token"] = csrfToken;
}
const res = await fetch(path, {
method,
headers,
body: payload,
credentials: "same-origin",
signal: opts.signal,
});
const text = await res.text();
let data = null;
if (text) {
try {
data = JSON.parse(text);
} catch {
data = { raw: text };
}
}
if (res.status === 401) {
// allow401 lets boot/login/setup read the JSON body (e.g. needs_setup)
// without treating it as a hard failure or kicking the session.
if (opts.allow401) return data;
for (const fn of onUnauthorized) fn(data);
throw new ApiError((data && data.error) || "not signed in", 401, data);
}
if (!res.ok) {
const msg = (data && data.error) || res.statusText || `request failed (${res.status})`;
throw new ApiError(msg, res.status, data);
}
return data;
}
export const api = {
get: (p, opts) => request("GET", p, null, opts),
post: (p, b, opts) => request("POST", p, b ?? {}, opts),
patch: (p, b, opts) => request("PATCH", p, b ?? {}, opts),
put: (p, b, opts) => request("PUT", p, b ?? {}, opts),
del: (p, opts) => request("DELETE", p, null, opts),
// --- session ---
me: () => request("GET", "/api/me", null, { allow401: true }),
login: (username, password) => request("POST", "/api/login", { username, password }, { allow401: true }),
setup: (setup_token, username, password) =>
request("POST", "/api/setup", { setup_token, username, password }, { allow401: true }),
logout: () => request("POST", "/api/logout", {}),
setupState: () => request("GET", "/api/setup", null, { allow401: true }),
// --- streams ---
health: () => request("GET", "/api/health", null, { allow401: true }),
overview: () => api.get("/api/overview"),
streams: () => api.get("/api/streams"),
stream: (id, reveal) => api.get(`/api/streams/${id}${reveal ? "?reveal=1" : ""}`),
createStream: (body) => api.post("/api/streams", body),
patchStream: (id, body) => api.patch(`/api/streams/${id}`, body),
deleteStream: (id) => api.del(`/api/streams/${id}`),
action: (id, what) => api.post(`/api/streams/${id}/${what}`),
bulk: (ids, action) => api.post("/api/streams/bulk", { ids, action }),
duplicate: (id, body) => api.post(`/api/streams/${id}/duplicate`, body),
probe: (id, mpd) => api.post(`/api/streams/${id}/probe`, mpd ? { mpd } : {}),
capture: (id) => api.post(`/api/streams/${id}/capture`),
credentials: (id, body) => api.post(`/api/streams/${id}/credentials`, body),
// --- history ---
events: (id, limit = 100) => api.get(`/api/streams/${id}/events?limit=${limit}`),
metrics: (id, window = "1h") => api.get(`/api/streams/${id}/metrics?window=${encodeURIComponent(window)}`),
logs: (id, source, variant) => {
const q = new URLSearchParams({ source, tail: "600" });
if (variant) q.set("variant", variant);
return api.get(`/api/streams/${id}/logs?${q}`);
},
// --- schedules ---
schedules: (id) => api.get(`/api/streams/${id}/schedules`),
allSchedules: () => api.get("/api/schedules"),
upcoming: (limit = 10) => api.get(`/api/schedules?upcoming=1&limit=${limit}`),
createSchedule: (id, body) => api.post(`/api/streams/${id}/schedules`, body),
patchSchedule: (sid, body) => api.patch(`/api/schedules/${sid}`, body),
deleteSchedule: (sid) => api.del(`/api/schedules/${sid}`),
// --- admin ---
settings: () => api.get("/api/settings"),
saveSettings: (body) => api.put("/api/settings", body),
users: () => api.get("/api/users"),
createUser: (body) => api.post("/api/users", body),
patchUser: (id, body) => api.patch(`/api/users/${id}`, body),
deleteUser: (id) => api.del(`/api/users/${id}`),
setPassword: (id, body) => api.post(`/api/users/${id}/password`, body),
apiKeys: () => api.get("/api/apikeys"),
createKey: (body) => api.post("/api/apikeys", body),
revokeKey: (id) => api.del(`/api/apikeys/${id}`),
audit: (limit = 200) => api.get(`/api/audit?limit=${limit}`),
agents: () => api.get("/api/agents"),
apps: () => api.get("/api/apps"),
};