import { ok } from "@atcute/client"; import { mainSchema as createRecordSchema } from "@atcute/atproto/types/repo/createRecord"; import { mainSchema as deleteRecordSchema } from "@atcute/atproto/types/repo/deleteRecord"; import { mainSchema as putRecordSchema } from "@atcute/atproto/types/repo/putRecord"; import type { Cid, Nsid, RecordKey, ResourceUri } from "@atcute/lexicons/syntax"; import type { OAuthUserAgent } from "@atcute/oauth-browser-client"; import { createClient } from "$lib/auth/agent"; import { clear } from "$lib/api/cache"; export interface WrittenRecord { uri: ResourceUri; cid: Cid; } // every record write in the app goes through here, so the read cache is dropped // on any mutation instead of each write site remembering which keys it dirties export const createRecord = async ( agent: OAuthUserAgent, collection: Nsid, record: T, rkey?: string ): Promise => { const { uri, cid } = await ok( createClient(agent).call(createRecordSchema, { input: { repo: agent.sub, collection, ...(rkey ? { rkey: rkey as RecordKey } : {}), record } }) ); clear(); return { uri, cid }; }; export const putRecord = async ( agent: OAuthUserAgent, collection: Nsid, rkey: string, record: T, // the write fails if the record moved since it was read swapRecord?: string ): Promise => { const { uri, cid } = await ok( createClient(agent).call(putRecordSchema, { input: { repo: agent.sub, collection, rkey: rkey as RecordKey, ...(swapRecord ? { swapRecord } : {}), record } }) ); clear(); return { uri, cid }; }; export const deleteRecord = async ( agent: OAuthUserAgent, collection: Nsid, rkey: string, swapRecord?: string ): Promise => { await ok( createClient(agent).call(deleteRecordSchema, { input: { repo: agent.sub, collection, rkey: rkey as RecordKey, ...(swapRecord ? { swapRecord } : {}) } }) ); clear(); };