@atproto/lex-builder 0.1.1 → 0.1.3
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 +24 -0
- package/dist/filtered-indexer.d.ts +24 -24
- package/dist/filtered-indexer.d.ts.map +1 -1
- package/dist/formatter.d.ts.map +1 -1
- package/dist/formatter.js.map +1 -1
- package/dist/lex-builder.d.ts +34 -0
- package/dist/lex-builder.d.ts.map +1 -1
- package/dist/lex-builder.js +18 -9
- package/dist/lex-builder.js.map +1 -1
- package/dist/lex-def-builder.d.ts +6 -13
- package/dist/lex-def-builder.d.ts.map +1 -1
- package/dist/lex-def-builder.js +135 -106
- package/dist/lex-def-builder.js.map +1 -1
- package/dist/lexicon-directory-indexer.d.ts.map +1 -1
- package/dist/ref-resolver.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/lex-builder.ts +53 -9
- package/src/lex-def-builder.ts +160 -137
- package/tsconfig.build.json +2 -2
- package/tsconfig.json +2 -2
- package/tsconfig.tests.json +1 -2
package/src/lex-def-builder.ts
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
LexiconError,
|
|
16
16
|
LexiconIndexer,
|
|
17
17
|
LexiconInteger,
|
|
18
|
+
LexiconMessage,
|
|
18
19
|
LexiconObject,
|
|
19
20
|
LexiconParameters,
|
|
20
21
|
LexiconPayload,
|
|
@@ -50,15 +51,6 @@ export type LexDefBuilderOptions = RefResolverOptions & {
|
|
|
50
51
|
* @default '@atproto/lex-schema'
|
|
51
52
|
*/
|
|
52
53
|
lib?: string
|
|
53
|
-
/**
|
|
54
|
-
* Whether to add `#__PURE__` annotations to function calls.
|
|
55
|
-
*
|
|
56
|
-
* These annotations help bundlers with tree-shaking by marking
|
|
57
|
-
* side-effect-free function calls.
|
|
58
|
-
*
|
|
59
|
-
* @default false
|
|
60
|
-
*/
|
|
61
|
-
pureAnnotations?: boolean
|
|
62
54
|
}
|
|
63
55
|
|
|
64
56
|
/**
|
|
@@ -86,10 +78,6 @@ export class LexDefBuilder {
|
|
|
86
78
|
this.refResolver = new RefResolver(doc, file, indexer, options)
|
|
87
79
|
}
|
|
88
80
|
|
|
89
|
-
private pure(code: string) {
|
|
90
|
-
return this.options.pureAnnotations ? markPure(code) : code
|
|
91
|
-
}
|
|
92
|
-
|
|
93
81
|
async build() {
|
|
94
82
|
this.file.addVariableStatement({
|
|
95
83
|
declarationKind: VariableDeclarationKind.Const,
|
|
@@ -160,7 +148,7 @@ export class LexDefBuilder {
|
|
|
160
148
|
private async addPermissionSet(hash: string, def: LexiconPermissionSet) {
|
|
161
149
|
const permission = def.permissions.map((def) => {
|
|
162
150
|
const options = stringifyOptions(def, undefined, ['resource', 'type'])
|
|
163
|
-
return
|
|
151
|
+
return markPure(
|
|
164
152
|
`l.permission(${JSON.stringify(def.resource)}, ${options})`,
|
|
165
153
|
)
|
|
166
154
|
})
|
|
@@ -173,134 +161,175 @@ export class LexDefBuilder {
|
|
|
173
161
|
] satisfies (keyof l.PermissionSetOptions)[])
|
|
174
162
|
|
|
175
163
|
await this.addSchema(hash, def, {
|
|
176
|
-
schema:
|
|
164
|
+
schema: markPure(
|
|
177
165
|
`l.permissionSet($nsid, [${permission.join(',')}], ${options})`,
|
|
178
166
|
),
|
|
179
167
|
})
|
|
180
168
|
}
|
|
181
169
|
|
|
182
|
-
private async
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
170
|
+
private async addParameters(parameters?: LexiconParameters): Promise<string> {
|
|
171
|
+
const varName = '$params'
|
|
172
|
+
|
|
173
|
+
this.addUtils({
|
|
174
|
+
[varName]: await this.compileParamsSchema(parameters),
|
|
175
|
+
})
|
|
186
176
|
|
|
187
177
|
// @TODO Build the types instead of using an inferred type.
|
|
178
|
+
this.file.addTypeAlias({
|
|
179
|
+
isExported: true,
|
|
180
|
+
name: '$Params',
|
|
181
|
+
type: `l.InferOutput<typeof ${varName}>`,
|
|
182
|
+
docs: compileDocs(parameters?.description),
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
return varName
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private async addInput(input?: LexiconPayload): Promise<string> {
|
|
189
|
+
const varName = '$input'
|
|
188
190
|
|
|
189
|
-
// Declare each piece of the method as its own top-level exported const
|
|
190
|
-
// *before* `main`, so consumers that import a single helper (e.g.
|
|
191
|
-
// `$lxm`) only pull in that subtree rather than the whole procedure.
|
|
192
|
-
// `main.parameters` etc. would otherwise pin `main` in the module graph.
|
|
193
191
|
this.addUtils({
|
|
194
|
-
|
|
195
|
-
$params: await this.compileParamsSchema(def.parameters),
|
|
196
|
-
$input: await this.compilePayload(def.input),
|
|
197
|
-
$output: await this.compilePayload(def.output),
|
|
192
|
+
[varName]: await this.compilePayload(input),
|
|
198
193
|
})
|
|
199
194
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
195
|
+
// @TODO Build the types instead of using an inferred type.
|
|
196
|
+
this.file.addTypeAlias({
|
|
197
|
+
isExported: true,
|
|
198
|
+
name: '$Input<B = l.BinaryData>',
|
|
199
|
+
type: `l.InferPayload<typeof ${varName}, B>`,
|
|
200
|
+
docs: compileDocs(input?.description),
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
this.file.addTypeAlias({
|
|
204
|
+
isExported: true,
|
|
205
|
+
name: '$InputBody<B = l.BinaryData>',
|
|
206
|
+
type: `l.InferPayloadBody<typeof ${varName}, B>`,
|
|
207
|
+
docs: compileDocs(input?.description),
|
|
204
208
|
})
|
|
205
209
|
|
|
206
|
-
|
|
210
|
+
return varName
|
|
207
211
|
}
|
|
208
212
|
|
|
209
|
-
private async
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
+
private async addOutput(output?: LexiconPayload): Promise<string> {
|
|
214
|
+
const varName = '$output'
|
|
215
|
+
|
|
216
|
+
this.addUtils({
|
|
217
|
+
[varName]: await this.compilePayload(output),
|
|
218
|
+
})
|
|
213
219
|
|
|
214
220
|
// @TODO Build the types instead of using an inferred type.
|
|
221
|
+
this.file.addTypeAlias({
|
|
222
|
+
isExported: true,
|
|
223
|
+
name: '$Output<B = l.BinaryData>',
|
|
224
|
+
type: `l.InferPayload<typeof ${varName}, B>`,
|
|
225
|
+
docs: compileDocs(output?.description),
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
this.file.addTypeAlias({
|
|
229
|
+
isExported: true,
|
|
230
|
+
name: '$OutputBody<B = l.BinaryData>',
|
|
231
|
+
type: `l.InferPayloadBody<typeof ${varName}, B>`,
|
|
232
|
+
docs: compileDocs(output?.description),
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
return varName
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private async addMessage(message?: LexiconMessage) {
|
|
239
|
+
const varName = '$message'
|
|
215
240
|
|
|
216
241
|
this.addUtils({
|
|
217
|
-
|
|
218
|
-
$params: await this.compileParamsSchema(def.parameters),
|
|
219
|
-
$output: await this.compilePayload(def.output),
|
|
242
|
+
[varName]: await this.compileBodySchema(message?.schema),
|
|
220
243
|
})
|
|
221
244
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
245
|
+
// @TODO Build the types instead of using an inferred type.
|
|
246
|
+
this.file.addTypeAlias({
|
|
247
|
+
isExported: true,
|
|
248
|
+
name: '$Message',
|
|
249
|
+
type: `l.InferOutput<typeof ${varName}>`,
|
|
250
|
+
docs: compileDocs(message?.description),
|
|
226
251
|
})
|
|
227
252
|
|
|
228
|
-
|
|
253
|
+
return varName
|
|
229
254
|
}
|
|
230
255
|
|
|
231
|
-
private async
|
|
256
|
+
private async addProcedure(hash: string, def: LexiconProcedure) {
|
|
232
257
|
if (hash !== 'main') {
|
|
233
258
|
throw new Error(`Definition ${hash} cannot be of type ${def.type}`)
|
|
234
259
|
}
|
|
235
260
|
|
|
236
|
-
//
|
|
261
|
+
// Declare each piece of the method as its own top-level exported const
|
|
262
|
+
// *before* `main`. This allows to export those pieces individually instead
|
|
263
|
+
// of "extracting" them from the "main" definition as below, which is bad
|
|
264
|
+
// for tree-shaking.
|
|
265
|
+
//
|
|
266
|
+
// export const $params = main.params`
|
|
237
267
|
|
|
238
|
-
this.
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
$message: await this.compileBodySchema(def.message?.schema),
|
|
242
|
-
})
|
|
268
|
+
const paramsVar = await this.addParameters(def.parameters)
|
|
269
|
+
const inputVar = await this.addInput(def.input)
|
|
270
|
+
const outputVar = await this.addOutput(def.output)
|
|
243
271
|
|
|
244
|
-
|
|
245
|
-
schema:
|
|
246
|
-
`l.
|
|
272
|
+
await this.addSchema(hash, def, {
|
|
273
|
+
schema: markPure(
|
|
274
|
+
`l.procedure($nsid, ${paramsVar}, ${inputVar}, ${outputVar}${formatErrorsArg(await this.compileErrors(def.errors))})`,
|
|
247
275
|
),
|
|
248
276
|
})
|
|
249
277
|
|
|
250
|
-
this.
|
|
278
|
+
this.addUtils({
|
|
279
|
+
$lxm: '$nsid',
|
|
280
|
+
})
|
|
251
281
|
}
|
|
252
282
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
283
|
+
private async addQuery(hash: string, def: LexiconQuery) {
|
|
284
|
+
if (hash !== 'main') {
|
|
285
|
+
throw new Error(`Definition ${hash} cannot be of type ${def.type}`)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Declare each piece of the method as its own top-level exported const
|
|
289
|
+
// *before* `main`. This allows to export those pieces individually instead
|
|
290
|
+
// of "extracting" them from the "main" definition as below, which is bad
|
|
291
|
+
// for tree-shaking:
|
|
292
|
+
//
|
|
293
|
+
// export const $params = main.params
|
|
294
|
+
|
|
295
|
+
const paramsVar = await this.addParameters(def.parameters)
|
|
296
|
+
const outputVar = await this.addOutput(def.output)
|
|
297
|
+
|
|
298
|
+
await this.addSchema(hash, def, {
|
|
299
|
+
schema: markPure(
|
|
300
|
+
`l.query($nsid, ${paramsVar}, ${outputVar}${formatErrorsArg(await this.compileErrors(def.errors))})`,
|
|
301
|
+
),
|
|
262
302
|
})
|
|
263
303
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
type: `l.InferMethodInput<typeof ${ref.varName}, B>`,
|
|
269
|
-
docs: compileDocs(def.input?.description),
|
|
270
|
-
})
|
|
304
|
+
this.addUtils({
|
|
305
|
+
$lxm: '$nsid',
|
|
306
|
+
})
|
|
307
|
+
}
|
|
271
308
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
type: `l.InferMethodInputBody<typeof ${ref.varName}, B>`,
|
|
276
|
-
docs: compileDocs(def.input?.description),
|
|
277
|
-
})
|
|
309
|
+
private async addSubscription(hash: string, def: LexiconSubscription) {
|
|
310
|
+
if (hash !== 'main') {
|
|
311
|
+
throw new Error(`Definition ${hash} cannot be of type ${def.type}`)
|
|
278
312
|
}
|
|
279
313
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
})
|
|
314
|
+
// Declare each piece of the method as its own top-level exported const
|
|
315
|
+
// *before* `main`. This allows to export those pieces individually instead
|
|
316
|
+
// of "extracting" them from the "main" definition as below, which is bad
|
|
317
|
+
// for tree-shaking.
|
|
318
|
+
//
|
|
319
|
+
// export const $params = main.params`
|
|
287
320
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
name: '$OutputBody<B = l.BinaryData>',
|
|
291
|
-
type: `l.InferMethodOutputBody<typeof ${ref.varName}, B>`,
|
|
292
|
-
docs: compileDocs(def.output?.description),
|
|
293
|
-
})
|
|
294
|
-
}
|
|
321
|
+
const paramsVar = await this.addParameters(def.parameters)
|
|
322
|
+
const messageVar = await this.addMessage(def.message)
|
|
295
323
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
324
|
+
await this.addSchema(hash, def, {
|
|
325
|
+
schema: markPure(
|
|
326
|
+
`l.subscription($nsid, ${paramsVar}, ${messageVar}${formatErrorsArg(await this.compileErrors(def.errors))})`,
|
|
327
|
+
),
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
this.addUtils({
|
|
331
|
+
$lxm: '$nsid',
|
|
332
|
+
})
|
|
304
333
|
}
|
|
305
334
|
|
|
306
335
|
private async addRecord(hash: string, def: LexiconRecord) {
|
|
@@ -317,7 +346,7 @@ export class LexDefBuilder {
|
|
|
317
346
|
await this.addSchema(hash, def, {
|
|
318
347
|
type: `{ ${properties.join(';')} }`,
|
|
319
348
|
schema: (ref) =>
|
|
320
|
-
|
|
349
|
+
markPure(
|
|
321
350
|
`l.record<${key}, ${ref.typeName}>(${key}, $nsid, ${objectSchema})`,
|
|
322
351
|
),
|
|
323
352
|
objectUtils: true,
|
|
@@ -334,7 +363,7 @@ export class LexDefBuilder {
|
|
|
334
363
|
await this.addSchema(hash, def, {
|
|
335
364
|
type: `{ ${properties.join(';')} }`,
|
|
336
365
|
schema: (ref) =>
|
|
337
|
-
|
|
366
|
+
markPure(
|
|
338
367
|
`l.typedObject<${ref.typeName}>($nsid, ${JSON.stringify(hash)}, ${objectSchema})`,
|
|
339
368
|
),
|
|
340
369
|
objectUtils: true,
|
|
@@ -344,7 +373,7 @@ export class LexDefBuilder {
|
|
|
344
373
|
|
|
345
374
|
private async addToken(hash: string, def: LexiconToken) {
|
|
346
375
|
await this.addSchema(hash, def, {
|
|
347
|
-
schema:
|
|
376
|
+
schema: markPure(`l.token($nsid, ${JSON.stringify(hash)})`),
|
|
348
377
|
type: JSON.stringify(l.$type(this.doc.id, hash)),
|
|
349
378
|
validationUtils: true,
|
|
350
379
|
})
|
|
@@ -366,9 +395,7 @@ export class LexDefBuilder {
|
|
|
366
395
|
// @NOTE Not using compileArraySchema to allow specifying the generic
|
|
367
396
|
// parameter to l.array<>.
|
|
368
397
|
schema: (ref) =>
|
|
369
|
-
|
|
370
|
-
`l.array<${ref.typeName}[number]>(${itemSchema}, ${options})`,
|
|
371
|
-
),
|
|
398
|
+
markPure(`l.array<${ref.typeName}[number]>(${itemSchema}, ${options})`),
|
|
372
399
|
validationUtils: true,
|
|
373
400
|
})
|
|
374
401
|
}
|
|
@@ -439,9 +466,9 @@ export class LexDefBuilder {
|
|
|
439
466
|
|
|
440
467
|
if (hash === 'main' && objectUtils) {
|
|
441
468
|
this.addUtils({
|
|
469
|
+
$type: `$nsid`,
|
|
442
470
|
$isTypeOf: markPure(`${ref.varName}.isTypeOf.bind(${ref.varName})`),
|
|
443
471
|
$build: markPure(`${ref.varName}.build.bind(${ref.varName})`),
|
|
444
|
-
$type: `${ref.varName}.$type`,
|
|
445
472
|
})
|
|
446
473
|
}
|
|
447
474
|
|
|
@@ -465,20 +492,20 @@ export class LexDefBuilder {
|
|
|
465
492
|
}
|
|
466
493
|
|
|
467
494
|
private async compilePayload(def: LexiconPayload | undefined) {
|
|
468
|
-
if (!def) return
|
|
495
|
+
if (!def) return markPure(`l.payload()`)
|
|
469
496
|
|
|
470
497
|
// Special case for JSON object payloads
|
|
471
498
|
if (def.encoding === 'application/json' && def.schema?.type === 'object') {
|
|
472
499
|
const properties = await this.compilePropertiesSchemas(def.schema)
|
|
473
|
-
return
|
|
500
|
+
return markPure(`l.jsonPayload({${properties.join(',')}})`)
|
|
474
501
|
}
|
|
475
502
|
|
|
476
503
|
const encodedEncoding = JSON.stringify(def.encoding)
|
|
477
504
|
if (def.schema) {
|
|
478
505
|
const bodySchema = await this.compileBodySchema(def.schema)
|
|
479
|
-
return
|
|
506
|
+
return markPure(`l.payload(${encodedEncoding}, ${bodySchema})`)
|
|
480
507
|
} else {
|
|
481
|
-
return
|
|
508
|
+
return markPure(`l.payload(${encodedEncoding})`)
|
|
482
509
|
}
|
|
483
510
|
}
|
|
484
511
|
|
|
@@ -491,10 +518,10 @@ export class LexDefBuilder {
|
|
|
491
518
|
}
|
|
492
519
|
|
|
493
520
|
private async compileParamsSchema(def: undefined | LexiconParameters) {
|
|
494
|
-
if (!def) return
|
|
521
|
+
if (!def) return markPure(`l.params()`)
|
|
495
522
|
|
|
496
523
|
const properties = await this.compilePropertiesSchemas(def)
|
|
497
|
-
return
|
|
524
|
+
return markPure(
|
|
498
525
|
properties.length === 0
|
|
499
526
|
? `l.params()`
|
|
500
527
|
: `l.params({${properties.join(',')}})`,
|
|
@@ -508,7 +535,7 @@ export class LexDefBuilder {
|
|
|
508
535
|
|
|
509
536
|
private async compileObjectSchema(def: LexiconObject): Promise<string> {
|
|
510
537
|
const properties = await this.compilePropertiesSchemas(def)
|
|
511
|
-
return
|
|
538
|
+
return markPure(`l.object({${properties.join(',')}})`)
|
|
512
539
|
}
|
|
513
540
|
|
|
514
541
|
private async compilePropertiesSchemas(options: {
|
|
@@ -558,11 +585,11 @@ export class LexDefBuilder {
|
|
|
558
585
|
let schema = await this.compileContainedSchema(def)
|
|
559
586
|
|
|
560
587
|
if (isNullable) {
|
|
561
|
-
schema =
|
|
588
|
+
schema = markPure(`l.nullable(${schema})`)
|
|
562
589
|
}
|
|
563
590
|
|
|
564
591
|
if (!isRequired) {
|
|
565
|
-
schema =
|
|
592
|
+
schema = markPure(`l.optional(${schema})`)
|
|
566
593
|
}
|
|
567
594
|
|
|
568
595
|
return `${JSON.stringify(key)}:${schema}`
|
|
@@ -654,7 +681,7 @@ export class LexDefBuilder {
|
|
|
654
681
|
'minLength',
|
|
655
682
|
'maxLength',
|
|
656
683
|
] satisfies (keyof l.ArraySchemaOptions)[])
|
|
657
|
-
return
|
|
684
|
+
return markPure(`l.array(${itemSchema}, ${options})`)
|
|
658
685
|
}
|
|
659
686
|
|
|
660
687
|
private async compileArrayType(def: LexiconArray): Promise<string> {
|
|
@@ -662,7 +689,7 @@ export class LexDefBuilder {
|
|
|
662
689
|
}
|
|
663
690
|
|
|
664
691
|
private async compileUnknownSchema(_def: LexiconUnknown): Promise<string> {
|
|
665
|
-
return
|
|
692
|
+
return markPure(`l.lexMap()`)
|
|
666
693
|
}
|
|
667
694
|
|
|
668
695
|
private async compileUnknownType(_def: LexiconUnknown): Promise<string> {
|
|
@@ -672,9 +699,7 @@ export class LexDefBuilder {
|
|
|
672
699
|
private withDefault(schema: string, defaultValue: unknown) {
|
|
673
700
|
if (defaultValue === undefined) return schema
|
|
674
701
|
|
|
675
|
-
return
|
|
676
|
-
`l.withDefault(${schema}, ${JSON.stringify(defaultValue)})`,
|
|
677
|
-
)
|
|
702
|
+
return markPure(`l.withDefault(${schema}, ${JSON.stringify(defaultValue)})`)
|
|
678
703
|
}
|
|
679
704
|
|
|
680
705
|
private async compileBooleanSchema(def: LexiconBoolean): Promise<string> {
|
|
@@ -686,7 +711,7 @@ export class LexDefBuilder {
|
|
|
686
711
|
|
|
687
712
|
if (hasConst(def)) return this.compileConstSchema(def)
|
|
688
713
|
|
|
689
|
-
return this.withDefault(
|
|
714
|
+
return this.withDefault(markPure(`l.boolean()`), def.default)
|
|
690
715
|
}
|
|
691
716
|
|
|
692
717
|
private async compileBooleanType(def: LexiconBoolean): Promise<string> {
|
|
@@ -717,7 +742,7 @@ export class LexDefBuilder {
|
|
|
717
742
|
'minimum',
|
|
718
743
|
] satisfies (keyof l.IntegerSchemaOptions)[])
|
|
719
744
|
|
|
720
|
-
return this.withDefault(
|
|
745
|
+
return this.withDefault(markPure(`l.integer(${options})`), def.default)
|
|
721
746
|
}
|
|
722
747
|
|
|
723
748
|
private async compileIntegerType(def: LexiconInteger): Promise<string> {
|
|
@@ -769,7 +794,7 @@ export class LexDefBuilder {
|
|
|
769
794
|
: undefined
|
|
770
795
|
|
|
771
796
|
return this.withDefault(
|
|
772
|
-
|
|
797
|
+
markPure(`l.string${generic ? `<${generic}>` : ''}(${options})`),
|
|
773
798
|
def.default,
|
|
774
799
|
)
|
|
775
800
|
}
|
|
@@ -822,7 +847,7 @@ export class LexDefBuilder {
|
|
|
822
847
|
'minLength',
|
|
823
848
|
'maxLength',
|
|
824
849
|
] satisfies (keyof l.BytesSchemaOptions)[])
|
|
825
|
-
return
|
|
850
|
+
return markPure(`l.bytes(${options})`)
|
|
826
851
|
}
|
|
827
852
|
|
|
828
853
|
private async compileBytesType(_def: LexiconBytes): Promise<string> {
|
|
@@ -834,7 +859,7 @@ export class LexDefBuilder {
|
|
|
834
859
|
'maxSize',
|
|
835
860
|
'accept',
|
|
836
861
|
] satisfies (keyof l.BlobSchemaOptions)[])
|
|
837
|
-
return
|
|
862
|
+
return markPure(`l.blob(${options})`)
|
|
838
863
|
}
|
|
839
864
|
|
|
840
865
|
private async compileBlobType(_def: LexiconBlob): Promise<string> {
|
|
@@ -842,7 +867,7 @@ export class LexDefBuilder {
|
|
|
842
867
|
}
|
|
843
868
|
|
|
844
869
|
private async compileCidLinkSchema(_def: LexiconCid): Promise<string> {
|
|
845
|
-
return
|
|
870
|
+
return markPure(`l.cid()`)
|
|
846
871
|
}
|
|
847
872
|
|
|
848
873
|
private async compileCidLinkType(_def: LexiconCid): Promise<string> {
|
|
@@ -853,7 +878,7 @@ export class LexDefBuilder {
|
|
|
853
878
|
const { varName, typeName } = await this.refResolver.resolve(def.ref)
|
|
854
879
|
// @NOTE "as any" is needed in schemas with circular refs as TypeScript
|
|
855
880
|
// cannot infer the type of a value that depends on its initializer type
|
|
856
|
-
return
|
|
881
|
+
return markPure(`l.ref<${typeName}>((() => ${varName}) as any)`)
|
|
857
882
|
}
|
|
858
883
|
|
|
859
884
|
private async compileRefType(def: LexiconRef): Promise<string> {
|
|
@@ -863,7 +888,7 @@ export class LexDefBuilder {
|
|
|
863
888
|
|
|
864
889
|
private async compileRefUnionSchema(def: LexiconRefUnion): Promise<string> {
|
|
865
890
|
if (def.refs.length === 0 && def.closed) {
|
|
866
|
-
return
|
|
891
|
+
return markPure(`l.never()`)
|
|
867
892
|
}
|
|
868
893
|
|
|
869
894
|
const refs = await Promise.all(
|
|
@@ -871,13 +896,11 @@ export class LexDefBuilder {
|
|
|
871
896
|
const { varName, typeName } = await this.refResolver.resolve(ref)
|
|
872
897
|
// @NOTE "as any" is needed in schemas with circular refs as TypeScript
|
|
873
898
|
// cannot infer the type of a value that depends on its initializer type
|
|
874
|
-
return
|
|
899
|
+
return markPure(`l.typedRef<${typeName}>((() => ${varName}) as any)`)
|
|
875
900
|
}),
|
|
876
901
|
)
|
|
877
902
|
|
|
878
|
-
return
|
|
879
|
-
`l.typedUnion([${refs.join(',')}], ${def.closed ?? false})`,
|
|
880
|
-
)
|
|
903
|
+
return markPure(`l.typedUnion([${refs.join(',')}], ${def.closed ?? false})`)
|
|
881
904
|
}
|
|
882
905
|
|
|
883
906
|
private async compileRefUnionType(def: LexiconRefUnion): Promise<string> {
|
|
@@ -895,10 +918,10 @@ export class LexDefBuilder {
|
|
|
895
918
|
T extends null | number | string | boolean,
|
|
896
919
|
>(def: { const: T; enum?: readonly T[]; default?: T }): Promise<string> {
|
|
897
920
|
if (hasEnum(def) && !def.enum.includes(def.const)) {
|
|
898
|
-
return
|
|
921
|
+
return markPure(`l.never()`)
|
|
899
922
|
}
|
|
900
923
|
|
|
901
|
-
const result =
|
|
924
|
+
const result = markPure(`l.literal(${JSON.stringify(def.const)})`)
|
|
902
925
|
|
|
903
926
|
return this.withDefault(result, def.default)
|
|
904
927
|
}
|
|
@@ -917,13 +940,13 @@ export class LexDefBuilder {
|
|
|
917
940
|
default?: T
|
|
918
941
|
}): Promise<string> {
|
|
919
942
|
if (def.enum.length === 0) {
|
|
920
|
-
return
|
|
943
|
+
return markPure(`l.never()`)
|
|
921
944
|
}
|
|
922
945
|
|
|
923
946
|
const result =
|
|
924
947
|
def.enum.length === 1
|
|
925
|
-
?
|
|
926
|
-
:
|
|
948
|
+
? markPure(`l.literal(${JSON.stringify(def.enum[0])})`)
|
|
949
|
+
: markPure(`l.enum(${JSON.stringify(def.enum)})`)
|
|
927
950
|
|
|
928
951
|
return this.withDefault(result, def.default)
|
|
929
952
|
}
|
package/tsconfig.build.json
CHANGED
package/tsconfig.json
CHANGED