Suite of AT Protocol TypeScript libraries built on web standards
21
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 45 lines 1.2 kB view raw
1import type { Cid } from "@atp/lex/data"; 2import { decode as decodeLexCbor } from "@atp/lex/cbor"; 3import type { check } from "@atp/common"; 4import type { BlockMap } from "./block-map.ts"; 5import { MissingBlockError, UnexpectedObjectError } from "./error.ts"; 6import type { RepoRecord } from "./types.ts"; 7import { cborToLexRecord } from "./util.ts"; 8 9export const getAndParseRecord = ( 10 blocks: BlockMap, 11 cid: Cid, 12): { record: RepoRecord; bytes: Uint8Array } => { 13 const bytes = blocks.get(cid); 14 if (!bytes) { 15 throw new MissingBlockError(cid, "record"); 16 } 17 const record = cborToLexRecord(bytes); 18 return { record, bytes }; 19}; 20 21export const getAndParseByDef = <T>( 22 blocks: BlockMap, 23 cid: Cid, 24 def: check.Def<T>, 25): { obj: T; bytes: Uint8Array } => { 26 const bytes = blocks.get(cid); 27 if (!bytes) { 28 throw new MissingBlockError(cid, def.name); 29 } 30 return parseObjByDef(bytes, cid, def); 31}; 32 33export const parseObjByDef = <T>( 34 bytes: Uint8Array, 35 cid: Cid, 36 def: check.Def<T>, 37): { obj: T; bytes: Uint8Array } => { 38 const obj = decodeLexCbor(bytes); 39 const res = def.schema.safeParse(obj); 40 if (res.success) { 41 return { obj: res.data, bytes }; 42 } else { 43 throw new UnexpectedObjectError(cid, def.name); 44 } 45};