publishedSchema

tcmts.dev.lexicons.publishedSchema · #gza4bk4r · term · runs anywhere

A lexicon's published schema record, from a PDS: its CID and value, or undefined if there's none.

import { okJson } from "#codebase/tcmts/atproto";
import { SCHEMA_COLLECTION } from "#codebase/tcmts/dev/lexicons";

export async function publishedSchema(
  pds: string,
  did: string,
  nsid: string,
): Promise<{ cid: string; value: unknown } | undefined> {
  const q = new URLSearchParams({ repo: did, collection: SCHEMA_COLLECTION, rkey: nsid });
  const res = await fetch(`${pds}/xrpc/com.atproto.repo.getRecord?${q}`, {
    headers: { accept: "application/json" },
    signal: AbortSignal.timeout(30_000),
  });
  if (res.status === 400 || res.status === 404) {
    const e = (await res.json().catch(() => ({}))) as { error?: string };
    if (res.status === 404 || e.error === "RecordNotFound") return undefined;
    throw new Error(`reading ${did}'s ${nsid} schema: ${res.status} ${e.error ?? ""}`.trim());
  }
  return okJson<{ cid: string; value: unknown }>(res, `reading ${did}'s ${nsid} schema`);
}

uses

used by