import type { HostInfo, HostInventoryResponse, SaveHostRequest, NotebookListResponse, HostsResponse, NotebookResponse, SaveNotebookResponse, StartRunRequest, StartRunResponse, ServerEvent, } from "@shellbook/core"; export class HttpError extends Error { constructor( public status: number, serverMessage?: string, ) { super(serverMessage || `Request failed (${status})`); } } /** Deliberate errors (4xx, 502, 503) carry a user-facing `message`; unexpected 500s may expose internals. */ async function serverMessage(response: Response): Promise { if (response.status === 500) return undefined; try { const body = (await response.json()) as { message?: unknown }; return typeof body.message === "string" ? body.message.slice(0, 500) : undefined; } catch { return undefined; } } export async function json( path: string, method = "GET", body?: unknown, ): Promise { const response = await fetch(`/api${path}`, { method, credentials: "same-origin", headers: body === undefined ? {} : { "Content-Type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body), }); if (!response.ok) throw new HttpError(response.status, await serverMessage(response)); return response.status === 204 ? (undefined as T) : (response.json() as Promise); } export interface Api { create( path: string, source: string, ): Promise<{ path: string; version: string }>; move( path: string, target: string, baseVersion: string, duplicate?: boolean, ): Promise<{ path: string; version: string }>; deleteNotebook(path: string, baseVersion: string): Promise; me(): Promise; login(password: string): Promise; list(): Promise; hosts(): Promise; notebook(path: string): Promise; save( path: string, source: string, baseVersion: string, ): Promise; run(request: StartRunRequest): Promise; /** Add a host to the inventory (a notebook's `::host` block); 409 if the name is taken. */ addHost(host: HostInfo): Promise; events( receive: (event: ServerEvent) => void, connection: (connected: boolean) => void, ): () => void; artifact(runId: string, name: string): string; } export const liveApi: Api = { create: (path, source) => json("/notebooks", "POST", { path, source }), move: (path, target, baseVersion, duplicate = false) => json( `/notebooks/${encodeURIComponent(path)}/${duplicate ? "duplicate" : "rename"}`, "POST", { path: target, baseVersion }, ), deleteNotebook: (path, baseVersion) => json(`/notebooks/${encodeURIComponent(path)}`, "DELETE", { baseVersion }), me: () => json("/auth/me"), login: (password) => json("/auth/login", "POST", { password }), list: () => json("/notebooks"), hosts: () => json("/hosts"), notebook: (path) => json(`/notebooks/${encodeURIComponent(path)}`), save: (path, source, baseVersion) => json(`/notebooks/${encodeURIComponent(path)}`, "PUT", { source, baseVersion, }), run: (request) => json("/runs", "POST", request), async addHost(host) { const { version } = await json("/host-management"); const request: SaveHostRequest = { host, baseVersion: version }; await json("/host-management", "POST", request); }, artifact: (id, name) => `/api/runs/${encodeURIComponent(id)}/artifacts/${encodeURIComponent(name)}`, events(receive, connection) { const ws = new WebSocket( `${location.protocol === "https:" ? "wss:" : "ws:"}//${location.host}/api/events`, ); ws.onopen = () => connection(true); ws.onclose = () => connection(false); ws.onerror = () => connection(false); ws.onmessage = (message) => { try { receive(JSON.parse(String(message.data)) as ServerEvent); } catch { connection(false); } }; return () => ws.close(); }, };