Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import { ok } from "@atcute/client";import { mainSchema as createRecordSchema } from "@atcute/atproto/types/repo/createRecord";import { mainSchema as getRecordSchema } from "@atcute/atproto/types/repo/getRecord";import { mainSchema as deleteRecordSchema } from "@atcute/atproto/types/repo/deleteRecord";import { mainSchema as putRecordSchema } from "@atcute/atproto/types/repo/putRecord";import type { Cid, Nsid, 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";import { recordKeyOf } from "$lib/api/syntax";
export interface WrittenRecord { uri: ResourceUri; cid: Cid;}
interface ReadRecord<T> { value: T; /** getRecord returns cid only when present, null can't be used as swapRecord */ written: WrittenRecord | null;}
export const readRecord = async <T>( agent: OAuthUserAgent, collection: Nsid, rkey: string): Promise<ReadRecord<T> | null> => { const response = await createClient(agent).call(getRecordSchema, { params: { repo: agent.sub, collection, rkey: recordKeyOf(rkey) } }); if (response.ok) return { value: response.data.value as T, written: response.data.cid ? { uri: response.data.uri, cid: response.data.cid } : null }; const error = (response.data as { error?: string } | undefined)?.error; if (error === "RecordNotFound" || error === "CouldNotFindRecord") return null; await ok(response); return null;};
// 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 dirtiesexport const createRecord = async <T extends { $type: string }>( agent: OAuthUserAgent, collection: Nsid, record: T, rkey?: string): Promise<WrittenRecord> => { const { uri, cid } = await ok( createClient(agent).call(createRecordSchema, { input: { repo: agent.sub, collection, ...(rkey ? { rkey: recordKeyOf(rkey) } : {}), record } }) ); clear(); return { uri, cid };};
export const putRecord = async <T extends { $type: string }>( agent: OAuthUserAgent, collection: Nsid, rkey: string, record: T, // the write fails if the record moved since it was read swapRecord?: string): Promise<WrittenRecord> => { const { uri, cid } = await ok( createClient(agent).call(putRecordSchema, { input: { repo: agent.sub, collection, rkey: recordKeyOf(rkey), ...(swapRecord ? { swapRecord } : {}), record } }) ); clear(); return { uri, cid };};
export const deleteRecord = async ( agent: OAuthUserAgent, collection: Nsid, rkey: string, swapRecord?: string): Promise<void> => { await ok( createClient(agent).call(deleteRecordSchema, { input: { repo: agent.sub, collection, rkey: recordKeyOf(rkey), ...(swapRecord ? { swapRecord } : {}) } }) ); clear();};