recordsIn

tcmts.cli.publish.recordsIn · #xos4poob · function · runs anywhere

The signed-in account's records in collection, by key.

import type { Session } from "#codebase/tcmts/atproto";

export async function recordsIn(
  s: Session,
  collection: string,
): Promise<Map<string, { cid: string; value: unknown }>> {
  const out = new Map<string, { cid: string; value: unknown }>();
  let cursor: string | undefined;
  let pages = 0;
  do {
    const q = new URLSearchParams({
      repo: s.did,
      collection,
      limit: "100",
      ...(cursor ? { cursor } : {}),
    });
    const res = await s.fetch(`/xrpc/com.atproto.repo.listRecords?${q}`, {
      headers: { accept: "application/json" },
    });
    if (!res.ok)
      throw new Error(
        `reading ${collection} records: ${res.status} ${await res.text().catch(() => "")}`.trim(),
      );
    const page = (await res.json()) as {
      records: { uri: string; cid: string; value: unknown }[];
      cursor?: string;
    };
    for (const r of page.records)
      out.set(r.uri.slice(r.uri.lastIndexOf("/") + 1), { cid: r.cid, value: r.value });
    cursor = page.records.length && ++pages < 50 ? page.cursor : undefined;
  } while (cursor);
  return out;
}

uses

used by