initial commit

This commit is contained in:
Hunter
2026-11-01 13:23:44 -05:00
commit e03ea4f770
53 changed files with 10802 additions and 0 deletions

26
lib/client.ts Normal file
View File

@@ -0,0 +1,26 @@
/* Browser-side fetch helper for the JSON API. */
export class ApiError extends Error {
constructor(
public status: number,
message: string,
public details?: unknown
) {
super(message);
}
}
export async function api<T = unknown>(url: string, init?: RequestInit & { json?: unknown }): Promise<T> {
const { json, ...rest } = init ?? {};
const res = await fetch(url, {
...rest,
headers: json !== undefined ? { "content-type": "application/json", ...rest.headers } : rest.headers,
body: json !== undefined ? JSON.stringify(json) : rest.body,
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
const details = Array.isArray(data.details) ? `: ${data.details.join("; ")}` : "";
throw new ApiError(res.status, (data.error ?? res.statusText) + details, data.details);
}
return data as T;
}