@kubb/ast 5.0.0-beta.58 → 5.0.0-beta.59
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/README.md +10 -0
- package/dist/casing-BE2R1RXg.cjs +88 -0
- package/dist/casing-BE2R1RXg.cjs.map +1 -0
- package/dist/extractStringsFromNodes-WMYJ8nQL.d.ts +82 -0
- package/dist/factory-BmcGBdeg.cjs +1251 -0
- package/dist/factory-BmcGBdeg.cjs.map +1 -0
- package/dist/factory-Du7nEP4B.js +282 -0
- package/dist/factory-Du7nEP4B.js.map +1 -0
- package/dist/factory.cjs +25 -28
- package/dist/factory.d.ts +3 -3
- package/dist/factory.js +3 -3
- package/dist/{ast-ClnJg9BN.d.ts → index-BzjwdK2M.d.ts} +74 -372
- package/dist/index.cjs +445 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +94 -59
- package/dist/index.js +7 -113
- package/dist/index.js.map +1 -1
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-DKxTr522.js +683 -0
- package/dist/response-DKxTr522.js.map +1 -0
- package/dist/{types-CB2oY8Dw.d.ts → types-olVl9v5p.d.ts} +222 -227
- package/dist/types.d.ts +4 -3
- package/dist/utils-BCtRXfhI.cjs +275 -0
- package/dist/utils-BCtRXfhI.cjs.map +1 -0
- package/dist/{utils-DN4XLVqz.js → utils-SdZU0F3H.js} +472 -1235
- package/dist/utils-SdZU0F3H.js.map +1 -0
- package/dist/utils.cjs +127 -22
- package/dist/utils.d.ts +112 -80
- package/dist/utils.js +3 -2
- package/package.json +1 -1
- package/src/constants.ts +8 -14
- package/src/dedupe.ts +8 -0
- package/src/factory.ts +1 -2
- package/src/guards.ts +1 -1
- package/src/index.ts +4 -13
- package/src/nodes/code.ts +29 -47
- package/src/nodes/content.ts +2 -2
- package/src/nodes/file.ts +28 -23
- package/src/nodes/function.ts +0 -15
- package/src/nodes/input.ts +6 -6
- package/src/nodes/operation.ts +1 -1
- package/src/nodes/parameter.ts +0 -3
- package/src/nodes/property.ts +0 -3
- package/src/nodes/requestBody.ts +3 -6
- package/src/nodes/schema.ts +3 -2
- package/src/printer.ts +1 -1
- package/src/registry.ts +31 -26
- package/src/signature.ts +3 -3
- package/src/transformers.ts +28 -1
- package/src/types.ts +2 -53
- package/src/utils/codegen.ts +104 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +7 -339
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +112 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +43 -19
- package/dist/extractStringsFromNodes-Bn9cOos9.d.ts +0 -14
- package/dist/factory-C5gHvtLU.js +0 -138
- package/dist/factory-C5gHvtLU.js.map +0 -1
- package/dist/factory-JN-Ylfl6.cjs +0 -155
- package/dist/factory-JN-Ylfl6.cjs.map +0 -1
- package/dist/utils-C8bWAzhv.cjs +0 -2696
- package/dist/utils-C8bWAzhv.cjs.map +0 -1
- package/dist/utils-DN4XLVqz.js.map +0 -1
- package/src/utils/ast.ts +0 -816
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { n as __name } from "./chunk-CNktS9qV.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Et as PropertyNode, _t as SchemaNode, f as OperationNode, h as ResponseNode, n as OutputNode, nt as ContentNode, o as InputNode, t as Node, vt as SchemaNodeByType, w as ParameterNode, y as RequestBodyNode, yt as SchemaType } from "./index-BzjwdK2M.js";
|
|
3
3
|
|
|
4
4
|
//#region src/constants.d.ts
|
|
5
5
|
/**
|
|
@@ -12,9 +12,8 @@ type VisitorDepth = 'shallow' | 'deep';
|
|
|
12
12
|
/**
|
|
13
13
|
* Schema type discriminators used by all AST schema nodes.
|
|
14
14
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
|
|
15
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
16
|
+
* Call `isScalarPrimitive()` to check for the scalar types.
|
|
18
17
|
*/
|
|
19
18
|
declare const schemaTypes: {
|
|
20
19
|
/**
|
|
@@ -34,7 +33,7 @@ declare const schemaTypes: {
|
|
|
34
33
|
*/
|
|
35
34
|
readonly bigint: "bigint";
|
|
36
35
|
/**
|
|
37
|
-
* Boolean value
|
|
36
|
+
* Boolean value.
|
|
38
37
|
*/
|
|
39
38
|
readonly boolean: "boolean";
|
|
40
39
|
/**
|
|
@@ -124,8 +123,6 @@ declare const schemaTypes: {
|
|
|
124
123
|
};
|
|
125
124
|
/**
|
|
126
125
|
* HTTP method identifiers used by operation nodes.
|
|
127
|
-
*
|
|
128
|
-
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
|
|
129
126
|
*/
|
|
130
127
|
declare const httpMethods: {
|
|
131
128
|
readonly get: "GET";
|
|
@@ -293,210 +290,6 @@ type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema,
|
|
|
293
290
|
*/
|
|
294
291
|
declare function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
|
|
295
292
|
//#endregion
|
|
296
|
-
//#region src/printer.d.ts
|
|
297
|
-
/**
|
|
298
|
-
* Runtime context passed as `this` to printer handlers.
|
|
299
|
-
*
|
|
300
|
-
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
301
|
-
*
|
|
302
|
-
* @example
|
|
303
|
-
* ```ts
|
|
304
|
-
* const context: PrinterHandlerContext<string, {}> = {
|
|
305
|
-
* options: {},
|
|
306
|
-
* transform: () => 'value',
|
|
307
|
-
* }
|
|
308
|
-
* ```
|
|
309
|
-
*/
|
|
310
|
-
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
311
|
-
/**
|
|
312
|
-
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
313
|
-
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
314
|
-
*/
|
|
315
|
-
transform: (node: SchemaNode) => TOutput | null;
|
|
316
|
-
/**
|
|
317
|
-
* Options for this printer instance.
|
|
318
|
-
*/
|
|
319
|
-
options: TOptions;
|
|
320
|
-
};
|
|
321
|
-
/**
|
|
322
|
-
* Handler for one schema node type.
|
|
323
|
-
*
|
|
324
|
-
* Use a regular function (not an arrow function) if you need `this`.
|
|
325
|
-
*
|
|
326
|
-
* @example
|
|
327
|
-
* ```ts
|
|
328
|
-
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
329
|
-
* return 'string'
|
|
330
|
-
* }
|
|
331
|
-
* ```
|
|
332
|
-
*/
|
|
333
|
-
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
|
|
334
|
-
/**
|
|
335
|
-
* Partial map of per-node-type handler overrides for a printer.
|
|
336
|
-
*
|
|
337
|
-
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
338
|
-
* Supply only the handlers you want to replace. The printer's built-in
|
|
339
|
-
* defaults fill in the rest.
|
|
340
|
-
*
|
|
341
|
-
* @example
|
|
342
|
-
* ```ts
|
|
343
|
-
* pluginZod({
|
|
344
|
-
* printer: {
|
|
345
|
-
* nodes: {
|
|
346
|
-
* date(): string {
|
|
347
|
-
* return 'z.string().date()'
|
|
348
|
-
* },
|
|
349
|
-
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
350
|
-
* },
|
|
351
|
-
* })
|
|
352
|
-
* ```
|
|
353
|
-
*/
|
|
354
|
-
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
355
|
-
/**
|
|
356
|
-
* Generic shape used by `definePrinter`.
|
|
357
|
-
*
|
|
358
|
-
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
359
|
-
* - `TOptions` options passed to and stored on the printer instance
|
|
360
|
-
* - `TOutput` the type emitted by node handlers
|
|
361
|
-
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
362
|
-
*
|
|
363
|
-
* @example
|
|
364
|
-
* ```ts
|
|
365
|
-
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
366
|
-
* ```
|
|
367
|
-
*/
|
|
368
|
-
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
369
|
-
name: TName;
|
|
370
|
-
options: TOptions;
|
|
371
|
-
output: TOutput;
|
|
372
|
-
printOutput: TPrintOutput;
|
|
373
|
-
};
|
|
374
|
-
/**
|
|
375
|
-
* Printer instance returned by a printer factory.
|
|
376
|
-
*
|
|
377
|
-
* @example
|
|
378
|
-
* ```ts
|
|
379
|
-
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
380
|
-
* ```
|
|
381
|
-
*/
|
|
382
|
-
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
383
|
-
/**
|
|
384
|
-
* Unique identifier supplied at creation time.
|
|
385
|
-
*/
|
|
386
|
-
name: T['name'];
|
|
387
|
-
/**
|
|
388
|
-
* Options for this printer instance.
|
|
389
|
-
*/
|
|
390
|
-
options: T['options'];
|
|
391
|
-
/**
|
|
392
|
-
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
393
|
-
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
394
|
-
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
395
|
-
*/
|
|
396
|
-
transform: (node: SchemaNode) => T['output'] | null;
|
|
397
|
-
/**
|
|
398
|
-
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
399
|
-
* higher-level function (which may produce full declarations).
|
|
400
|
-
* Otherwise, falls back to the node-level dispatcher.
|
|
401
|
-
*/
|
|
402
|
-
print: (node: SchemaNode) => T['printOutput'] | null;
|
|
403
|
-
};
|
|
404
|
-
/**
|
|
405
|
-
* Builder function passed to `definePrinter`.
|
|
406
|
-
*
|
|
407
|
-
* It receives resolved options and returns:
|
|
408
|
-
* - `name`
|
|
409
|
-
* - `options`
|
|
410
|
-
* - `nodes` handlers
|
|
411
|
-
* - optional top-level `print` override
|
|
412
|
-
*
|
|
413
|
-
* @example
|
|
414
|
-
* ```ts
|
|
415
|
-
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
416
|
-
* ```
|
|
417
|
-
*/
|
|
418
|
-
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
419
|
-
name: T['name'];
|
|
420
|
-
/**
|
|
421
|
-
* Options to store on the printer.
|
|
422
|
-
*/
|
|
423
|
-
options: T['options'];
|
|
424
|
-
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
425
|
-
/**
|
|
426
|
-
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
427
|
-
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
428
|
-
* not the override itself, so recursion is safe.
|
|
429
|
-
*/
|
|
430
|
-
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
431
|
-
};
|
|
432
|
-
/**
|
|
433
|
-
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
434
|
-
* code in your target language. Each plugin that produces code from schemas
|
|
435
|
-
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
436
|
-
* with this helper.
|
|
437
|
-
*
|
|
438
|
-
* The builder receives resolved options and returns:
|
|
439
|
-
*
|
|
440
|
-
* - `name` unique identifier for the printer.
|
|
441
|
-
* - `options` stored on the returned printer instance.
|
|
442
|
-
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
443
|
-
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
444
|
-
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
445
|
-
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
446
|
-
*
|
|
447
|
-
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
448
|
-
* (the node-level dispatcher).
|
|
449
|
-
*
|
|
450
|
-
* @example Tiny Zod printer
|
|
451
|
-
* ```ts
|
|
452
|
-
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
453
|
-
*
|
|
454
|
-
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
455
|
-
*
|
|
456
|
-
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
457
|
-
* name: 'zod',
|
|
458
|
-
* options: { strict: options.strict ?? true },
|
|
459
|
-
* nodes: {
|
|
460
|
-
* string: () => 'z.string()',
|
|
461
|
-
* object(node) {
|
|
462
|
-
* const props = node.properties
|
|
463
|
-
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
464
|
-
* .join(', ')
|
|
465
|
-
* return `z.object({ ${props} })`
|
|
466
|
-
* },
|
|
467
|
-
* },
|
|
468
|
-
* }))
|
|
469
|
-
* ```
|
|
470
|
-
*/
|
|
471
|
-
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
472
|
-
/**
|
|
473
|
-
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
474
|
-
*
|
|
475
|
-
* @example
|
|
476
|
-
* ```ts
|
|
477
|
-
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
|
|
478
|
-
* (node) => node.kind,
|
|
479
|
-
* )
|
|
480
|
-
* ```
|
|
481
|
-
*/
|
|
482
|
-
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
|
|
483
|
-
name: T["name"];
|
|
484
|
-
options: T["options"];
|
|
485
|
-
nodes: Partial<{ [K in TKey]: (this: {
|
|
486
|
-
transform: (node: TNode) => T["output"] | null;
|
|
487
|
-
options: T["options"];
|
|
488
|
-
}, node: TNodeByKey[K]) => T["output"] | null }>;
|
|
489
|
-
print?: (this: {
|
|
490
|
-
transform: (node: TNode) => T["output"] | null;
|
|
491
|
-
options: T["options"];
|
|
492
|
-
}, node: TNode) => T["printOutput"] | null;
|
|
493
|
-
}) => (options?: T["options"]) => {
|
|
494
|
-
name: T["name"];
|
|
495
|
-
options: T["options"];
|
|
496
|
-
transform: (node: TNode) => T["output"] | null;
|
|
497
|
-
print: (node: TNode) => T["printOutput"] | null;
|
|
498
|
-
};
|
|
499
|
-
//#endregion
|
|
500
293
|
//#region src/visitor.d.ts
|
|
501
294
|
/**
|
|
502
295
|
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
@@ -507,8 +300,7 @@ type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [Operatio
|
|
|
507
300
|
/**
|
|
508
301
|
* Resolves the parent node type for a given AST node type.
|
|
509
302
|
*
|
|
510
|
-
*
|
|
511
|
-
* for each callback.
|
|
303
|
+
* Visitor context relies on this so `ctx.parent` is typed for each callback.
|
|
512
304
|
*
|
|
513
305
|
* @example
|
|
514
306
|
* ```ts
|
|
@@ -555,8 +347,7 @@ type VisitorContext<T extends Node = Node> = {
|
|
|
555
347
|
* to leave it untouched.
|
|
556
348
|
*
|
|
557
349
|
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
558
|
-
* rewrites
|
|
559
|
-
* before printing.
|
|
350
|
+
* rewrites the AST before printing.
|
|
560
351
|
*
|
|
561
352
|
* @example Prefix every operationId
|
|
562
353
|
* ```ts
|
|
@@ -586,7 +377,7 @@ type Visitor = {
|
|
|
586
377
|
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
|
|
587
378
|
};
|
|
588
379
|
/**
|
|
589
|
-
*
|
|
380
|
+
* A visitor callback result that may be sync or async.
|
|
590
381
|
*/
|
|
591
382
|
type MaybePromise<T> = T | Promise<T>;
|
|
592
383
|
/**
|
|
@@ -647,7 +438,7 @@ type CollectVisitor<T> = {
|
|
|
647
438
|
*/
|
|
648
439
|
type TransformOptions = Visitor & {
|
|
649
440
|
/**
|
|
650
|
-
* Traversal depth
|
|
441
|
+
* Traversal depth.
|
|
651
442
|
* @default 'deep'
|
|
652
443
|
*/
|
|
653
444
|
depth?: VisitorDepth;
|
|
@@ -666,7 +457,7 @@ type TransformOptions = Visitor & {
|
|
|
666
457
|
*/
|
|
667
458
|
type WalkOptions = AsyncVisitor & {
|
|
668
459
|
/**
|
|
669
|
-
* Traversal depth
|
|
460
|
+
* Traversal depth.
|
|
670
461
|
* @default 'deep'
|
|
671
462
|
*/
|
|
672
463
|
depth?: VisitorDepth;
|
|
@@ -686,7 +477,7 @@ type WalkOptions = AsyncVisitor & {
|
|
|
686
477
|
*/
|
|
687
478
|
type CollectOptions<T> = CollectVisitor<T> & {
|
|
688
479
|
/**
|
|
689
|
-
* Traversal depth
|
|
480
|
+
* Traversal depth.
|
|
690
481
|
* @default 'deep'
|
|
691
482
|
*/
|
|
692
483
|
depth?: VisitorDepth;
|
|
@@ -719,11 +510,11 @@ type CollectOptions<T> = CollectVisitor<T> & {
|
|
|
719
510
|
*/
|
|
720
511
|
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
721
512
|
/**
|
|
722
|
-
* Synchronous depth-first transform. Each visitor callback
|
|
723
|
-
*
|
|
513
|
+
* Synchronous depth-first transform. Each visitor callback can return a
|
|
514
|
+
* replacement node. Returning `undefined` keeps the original.
|
|
724
515
|
*
|
|
725
|
-
* The
|
|
726
|
-
*
|
|
516
|
+
* The original tree is never mutated, a new tree is returned. Pass
|
|
517
|
+
* `depth: 'shallow'` to skip recursion into children.
|
|
727
518
|
*
|
|
728
519
|
* @example Prefix every operationId
|
|
729
520
|
* ```ts
|
|
@@ -751,8 +542,8 @@ declare function transform(node: ParameterNode, options: TransformOptions): Para
|
|
|
751
542
|
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
752
543
|
declare function transform(node: Node, options: TransformOptions): Node;
|
|
753
544
|
/**
|
|
754
|
-
* Eager depth-first collection pass.
|
|
755
|
-
*
|
|
545
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
546
|
+
* callbacks return into an array.
|
|
756
547
|
*
|
|
757
548
|
* @example Collect every operationId
|
|
758
549
|
* ```ts
|
|
@@ -765,5 +556,209 @@ declare function transform(node: Node, options: TransformOptions): Node;
|
|
|
765
556
|
*/
|
|
766
557
|
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
767
558
|
//#endregion
|
|
768
|
-
|
|
769
|
-
|
|
559
|
+
//#region src/printer.d.ts
|
|
560
|
+
/**
|
|
561
|
+
* Runtime context passed as `this` to printer handlers.
|
|
562
|
+
*
|
|
563
|
+
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ```ts
|
|
567
|
+
* const context: PrinterHandlerContext<string, {}> = {
|
|
568
|
+
* options: {},
|
|
569
|
+
* transform: () => 'value',
|
|
570
|
+
* }
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
574
|
+
/**
|
|
575
|
+
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
576
|
+
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
577
|
+
*/
|
|
578
|
+
transform: (node: SchemaNode) => TOutput | null;
|
|
579
|
+
/**
|
|
580
|
+
* Options for this printer instance.
|
|
581
|
+
*/
|
|
582
|
+
options: TOptions;
|
|
583
|
+
};
|
|
584
|
+
/**
|
|
585
|
+
* Handler for one schema node type.
|
|
586
|
+
*
|
|
587
|
+
* Use a regular function (not an arrow function) if you need `this`.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
592
|
+
* return 'string'
|
|
593
|
+
* }
|
|
594
|
+
* ```
|
|
595
|
+
*/
|
|
596
|
+
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
|
|
597
|
+
/**
|
|
598
|
+
* Partial map of per-node-type handler overrides for a printer.
|
|
599
|
+
*
|
|
600
|
+
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
601
|
+
* Supply only the handlers you want to replace. The printer's built-in
|
|
602
|
+
* defaults fill in the rest.
|
|
603
|
+
*
|
|
604
|
+
* @example
|
|
605
|
+
* ```ts
|
|
606
|
+
* pluginZod({
|
|
607
|
+
* printer: {
|
|
608
|
+
* nodes: {
|
|
609
|
+
* date(): string {
|
|
610
|
+
* return 'z.string().date()'
|
|
611
|
+
* },
|
|
612
|
+
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
613
|
+
* },
|
|
614
|
+
* })
|
|
615
|
+
* ```
|
|
616
|
+
*/
|
|
617
|
+
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
618
|
+
/**
|
|
619
|
+
* Generic shape used by `definePrinter`.
|
|
620
|
+
*
|
|
621
|
+
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
622
|
+
* - `TOptions` options passed to and stored on the printer instance
|
|
623
|
+
* - `TOutput` the type emitted by node handlers
|
|
624
|
+
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
625
|
+
*
|
|
626
|
+
* @example
|
|
627
|
+
* ```ts
|
|
628
|
+
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
629
|
+
* ```
|
|
630
|
+
*/
|
|
631
|
+
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
632
|
+
name: TName;
|
|
633
|
+
options: TOptions;
|
|
634
|
+
output: TOutput;
|
|
635
|
+
printOutput: TPrintOutput;
|
|
636
|
+
};
|
|
637
|
+
/**
|
|
638
|
+
* Printer instance returned by a printer factory.
|
|
639
|
+
*
|
|
640
|
+
* @example
|
|
641
|
+
* ```ts
|
|
642
|
+
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
645
|
+
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
646
|
+
/**
|
|
647
|
+
* Unique identifier supplied at creation time.
|
|
648
|
+
*/
|
|
649
|
+
name: T['name'];
|
|
650
|
+
/**
|
|
651
|
+
* Options for this printer instance.
|
|
652
|
+
*/
|
|
653
|
+
options: T['options'];
|
|
654
|
+
/**
|
|
655
|
+
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
656
|
+
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
657
|
+
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
658
|
+
*/
|
|
659
|
+
transform: (node: SchemaNode) => T['output'] | null;
|
|
660
|
+
/**
|
|
661
|
+
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
662
|
+
* higher-level function (which may produce full declarations).
|
|
663
|
+
* Otherwise, falls back to the node-level dispatcher.
|
|
664
|
+
*/
|
|
665
|
+
print: (node: SchemaNode) => T['printOutput'] | null;
|
|
666
|
+
};
|
|
667
|
+
/**
|
|
668
|
+
* Builder function passed to `definePrinter`.
|
|
669
|
+
*
|
|
670
|
+
* It receives resolved options and returns:
|
|
671
|
+
* - `name`
|
|
672
|
+
* - `options`
|
|
673
|
+
* - `nodes` handlers
|
|
674
|
+
* - optional top-level `print` override
|
|
675
|
+
*
|
|
676
|
+
* @example
|
|
677
|
+
* ```ts
|
|
678
|
+
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
679
|
+
* ```
|
|
680
|
+
*/
|
|
681
|
+
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
682
|
+
name: T['name'];
|
|
683
|
+
/**
|
|
684
|
+
* Options to store on the printer.
|
|
685
|
+
*/
|
|
686
|
+
options: T['options'];
|
|
687
|
+
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
688
|
+
/**
|
|
689
|
+
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
690
|
+
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
691
|
+
* not the override itself, so recursion is safe.
|
|
692
|
+
*/
|
|
693
|
+
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
694
|
+
};
|
|
695
|
+
/**
|
|
696
|
+
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
697
|
+
* code in your target language. Each plugin that produces code from schemas
|
|
698
|
+
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
699
|
+
* with this helper.
|
|
700
|
+
*
|
|
701
|
+
* The builder receives resolved options and returns:
|
|
702
|
+
*
|
|
703
|
+
* - `name` unique identifier for the printer.
|
|
704
|
+
* - `options` stored on the returned printer instance.
|
|
705
|
+
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
706
|
+
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
707
|
+
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
708
|
+
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
709
|
+
*
|
|
710
|
+
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
711
|
+
* (the node-level dispatcher).
|
|
712
|
+
*
|
|
713
|
+
* @example Tiny Zod printer
|
|
714
|
+
* ```ts
|
|
715
|
+
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
716
|
+
*
|
|
717
|
+
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
718
|
+
*
|
|
719
|
+
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
720
|
+
* name: 'zod',
|
|
721
|
+
* options: { strict: options.strict ?? true },
|
|
722
|
+
* nodes: {
|
|
723
|
+
* string: () => 'z.string()',
|
|
724
|
+
* object(node) {
|
|
725
|
+
* const props = node.properties
|
|
726
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
727
|
+
* .join(', ')
|
|
728
|
+
* return `z.object({ ${props} })`
|
|
729
|
+
* },
|
|
730
|
+
* },
|
|
731
|
+
* }))
|
|
732
|
+
* ```
|
|
733
|
+
*/
|
|
734
|
+
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
735
|
+
/**
|
|
736
|
+
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
737
|
+
*
|
|
738
|
+
* @example
|
|
739
|
+
* ```ts
|
|
740
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
|
|
741
|
+
* (node) => node.kind,
|
|
742
|
+
* )
|
|
743
|
+
* ```
|
|
744
|
+
*/
|
|
745
|
+
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
|
|
746
|
+
name: T["name"];
|
|
747
|
+
options: T["options"];
|
|
748
|
+
nodes: Partial<{ [K in TKey]: (this: {
|
|
749
|
+
transform: (node: TNode) => T["output"] | null;
|
|
750
|
+
options: T["options"];
|
|
751
|
+
}, node: TNodeByKey[K]) => T["output"] | null }>;
|
|
752
|
+
print?: (this: {
|
|
753
|
+
transform: (node: TNode) => T["output"] | null;
|
|
754
|
+
options: T["options"];
|
|
755
|
+
}, node: TNode) => T["printOutput"] | null;
|
|
756
|
+
}) => (options?: T["options"]) => {
|
|
757
|
+
name: T["name"];
|
|
758
|
+
options: T["options"];
|
|
759
|
+
transform: (node: TNode) => T["output"] | null;
|
|
760
|
+
print: (node: TNode) => T["printOutput"] | null;
|
|
761
|
+
};
|
|
762
|
+
//#endregion
|
|
763
|
+
export { applyDedupe as _, definePrinter as a, schemaTypes as b, VisitorContext as c, walk as d, SchemaDialect as f, DedupePlan as g, DedupeLookups as h, createPrinterFactory as i, collect as l, DedupeCanonical as m, PrinterFactoryOptions as n, ParentOf as o, defineSchemaDialect as p, PrinterPartial as r, Visitor as s, Printer as t, transform as u, buildDedupePlan as v, httpMethods as y };
|
|
764
|
+
//# sourceMappingURL=types-olVl9v5p.d.ts.map
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as FunctionParametersNode, At as InferSchemaNode, Bt as TypeNode, C as ParameterLocation, Ct as UrlSchemaNode, D as FunctionParamKind, Et as PropertyNode, Ft as ConstNode, G as ExportNode, It as FunctionNode, J as SourceNode, K as FileNode, Lt as JSDocNode, M as ObjectBindingPatternNode, Mt as ArrowFunctionNode, N as TypeExpression, Nt as BreakNode, O as FunctionParamNode, P as TypeLiteralNode, Pt as CodeNode, Rt as JsxNode, St as UnionSchemaNode, _t as SchemaNode, a as InputMeta, bt as StringSchemaNode, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, f as OperationNode, ft as ObjectSchemaNode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, j as IndexedAccessTypeNode, jt as ParserOptions, k as FunctionParameterNode, l as GenericOperationNode, lt as EnumSchemaNode, mt as RefSchemaNode, n as OutputNode, nt as ContentNode, o as InputNode, on as NodeKind, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as ImportNode, st as DateSchemaNode, t as Node, tn as DistributiveOmit, u as HttpMethod, ut as IntersectionSchemaNode, v as StatusCode, vt as SchemaNodeByType, w as ParameterNode, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, zt as TextNode } from "./index-BzjwdK2M.js";
|
|
2
2
|
import { UserFileNode } from "./factory.js";
|
|
3
|
-
import { c as
|
|
4
|
-
|
|
3
|
+
import { c as VisitorContext, f as SchemaDialect, g as DedupePlan, h as DedupeLookups, m as DedupeCanonical, n as PrinterFactoryOptions, o as ParentOf, r as PrinterPartial, s as Visitor, t as Printer } from "./types-olVl9v5p.js";
|
|
4
|
+
import { n as OperationParamsResolver } from "./operationParams-BZ07xDm0.js";
|
|
5
|
+
export type { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupeLookups, DedupePlan, DistributiveOmit, EnumSchemaNode, ExportNode, FileNode, FunctionNode, FunctionParamKind, FunctionParamNode, FunctionParameterNode, FunctionParametersNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, IndexedAccessTypeNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Node, NodeKind, NumberSchemaNode, ObjectBindingPatternNode, ObjectSchemaNode, OperationNode, OperationParamsResolver, OutputNode, ParameterLocation, ParameterNode, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeExpression, TypeLiteralNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext };
|