a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
atproto bluesky typescript npm
101
fork

Configure Feed

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

refactor(lex-cli): make use of lexicon-doc's new ref parsing utilities

Mary bfebed5e 2c386c71

+45 -45
+5
.changeset/metal-sheep-notice.md
··· 1 + --- 2 + '@atcute/lex-cli': patch 3 + --- 4 + 5 + make use of lexicon-doc's new ref parsing utilities
+40 -45
packages/lexicons/lex-cli/src/codegen.ts
··· 14 14 LexXrpcQuery, 15 15 LexXrpcSubscription, 16 16 } from '@atcute/lexicon-doc'; 17 + import { formatLexiconRef, parseLexiconRef, type ParsedLexiconRef } from '@atcute/lexicon-doc'; 17 18 18 19 import * as prettier from 'prettier'; 19 20 ··· 49 50 50 51 const lit: (val: Literal | Literal[]) => string = JSON.stringify; 51 52 52 - interface LexPath { 53 - nsid: string; 54 - defId: string; 55 - } 56 - 57 - const toLexUri = (path: LexPath): string => { 58 - const { nsid, defId } = path; 59 - return defId === 'main' ? nsid : `${nsid}#${defId}`; 60 - }; 61 - 62 - const resolvePath = (from: LexPath, ref: string): LexPath => { 63 - const index = ref.indexOf('#'); 64 - 65 - // nsid (no hash) 66 - if (index === -1) { 67 - return { nsid: ref, defId: 'main' }; 68 - } 69 - 70 - // #defId (local ref) 71 - if (index === 0) { 72 - return { nsid: from.nsid, defId: ref.slice(1) }; 73 - } 74 - 75 - // nsid#defId (full ref) 76 - return { nsid: ref.slice(0, index), defId: ref.slice(index + 1) }; 53 + const resolvePath = (from: ParsedLexiconRef, ref: string): ParsedLexiconRef => { 54 + return parseLexiconRef(ref, from.nsid); 77 55 }; 78 56 79 57 const resolveExternalImport = (nsid: string, mappings: ImportMapping[]): ImportMapping | undefined => { ··· 139 117 140 118 for (const defId of sortedDefIds) { 141 119 const def = doc.defs[defId]; 142 - const path: LexPath = { nsid: doc.id, defId }; 120 + const path: ParsedLexiconRef = { nsid: doc.id, defId }; 143 121 144 122 const camelcased = toCamelCase(defId); 145 123 const varname = `${camelcased}Schema`; ··· 153 131 154 132 file.ambients += `declare module '@atcute/lexicons/ambient' {\n`; 155 133 file.ambients += ` interface XRPCQueries {\n`; 156 - file.ambients += ` ${lit(toLexUri(path))}: ${camelcased}Schema;\n`; 134 + file.ambients += ` ${lit(formatLexiconRef(path))}: ${camelcased}Schema;\n`; 157 135 file.ambients += ` }\n`; 158 136 file.ambients += `}`; 159 137 break; ··· 165 143 166 144 file.ambients += `declare module '@atcute/lexicons/ambient' {\n`; 167 145 file.ambients += ` interface XRPCProcedures {\n`; 168 - file.ambients += ` ${lit(toLexUri(path))}: ${camelcased}Schema;\n`; 146 + file.ambients += ` ${lit(formatLexiconRef(path))}: ${camelcased}Schema;\n`; 169 147 file.ambients += ` }\n`; 170 148 file.ambients += `}`; 171 149 break; ··· 177 155 178 156 file.ambients += `declare module '@atcute/lexicons/ambient' {\n`; 179 157 file.ambients += ` interface XRPCSubscriptions {\n`; 180 - file.ambients += ` ${lit(toLexUri(path))}: ${camelcased}Schema;\n`; 158 + file.ambients += ` ${lit(formatLexiconRef(path))}: ${camelcased}Schema;\n`; 181 159 file.ambients += ` }\n`; 182 160 file.ambients += `}`; 183 161 break; ··· 193 171 194 172 file.ambients += `declare module '@atcute/lexicons/ambient' {\n`; 195 173 file.ambients += ` interface Records {\n`; 196 - file.ambients += ` ${lit(toLexUri(path))}: ${camelcased}Schema;\n`; 174 + file.ambients += ` ${lit(formatLexiconRef(path))}: ${camelcased}Schema;\n`; 197 175 file.ambients += ` }\n`; 198 176 file.ambients += `}`; 199 177 break; 200 178 } 201 179 case 'token': { 202 - result = `${PURE} v.literal(${lit(toLexUri(path))})`; 180 + result = `${PURE} v.literal(${lit(formatLexiconRef(path))})`; 203 181 break; 204 182 } 205 183 case 'permission-set': { ··· 409 387 return { files }; 410 388 }; 411 389 412 - const generateXrpcQuery = (imports: ImportSet, path: LexPath, spec: LexXrpcQuery): string => { 390 + const generateXrpcQuery = (imports: ImportSet, path: ParsedLexiconRef, spec: LexXrpcQuery): string => { 413 391 const params = generateXrpcParameters(imports, path, spec.parameters); 414 392 const output = generateXrpcBody(imports, path, spec.output); 415 393 416 - return `${PURE} v.query(${lit(toLexUri(path))}, {\n"params": ${params}, "output": ${output} })`; 394 + return `${PURE} v.query(${lit(formatLexiconRef(path))}, {\n"params": ${params}, "output": ${output} })`; 417 395 }; 418 396 419 - const generateXrpcProcedure = (imports: ImportSet, path: LexPath, spec: LexXrpcProcedure): string => { 397 + const generateXrpcProcedure = ( 398 + imports: ImportSet, 399 + path: ParsedLexiconRef, 400 + spec: LexXrpcProcedure, 401 + ): string => { 420 402 const params = generateXrpcParameters(imports, path, spec.parameters); 421 403 const input = generateXrpcBody(imports, path, spec.input); 422 404 const output = generateXrpcBody(imports, path, spec.output); 423 405 424 - return `${PURE} v.procedure(${lit(toLexUri(path))}, {\n"params": ${params}, "input": ${input}, "output": ${output} })`; 406 + return `${PURE} v.procedure(${lit(formatLexiconRef(path))}, {\n"params": ${params}, "input": ${input}, "output": ${output} })`; 425 407 }; 426 408 427 - const generateXrpcSubscription = (imports: ImportSet, path: LexPath, spec: LexXrpcSubscription): string => { 409 + const generateXrpcSubscription = ( 410 + imports: ImportSet, 411 + path: ParsedLexiconRef, 412 + spec: LexXrpcSubscription, 413 + ): string => { 428 414 const schema = spec.message?.schema; 429 415 430 416 const params = generateXrpcParameters(imports, path, spec.parameters); ··· 441 427 inner += `"message": null,`; 442 428 } 443 429 444 - return `${PURE} v.subscription(${lit(toLexUri(path))}, {\n${inner}})`; 430 + return `${PURE} v.subscription(${lit(formatLexiconRef(path))}, {\n${inner}})`; 445 431 }; 446 432 447 - const generateXrpcBody = (imports: ImportSet, path: LexPath, spec: LexXrpcBody | undefined): string => { 433 + const generateXrpcBody = ( 434 + imports: ImportSet, 435 + path: ParsedLexiconRef, 436 + spec: LexXrpcBody | undefined, 437 + ): string => { 448 438 if (spec === undefined) { 449 439 return `null`; 450 440 } ··· 489 479 490 480 const generateXrpcParameters = ( 491 481 imports: ImportSet, 492 - path: LexPath, 482 + path: ParsedLexiconRef, 493 483 spec: LexXrpcParameters | undefined, 494 484 ): string => { 495 485 if (spec === undefined) { ··· 529 519 return generateObject(imports, path, mask, 'none'); 530 520 }; 531 521 532 - const generateRecord = (imports: ImportSet, path: LexPath, spec: LexRecord): string => { 522 + const generateRecord = (imports: ImportSet, path: ParsedLexiconRef, spec: LexRecord): string => { 533 523 const schema = generateObject(imports, path, spec.record, 'required'); 534 524 535 525 let key = `${PURE} v.string()`; ··· 548 538 549 539 const generateObject = ( 550 540 imports: ImportSet, 551 - path: LexPath, 541 + path: ParsedLexiconRef, 552 542 spec: LexObject, 553 543 writeType: 'required' | 'optional' | 'none' = 'optional', 554 544 ): string => { ··· 559 549 560 550 switch (writeType) { 561 551 case 'optional': { 562 - inner += `"$type": ${PURE} v.optional(${PURE} v.literal(${lit(toLexUri(path))})),`; 552 + inner += `"$type": ${PURE} v.optional(${PURE} v.literal(${lit(formatLexiconRef(path))})),`; 563 553 break; 564 554 } 565 555 case 'required': { 566 - inner += `"$type": ${PURE} v.literal(${lit(toLexUri(path))}),`; 556 + inner += `"$type": ${PURE} v.literal(${lit(formatLexiconRef(path))}),`; 567 557 break; 568 558 } 569 559 } ··· 715 705 return res; 716 706 }; 717 707 718 - const generateType = (imports: ImportSet, path: LexPath, spec: LexDefinableField, lazy = false): string => { 708 + const generateType = ( 709 + imports: ImportSet, 710 + path: ParsedLexiconRef, 711 + spec: LexDefinableField, 712 + lazy = false, 713 + ): string => { 719 714 switch (spec.type) { 720 715 // LexRefVariant 721 716 case 'ref': { ··· 732 727 const refs = spec.refs 733 728 .map((ref) => { 734 729 const refPath = resolvePath(path, ref); 735 - return { path: refPath, uri: toLexUri(refPath) }; 730 + return { path: refPath, uri: formatLexiconRef(refPath) }; 736 731 }) 737 732 .sort((a, b) => { 738 733 if (a.uri < b.uri) {