diff --git a/repos_extra/frontpage/packages/frontpage/lib/data/atproto/record.ts b/repos_extra/frontpage/packages/frontpage/lib/data/atproto/record.ts new file mode 100644 index 0000000..8445f27 --- /dev/null +++ b/repos_extra/frontpage/packages/frontpage/lib/data/atproto/record.ts @@ -0,0 +1,50 @@ +import "server-only"; +import { z } from "zod"; +import { ensureUser } from "../user"; +import { DataLayerError } from "../error"; +import { fetchAuthenticatedAtproto } from "@/lib/auth"; +import { AtUri } from "./uri"; + +const PutRecordResponse = z.object({ + uri: AtUri, + cid: z.string(), +}); + +type PutRecordInput = { + rkey?: string; + record: unknown; + collection: string; +}; + +export async function atprotoPutRecord({ + rkey, + record, + collection, +}: PutRecordInput) { + const user = await ensureUser(); + const pdsUrl = new URL(user.pdsUrl); + pdsUrl.pathname = "/xrpc/com.atproto.repo.putRecord"; + + const response = await fetchAuthenticatedAtproto(pdsUrl.toString(), { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + rkey: rkey || "self", + repo: user.did, + collection, + validate: false, + record: record, + }), + }); + + if (!response.ok) { + throw new DataLayerError(`Failed to create record ${response.status}`, { + cause: response, + }); + } + + return PutRecordResponse.parse(await response.json()); +} +