import { apiGet, apiPost, type ApiResult } from "../../../utils/api.js"; import { pluginEndpoints } from "../../api.js"; import type { WikiEndpoints } from "../index.js"; export interface SnapshotSummary { stamp: string; bytes: number; ts: string; editor: "user" | "llm" | "system"; sessionId?: string; reason?: string; } export interface SnapshotContent extends SnapshotSummary { meta: Record; body: string; } interface ListResponse { slug: string; snapshots: SnapshotSummary[]; } interface ReadResponse { slug: string; snapshot: SnapshotContent; } interface RestoreResponse { slug: string; restored: { fromStamp: string }; } function fillRoute(template: string, params: Record): string { return template.replace(/:([a-zA-Z]+)/g, (_, key: string) => { const value = params[key]; // Silently substituting "undefined" would request a page by that // literal name, so an unfilled placeholder has to be loud. if (value === undefined) throw new Error(`Missing route param ":${key}" for ${template}`); return encodeURIComponent(value); }); } function endpoints(): WikiEndpoints { return pluginEndpoints("wiki"); } export function fetchHistoryList(slug: string): Promise> { return apiGet(fillRoute(endpoints().pageHistory, { slug })); } export function fetchHistorySnapshot(slug: string, stamp: string): Promise> { return apiGet(fillRoute(endpoints().pageHistorySnapshot, { slug, stamp })); } export function restoreHistorySnapshot(slug: string, stamp: string): Promise> { return apiPost(fillRoute(endpoints().pageHistoryRestore, { slug, stamp })); }