96 lines
2.5 KiB
TypeScript
96 lines
2.5 KiB
TypeScript
// Minimal port of the Vue dashboard's api client: same request semantics
|
|
// (timeout, CSRF on mutation, server-first error text), only the endpoints
|
|
// v2 pages use. The server owns every shape below.
|
|
|
|
const DEFAULT_TIMEOUT_MS = 15000;
|
|
|
|
export class ApiError extends Error {
|
|
status: number;
|
|
|
|
constructor(message: string, status: number) {
|
|
super(message);
|
|
this.name = "ApiError";
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
// errorMessage prefers what the server actually said: archie-ui answers with
|
|
// a plain-text reason, the only part an operator can act on.
|
|
async function errorMessage(res: Response): Promise<string> {
|
|
try {
|
|
const body = (await res.text()).trim();
|
|
if (body) return body.split("\n")[0].slice(0, 200);
|
|
} catch {
|
|
// Body already consumed or unreadable; fall through to the status.
|
|
}
|
|
return `${res.status} ${res.statusText}`;
|
|
}
|
|
|
|
interface RequestOptions {
|
|
method?: string;
|
|
body?: unknown;
|
|
timeoutMs?: number;
|
|
}
|
|
|
|
async function request<T>(
|
|
path: string,
|
|
opts: RequestOptions = {},
|
|
): Promise<T> {
|
|
const { method = "GET", body, timeoutMs = DEFAULT_TIMEOUT_MS } = opts;
|
|
const headers: Record<string, string> = { Accept: "application/json" };
|
|
const init: RequestInit = {
|
|
method,
|
|
headers,
|
|
signal: AbortSignal.timeout(timeoutMs),
|
|
};
|
|
if (method !== "GET") {
|
|
headers["Content-Type"] = "application/json";
|
|
headers["X-Archie-CSRF"] = "1";
|
|
}
|
|
if (body !== undefined) init.body = JSON.stringify(body);
|
|
|
|
const res = await fetch(path, init);
|
|
if (!res.ok) throw new ApiError(await errorMessage(res), res.status);
|
|
return (await res.json()) as T;
|
|
}
|
|
|
|
export interface ServiceCheck {
|
|
name: string;
|
|
status: "ok" | "degraded";
|
|
detail?: string;
|
|
}
|
|
|
|
export interface Service {
|
|
service: string;
|
|
instance_id: string;
|
|
state: "up" | "degraded" | "down" | "unknown";
|
|
version: string;
|
|
install_type: string;
|
|
started_at: string;
|
|
reported_at: string;
|
|
detail?: string;
|
|
applied: {
|
|
kind: string;
|
|
applied_version: number;
|
|
error?: string;
|
|
state: "current" | "failed" | "unknown";
|
|
}[];
|
|
checks?: ServiceCheck[];
|
|
}
|
|
|
|
export interface ComponentVersion {
|
|
id: string;
|
|
label: string;
|
|
install_type?: string;
|
|
reference?: string;
|
|
running_version?: string;
|
|
installed_claim?: string;
|
|
latest_available?: string;
|
|
status: "ok" | "update_available" | "drift" | "unknown";
|
|
}
|
|
|
|
export const api = {
|
|
services: () => request<Service[]>("/api/services"),
|
|
version: () => request<{ components: ComponentVersion[] }>("/api/version"),
|
|
};
|