writeAttestation

tcmts.cli.attest.writeAttestation · #77l5d4hk · term · runs anywhere

Write an attestation to the signed-in account's repository: its URI.

import { pdsFetch } from "#codebase/tcmts/atproto";
import type { OAuthSession } from "#codebase/tcmts/atproto";
import { ATTESTATION_KINDS, attestationRecord } from "#codebase/tcmts/records";
import type { Attestation } from "#codebase/tcmts/records";

export async function writeAttestation(s: OAuthSession, a: Attestation): Promise<string> {
  const res = await pdsFetch(s, "/xrpc/com.atproto.repo.createRecord", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({
      repo: s.did,
      collection: ATTESTATION_KINDS[a.kind],
      record: attestationRecord(a),
    }),
  });
  const got = (await res.json().catch(() => ({}))) as {
    uri?: string;
    error?: string;
    message?: string;
  };
  if (!res.ok || !got.uri)
    throw new Error(
      `writing the ${a.kind}: ${res.status} ${got.message ?? got.error ?? ""}`.trim(),
    );
  return got.uri;
}

uses

used by