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 448 lines 12 kB view raw
1import { 2 type $Type, 3 $type, 4 type $TypeOf, 5 type LexiconRecordKey, 6 type NsidString, 7 type Restricted, 8} from "./core.ts"; 9import type { Infer, PropertyKey, Validator } from "./validation.ts"; 10import { 11 ArraySchema, 12 type ArraySchemaOptions, 13 BlobSchema, 14 type BlobSchemaOptions, 15 BooleanSchema, 16 type BooleanSchemaOptions, 17 BytesSchema, 18 type BytesSchemaOptions, 19 CidSchema, 20 type CidSchemaOptions, 21 type CustomAssertion, 22 CustomSchema, 23 DictSchema, 24 DiscriminatedUnionSchema, 25 type DiscriminatedUnionVariants, 26 EnumSchema, 27 type EnumSchemaOptions, 28 type InferPayload, 29 type InferPayloadBody, 30 type InferPayloadEncoding, 31 IntegerSchema, 32 type IntegerSchemaOptions, 33 IntersectionSchema, 34 LiteralSchema, 35 type LiteralSchemaOptions, 36 NeverSchema, 37 NullableSchema, 38 NullSchema, 39 ObjectSchema, 40 type ObjectSchemaShape, 41 OptionalSchema, 42 ParamsSchema, 43 type ParamsSchemaShape, 44 Payload, 45 type PayloadBody, 46 Permission, 47 type PermissionOptions, 48 PermissionSet, 49 type PermissionSetOptions, 50 Procedure, 51 Query, 52 RecordSchema, 53 refine, 54 RefSchema, 55 type RefSchemaGetter, 56 RegexpSchema, 57 StringSchema, 58 type StringSchemaOptions, 59 Subscription, 60 TokenSchema, 61 TypedObjectSchema, 62 type TypedRefGetter, 63 TypedRefSchema, 64 TypedUnionSchema, 65 UnionSchema, 66 type UnionSchemaValidators, 67 type UnknownObjectOutput, 68 UnknownObjectSchema, 69 UnknownSchema, 70} from "./schema.ts"; 71 72export * from "./core.ts"; 73export * from "./schema.ts"; 74export * from "./validation.ts"; 75 76export { refine }; 77 78export type BinaryData = Restricted<"Binary data">; 79 80export type InferMethodParams< 81 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 82> = M extends Procedure<any, infer P extends ParamsSchema, any, any, any> 83 ? Infer<P> 84 : M extends Query<any, infer P extends ParamsSchema, any, any> ? Infer<P> 85 : M extends Subscription<any, infer P extends ParamsSchema, any, any> 86 ? Infer<P> 87 : never; 88 89export type InferMethodInput< 90 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 91 B = BinaryData, 92> = M extends Procedure<any, any, infer I extends Payload, any, any> 93 ? InferPayload<I, B> 94 : undefined; 95 96export type InferMethodInputBody< 97 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 98 B = BinaryData, 99> = M extends Procedure<any, any, infer I extends Payload, any, any> 100 ? InferPayloadBody<I, B> 101 : undefined; 102 103export type InferMethodInputEncoding< 104 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 105> = M extends Procedure<any, any, infer I extends Payload, any, any> 106 ? InferPayloadEncoding<I> 107 : undefined; 108 109export type InferMethodOutput< 110 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 111 B = BinaryData, 112> = M extends Procedure<any, any, any, infer O extends Payload, any> 113 ? InferPayload<O, B> 114 : M extends Query<any, any, infer O extends Payload, any> ? InferPayload<O, B> 115 : undefined; 116 117export type InferMethodOutputBody< 118 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 119 B = BinaryData, 120> = M extends Procedure<any, any, any, infer O extends Payload, any> 121 ? InferPayloadBody<O, B> 122 : M extends Query<any, any, infer O extends Payload, any> 123 ? InferPayloadBody<O, B> 124 : undefined; 125 126export type InferMethodOutputEncoding< 127 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 128> = M extends Procedure<any, any, any, infer O extends Payload, any> 129 ? InferPayloadEncoding<O> 130 : M extends Query<any, any, infer O extends Payload, any> 131 ? InferPayloadEncoding<O> 132 : undefined; 133 134export type InferMethodMessage< 135 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 136> = M extends Subscription<any, any, infer T, any> 137 ? T extends Validator ? Infer<T> 138 : undefined 139 : undefined; 140 141export type InferMethodError< 142 M extends Procedure | Query | Subscription = Procedure | Query | Subscription, 143> = M extends { errors: readonly (infer E extends string)[] } ? E : never; 144 145export function never(): NeverSchema { 146 return new NeverSchema(); 147} 148 149export function unknown(): UnknownSchema { 150 return new UnknownSchema(); 151} 152 153function _null(): NullSchema { 154 return new NullSchema(); 155} 156export { _null as null }; 157 158export function literal<const V extends null | string | number | boolean>( 159 value: V, 160 options?: LiteralSchemaOptions<V>, 161): LiteralSchema<V> { 162 return new LiteralSchema<V>(value, options); 163} 164 165function _enum<const V extends null | string | number | boolean>( 166 values: readonly V[], 167 options?: EnumSchemaOptions<V>, 168): EnumSchema<V> { 169 return new EnumSchema<V>(values, options); 170} 171export { _enum as enum }; 172 173export function boolean(options?: BooleanSchemaOptions): BooleanSchema { 174 return new BooleanSchema(options ?? {}); 175} 176 177export function integer(options?: IntegerSchemaOptions): IntegerSchema { 178 return new IntegerSchema(options ?? {}); 179} 180 181export function cidLink(options?: CidSchemaOptions): CidSchema { 182 return new CidSchema(options ?? {}); 183} 184 185export function bytes(options?: BytesSchemaOptions): BytesSchema { 186 return new BytesSchema(options ?? {}); 187} 188 189export function blob<O extends BlobSchemaOptions = NonNullable<unknown>>( 190 options: O = {} as O, 191): BlobSchema<O> { 192 return new BlobSchema(options); 193} 194 195export function string< 196 const O extends StringSchemaOptions = NonNullable<unknown>, 197>(options: StringSchemaOptions & O = {} as O): StringSchema<O> { 198 return new StringSchema<O>(options); 199} 200 201export function regexp<T extends string = string>( 202 pattern: RegExp, 203): RegexpSchema<T> { 204 return new RegexpSchema<T>(pattern); 205} 206 207export function array<const S extends Validator>( 208 items: S, 209 options?: ArraySchemaOptions, 210): ArraySchema<S>; 211export function array<T, const S extends Validator<T> = Validator<T>>( 212 items: S, 213 options?: ArraySchemaOptions, 214): ArraySchema<S>; 215export function array<const S extends Validator>( 216 items: S, 217 options?: ArraySchemaOptions, 218): ArraySchema<S> { 219 return new ArraySchema<S>(items, options ?? {}); 220} 221 222export function object<const P extends ObjectSchemaShape>( 223 properties: P, 224): ObjectSchema<P> { 225 return new ObjectSchema<P>(properties); 226} 227 228export function dict< 229 const K extends Validator<string>, 230 const V extends Validator, 231>(key: K, value: V): DictSchema<K, V> { 232 return new DictSchema<K, V>(key, value); 233} 234 235export type { UnknownObjectOutput as UnknownObject }; 236 237export function unknownObject(): UnknownObjectSchema { 238 return new UnknownObjectSchema(); 239} 240 241export function ref<T>(get: RefSchemaGetter<T>): RefSchema<T> { 242 return new RefSchema<T>(get); 243} 244 245export function custom<T>( 246 assertion: CustomAssertion<T>, 247 message: string, 248 path?: PropertyKey | readonly PropertyKey[], 249): CustomSchema<T> { 250 return new CustomSchema<T>(assertion, message, path); 251} 252 253export function nullable<const S extends Validator>( 254 schema: S, 255): NullableSchema<Infer<S>> { 256 return new NullableSchema<Infer<S>>(schema); 257} 258 259export function optional<const S extends Validator>( 260 schema: S, 261): OptionalSchema<Infer<S>> { 262 return new OptionalSchema<Infer<S>>(schema); 263} 264 265export function union<const V extends UnionSchemaValidators>( 266 validators: V, 267): UnionSchema<V> { 268 return new UnionSchema<V>(validators); 269} 270 271export function intersection< 272 const Left extends ObjectSchema, 273 const Right extends DictSchema, 274>(left: Left, right: Right): IntersectionSchema<Left, Right> { 275 return new IntersectionSchema<Left, Right>(left, right); 276} 277 278export function discriminatedUnion< 279 const Discriminator extends string, 280 const Options extends DiscriminatedUnionVariants<Discriminator>, 281>( 282 discriminator: Discriminator, 283 variants: Options, 284): DiscriminatedUnionSchema<Discriminator, Options> { 285 return new DiscriminatedUnionSchema<Discriminator, Options>( 286 discriminator, 287 variants, 288 ); 289} 290 291export function token<const N extends NsidString, const H extends string>( 292 nsid: N, 293 hash: H, 294): TokenSchema<$Type<N, H>> { 295 return new TokenSchema($type(nsid, hash)); 296} 297 298export function typedRef<const V extends { $type?: string }>( 299 get: TypedRefGetter<V>, 300): TypedRefSchema<V> { 301 return new TypedRefSchema<V>(get); 302} 303 304export function typedUnion< 305 const R extends readonly TypedRefSchema[], 306 const C extends boolean, 307>(refs: R, closed: C): TypedUnionSchema<R, C> { 308 return new TypedUnionSchema<R, C>(refs, closed); 309} 310 311export function typedObject< 312 const N extends NsidString, 313 const H extends string, 314 const S extends Validator<{ [_ in string]?: unknown }>, 315>(nsid: N, hash: H, schema: S): TypedObjectSchema<$Type<N, H>, S>; 316export function typedObject<V extends { $type?: $Type }>( 317 nsid: V extends { $type?: infer T extends string } 318 ? T extends `${infer N}#${string}` ? N : T 319 : never, 320 hash: V extends { $type?: infer T extends string } 321 ? T extends `${string}#${infer H}` ? H : "main" 322 : never, 323 schema: Validator<Omit<V, "$type">>, 324): TypedObjectSchema<$TypeOf<V>, Validator<Omit<V, "$type">>>; 325export function typedObject< 326 const N extends NsidString, 327 const H extends string, 328 const S extends Validator<{ [_ in string]?: unknown }>, 329>(nsid: N, hash: H, schema: S) { 330 return new TypedObjectSchema<$Type<N, H>, S>($type(nsid, hash), schema); 331} 332 333type AsNsid<T> = T extends `${string}#${string}` ? never : T; 334 335export function record< 336 const K extends LexiconRecordKey, 337 const T extends NsidString, 338 const S extends Validator<{ [_ in string]?: unknown }>, 339>(key: K, type: AsNsid<T>, schema: S): RecordSchema<K, T, S>; 340export function record< 341 const K extends LexiconRecordKey, 342 const V extends { $type: NsidString }, 343>( 344 key: K, 345 type: AsNsid<V["$type"]>, 346 schema: Validator<Omit<V, "$type">>, 347): RecordSchema<K, V["$type"], Validator<Omit<V, "$type">>>; 348export function record< 349 const K extends LexiconRecordKey, 350 const T extends NsidString, 351 const S extends Validator<{ [_ in string]?: unknown }>, 352>(key: K, type: T, schema: S) { 353 return new RecordSchema<K, T, S>(key, type, schema); 354} 355 356export function params< 357 const P extends ParamsSchemaShape = NonNullable<unknown>, 358>(properties: P = {} as P): ParamsSchema<P> { 359 return new ParamsSchema<P>(properties); 360} 361 362export const paramsSchema: ParamsSchema<Record<PropertyKey, never>> = 363 new ParamsSchema({}); 364 365export function payload< 366 const E extends string | undefined = undefined, 367 const S extends PayloadBody<E> = undefined, 368>( 369 encoding: E = undefined as E, 370 schema: S = undefined as S, 371): Payload<E, S> { 372 return new Payload<E, S>(encoding, schema); 373} 374 375export function jsonPayload<const P extends ObjectSchemaShape>( 376 properties: P, 377): Payload<"application/json", ObjectSchema<P>> { 378 return payload("application/json", object(properties)); 379} 380 381export function query< 382 const N extends NsidString, 383 const P extends ParamsSchema, 384 const O extends Payload, 385 const E extends undefined | readonly string[] = undefined, 386>( 387 nsid: N, 388 parameters: P, 389 output: O, 390 errors: E = undefined as E, 391): Query<N, P, O, E> { 392 return new Query<N, P, O, E>(nsid, parameters, output, errors); 393} 394 395export function procedure< 396 const N extends NsidString, 397 const P extends ParamsSchema, 398 const I extends Payload, 399 const O extends Payload, 400 const E extends undefined | readonly string[] = undefined, 401>( 402 nsid: N, 403 parameters: P, 404 input: I, 405 output: O, 406 errors: E = undefined as E, 407): Procedure<N, P, I, O, E> { 408 return new Procedure<N, P, I, O, E>(nsid, parameters, input, output, errors); 409} 410 411export function subscription< 412 const N extends NsidString, 413 const P extends ParamsSchema, 414 const M extends 415 | undefined 416 | RefSchema 417 | TypedUnionSchema 418 | ObjectSchema, 419 const E extends undefined | readonly string[] = undefined, 420>( 421 nsid: N, 422 parameters: P, 423 message: M, 424 errors: E = undefined as E, 425): Subscription<N, P, M, E> { 426 return new Subscription<N, P, M, E>(nsid, parameters, message, errors); 427} 428 429export function permission< 430 const R extends string, 431 const O extends PermissionOptions, 432>( 433 resource: R, 434 options: PermissionOptions & O = {} as O, 435): Permission<R, O> { 436 return new Permission<R, O>(resource, options); 437} 438 439export function permissionSet< 440 const N extends NsidString, 441 const P extends readonly Permission[], 442>( 443 nsid: N, 444 permissions: P, 445 options?: PermissionSetOptions, 446): PermissionSet<N, P> { 447 return new PermissionSet<N, P>(nsid, permissions, options); 448}