ai/at
ai/at
1
0
This commit is contained in:
syui 2024-11-12 16:38:53 +09:00
parent e7471b20e4
commit fb987a6f73
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56

@ -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());
}