mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
Replace the single-file frontend (app.js, styles.css) with a multi-file ES module structure served from go:embed, and reorganize the 13 pages into 8 tabbed pages under 4 navigation groups: runtime (overview, diagnostics), integrations (access guide, API keys), models (quotas, rotation), and system (configuration, account). The redesign adopts a monochrome light/dark design system driven by prefers-color-scheme, replaces prompt()/confirm() flows with in-page dialogs, and keeps every management API call and deep link (old hashes alias to the new pages). Behavior changes: the log SSE connection now spans the whole console session and buffers while the stream tab is hidden; quota polling runs only while its tab is active; the first-run checklist appears on the overview page and hides once ready. Also fixes a crash on the rotation page: the persisted state serializes empty orders and maps as JSON null, which is now normalized before rendering, with the same defensive handling in the quota and API key pages. CI now syntax-checks the ES modules via node --input-type=module, and the README structure notes are updated.
51 lines
1.6 KiB
JavaScript
51 lines
1.6 KiB
JavaScript
// fetch wrapper: JSON handling, CSRF for writes, 401 routed to the login view.
|
|
import * as store from "./store.js";
|
|
|
|
let csrf = "";
|
|
let onUnauthorized = null;
|
|
|
|
export function setCsrf(value) {
|
|
csrf = value || "";
|
|
}
|
|
|
|
export function clearCsrf() {
|
|
csrf = "";
|
|
}
|
|
|
|
export function hasSession() {
|
|
return csrf !== "";
|
|
}
|
|
|
|
export function setUnauthorizedHandler(handler) {
|
|
onUnauthorized = handler;
|
|
}
|
|
|
|
export async function api(path, options = {}) {
|
|
const headers = { ...(options.headers || {}), Accept: "application/json" };
|
|
if (options.body) headers["Content-Type"] = "application/json";
|
|
if (csrf && !["GET", "HEAD"].includes((options.method || "GET").toUpperCase()))
|
|
headers["X-CSRF-Token"] = csrf;
|
|
const response = await fetch(path, { ...options, headers });
|
|
if (response.status === 204) return null;
|
|
const data = await response.json().catch(() => ({}));
|
|
if (!response.ok) {
|
|
// The login endpoint answers 401 for bad credentials: surface its message
|
|
// instead of treating the attempt as an expired session.
|
|
if (response.status === 401 && path !== "/api/auth/login") {
|
|
if (onUnauthorized) onUnauthorized();
|
|
throw new Error("登录已失效");
|
|
}
|
|
throw new Error(data.error?.message || `HTTP ${response.status}`);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
// Client-facing API base derived from the configured listen address.
|
|
export function derivedAPIBase() {
|
|
const config = store.get("config");
|
|
const listen = config?.effective?.listen || config?.listen || ":8080";
|
|
const match = listen.match(/^\[.*\]:(\d+)$/) || listen.match(/:(\d+)$/);
|
|
const port = match?.[1] || "8080";
|
|
return `${location.protocol}//${location.hostname}:${port}`;
|
|
}
|