Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061// The group.standards.* XRPC methods on our own origin (Vite proxy in dev,// the Node server deployed).import { NS, type RefView, type SpecDoc, type SpecSummary, type TermView } from '../../shared/types.ts';
export class ApiError extends Error { readonly error: string; readonly status: number; constructor(status: number, error: string, message: string) { super(message); this.error = error; this.status = status; }}
async function parseOrThrow<T>(res: Response): Promise<T> { if (!res.ok) { let error = 'RequestFailed'; let msg = `request failed (${res.status})`; try { const body = (await res.json()) as { error?: string; message?: string }; error = body.error ?? error; msg = body.message ?? msg; } catch { // keep the status message } throw new ApiError(res.status, error, msg); } return res.json() as Promise<T>;}
async function query<T>(method: string, params: Record<string, string | string[] | undefined>): Promise<T> { const url = new URL(`/xrpc/${NS}.${method}`, location.origin); for (const [k, v] of Object.entries(params)) { if (Array.isArray(v)) for (const x of v) url.searchParams.append(k, x); else if (v) url.searchParams.set(k, v); } return parseOrThrow(await fetch(url, { credentials: 'omit' }));}
async function procedure<T>(method: string, input: unknown): Promise<T> { const url = new URL(`/xrpc/${NS}.${method}`, location.origin); return parseOrThrow( await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(input), credentials: 'omit', }), );}
export type SpecList = { did: string; handle?: string; specs: SpecSummary[] };
export const getSpec = (repo: string, shortname: string) => query<SpecDoc>('getSpec', { repo, shortname });export const listSpecs = (repo: string) => query<SpecList>('listSpecs', { repo });export const searchRefs = (q: string, limit = 12) => query<{ refs: RefView[] }>('searchRefs', { q, limit: String(limit) });export const searchTerms = (q: string, refs: string[], limit = 8) => query<{ terms: TermView[] }>('searchTerms', { q, refs, limit: String(limit) });export const getRef = (uri: string) => query<RefView>('getRef', { uri });export const requestTracking = (did: string) => procedure<{ did: string; handle?: string; tracked: boolean }>('requestTracking', { did });