export function json(data: unknown, status = 200, headers?: HeadersInit): Response { return new Response(JSON.stringify(data), { status, headers: { "content-type": "application/json; charset=utf-8", ...headers, }, }); } export function xrpcError(error: string, message: string, status = 400): Response { return json({ error, message }, status); } export async function readJson(request: Request): Promise> { try { const body: unknown = await request.json(); if (typeof body !== "object" || body === null || Array.isArray(body)) { throw new Error("expected object"); } return body as Record; } catch { throw new XrpcRequestError("InvalidRequest", "expected a JSON object"); } } export class XrpcRequestError extends Error { error: string; status: number; constructor(error: string, message: string, status = 400) { super(message); this.error = error; this.status = status; } }