prototypey.org - atproto lexicon typescript toolkit - mirror https://github.com/tylersayshi/prototypey

jsdoc generate everything from https://atproto.com/specs/lexicon

Tyler 4ce3e955 de262808

+235 -12
+235 -12
src/lib.ts
··· 24 | "procedure" 25 | "subscription"; 26 27 interface LexiconItemCommonOptions { 28 required?: boolean; 29 nullable?: boolean; 30 } 31 32 interface LexiconItem extends LexiconItemCommonOptions { 33 type: LexiconType; 34 } 35 36 type Def = { 37 type: LexiconType; 38 }; 39 40 interface LexiconNamespace { 41 id: string; 42 defs: { 43 [name: string]: Def; 44 }; 45 } 46 47 interface StringOptions extends LexiconItemCommonOptions { 48 format?: 49 - | "at-identifier" 50 - | "at-uri" 51 - | "cid" 52 - | "datetime" 53 - | "did" 54 - | "handle" 55 - | "nsid" 56 - | "tid" 57 - | "record-key" 58 - | "uri" 59 - | "language"; 60 maxLength?: number; 61 minLength?: number; 62 maxGraphemes?: number; 63 minGraphemes?: number; 64 knownValues?: string[]; 65 enum?: string[]; 66 default?: string; 67 const?: string; 68 } 69 70 interface BooleanOptions extends LexiconItemCommonOptions { 71 default?: boolean; 72 const?: boolean; 73 } 74 75 interface IntegerOptions extends LexiconItemCommonOptions { 76 minimum?: number; 77 maximum?: number; 78 enum?: number[]; 79 default?: number; 80 const?: number; 81 } 82 83 interface BytesOptions extends LexiconItemCommonOptions { 84 minLength?: number; 85 maxLength?: number; 86 } 87 88 interface BlobOptions extends LexiconItemCommonOptions { 89 accept?: string[]; 90 maxSize?: number; 91 } 92 93 interface ArrayOptions extends LexiconItemCommonOptions { 94 minLength?: number; 95 maxLength?: number; 96 } 97 98 interface RecordOptions { 99 key: "self" | "tid"; 100 record: { type: "object" }; 101 description?: string; 102 } 103 104 interface UnionOptions extends LexiconItemCommonOptions { 105 closed?: boolean; 106 } 107 108 interface ObjectProperties { 109 [key: string]: LexiconItem; 110 } 111 112 interface ObjectResult<T extends ObjectProperties> { 113 type: "object"; 114 properties: T; 115 required?: string[]; 116 nullable?: string[]; 117 } 118 119 interface ParamsProperties { 120 [key: string]: LexiconItem; 121 } 122 123 interface ParamsResult<T extends ParamsProperties> { 124 type: "params"; 125 properties: T; 126 required?: string[]; 127 } 128 129 interface BodySchema { 130 encoding: "application/json" | (string & {}); 131 description?: string; 132 schema?: ObjectResult<ObjectProperties>; 133 } 134 135 interface ErrorDef { 136 name: string; 137 description?: string; 138 } 139 140 interface QueryOptions { 141 description?: string; 142 parameters?: ParamsResult<ParamsProperties>; 143 output?: BodySchema; 144 errors?: ErrorDef[]; 145 } 146 147 interface ProcedureOptions { 148 description?: string; 149 parameters?: ParamsResult<ParamsProperties>; 150 input?: BodySchema; 151 output?: BodySchema; 152 errors?: ErrorDef[]; 153 } 154 155 interface MessageSchema { 156 description?: string; 157 schema: { type: "union"; refs: readonly string[] }; 158 } 159 160 interface SubscriptionOptions { 161 description?: string; 162 parameters?: ParamsResult<ParamsProperties>; 163 message?: MessageSchema; 164 errors?: ErrorDef[]; 165 } 166 167 /** 168 - * Main API: Create a namespace (lexicon document) 169 */ 170 export const lx = { 171 null( 172 options?: LexiconItemCommonOptions, 173 ): { type: "null" } & LexiconItemCommonOptions { ··· 176 ...options, 177 }; 178 }, 179 boolean<T extends BooleanOptions>(options?: T): T & { type: "boolean" } { 180 return { 181 type: "boolean", 182 ...options, 183 } as T & { type: "boolean" }; 184 }, 185 integer<T extends IntegerOptions>(options?: T): T & { type: "integer" } { 186 return { 187 type: "integer", 188 ...options, 189 } as T & { type: "integer" }; 190 }, 191 string<T extends StringOptions>(options?: T): T & { type: "string" } { 192 return { 193 type: "string", 194 ...options, 195 } as T & { type: "string" }; 196 }, 197 unknown( 198 options?: LexiconItemCommonOptions, 199 ): { type: "unknown" } & LexiconItemCommonOptions { ··· 202 ...options, 203 }; 204 }, 205 bytes<T extends BytesOptions>(options?: T): T & { type: "bytes" } { 206 return { 207 type: "bytes", 208 ...options, 209 } as T & { type: "bytes" }; 210 }, 211 cidLink<Link extends string>(link: Link): { type: "cid-link"; $link: Link } { 212 return { 213 type: "cid-link", 214 $link: link, 215 }; 216 }, 217 blob<T extends BlobOptions>(options?: T): T & { type: "blob" } { 218 return { 219 type: "blob", 220 ...options, 221 } as T & { type: "blob" }; 222 }, 223 array<Items extends LexiconItem, Options extends ArrayOptions>( 224 items: Items, 225 options?: Options, ··· 230 ...options, 231 } as Options & { type: "array"; items: Items }; 232 }, 233 token<Description extends string>( 234 description: Description, 235 ): { type: "token"; description: Description } { 236 return { type: "token", description }; 237 }, 238 ref<Ref extends string>( 239 ref: Ref, 240 options?: LexiconItemCommonOptions, ··· 245 ...options, 246 } as LexiconItemCommonOptions & { type: "ref"; ref: Ref }; 247 }, 248 union<const Refs extends readonly string[], Options extends UnionOptions>( 249 refs: Refs, 250 options?: Options, ··· 255 ...options, 256 } as Options & { type: "union"; refs: Refs }; 257 }, 258 record<T extends RecordOptions>(options: T): T & { type: "record" } { 259 return { 260 type: "record", 261 ...options, 262 }; 263 }, 264 object<T extends ObjectProperties>(options: T): ObjectResult<T> { 265 const required = Object.keys(options).filter( 266 (key) => options[key].required, ··· 280 } 281 return result; 282 }, 283 params<Properties extends ParamsProperties>( 284 properties: Properties, 285 ): ParamsResult<Properties> { ··· 299 } 300 return result; 301 }, 302 query<T extends QueryOptions>(options?: T): T & { type: "query" } { 303 return { 304 type: "query", 305 ...options, 306 } as T & { type: "query" }; 307 }, 308 procedure<T extends ProcedureOptions>( 309 options?: T, 310 ): T & { type: "procedure" } { ··· 313 ...options, 314 } as T & { type: "procedure" }; 315 }, 316 subscription<T extends SubscriptionOptions>( 317 options?: T, 318 ): T & { type: "subscription" } { ··· 321 ...options, 322 } as T & { type: "subscription" }; 323 }, 324 namespace<ID extends string, D extends LexiconNamespace["defs"]>( 325 id: ID, 326 defs: D,
··· 24 | "procedure" 25 | "subscription"; 26 27 + /** 28 + * Common options available for lexicon items. 29 + * @see https://atproto.com/specs/lexicon#string-formats 30 + */ 31 interface LexiconItemCommonOptions { 32 + /** Indicates this field must be provided */ 33 required?: boolean; 34 + /** Indicates this field can be explicitly set to null */ 35 nullable?: boolean; 36 } 37 38 + /** 39 + * Base interface for all lexicon items. 40 + * @see https://atproto.com/specs/lexicon#overview-of-types 41 + */ 42 interface LexiconItem extends LexiconItemCommonOptions { 43 type: LexiconType; 44 } 45 46 + /** 47 + * Definition in a lexicon namespace. 48 + * @see https://atproto.com/specs/lexicon#lexicon-document 49 + */ 50 type Def = { 51 type: LexiconType; 52 }; 53 54 + /** 55 + * Lexicon namespace document structure. 56 + * @see https://atproto.com/specs/lexicon#lexicon-document 57 + */ 58 interface LexiconNamespace { 59 + /** Namespaced identifier (NSID) for this lexicon */ 60 id: string; 61 + /** Named definitions within this namespace */ 62 defs: { 63 [name: string]: Def; 64 }; 65 } 66 67 + /** 68 + * String type options. 69 + * @see https://atproto.com/specs/lexicon#string 70 + */ 71 interface StringOptions extends LexiconItemCommonOptions { 72 + /** 73 + * Semantic string format constraint. 74 + * @see https://atproto.com/specs/lexicon#string-formats 75 + */ 76 format?: 77 + | "at-identifier" // Handle or DID 78 + | "at-uri" // AT Protocol URI 79 + | "cid" // Content Identifier 80 + | "datetime" // Timestamp (UTC, ISO 8601) 81 + | "did" // Decentralized Identifier 82 + | "handle" // User handle identifier 83 + | "nsid" // Namespaced Identifier 84 + | "tid" // Timestamp Identifier 85 + | "record-key" // Repository record key 86 + | "uri" // Generic URI 87 + | "language"; // IETF BCP 47 language tag 88 + /** Maximum string length in bytes */ 89 maxLength?: number; 90 + /** Minimum string length in bytes */ 91 minLength?: number; 92 + /** Maximum string length in Unicode graphemes */ 93 maxGraphemes?: number; 94 + /** Minimum string length in Unicode graphemes */ 95 minGraphemes?: number; 96 + /** Hints at expected values, not enforced */ 97 knownValues?: string[]; 98 + /** Restricts to an exact set of string values */ 99 enum?: string[]; 100 + /** Default value if not provided */ 101 default?: string; 102 + /** Fixed, unchangeable value */ 103 const?: string; 104 } 105 106 + /** 107 + * Boolean type options. 108 + * @see https://atproto.com/specs/lexicon#boolean 109 + */ 110 interface BooleanOptions extends LexiconItemCommonOptions { 111 + /** Default value if not provided */ 112 default?: boolean; 113 + /** Fixed, unchangeable value */ 114 const?: boolean; 115 } 116 117 + /** 118 + * Integer type options. 119 + * @see https://atproto.com/specs/lexicon#integer 120 + */ 121 interface IntegerOptions extends LexiconItemCommonOptions { 122 + /** Minimum allowed value (inclusive) */ 123 minimum?: number; 124 + /** Maximum allowed value (inclusive) */ 125 maximum?: number; 126 + /** Restricts to an exact set of integer values */ 127 enum?: number[]; 128 + /** Default value if not provided */ 129 default?: number; 130 + /** Fixed, unchangeable value */ 131 const?: number; 132 } 133 134 + /** 135 + * Bytes type options for arbitrary byte arrays. 136 + * @see https://atproto.com/specs/lexicon#bytes 137 + */ 138 interface BytesOptions extends LexiconItemCommonOptions { 139 + /** Minimum byte array length */ 140 minLength?: number; 141 + /** Maximum byte array length */ 142 maxLength?: number; 143 } 144 145 + /** 146 + * Blob type options for binary data with MIME types. 147 + * @see https://atproto.com/specs/lexicon#blob 148 + */ 149 interface BlobOptions extends LexiconItemCommonOptions { 150 + /** Allowed MIME types (e.g., ["image/png", "image/jpeg"]) */ 151 accept?: string[]; 152 + /** Maximum blob size in bytes */ 153 maxSize?: number; 154 } 155 156 + /** 157 + * Array type options. 158 + * @see https://atproto.com/specs/lexicon#array 159 + */ 160 interface ArrayOptions extends LexiconItemCommonOptions { 161 + /** Minimum array length */ 162 minLength?: number; 163 + /** Maximum array length */ 164 maxLength?: number; 165 } 166 167 + /** 168 + * Record type options for repository records. 169 + * @see https://atproto.com/specs/lexicon#record 170 + */ 171 interface RecordOptions { 172 + /** Record key strategy: "self" for self-describing or "tid" for timestamp IDs */ 173 key: "self" | "tid"; 174 + /** Object schema defining the record structure */ 175 record: { type: "object" }; 176 + /** Human-readable description */ 177 description?: string; 178 } 179 180 + /** 181 + * Union type options for multiple possible types. 182 + * @see https://atproto.com/specs/lexicon#union 183 + */ 184 interface UnionOptions extends LexiconItemCommonOptions { 185 + /** If true, only listed refs are allowed; if false, additional types may be added */ 186 closed?: boolean; 187 } 188 189 + /** 190 + * Map of property names to their lexicon item definitions. 191 + * @see https://atproto.com/specs/lexicon#object 192 + */ 193 interface ObjectProperties { 194 [key: string]: LexiconItem; 195 } 196 197 + /** 198 + * Resulting object schema with required and nullable fields extracted. 199 + * @see https://atproto.com/specs/lexicon#object 200 + */ 201 interface ObjectResult<T extends ObjectProperties> { 202 type: "object"; 203 + /** Property definitions */ 204 properties: T; 205 + /** List of required property names */ 206 required?: string[]; 207 + /** List of nullable property names */ 208 nullable?: string[]; 209 } 210 211 + /** 212 + * Map of parameter names to their lexicon item definitions. 213 + * @see https://atproto.com/specs/lexicon#params 214 + */ 215 interface ParamsProperties { 216 [key: string]: LexiconItem; 217 } 218 219 + /** 220 + * Resulting params schema with required fields extracted. 221 + * @see https://atproto.com/specs/lexicon#params 222 + */ 223 interface ParamsResult<T extends ParamsProperties> { 224 type: "params"; 225 + /** Parameter definitions */ 226 properties: T; 227 + /** List of required parameter names */ 228 required?: string[]; 229 } 230 231 + /** 232 + * HTTP request or response body schema. 233 + * @see https://atproto.com/specs/lexicon#http-endpoints 234 + */ 235 interface BodySchema { 236 + /** MIME type encoding (typically "application/json") */ 237 encoding: "application/json" | (string & {}); 238 + /** Human-readable description */ 239 description?: string; 240 + /** Object schema defining the body structure */ 241 schema?: ObjectResult<ObjectProperties>; 242 } 243 244 + /** 245 + * Error definition for HTTP endpoints. 246 + * @see https://atproto.com/specs/lexicon#http-endpoints 247 + */ 248 interface ErrorDef { 249 + /** Error name/code */ 250 name: string; 251 + /** Human-readable error description */ 252 description?: string; 253 } 254 255 + /** 256 + * Query endpoint options (HTTP GET). 257 + * @see https://atproto.com/specs/lexicon#query 258 + */ 259 interface QueryOptions { 260 + /** Human-readable description */ 261 description?: string; 262 + /** Query string parameters */ 263 parameters?: ParamsResult<ParamsProperties>; 264 + /** Response body schema */ 265 output?: BodySchema; 266 + /** Possible error responses */ 267 errors?: ErrorDef[]; 268 } 269 270 + /** 271 + * Procedure endpoint options (HTTP POST). 272 + * @see https://atproto.com/specs/lexicon#procedure 273 + */ 274 interface ProcedureOptions { 275 + /** Human-readable description */ 276 description?: string; 277 + /** Query string parameters */ 278 parameters?: ParamsResult<ParamsProperties>; 279 + /** Request body schema */ 280 input?: BodySchema; 281 + /** Response body schema */ 282 output?: BodySchema; 283 + /** Possible error responses */ 284 errors?: ErrorDef[]; 285 } 286 287 + /** 288 + * WebSocket message schema for subscriptions. 289 + * @see https://atproto.com/specs/lexicon#subscription 290 + */ 291 interface MessageSchema { 292 + /** Human-readable description */ 293 description?: string; 294 + /** Union of possible message types */ 295 schema: { type: "union"; refs: readonly string[] }; 296 } 297 298 + /** 299 + * Subscription endpoint options (WebSocket). 300 + * @see https://atproto.com/specs/lexicon#subscription 301 + */ 302 interface SubscriptionOptions { 303 + /** Human-readable description */ 304 description?: string; 305 + /** Query string parameters */ 306 parameters?: ParamsResult<ParamsProperties>; 307 + /** Message schema for events */ 308 message?: MessageSchema; 309 + /** Possible error responses */ 310 errors?: ErrorDef[]; 311 } 312 313 /** 314 + * Main API for creating lexicon schemas. 315 + * @see https://atproto.com/specs/lexicon 316 */ 317 export const lx = { 318 + /** 319 + * Creates a null type. 320 + * @see https://atproto.com/specs/lexicon#null 321 + */ 322 null( 323 options?: LexiconItemCommonOptions, 324 ): { type: "null" } & LexiconItemCommonOptions { ··· 327 ...options, 328 }; 329 }, 330 + /** 331 + * Creates a boolean type with optional constraints. 332 + * @see https://atproto.com/specs/lexicon#boolean 333 + */ 334 boolean<T extends BooleanOptions>(options?: T): T & { type: "boolean" } { 335 return { 336 type: "boolean", 337 ...options, 338 } as T & { type: "boolean" }; 339 }, 340 + /** 341 + * Creates an integer type with optional min/max and enum constraints. 342 + * @see https://atproto.com/specs/lexicon#integer 343 + */ 344 integer<T extends IntegerOptions>(options?: T): T & { type: "integer" } { 345 return { 346 type: "integer", 347 ...options, 348 } as T & { type: "integer" }; 349 }, 350 + /** 351 + * Creates a string type with optional format, length, and value constraints. 352 + * @see https://atproto.com/specs/lexicon#string 353 + */ 354 string<T extends StringOptions>(options?: T): T & { type: "string" } { 355 return { 356 type: "string", 357 ...options, 358 } as T & { type: "string" }; 359 }, 360 + /** 361 + * Creates an unknown type for flexible, unvalidated objects. 362 + * @see https://atproto.com/specs/lexicon#unknown 363 + */ 364 unknown( 365 options?: LexiconItemCommonOptions, 366 ): { type: "unknown" } & LexiconItemCommonOptions { ··· 369 ...options, 370 }; 371 }, 372 + /** 373 + * Creates a bytes type for arbitrary byte arrays. 374 + * @see https://atproto.com/specs/lexicon#bytes 375 + */ 376 bytes<T extends BytesOptions>(options?: T): T & { type: "bytes" } { 377 return { 378 type: "bytes", 379 ...options, 380 } as T & { type: "bytes" }; 381 }, 382 + /** 383 + * Creates a CID link reference to content-addressed data. 384 + * @see https://atproto.com/specs/lexicon#cid-link 385 + */ 386 cidLink<Link extends string>(link: Link): { type: "cid-link"; $link: Link } { 387 return { 388 type: "cid-link", 389 $link: link, 390 }; 391 }, 392 + /** 393 + * Creates a blob type for binary data with MIME type constraints. 394 + * @see https://atproto.com/specs/lexicon#blob 395 + */ 396 blob<T extends BlobOptions>(options?: T): T & { type: "blob" } { 397 return { 398 type: "blob", 399 ...options, 400 } as T & { type: "blob" }; 401 }, 402 + /** 403 + * Creates an array type with item schema and length constraints. 404 + * @see https://atproto.com/specs/lexicon#array 405 + */ 406 array<Items extends LexiconItem, Options extends ArrayOptions>( 407 items: Items, 408 options?: Options, ··· 413 ...options, 414 } as Options & { type: "array"; items: Items }; 415 }, 416 + /** 417 + * Creates a token type for symbolic values in unions. 418 + * @see https://atproto.com/specs/lexicon#token 419 + */ 420 token<Description extends string>( 421 description: Description, 422 ): { type: "token"; description: Description } { 423 return { type: "token", description }; 424 }, 425 + /** 426 + * Creates a reference to another schema definition. 427 + * @see https://atproto.com/specs/lexicon#ref 428 + */ 429 ref<Ref extends string>( 430 ref: Ref, 431 options?: LexiconItemCommonOptions, ··· 436 ...options, 437 } as LexiconItemCommonOptions & { type: "ref"; ref: Ref }; 438 }, 439 + /** 440 + * Creates a union type for multiple possible type variants. 441 + * @see https://atproto.com/specs/lexicon#union 442 + */ 443 union<const Refs extends readonly string[], Options extends UnionOptions>( 444 refs: Refs, 445 options?: Options, ··· 450 ...options, 451 } as Options & { type: "union"; refs: Refs }; 452 }, 453 + /** 454 + * Creates a record type for repository records. 455 + * @see https://atproto.com/specs/lexicon#record 456 + */ 457 record<T extends RecordOptions>(options: T): T & { type: "record" } { 458 return { 459 type: "record", 460 ...options, 461 }; 462 }, 463 + /** 464 + * Creates an object type with defined properties. 465 + * @see https://atproto.com/specs/lexicon#object 466 + */ 467 object<T extends ObjectProperties>(options: T): ObjectResult<T> { 468 const required = Object.keys(options).filter( 469 (key) => options[key].required, ··· 483 } 484 return result; 485 }, 486 + /** 487 + * Creates a params type for query string parameters. 488 + * @see https://atproto.com/specs/lexicon#params 489 + */ 490 params<Properties extends ParamsProperties>( 491 properties: Properties, 492 ): ParamsResult<Properties> { ··· 506 } 507 return result; 508 }, 509 + /** 510 + * Creates a query endpoint definition (HTTP GET). 511 + * @see https://atproto.com/specs/lexicon#query 512 + */ 513 query<T extends QueryOptions>(options?: T): T & { type: "query" } { 514 return { 515 type: "query", 516 ...options, 517 } as T & { type: "query" }; 518 }, 519 + /** 520 + * Creates a procedure endpoint definition (HTTP POST). 521 + * @see https://atproto.com/specs/lexicon#procedure 522 + */ 523 procedure<T extends ProcedureOptions>( 524 options?: T, 525 ): T & { type: "procedure" } { ··· 528 ...options, 529 } as T & { type: "procedure" }; 530 }, 531 + /** 532 + * Creates a subscription endpoint definition (WebSocket). 533 + * @see https://atproto.com/specs/lexicon#subscription 534 + */ 535 subscription<T extends SubscriptionOptions>( 536 options?: T, 537 ): T & { type: "subscription" } { ··· 540 ...options, 541 } as T & { type: "subscription" }; 542 }, 543 + /** 544 + * Creates a lexicon namespace document. 545 + * @see https://atproto.com/specs/lexicon#lexicon-document 546 + */ 547 namespace<ID extends string, D extends LexiconNamespace["defs"]>( 548 id: ID, 549 defs: D,