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 84 lines 1.9 kB view raw
1import type { Lexicons } from "../lexicons.ts"; 2import type { 3 LexRecord, 4 LexRefVariant, 5 LexUserType, 6 LexXrpcProcedure, 7 LexXrpcQuery, 8 LexXrpcSubscription, 9} from "../types.ts"; 10import { object, validateOneOf } from "./complex.ts"; 11import { params } from "./xrpc.ts"; 12 13export function assertValidRecord( 14 lexicons: Lexicons, 15 def: LexRecord, 16 value: unknown, 17) { 18 const res = object(lexicons, "Record", def.record, value); 19 if (!res.success) throw res.error; 20 return res.value; 21} 22 23export function assertValidXrpcParams( 24 lexicons: Lexicons, 25 def: LexXrpcProcedure | LexXrpcQuery | LexXrpcSubscription, 26 value: unknown, 27) { 28 if (def.parameters) { 29 const res = params(lexicons, "Params", def.parameters, value); 30 if (!res.success) throw res.error; 31 return res.value; 32 } 33} 34 35export function assertValidXrpcInput( 36 lexicons: Lexicons, 37 def: LexXrpcProcedure, 38 value: unknown, 39) { 40 if (def.input?.schema) { 41 // loop: all input schema definitions 42 return assertValidOneOf(lexicons, "Input", def.input.schema, value, true); 43 } 44} 45 46export function assertValidXrpcOutput( 47 lexicons: Lexicons, 48 def: LexXrpcProcedure | LexXrpcQuery, 49 value: unknown, 50) { 51 if (def.output?.schema) { 52 // loop: all output schema definitions 53 return assertValidOneOf(lexicons, "Output", def.output.schema, value, true); 54 } 55} 56 57export function assertValidXrpcMessage( 58 lexicons: Lexicons, 59 def: LexXrpcSubscription, 60 value: unknown, 61) { 62 if (def.message?.schema) { 63 // loop: all output schema definitions 64 return assertValidOneOf( 65 lexicons, 66 "Message", 67 def.message.schema, 68 value, 69 true, 70 ); 71 } 72} 73 74function assertValidOneOf( 75 lexicons: Lexicons, 76 path: string, 77 def: LexRefVariant | LexUserType, 78 value: unknown, 79 mustBeObj = false, 80) { 81 const res = validateOneOf(lexicons, path, def, value, mustBeObj); 82 if (!res.success) throw res.error; 83 return res.value; 84}