@atproto/lex-cli 0.5.7 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/codegen/client.js +18 -50
- package/dist/codegen/client.js.map +1 -1
- package/dist/codegen/common.d.ts.map +1 -1
- package/dist/codegen/common.js +173 -22
- package/dist/codegen/common.js.map +1 -1
- package/dist/codegen/lex-gen.d.ts +4 -5
- package/dist/codegen/lex-gen.d.ts.map +1 -1
- package/dist/codegen/lex-gen.js +161 -49
- package/dist/codegen/lex-gen.js.map +1 -1
- package/dist/codegen/server.js +9 -45
- package/dist/codegen/server.js.map +1 -1
- package/package.json +3 -3
- package/src/codegen/client.ts +22 -60
- package/src/codegen/common.ts +182 -27
- package/src/codegen/lex-gen.ts +212 -71
- package/src/codegen/server.ts +12 -55
package/src/codegen/lex-gen.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { relative as getRelativePath } from 'node:path'
|
|
1
|
+
import { relative as getRelativePath } from 'node:path/posix'
|
|
2
2
|
import { SourceFile, VariableDeclarationKind } from 'ts-morph'
|
|
3
3
|
import {
|
|
4
4
|
LexArray,
|
|
@@ -27,6 +27,67 @@ export function genComment<T>(
|
|
|
27
27
|
return commentable as T
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
export function genCommonImports(file: SourceFile, baseNsid: string) {
|
|
31
|
+
//= import {ValidationResult, BlobRef} from '@atproto/lexicon'
|
|
32
|
+
file
|
|
33
|
+
.addImportDeclaration({
|
|
34
|
+
moduleSpecifier: '@atproto/lexicon',
|
|
35
|
+
})
|
|
36
|
+
.addNamedImports([{ name: 'ValidationResult' }, { name: 'BlobRef' }])
|
|
37
|
+
|
|
38
|
+
//= import {CID} from 'multiformats/cid'
|
|
39
|
+
file
|
|
40
|
+
.addImportDeclaration({
|
|
41
|
+
moduleSpecifier: 'multiformats/cid',
|
|
42
|
+
})
|
|
43
|
+
.addNamedImports([{ name: 'CID' }])
|
|
44
|
+
|
|
45
|
+
//= import { validate as _validate } from '../../lexicons.ts'
|
|
46
|
+
file
|
|
47
|
+
.addImportDeclaration({
|
|
48
|
+
moduleSpecifier: `${baseNsid
|
|
49
|
+
.split('.')
|
|
50
|
+
.map((_str) => '..')
|
|
51
|
+
.join('/')}/lexicons`,
|
|
52
|
+
})
|
|
53
|
+
.addNamedImports([{ name: 'validate', alias: '_validate' }])
|
|
54
|
+
|
|
55
|
+
//= import { $Typed, is$typed as _is$typed, OmitKey } from '../[...]/util.ts'
|
|
56
|
+
file
|
|
57
|
+
.addImportDeclaration({
|
|
58
|
+
moduleSpecifier: `${baseNsid
|
|
59
|
+
.split('.')
|
|
60
|
+
.map((_str) => '..')
|
|
61
|
+
.join('/')}/util`,
|
|
62
|
+
})
|
|
63
|
+
.addNamedImports([
|
|
64
|
+
{ name: '$Typed' },
|
|
65
|
+
{ name: 'is$typed', alias: '_is$typed' },
|
|
66
|
+
{ name: 'OmitKey' },
|
|
67
|
+
])
|
|
68
|
+
|
|
69
|
+
// tsc adds protection against circular imports, which hurts bundle size.
|
|
70
|
+
// Since we know that lexicon.ts and util.ts do not depend on the file being
|
|
71
|
+
// generated, we can safely bypass this protection.
|
|
72
|
+
// Note that we are not using `import * as util from '../../util'` because
|
|
73
|
+
// typescript will emit is own helpers for the import, which we want to avoid.
|
|
74
|
+
file.addVariableStatement({
|
|
75
|
+
isExported: false,
|
|
76
|
+
declarationKind: VariableDeclarationKind.Const,
|
|
77
|
+
declarations: [
|
|
78
|
+
{ name: 'is$typed', initializer: '_is$typed' },
|
|
79
|
+
{ name: 'validate', initializer: '_validate' },
|
|
80
|
+
],
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
//= const id = "{baseNsid}"
|
|
84
|
+
file.addVariableStatement({
|
|
85
|
+
isExported: false, // Do not export to allow tree-shaking
|
|
86
|
+
declarationKind: VariableDeclarationKind.Const,
|
|
87
|
+
declarations: [{ name: 'id', initializer: JSON.stringify(baseNsid) }],
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
30
91
|
export function genImports(
|
|
31
92
|
file: SourceFile,
|
|
32
93
|
imports: Set<string>,
|
|
@@ -35,12 +96,13 @@ export function genImports(
|
|
|
35
96
|
const startPath = '/' + baseNsid.split('.').slice(0, -1).join('/')
|
|
36
97
|
|
|
37
98
|
for (const nsid of imports) {
|
|
38
|
-
const targetPath = '/' + nsid.split('.').join('/')
|
|
99
|
+
const targetPath = '/' + nsid.split('.').join('/') + '.js'
|
|
39
100
|
let resolvedPath = getRelativePath(startPath, targetPath)
|
|
40
101
|
if (!resolvedPath.startsWith('.')) {
|
|
41
102
|
resolvedPath = `./${resolvedPath}`
|
|
42
103
|
}
|
|
43
104
|
file.addImportDeclaration({
|
|
105
|
+
isTypeOnly: true,
|
|
44
106
|
moduleSpecifier: resolvedPath,
|
|
45
107
|
namespaceImport: toTitleCase(nsid),
|
|
46
108
|
})
|
|
@@ -61,10 +123,16 @@ export function genUserType(
|
|
|
61
123
|
case 'token':
|
|
62
124
|
genToken(file, lexUri, def)
|
|
63
125
|
break
|
|
64
|
-
case 'object':
|
|
65
|
-
|
|
66
|
-
|
|
126
|
+
case 'object': {
|
|
127
|
+
const ifaceName: string = toTitleCase(getHash(lexUri))
|
|
128
|
+
genObject(file, imports, lexUri, def, ifaceName, {
|
|
129
|
+
typeProperty: true,
|
|
130
|
+
})
|
|
131
|
+
genObjHelpers(file, lexUri, ifaceName, {
|
|
132
|
+
requireTypeProperty: false,
|
|
133
|
+
})
|
|
67
134
|
break
|
|
135
|
+
}
|
|
68
136
|
|
|
69
137
|
case 'blob':
|
|
70
138
|
case 'bytes':
|
|
@@ -83,19 +151,44 @@ export function genUserType(
|
|
|
83
151
|
}
|
|
84
152
|
}
|
|
85
153
|
|
|
86
|
-
|
|
154
|
+
function genObject(
|
|
87
155
|
file: SourceFile,
|
|
88
156
|
imports: Set<string>,
|
|
89
157
|
lexUri: string,
|
|
90
158
|
def: LexObject,
|
|
91
|
-
ifaceName
|
|
92
|
-
|
|
159
|
+
ifaceName: string,
|
|
160
|
+
{
|
|
161
|
+
defaultsArePresent = true,
|
|
162
|
+
allowUnknownProperties = false,
|
|
163
|
+
typeProperty = false,
|
|
164
|
+
}: {
|
|
165
|
+
defaultsArePresent?: boolean
|
|
166
|
+
allowUnknownProperties?: boolean
|
|
167
|
+
typeProperty?: boolean | 'required'
|
|
168
|
+
} = {},
|
|
93
169
|
) {
|
|
94
170
|
const iface = file.addInterface({
|
|
95
|
-
name: ifaceName
|
|
171
|
+
name: ifaceName,
|
|
96
172
|
isExported: true,
|
|
97
173
|
})
|
|
98
174
|
genComment(iface, def)
|
|
175
|
+
|
|
176
|
+
if (typeProperty) {
|
|
177
|
+
const hash = getHash(lexUri)
|
|
178
|
+
const baseNsid = stripScheme(stripHash(lexUri))
|
|
179
|
+
|
|
180
|
+
//= $type?: <uri>
|
|
181
|
+
iface.addProperty({
|
|
182
|
+
name: typeProperty === 'required' ? `$type` : `$type?`,
|
|
183
|
+
type:
|
|
184
|
+
// Not using $Type here because it is less readable than a plain string
|
|
185
|
+
// `$Type<${JSON.stringify(baseNsid)}, ${JSON.stringify(hash)}>`
|
|
186
|
+
hash === 'main'
|
|
187
|
+
? JSON.stringify(`${baseNsid}`)
|
|
188
|
+
: JSON.stringify(`${baseNsid}#${hash}`),
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
|
|
99
192
|
const nullableProps = new Set(def.nullable)
|
|
100
193
|
if (def.properties) {
|
|
101
194
|
for (const propKey in def.properties) {
|
|
@@ -108,12 +201,12 @@ export function genObject(
|
|
|
108
201
|
propDef.default !== undefined)
|
|
109
202
|
if (propDef.type === 'ref' || propDef.type === 'union') {
|
|
110
203
|
//= propName: External|External
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
204
|
+
const types =
|
|
205
|
+
propDef.type === 'union'
|
|
206
|
+
? propDef.refs.map((ref) => refToUnionType(ref, lexUri, imports))
|
|
207
|
+
: [refToType(propDef.ref, stripScheme(stripHash(lexUri)), imports)]
|
|
115
208
|
if (propDef.type === 'union' && !propDef.closed) {
|
|
116
|
-
types.push('{$type: string
|
|
209
|
+
types.push('{ $type: string }')
|
|
117
210
|
}
|
|
118
211
|
iface.addProperty({
|
|
119
212
|
name: `${propKey}${req ? '' : '?'}`,
|
|
@@ -141,10 +234,10 @@ export function genObject(
|
|
|
141
234
|
})
|
|
142
235
|
} else if (propDef.items.type === 'union') {
|
|
143
236
|
const types = propDef.items.refs.map((ref) =>
|
|
144
|
-
|
|
237
|
+
refToUnionType(ref, lexUri, imports),
|
|
145
238
|
)
|
|
146
239
|
if (!propDef.items.closed) {
|
|
147
|
-
types.push('{$type: string
|
|
240
|
+
types.push('{ $type: string }')
|
|
148
241
|
}
|
|
149
242
|
propAst = iface.addProperty({
|
|
150
243
|
name: `${propKey}${req ? '' : '?'}`,
|
|
@@ -177,16 +270,21 @@ export function genObject(
|
|
|
177
270
|
}
|
|
178
271
|
}
|
|
179
272
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
273
|
+
|
|
274
|
+
if (allowUnknownProperties) {
|
|
275
|
+
//= [k: string]: unknown
|
|
276
|
+
iface.addIndexSignature({
|
|
277
|
+
keyName: 'k',
|
|
278
|
+
keyType: 'string',
|
|
279
|
+
returnType: 'unknown',
|
|
280
|
+
})
|
|
281
|
+
}
|
|
186
282
|
}
|
|
187
283
|
}
|
|
188
284
|
|
|
189
285
|
export function genToken(file: SourceFile, lexUri: string, def: LexToken) {
|
|
286
|
+
//= /** <comment> */
|
|
287
|
+
//= export const <TOKEN> = `${id}#<token>`
|
|
190
288
|
genComment(
|
|
191
289
|
file.addVariableStatement({
|
|
192
290
|
isExported: true,
|
|
@@ -194,7 +292,7 @@ export function genToken(file: SourceFile, lexUri: string, def: LexToken) {
|
|
|
194
292
|
declarations: [
|
|
195
293
|
{
|
|
196
294
|
name: toScreamingSnakeCase(getHash(lexUri)),
|
|
197
|
-
initializer:
|
|
295
|
+
initializer: `\`\${id}#${getHash(lexUri)}\``,
|
|
198
296
|
},
|
|
199
297
|
],
|
|
200
298
|
}),
|
|
@@ -220,10 +318,10 @@ export function genArray(
|
|
|
220
318
|
})
|
|
221
319
|
} else if (def.items.type === 'union') {
|
|
222
320
|
const types = def.items.refs.map((ref) =>
|
|
223
|
-
|
|
321
|
+
refToUnionType(ref, lexUri, imports),
|
|
224
322
|
)
|
|
225
323
|
if (!def.items.closed) {
|
|
226
|
-
types.push('{$type: string
|
|
324
|
+
types.push('{ $type: string }')
|
|
227
325
|
}
|
|
228
326
|
file.addTypeAlias({
|
|
229
327
|
name: toTitleCase(getHash(lexUri)),
|
|
@@ -308,15 +406,22 @@ export function genXrpcInput(
|
|
|
308
406
|
if (def.type === 'procedure' && def.input?.schema) {
|
|
309
407
|
if (def.input.schema.type === 'ref' || def.input.schema.type === 'union') {
|
|
310
408
|
//= export type InputSchema = ...
|
|
311
|
-
|
|
409
|
+
|
|
410
|
+
const types =
|
|
312
411
|
def.input.schema.type === 'union'
|
|
313
|
-
? def.input.schema.refs
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
412
|
+
? def.input.schema.refs.map((ref) =>
|
|
413
|
+
refToUnionType(ref, lexUri, imports),
|
|
414
|
+
)
|
|
415
|
+
: [
|
|
416
|
+
refToType(
|
|
417
|
+
def.input.schema.ref,
|
|
418
|
+
stripScheme(stripHash(lexUri)),
|
|
419
|
+
imports,
|
|
420
|
+
),
|
|
421
|
+
]
|
|
422
|
+
|
|
318
423
|
if (def.input.schema.type === 'union' && !def.input.schema.closed) {
|
|
319
|
-
types.push('{$type: string
|
|
424
|
+
types.push('{ $type: string }')
|
|
320
425
|
}
|
|
321
426
|
file.addTypeAlias({
|
|
322
427
|
name: 'InputSchema',
|
|
@@ -325,14 +430,9 @@ export function genXrpcInput(
|
|
|
325
430
|
})
|
|
326
431
|
} else {
|
|
327
432
|
//= export interface InputSchema {...}
|
|
328
|
-
genObject(
|
|
329
|
-
file,
|
|
330
|
-
imports,
|
|
331
|
-
lexUri,
|
|
332
|
-
def.input.schema,
|
|
333
|
-
`InputSchema`,
|
|
433
|
+
genObject(file, imports, lexUri, def.input.schema, `InputSchema`, {
|
|
334
434
|
defaultsArePresent,
|
|
335
|
-
)
|
|
435
|
+
})
|
|
336
436
|
}
|
|
337
437
|
} else if (def.type === 'procedure' && def.input?.encoding) {
|
|
338
438
|
//= export type InputSchema = string | Uint8Array | Blob
|
|
@@ -369,12 +469,12 @@ export function genXrpcOutput(
|
|
|
369
469
|
if (schema) {
|
|
370
470
|
if (schema.type === 'ref' || schema.type === 'union') {
|
|
371
471
|
//= export type OutputSchema = ...
|
|
372
|
-
const
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
472
|
+
const types =
|
|
473
|
+
schema.type === 'union'
|
|
474
|
+
? schema.refs.map((ref) => refToUnionType(ref, lexUri, imports))
|
|
475
|
+
: [refToType(schema.ref, stripScheme(stripHash(lexUri)), imports)]
|
|
376
476
|
if (schema.type === 'union' && !schema.closed) {
|
|
377
|
-
types.push('{$type: string
|
|
477
|
+
types.push('{ $type: string }')
|
|
378
478
|
}
|
|
379
479
|
file.addTypeAlias({
|
|
380
480
|
name: 'OutputSchema',
|
|
@@ -383,50 +483,79 @@ export function genXrpcOutput(
|
|
|
383
483
|
})
|
|
384
484
|
} else {
|
|
385
485
|
//= export interface OutputSchema {...}
|
|
386
|
-
genObject(
|
|
387
|
-
file,
|
|
388
|
-
imports,
|
|
389
|
-
lexUri,
|
|
390
|
-
schema,
|
|
391
|
-
`OutputSchema`,
|
|
486
|
+
genObject(file, imports, lexUri, schema, `OutputSchema`, {
|
|
392
487
|
defaultsArePresent,
|
|
393
|
-
)
|
|
488
|
+
})
|
|
394
489
|
}
|
|
395
490
|
}
|
|
396
491
|
}
|
|
397
492
|
|
|
398
|
-
export function
|
|
493
|
+
export function genRecord(
|
|
494
|
+
file: SourceFile,
|
|
495
|
+
imports: Set<string>,
|
|
496
|
+
lexicons: Lexicons,
|
|
497
|
+
lexUri: string,
|
|
498
|
+
) {
|
|
499
|
+
const def = lexicons.getDefOrThrow(lexUri, ['record'])
|
|
500
|
+
|
|
501
|
+
//= export interface Record {...}
|
|
502
|
+
genObject(file, imports, lexUri, def.record, 'Record', {
|
|
503
|
+
defaultsArePresent: true,
|
|
504
|
+
allowUnknownProperties: true,
|
|
505
|
+
typeProperty: 'required',
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
//= export function isRecord(v: unknown): v is Record {...}
|
|
509
|
+
genObjHelpers(file, lexUri, 'Record', {
|
|
510
|
+
requireTypeProperty: true,
|
|
511
|
+
})
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function genObjHelpers(
|
|
399
515
|
file: SourceFile,
|
|
400
516
|
lexUri: string,
|
|
401
|
-
ifaceName
|
|
517
|
+
ifaceName: string,
|
|
518
|
+
{
|
|
519
|
+
requireTypeProperty,
|
|
520
|
+
}: {
|
|
521
|
+
requireTypeProperty: boolean
|
|
522
|
+
},
|
|
402
523
|
) {
|
|
403
524
|
const hash = getHash(lexUri)
|
|
404
525
|
|
|
405
|
-
|
|
526
|
+
const hashVar = `hash${ifaceName}`
|
|
527
|
+
|
|
528
|
+
file.addVariableStatement({
|
|
529
|
+
isExported: false,
|
|
530
|
+
declarationKind: VariableDeclarationKind.Const,
|
|
531
|
+
declarations: [{ name: hashVar, initializer: JSON.stringify(hash) }],
|
|
532
|
+
})
|
|
533
|
+
|
|
534
|
+
const isX = toCamelCase(`is-${ifaceName}`)
|
|
535
|
+
|
|
536
|
+
//= export function is{X}<V>(v: V) {...}
|
|
406
537
|
file
|
|
407
538
|
.addFunction({
|
|
408
|
-
name:
|
|
409
|
-
|
|
410
|
-
|
|
539
|
+
name: isX,
|
|
540
|
+
typeParameters: [{ name: `V` }],
|
|
541
|
+
parameters: [{ name: `v`, type: `V` }],
|
|
411
542
|
isExported: true,
|
|
412
543
|
})
|
|
413
|
-
.setBodyText(
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
lexUri,
|
|
417
|
-
)}")`
|
|
418
|
-
: `return isObj(v) && hasProp(v, '$type') && v.$type === "${lexUri}"`,
|
|
419
|
-
)
|
|
544
|
+
.setBodyText(`return is$typed(v, id, ${hashVar})`)
|
|
545
|
+
|
|
546
|
+
const validateX = toCamelCase(`validate-${ifaceName}`)
|
|
420
547
|
|
|
421
|
-
//= export function validate{X}(v: unknown)
|
|
548
|
+
//= export function validate{X}(v: unknown) {...}
|
|
422
549
|
file
|
|
423
550
|
.addFunction({
|
|
424
|
-
name:
|
|
425
|
-
|
|
426
|
-
|
|
551
|
+
name: validateX,
|
|
552
|
+
typeParameters: [{ name: `V` }],
|
|
553
|
+
parameters: [{ name: `v`, type: `V` }],
|
|
427
554
|
isExported: true,
|
|
428
555
|
})
|
|
429
|
-
.setBodyText(
|
|
556
|
+
.setBodyText(
|
|
557
|
+
`return validate<${ifaceName} & V>(v, id, ${hashVar}${requireTypeProperty ? ', true' : ''})`,
|
|
558
|
+
)
|
|
430
559
|
}
|
|
431
560
|
|
|
432
561
|
export function stripScheme(uri: string): string {
|
|
@@ -449,7 +578,16 @@ export function ipldToType(def: LexCidLink | LexBytes) {
|
|
|
449
578
|
return 'CID'
|
|
450
579
|
}
|
|
451
580
|
|
|
452
|
-
|
|
581
|
+
function refToUnionType(
|
|
582
|
+
ref: string,
|
|
583
|
+
lexUri: string,
|
|
584
|
+
imports: Set<string>,
|
|
585
|
+
): string {
|
|
586
|
+
const baseNsid = stripScheme(stripHash(lexUri))
|
|
587
|
+
return `$Typed<${refToType(ref, baseNsid, imports)}>`
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
function refToType(
|
|
453
591
|
ref: string,
|
|
454
592
|
baseNsid: string,
|
|
455
593
|
imports: Set<string>,
|
|
@@ -510,7 +648,10 @@ export function primitiveToType(def: LexPrimitive): string {
|
|
|
510
648
|
}
|
|
511
649
|
return 'boolean'
|
|
512
650
|
case 'unknown':
|
|
513
|
-
|
|
651
|
+
// @TODO Should we use "object" here ?
|
|
652
|
+
// the "Record" identifier from typescript get overwritten by the Record
|
|
653
|
+
// interface created by lex-cli.
|
|
654
|
+
return '{ [_ in string]: unknown }' // Record<string, unknown>
|
|
514
655
|
default:
|
|
515
656
|
throw new Error(`Unexpected primitive type: ${JSON.stringify(def)}`)
|
|
516
657
|
}
|
package/src/codegen/server.ts
CHANGED
|
@@ -9,9 +9,9 @@ import { NSID } from '@atproto/syntax'
|
|
|
9
9
|
import { GeneratedAPI } from '../types'
|
|
10
10
|
import { gen, lexiconsTs, utilTs } from './common'
|
|
11
11
|
import {
|
|
12
|
+
genCommonImports,
|
|
12
13
|
genImports,
|
|
13
|
-
|
|
14
|
-
genObject,
|
|
14
|
+
genRecord,
|
|
15
15
|
genUserType,
|
|
16
16
|
genXrpcInput,
|
|
17
17
|
genXrpcOutput,
|
|
@@ -73,10 +73,10 @@ const indexTs = (
|
|
|
73
73
|
{ name: 'StreamAuthVerifier' },
|
|
74
74
|
],
|
|
75
75
|
})
|
|
76
|
-
//= import {schemas} from './lexicons'
|
|
76
|
+
//= import {schemas} from './lexicons.js'
|
|
77
77
|
file
|
|
78
78
|
.addImportDeclaration({
|
|
79
|
-
moduleSpecifier: './lexicons',
|
|
79
|
+
moduleSpecifier: './lexicons.js',
|
|
80
80
|
})
|
|
81
81
|
.addNamedImport({
|
|
82
82
|
name: 'schemas',
|
|
@@ -93,7 +93,7 @@ const indexTs = (
|
|
|
93
93
|
}
|
|
94
94
|
file
|
|
95
95
|
.addImportDeclaration({
|
|
96
|
-
moduleSpecifier: `./types/${lexiconDoc.id.split('.').join('/')}`,
|
|
96
|
+
moduleSpecifier: `./types/${lexiconDoc.id.split('.').join('/')}.js`,
|
|
97
97
|
})
|
|
98
98
|
.setNamespaceImport(toTitleCase(lexiconDoc.id))
|
|
99
99
|
}
|
|
@@ -319,8 +319,6 @@ const lexiconTs = (project, lexicons: Lexicons, lexiconDoc: LexiconDoc) =>
|
|
|
319
319
|
project,
|
|
320
320
|
`/types/${lexiconDoc.id.split('.').join('/')}.ts`,
|
|
321
321
|
async (file) => {
|
|
322
|
-
const imports: Set<string> = new Set()
|
|
323
|
-
|
|
324
322
|
const main = lexiconDoc.defs.main
|
|
325
323
|
if (main?.type === 'query' || main?.type === 'procedure') {
|
|
326
324
|
//= import express from 'express'
|
|
@@ -335,44 +333,17 @@ const lexiconTs = (project, lexicons: Lexicons, lexiconDoc: LexiconDoc) =>
|
|
|
335
333
|
!main.input.schema
|
|
336
334
|
const streamingOutput = main.output?.encoding && !main.output.schema
|
|
337
335
|
if (streamingInput || streamingOutput) {
|
|
338
|
-
//= import stream from 'stream'
|
|
336
|
+
//= import stream from 'node:stream'
|
|
339
337
|
file.addImportDeclaration({
|
|
340
|
-
moduleSpecifier: 'stream',
|
|
338
|
+
moduleSpecifier: 'node:stream',
|
|
341
339
|
defaultImport: 'stream',
|
|
342
340
|
})
|
|
343
341
|
}
|
|
344
342
|
}
|
|
345
|
-
//= import {ValidationResult, BlobRef} from '@atproto/lexicon'
|
|
346
|
-
file
|
|
347
|
-
.addImportDeclaration({
|
|
348
|
-
moduleSpecifier: '@atproto/lexicon',
|
|
349
|
-
})
|
|
350
|
-
.addNamedImports([{ name: 'ValidationResult' }, { name: 'BlobRef' }])
|
|
351
|
-
//= import {lexicons} from '../../lexicons.ts'
|
|
352
|
-
file
|
|
353
|
-
.addImportDeclaration({
|
|
354
|
-
moduleSpecifier: `${lexiconDoc.id
|
|
355
|
-
.split('.')
|
|
356
|
-
.map((_str) => '..')
|
|
357
|
-
.join('/')}/lexicons`,
|
|
358
|
-
})
|
|
359
|
-
.addNamedImports([{ name: 'lexicons' }])
|
|
360
|
-
//= import {isObj, hasProp} from '../../util.ts'
|
|
361
|
-
file
|
|
362
|
-
.addImportDeclaration({
|
|
363
|
-
moduleSpecifier: `${lexiconDoc.id
|
|
364
|
-
.split('.')
|
|
365
|
-
.map((_str) => '..')
|
|
366
|
-
.join('/')}/util`,
|
|
367
|
-
})
|
|
368
|
-
.addNamedImports([{ name: 'isObj' }, { name: 'hasProp' }])
|
|
369
|
-
//= import {CID} from 'multiformats/cid'
|
|
370
|
-
file
|
|
371
|
-
.addImportDeclaration({
|
|
372
|
-
moduleSpecifier: 'multiformats/cid',
|
|
373
|
-
})
|
|
374
|
-
.addNamedImports([{ name: 'CID' }])
|
|
375
343
|
|
|
344
|
+
genCommonImports(file, lexiconDoc.id)
|
|
345
|
+
|
|
346
|
+
const imports: Set<string> = new Set()
|
|
376
347
|
for (const defId in lexiconDoc.defs) {
|
|
377
348
|
const def = lexiconDoc.defs[defId]
|
|
378
349
|
const lexUri = `${lexiconDoc.id}#${defId}`
|
|
@@ -387,7 +358,7 @@ const lexiconTs = (project, lexicons: Lexicons, lexiconDoc: LexiconDoc) =>
|
|
|
387
358
|
genXrpcOutput(file, imports, lexicons, lexUri, false)
|
|
388
359
|
genServerXrpcStreaming(file, lexicons, lexUri)
|
|
389
360
|
} else if (def.type === 'record') {
|
|
390
|
-
|
|
361
|
+
genRecord(file, imports, lexicons, lexUri)
|
|
391
362
|
} else {
|
|
392
363
|
genUserType(file, imports, lexicons, lexUri)
|
|
393
364
|
}
|
|
@@ -547,7 +518,7 @@ function genServerXrpcStreaming(
|
|
|
547
518
|
})
|
|
548
519
|
|
|
549
520
|
file.addImportDeclaration({
|
|
550
|
-
moduleSpecifier: 'http',
|
|
521
|
+
moduleSpecifier: 'node:http',
|
|
551
522
|
namedImports: [{ name: 'IncomingMessage' }],
|
|
552
523
|
})
|
|
553
524
|
|
|
@@ -589,20 +560,6 @@ function genServerXrpcStreaming(
|
|
|
589
560
|
})
|
|
590
561
|
}
|
|
591
562
|
|
|
592
|
-
function genServerRecord(
|
|
593
|
-
file: SourceFile,
|
|
594
|
-
imports: Set<string>,
|
|
595
|
-
lexicons: Lexicons,
|
|
596
|
-
lexUri: string,
|
|
597
|
-
) {
|
|
598
|
-
const def = lexicons.getDefOrThrow(lexUri, ['record'])
|
|
599
|
-
|
|
600
|
-
//= export interface Record {...}
|
|
601
|
-
genObject(file, imports, lexUri, def.record, 'Record')
|
|
602
|
-
//= export function isRecord(v: unknown): v is Record {...}
|
|
603
|
-
genObjHelpers(file, lexUri, 'Record')
|
|
604
|
-
}
|
|
605
|
-
|
|
606
563
|
function arrayToUnion(arr?: string[]) {
|
|
607
564
|
if (!arr?.length) {
|
|
608
565
|
return 'never'
|