- One failure(res) in api.ts for every fetch that can fail (session,
remote and the libraries calls each had their own; the error's
attached body was never read). Session's lost() and vanished() were
the same but for the hook: stop("foreign" | "gone").
- linker: place() locates the words with locate() instead of a copy;
the scan reply's type is written once; LinkRow.at was never read; the
"import" origin is never passed.
- wiki.ts: cleanHtml's cache could never hit (its only caller,
articleText, is cached on the same key and evicts the same way);
known() returned an article nobody read.
- Gone: currentLibrary and idbKeys (no callers), useWikiSuggest's q,
a node's link flag spelled out where isLink says it. Exports used only
in their own file are not exported.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
59 lines
2.7 KiB
TypeScript
59 lines
2.7 KiB
TypeScript
/**
|
|
* Where the page's requests about its library go. The page holds one library, named by its
|
|
* address (`/l/<id>`); everything that reads or writes a library's content asks under
|
|
* `/api/l/<id>/`, and what holds no library's content (the model, Wikipedia, usage) under `/api/`.
|
|
* Switching library is a page load (`/l/<other id>`), so the prefix is set once, at boot.
|
|
*/
|
|
let library: string | null = null;
|
|
|
|
/** Set the library this page holds. */
|
|
export function setLibrary(id: string) {
|
|
library = id;
|
|
}
|
|
|
|
/**
|
|
* A path under this library's API: libApi("/links/jobs") → /api/l/<id>/links/jobs. Before a library
|
|
* is chosen (only in the tests: the page chooses one before anything asks) the unprefixed route,
|
|
* which the server answers for its first library.
|
|
*/
|
|
export function libApi(path: string): string {
|
|
return library ? `/api/l/${encodeURIComponent(library)}${path}` : `/api${path}`;
|
|
}
|
|
|
|
/** A library as /api/libraries lists it. */
|
|
export interface LibraryInfo {
|
|
id: string;
|
|
title: string;
|
|
created: string;
|
|
/** Nodes under its top-level nodes (the root and the Wikipedia container not counted). */
|
|
nodes: number;
|
|
branches: string[];
|
|
}
|
|
|
|
/** A failed answer as an error: the server's `error`, else its status. */
|
|
export async function failure(res: Response): Promise<Error> {
|
|
let message = `${res.status} ${res.statusText}`;
|
|
try {
|
|
message = String((await res.json())?.error ?? message);
|
|
} catch {
|
|
/* not JSON */
|
|
}
|
|
return new Error(message);
|
|
}
|
|
|
|
async function call<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const res = await fetch(path, init);
|
|
if (!res.ok) throw await failure(res);
|
|
return res.json().catch(() => ({})) as Promise<T>;
|
|
}
|
|
const send = (method: string, body?: unknown): RequestInit => ({ method, headers: { "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
|
|
/** The libraries, in order. */
|
|
export const listLibraries = () => call<{ libraries: LibraryInfo[] }>("/api/libraries").then((r) => r.libraries);
|
|
/** A new library (its root alone), at the end of the list. */
|
|
export const createLibrary = (title: string) => call<{ library: LibraryInfo }>("/api/libraries", send("POST", { title })).then((r) => r.library);
|
|
/** Rename a library (a commit on its root's title). */
|
|
export const renameLibrary = (id: string, title: string) => call<{ library: LibraryInfo }>(`/api/libraries/${encodeURIComponent(id)}`, send("PATCH", { title })).then((r) => r.library);
|
|
/** Delete a library: the server moves it aside whole. Refused for the last one. */
|
|
export const deleteLibrary = (id: string) => call<{ deleted: string; movedTo: string }>(`/api/libraries/${encodeURIComponent(id)}`, send("DELETE"));
|