All checks were successful
release / build-and-upload (push) Successful in 7m5s
- Rename product/host binary to streamd (drm compat copy still shipped) - Go module path streamd; STREAMD_* env aliases; agent UA streamd-agent - Panel Modules store + agent pull sync (modstore / modsync) - Rolling stable + beta download tags; README/docs channel curl blocks - Release notes in docs/releases/v0.2.0.md attached by Forgejo Actions
173 lines
6.2 KiB
JavaScript
173 lines
6.2 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"),
|
|
|
|
// --- modules ---
|
|
modules: () => api.get("/api/modules"),
|
|
module: (name, includeYaml) =>
|
|
api.get(`/api/modules/${encodeURIComponent(name)}${includeYaml ? "?include=yaml" : ""}`),
|
|
saveModuleYAML: (name, yaml) =>
|
|
api.put(`/api/modules/${encodeURIComponent(name)}`, { yaml }),
|
|
deleteModule: (name) => api.del(`/api/modules/${encodeURIComponent(name)}`),
|
|
uploadModuleZip: async (file, name) => {
|
|
const fd = new FormData();
|
|
fd.append("file", file, file.name || "module.zip");
|
|
if (name) fd.append("name", name);
|
|
const headers = {};
|
|
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
|
|
const res = await fetch("/api/modules/upload", {
|
|
method: "POST",
|
|
headers,
|
|
body: fd,
|
|
credentials: "same-origin",
|
|
});
|
|
const text = await res.text();
|
|
let data = null;
|
|
if (text) {
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
data = { raw: text };
|
|
}
|
|
}
|
|
if (res.status === 401) {
|
|
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 || `upload failed (${res.status})`;
|
|
throw new ApiError(msg, res.status, data);
|
|
}
|
|
return data;
|
|
},
|
|
};
|