// Server-side read-plane access. The web UI consumes only the public // read plane (parity by construction, design D22): these calls hit // the same XRPC endpoints any third-party client uses. import { env } from '$env/dynamic/private' import { error } from '@sveltejs/kit' import type { TownDistGetChangelog, TownDistGetProject, TownDistListChangelogs, TownDistListProjects, TownDistListRecentReleases, TownDistResolveRelease, } from '@dist-town/client' const appviewURL = () => env.APPVIEW_URL ?? 'http://127.0.0.1:8480' async function xrpc(fetchFn: typeof fetch, method: string, params: Record): Promise { const url = new URL(`/xrpc/${method}`, appviewURL()) for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v) const resp = await fetchFn(url) if (!resp.ok) { const body = await resp.json().catch(() => ({ message: resp.statusText })) // 400 covers unresolvable identifiers: a not-found for the visitor. // 410 travels intact: a retracted version is an answer the // publisher wrote, and routes render it rather than erroring. const status = resp.status === 404 || resp.status === 400 ? 404 : resp.status === 410 ? 410 : 502 error(status, body.message ?? body.error ?? 'read plane error') } return resp.json() as Promise } export function getProject(fetchFn: typeof fetch, repo: string, project: string) { return xrpc(fetchFn, 'town.dist.getProject', { repo, project }) } export function resolveRelease(fetchFn: typeof fetch, repo: string, project: string, selector: string) { return xrpc(fetchFn, 'town.dist.resolveRelease', { repo, project, selector, }) } export function listChangelogs(fetchFn: typeof fetch, repo: string, project: string) { return xrpc(fetchFn, 'town.dist.listChangelogs', { repo, project, }) } export function getChangelog(fetchFn: typeof fetch, repo: string, project: string, rkey: string) { return xrpc(fetchFn, 'town.dist.getChangelog', { repo, project, rkey, }) } export function listRecentReleases( fetchFn: typeof fetch, limit: number, scope?: { repo: string; project?: string }, ) { const params: Record = { limit: String(limit) } if (scope) { params.repo = scope.repo if (scope.project) params.project = scope.project } return xrpc( fetchFn, 'town.dist.listRecentReleases', params, ) } export function listProjects(fetchFn: typeof fetch, repo: string) { return xrpc(fetchFn, 'town.dist.listProjects', { repo }) }