@kubb/ast 5.0.0-beta.57 → 5.0.0-beta.58
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 +11 -7
- package/dist/{types-C5aVnRE1.d.ts → ast-ClnJg9BN.d.ts} +82 -836
- package/dist/chunk-CNktS9qV.js +17 -0
- package/dist/extractStringsFromNodes-Bn9cOos9.d.ts +14 -0
- package/dist/factory-C5gHvtLU.js +138 -0
- package/dist/factory-C5gHvtLU.js.map +1 -0
- package/dist/factory-JN-Ylfl6.cjs +155 -0
- package/dist/factory-JN-Ylfl6.cjs.map +1 -0
- package/dist/factory.cjs +31 -0
- package/dist/factory.d.ts +62 -0
- package/dist/factory.js +3 -0
- package/dist/index.cjs +86 -1746
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +177 -1809
- package/dist/index.js.map +1 -1
- package/dist/types-CB2oY8Dw.d.ts +769 -0
- package/dist/types.d.ts +4 -2
- package/dist/utils-C8bWAzhv.cjs +2696 -0
- package/dist/utils-C8bWAzhv.cjs.map +1 -0
- package/dist/utils-DN4XLVqz.js +2099 -0
- package/dist/utils-DN4XLVqz.js.map +1 -0
- package/dist/utils.cjs +12 -1
- package/dist/utils.d.ts +21 -2
- package/dist/utils.js +2 -2
- package/package.json +5 -1
- package/src/factory.ts +19 -1
- package/src/index.ts +14 -48
- package/src/node.ts +1 -1
- package/src/nodes/base.ts +0 -10
- package/src/nodes/function.ts +2 -2
- package/src/nodes/index.ts +1 -1
- package/src/nodes/input.ts +14 -13
- package/src/printer.ts +3 -3
- package/src/types.ts +1 -1
- package/src/utils/ast.ts +3 -66
- package/src/utils/extractStringsFromNodes.ts +34 -0
- package/src/utils/index.ts +44 -0
- package/dist/chunk-C0LytTxp.js +0 -8
- package/dist/utils-0p8ZO287.js +0 -570
- package/dist/utils-0p8ZO287.js.map +0 -1
- package/dist/utils-cdQ6Pzyi.cjs +0 -726
- package/dist/utils-cdQ6Pzyi.cjs.map +0 -1
|
@@ -1,142 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as __name } from "./chunk-CNktS9qV.js";
|
|
2
2
|
|
|
3
|
-
//#region src/constants.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Traversal depth for AST visitor utilities.
|
|
6
|
-
*
|
|
7
|
-
* - `'shallow'` visits only the immediate node, skipping children.
|
|
8
|
-
* - `'deep'` recursively visits all descendant nodes.
|
|
9
|
-
*/
|
|
10
|
-
type VisitorDepth = 'shallow' | 'deep';
|
|
11
|
-
/**
|
|
12
|
-
* Schema type discriminators used by all AST schema nodes.
|
|
13
|
-
*
|
|
14
|
-
* These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
|
|
15
|
-
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
|
|
16
|
-
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
|
|
17
|
-
*/
|
|
18
|
-
declare const schemaTypes: {
|
|
19
|
-
/**
|
|
20
|
-
* Text value.
|
|
21
|
-
*/
|
|
22
|
-
readonly string: "string";
|
|
23
|
-
/**
|
|
24
|
-
* Floating-point number (`float`, `double`).
|
|
25
|
-
*/
|
|
26
|
-
readonly number: "number";
|
|
27
|
-
/**
|
|
28
|
-
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
29
|
-
*/
|
|
30
|
-
readonly integer: "integer";
|
|
31
|
-
/**
|
|
32
|
-
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
33
|
-
*/
|
|
34
|
-
readonly bigint: "bigint";
|
|
35
|
-
/**
|
|
36
|
-
* Boolean value
|
|
37
|
-
*/
|
|
38
|
-
readonly boolean: "boolean";
|
|
39
|
-
/**
|
|
40
|
-
* Explicit null value.
|
|
41
|
-
*/
|
|
42
|
-
readonly null: "null";
|
|
43
|
-
/**
|
|
44
|
-
* Any value (no type restriction).
|
|
45
|
-
*/
|
|
46
|
-
readonly any: "any";
|
|
47
|
-
/**
|
|
48
|
-
* Unknown value (must be narrowed before usage).
|
|
49
|
-
*/
|
|
50
|
-
readonly unknown: "unknown";
|
|
51
|
-
/**
|
|
52
|
-
* No return value (`void`).
|
|
53
|
-
*/
|
|
54
|
-
readonly void: "void";
|
|
55
|
-
/**
|
|
56
|
-
* Object with named properties.
|
|
57
|
-
*/
|
|
58
|
-
readonly object: "object";
|
|
59
|
-
/**
|
|
60
|
-
* Sequential list of items.
|
|
61
|
-
*/
|
|
62
|
-
readonly array: "array";
|
|
63
|
-
/**
|
|
64
|
-
* Fixed-length list with position-specific items.
|
|
65
|
-
*/
|
|
66
|
-
readonly tuple: "tuple";
|
|
67
|
-
/**
|
|
68
|
-
* "One of" multiple schema members.
|
|
69
|
-
*/
|
|
70
|
-
readonly union: "union";
|
|
71
|
-
/**
|
|
72
|
-
* "All of" multiple schema members.
|
|
73
|
-
*/
|
|
74
|
-
readonly intersection: "intersection";
|
|
75
|
-
/**
|
|
76
|
-
* Enum schema.
|
|
77
|
-
*/
|
|
78
|
-
readonly enum: "enum";
|
|
79
|
-
/**
|
|
80
|
-
* Reference to another schema.
|
|
81
|
-
*/
|
|
82
|
-
readonly ref: "ref";
|
|
83
|
-
/**
|
|
84
|
-
* Calendar date (for example `2026-03-24`).
|
|
85
|
-
*/
|
|
86
|
-
readonly date: "date";
|
|
87
|
-
/**
|
|
88
|
-
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
89
|
-
*/
|
|
90
|
-
readonly datetime: "datetime";
|
|
91
|
-
/**
|
|
92
|
-
* Time-only value (for example `09:00:00`).
|
|
93
|
-
*/
|
|
94
|
-
readonly time: "time";
|
|
95
|
-
/**
|
|
96
|
-
* UUID value.
|
|
97
|
-
*/
|
|
98
|
-
readonly uuid: "uuid";
|
|
99
|
-
/**
|
|
100
|
-
* Email address value.
|
|
101
|
-
*/
|
|
102
|
-
readonly email: "email";
|
|
103
|
-
/**
|
|
104
|
-
* URL value.
|
|
105
|
-
*/
|
|
106
|
-
readonly url: "url";
|
|
107
|
-
/**
|
|
108
|
-
* IPv4 address value.
|
|
109
|
-
*/
|
|
110
|
-
readonly ipv4: "ipv4";
|
|
111
|
-
/**
|
|
112
|
-
* IPv6 address value.
|
|
113
|
-
*/
|
|
114
|
-
readonly ipv6: "ipv6";
|
|
115
|
-
/**
|
|
116
|
-
* Binary/blob value.
|
|
117
|
-
*/
|
|
118
|
-
readonly blob: "blob";
|
|
119
|
-
/**
|
|
120
|
-
* Impossible value (`never`).
|
|
121
|
-
*/
|
|
122
|
-
readonly never: "never";
|
|
123
|
-
};
|
|
124
|
-
/**
|
|
125
|
-
* HTTP method identifiers used by operation nodes.
|
|
126
|
-
*
|
|
127
|
-
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
|
|
128
|
-
*/
|
|
129
|
-
declare const httpMethods: {
|
|
130
|
-
readonly get: "GET";
|
|
131
|
-
readonly post: "POST";
|
|
132
|
-
readonly put: "PUT";
|
|
133
|
-
readonly patch: "PATCH";
|
|
134
|
-
readonly delete: "DELETE";
|
|
135
|
-
readonly head: "HEAD";
|
|
136
|
-
readonly options: "OPTIONS";
|
|
137
|
-
readonly trace: "TRACE";
|
|
138
|
-
};
|
|
139
|
-
//#endregion
|
|
140
3
|
//#region src/nodes/base.d.ts
|
|
141
4
|
/**
|
|
142
5
|
* `kind` values used by AST nodes.
|
|
@@ -1520,6 +1383,10 @@ type UserContent = Omit<ContentNode, 'kind'>;
|
|
|
1520
1383
|
* Definition for the {@link ContentNode}.
|
|
1521
1384
|
*/
|
|
1522
1385
|
declare const contentDef: NodeDef<ContentNode, UserContent>;
|
|
1386
|
+
/**
|
|
1387
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
1388
|
+
*/
|
|
1389
|
+
declare const createContent: (input: UserContent) => ContentNode;
|
|
1523
1390
|
//#endregion
|
|
1524
1391
|
//#region src/nodes/file.d.ts
|
|
1525
1392
|
/**
|
|
@@ -1963,9 +1830,9 @@ type FunctionParametersNode = BaseNode & {
|
|
|
1963
1830
|
*/
|
|
1964
1831
|
type FunctionParamNode = FunctionParameterNode | FunctionParametersNode | TypeLiteralNode | IndexedAccessTypeNode | ObjectBindingPatternNode;
|
|
1965
1832
|
/**
|
|
1966
|
-
* Handler
|
|
1833
|
+
* Handler-map keys for the function-parameter printer, one per {@link FunctionParamNode} kind.
|
|
1967
1834
|
*/
|
|
1968
|
-
type
|
|
1835
|
+
type FunctionParamKind = FunctionParamNode['kind'];
|
|
1969
1836
|
/**
|
|
1970
1837
|
* Definition for the {@link TypeLiteralNode}.
|
|
1971
1838
|
*/
|
|
@@ -2189,6 +2056,10 @@ type UserRequestBody = Omit<RequestBodyNode, 'kind' | 'content'> & {
|
|
|
2189
2056
|
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
|
|
2190
2057
|
*/
|
|
2191
2058
|
declare const requestBodyDef: NodeDef<RequestBodyNode, UserRequestBody>;
|
|
2059
|
+
/**
|
|
2060
|
+
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
|
|
2061
|
+
*/
|
|
2062
|
+
declare const createRequestBody: (input: UserRequestBody) => RequestBodyNode;
|
|
2192
2063
|
//#endregion
|
|
2193
2064
|
//#region src/nodes/http.d.ts
|
|
2194
2065
|
/**
|
|
@@ -2514,24 +2385,24 @@ type InputNode<Stream extends boolean = false> = BaseNode & {
|
|
|
2514
2385
|
*/
|
|
2515
2386
|
declare const inputDef: NodeDef<InputNode<false>, Partial<Omit<InputNode<false>, "kind">>>;
|
|
2516
2387
|
/**
|
|
2517
|
-
* Creates an `InputNode
|
|
2388
|
+
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
|
|
2389
|
+
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
|
|
2390
|
+
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
|
|
2518
2391
|
*
|
|
2519
|
-
* @example
|
|
2392
|
+
* @example Eager
|
|
2520
2393
|
* ```ts
|
|
2521
2394
|
* const input = createInput()
|
|
2522
2395
|
* // { kind: 'Input', schemas: [], operations: [] }
|
|
2523
2396
|
* ```
|
|
2524
|
-
*/
|
|
2525
|
-
declare function createInput(overrides?: Partial<Omit<InputNode, 'kind'>>): InputNode;
|
|
2526
|
-
/**
|
|
2527
|
-
* Creates a streaming `InputNode<true>` from pre-built `AsyncIterable` sources.
|
|
2528
2397
|
*
|
|
2529
|
-
* @example
|
|
2398
|
+
* @example Streaming
|
|
2530
2399
|
* ```ts
|
|
2531
|
-
* const node =
|
|
2400
|
+
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
|
|
2532
2401
|
* ```
|
|
2533
2402
|
*/
|
|
2534
|
-
declare function
|
|
2403
|
+
declare function createInput<Stream extends boolean = false>(options?: Partial<Omit<InputNode<Stream>, 'kind'>> & {
|
|
2404
|
+
stream?: Stream;
|
|
2405
|
+
}): InputNode<Stream>;
|
|
2535
2406
|
//#endregion
|
|
2536
2407
|
//#region src/nodes/output.d.ts
|
|
2537
2408
|
/**
|
|
@@ -2594,687 +2465,6 @@ declare function createOutput(overrides?: Partial<Omit<OutputNode, 'kind'>>): Ou
|
|
|
2594
2465
|
*/
|
|
2595
2466
|
type Node = InputNode | OutputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode | RequestBodyNode | ContentNode | FunctionParamNode | FileNode | ImportNode | ExportNode | SourceNode | ConstNode | TypeNode | FunctionNode | ArrowFunctionNode;
|
|
2596
2467
|
//#endregion
|
|
2597
|
-
//#region src/dedupe.d.ts
|
|
2598
|
-
/**
|
|
2599
|
-
* A canonical destination for a deduplicated shape: the shared schema name and
|
|
2600
|
-
* the synthetic `$ref` path that points at it.
|
|
2601
|
-
*/
|
|
2602
|
-
type DedupeCanonical = {
|
|
2603
|
-
/**
|
|
2604
|
-
* Canonical schema name every duplicate occurrence refers to.
|
|
2605
|
-
*/
|
|
2606
|
-
name: string;
|
|
2607
|
-
/**
|
|
2608
|
-
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
|
|
2609
|
-
*/
|
|
2610
|
-
ref: string;
|
|
2611
|
-
};
|
|
2612
|
-
/**
|
|
2613
|
-
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
|
|
2614
|
-
* canonical target, plus the freshly hoisted definitions that must be added to
|
|
2615
|
-
* the schema list.
|
|
2616
|
-
*/
|
|
2617
|
-
type DedupePlan = {
|
|
2618
|
-
/**
|
|
2619
|
-
* Maps a structural signature to the canonical schema that represents it.
|
|
2620
|
-
*/
|
|
2621
|
-
canonicalBySignature: Map<string, DedupeCanonical>;
|
|
2622
|
-
/**
|
|
2623
|
-
* Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
|
|
2624
|
-
* references to the duplicate can be repointed at the first schema with the same content.
|
|
2625
|
-
*/
|
|
2626
|
-
aliasNames: Map<string, DedupeCanonical>;
|
|
2627
|
-
/**
|
|
2628
|
-
* New top-level schema definitions created for inline shapes that had no existing
|
|
2629
|
-
* named component. Nested duplicates inside each definition are already collapsed.
|
|
2630
|
-
*/
|
|
2631
|
-
hoisted: Array<SchemaNode>;
|
|
2632
|
-
};
|
|
2633
|
-
/**
|
|
2634
|
-
* The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
|
|
2635
|
-
*/
|
|
2636
|
-
type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>;
|
|
2637
|
-
/**
|
|
2638
|
-
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
|
|
2639
|
-
* The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
|
|
2640
|
-
*/
|
|
2641
|
-
type BuildDedupePlanOptions = {
|
|
2642
|
-
/**
|
|
2643
|
-
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
|
|
2644
|
-
* reject both ineligible kinds (return `false` for anything other than, say, enums and
|
|
2645
|
-
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
|
|
2646
|
-
*/
|
|
2647
|
-
isCandidate: (node: SchemaNode) => boolean;
|
|
2648
|
-
/**
|
|
2649
|
-
* Produces the canonical name for an inline shape with no existing named component.
|
|
2650
|
-
* Return `null` to leave the shape inline (for example when no contextual name exists).
|
|
2651
|
-
*/
|
|
2652
|
-
nameFor: (node: SchemaNode, signature: string) => string | null;
|
|
2653
|
-
/**
|
|
2654
|
-
* Builds the `$ref` path for a canonical name.
|
|
2655
|
-
*/
|
|
2656
|
-
refFor: (name: string) => string;
|
|
2657
|
-
/**
|
|
2658
|
-
* Minimum number of occurrences before a shape is deduplicated.
|
|
2659
|
-
*
|
|
2660
|
-
* @default 2
|
|
2661
|
-
*/
|
|
2662
|
-
minOccurrences?: number;
|
|
2663
|
-
};
|
|
2664
|
-
/**
|
|
2665
|
-
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
|
|
2666
|
-
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
|
|
2667
|
-
* so nested duplicates inside a replaced shape are not visited again. A `ref` that points
|
|
2668
|
-
* at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
|
|
2669
|
-
* the same content.
|
|
2670
|
-
*
|
|
2671
|
-
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
|
|
2672
|
-
* turned into a reference to itself. Nested duplicates are still collapsed.
|
|
2673
|
-
*
|
|
2674
|
-
* @example
|
|
2675
|
-
* ```ts
|
|
2676
|
-
* const next = applyDedupe(operationNode, plan)
|
|
2677
|
-
* ```
|
|
2678
|
-
*/
|
|
2679
|
-
declare function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode;
|
|
2680
|
-
declare function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode;
|
|
2681
|
-
/**
|
|
2682
|
-
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
|
|
2683
|
-
*
|
|
2684
|
-
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
|
|
2685
|
-
* is a named top-level schema, the first one becomes the canonical (so other top-level
|
|
2686
|
-
* duplicates and inline copies turn into references to it). Every other top-level name with
|
|
2687
|
-
* the same content is recorded in `aliasNames`, so refs to it can be repointed at the
|
|
2688
|
-
* canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
|
|
2689
|
-
* per node with {@link applyDedupe}.
|
|
2690
|
-
*
|
|
2691
|
-
* @example
|
|
2692
|
-
* ```ts
|
|
2693
|
-
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
|
|
2694
|
-
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
|
|
2695
|
-
* nameFor: (node) => node.name ?? null,
|
|
2696
|
-
* refFor: (name) => `#/components/schemas/${name}`,
|
|
2697
|
-
* })
|
|
2698
|
-
* ```
|
|
2699
|
-
*/
|
|
2700
|
-
declare function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan;
|
|
2701
|
-
//#endregion
|
|
2702
|
-
//#region src/dialect.d.ts
|
|
2703
|
-
/**
|
|
2704
|
-
* The spec-specific questions a schema parser answers while turning a source document into Kubb
|
|
2705
|
-
* AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where
|
|
2706
|
-
* OpenAPI, AsyncAPI, and plain JSON Schema differ.
|
|
2707
|
-
*/
|
|
2708
|
-
type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
|
|
2709
|
-
/**
|
|
2710
|
-
* Identifies the dialect in logs and diagnostics.
|
|
2711
|
-
*/
|
|
2712
|
-
name: string;
|
|
2713
|
-
/**
|
|
2714
|
-
* Whether the schema is nullable.
|
|
2715
|
-
*/
|
|
2716
|
-
isNullable: (schema?: TSchema) => boolean;
|
|
2717
|
-
/**
|
|
2718
|
-
* Whether the value is a `$ref` pointer.
|
|
2719
|
-
*/
|
|
2720
|
-
isReference: (value?: unknown) => value is TRef;
|
|
2721
|
-
/**
|
|
2722
|
-
* Whether the schema carries a discriminator for polymorphism.
|
|
2723
|
-
*/
|
|
2724
|
-
isDiscriminator: (value?: unknown) => value is TDiscriminated;
|
|
2725
|
-
/**
|
|
2726
|
-
* Whether the schema is binary data, converted to a `blob` node.
|
|
2727
|
-
*/
|
|
2728
|
-
isBinary: (schema: TSchema) => boolean;
|
|
2729
|
-
/**
|
|
2730
|
-
* Resolves a local `$ref` against the document, or nullish when it cannot.
|
|
2731
|
-
*/
|
|
2732
|
-
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined;
|
|
2733
|
-
};
|
|
2734
|
-
/**
|
|
2735
|
-
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
|
|
2736
|
-
* dialect's type for inference.
|
|
2737
|
-
*
|
|
2738
|
-
* @example
|
|
2739
|
-
* ```ts
|
|
2740
|
-
* export const oasDialect = defineSchemaDialect({
|
|
2741
|
-
* name: 'oas',
|
|
2742
|
-
* isNullable,
|
|
2743
|
-
* isReference,
|
|
2744
|
-
* isDiscriminator,
|
|
2745
|
-
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
2746
|
-
* resolveRef,
|
|
2747
|
-
* })
|
|
2748
|
-
* ```
|
|
2749
|
-
*/
|
|
2750
|
-
declare function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
|
|
2751
|
-
//#endregion
|
|
2752
|
-
//#region src/factory.d.ts
|
|
2753
|
-
/**
|
|
2754
|
-
* Identity-preserving node update: returns `node` unchanged when every field in
|
|
2755
|
-
* `changes` already equals (by reference) the current value, otherwise a new node
|
|
2756
|
-
* with the changes applied.
|
|
2757
|
-
*
|
|
2758
|
-
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
|
|
2759
|
-
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
|
|
2760
|
-
* downstream passes can detect "nothing changed" by identity. Comparison is
|
|
2761
|
-
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
|
|
2762
|
-
*
|
|
2763
|
-
* @example
|
|
2764
|
-
* ```ts
|
|
2765
|
-
* update(node, { name: node.name }) // -> same `node` reference
|
|
2766
|
-
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
2767
|
-
* ```
|
|
2768
|
-
*/
|
|
2769
|
-
declare function update<T extends Node>(node: T, changes: Partial<T>): T;
|
|
2770
|
-
/**
|
|
2771
|
-
* Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed
|
|
2772
|
-
* and `imports`/`exports`/`sources` are deduplicated.
|
|
2773
|
-
*/
|
|
2774
|
-
type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> & Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>;
|
|
2775
|
-
/**
|
|
2776
|
-
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
2777
|
-
*
|
|
2778
|
-
* Computes:
|
|
2779
|
-
* - `id` SHA256 hash of the file path
|
|
2780
|
-
* - `name` `baseName` without extension
|
|
2781
|
-
* - `extname` extension extracted from `baseName`
|
|
2782
|
-
*
|
|
2783
|
-
* Deduplicates:
|
|
2784
|
-
* - `sources` via `combineSources`
|
|
2785
|
-
* - `exports` via `combineExports`
|
|
2786
|
-
* - `imports` via `combineImports` (also filters unused imports)
|
|
2787
|
-
*
|
|
2788
|
-
* @throws {Error} when `baseName` has no extension.
|
|
2789
|
-
*
|
|
2790
|
-
* @example
|
|
2791
|
-
* ```ts
|
|
2792
|
-
* const file = createFile({
|
|
2793
|
-
* baseName: 'petStore.ts',
|
|
2794
|
-
* path: 'src/models/petStore.ts',
|
|
2795
|
-
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
2796
|
-
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
2797
|
-
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
2798
|
-
* })
|
|
2799
|
-
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
2800
|
-
* // file.name = 'petStore'
|
|
2801
|
-
* // file.extname = '.ts'
|
|
2802
|
-
* ```
|
|
2803
|
-
*/
|
|
2804
|
-
declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>;
|
|
2805
|
-
//#endregion
|
|
2806
|
-
//#region src/printer.d.ts
|
|
2807
|
-
/**
|
|
2808
|
-
* Runtime context passed as `this` to printer handlers.
|
|
2809
|
-
*
|
|
2810
|
-
* `this.transform` dispatches to node-level handlers from `nodes`.
|
|
2811
|
-
*
|
|
2812
|
-
* @example
|
|
2813
|
-
* ```ts
|
|
2814
|
-
* const context: PrinterHandlerContext<string, {}> = {
|
|
2815
|
-
* options: {},
|
|
2816
|
-
* transform: () => 'value',
|
|
2817
|
-
* }
|
|
2818
|
-
* ```
|
|
2819
|
-
*/
|
|
2820
|
-
type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
2821
|
-
/**
|
|
2822
|
-
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
|
|
2823
|
-
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
|
|
2824
|
-
*/
|
|
2825
|
-
transform: (node: SchemaNode) => TOutput | null;
|
|
2826
|
-
/**
|
|
2827
|
-
* Options for this printer instance.
|
|
2828
|
-
*/
|
|
2829
|
-
options: TOptions;
|
|
2830
|
-
};
|
|
2831
|
-
/**
|
|
2832
|
-
* Handler for one schema node type.
|
|
2833
|
-
*
|
|
2834
|
-
* Use a regular function (not an arrow function) if you need `this`.
|
|
2835
|
-
*
|
|
2836
|
-
* @example
|
|
2837
|
-
* ```ts
|
|
2838
|
-
* const handler: PrinterHandler<string, {}, 'string'> = function () {
|
|
2839
|
-
* return 'string'
|
|
2840
|
-
* }
|
|
2841
|
-
* ```
|
|
2842
|
-
*/
|
|
2843
|
-
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
|
|
2844
|
-
/**
|
|
2845
|
-
* Partial map of per-node-type handler overrides for a printer.
|
|
2846
|
-
*
|
|
2847
|
-
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
|
|
2848
|
-
* Supply only the handlers you want to replace. The printer's built-in
|
|
2849
|
-
* defaults fill in the rest.
|
|
2850
|
-
*
|
|
2851
|
-
* @example
|
|
2852
|
-
* ```ts
|
|
2853
|
-
* pluginZod({
|
|
2854
|
-
* printer: {
|
|
2855
|
-
* nodes: {
|
|
2856
|
-
* date(): string {
|
|
2857
|
-
* return 'z.string().date()'
|
|
2858
|
-
* },
|
|
2859
|
-
* } satisfies PrinterPartial<string, PrinterZodOptions>,
|
|
2860
|
-
* },
|
|
2861
|
-
* })
|
|
2862
|
-
* ```
|
|
2863
|
-
*/
|
|
2864
|
-
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
|
|
2865
|
-
/**
|
|
2866
|
-
* Generic shape used by `definePrinter`.
|
|
2867
|
-
*
|
|
2868
|
-
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
|
|
2869
|
-
* - `TOptions` options passed to and stored on the printer instance
|
|
2870
|
-
* - `TOutput` the type emitted by node handlers
|
|
2871
|
-
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
|
|
2872
|
-
*
|
|
2873
|
-
* @example
|
|
2874
|
-
* ```ts
|
|
2875
|
-
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
|
|
2876
|
-
* ```
|
|
2877
|
-
*/
|
|
2878
|
-
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
|
|
2879
|
-
name: TName;
|
|
2880
|
-
options: TOptions;
|
|
2881
|
-
output: TOutput;
|
|
2882
|
-
printOutput: TPrintOutput;
|
|
2883
|
-
};
|
|
2884
|
-
/**
|
|
2885
|
-
* Printer instance returned by a printer factory.
|
|
2886
|
-
*
|
|
2887
|
-
* @example
|
|
2888
|
-
* ```ts
|
|
2889
|
-
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
|
|
2890
|
-
* ```
|
|
2891
|
-
*/
|
|
2892
|
-
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
2893
|
-
/**
|
|
2894
|
-
* Unique identifier supplied at creation time.
|
|
2895
|
-
*/
|
|
2896
|
-
name: T['name'];
|
|
2897
|
-
/**
|
|
2898
|
-
* Options for this printer instance.
|
|
2899
|
-
*/
|
|
2900
|
-
options: T['options'];
|
|
2901
|
-
/**
|
|
2902
|
-
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
|
|
2903
|
-
* Always dispatches through the `nodes` map. Never calls the `print` override.
|
|
2904
|
-
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
|
|
2905
|
-
*/
|
|
2906
|
-
transform: (node: SchemaNode) => T['output'] | null;
|
|
2907
|
-
/**
|
|
2908
|
-
* Public printer. If the builder provides a root-level `print`, this calls that
|
|
2909
|
-
* higher-level function (which may produce full declarations).
|
|
2910
|
-
* Otherwise, falls back to the node-level dispatcher.
|
|
2911
|
-
*/
|
|
2912
|
-
print: (node: SchemaNode) => T['printOutput'] | null;
|
|
2913
|
-
};
|
|
2914
|
-
/**
|
|
2915
|
-
* Builder function passed to `definePrinter`.
|
|
2916
|
-
*
|
|
2917
|
-
* It receives resolved options and returns:
|
|
2918
|
-
* - `name`
|
|
2919
|
-
* - `options`
|
|
2920
|
-
* - `nodes` handlers
|
|
2921
|
-
* - optional top-level `print` override
|
|
2922
|
-
*
|
|
2923
|
-
* @example
|
|
2924
|
-
* ```ts
|
|
2925
|
-
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
|
|
2926
|
-
* ```
|
|
2927
|
-
*/
|
|
2928
|
-
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
|
|
2929
|
-
name: T['name'];
|
|
2930
|
-
/**
|
|
2931
|
-
* Options to store on the printer.
|
|
2932
|
-
*/
|
|
2933
|
-
options: T['options'];
|
|
2934
|
-
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
|
|
2935
|
-
/**
|
|
2936
|
-
* Optional root-level print override. When provided, becomes the public `printer.print`.
|
|
2937
|
-
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
|
|
2938
|
-
* not the override itself, so recursion is safe.
|
|
2939
|
-
*/
|
|
2940
|
-
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
|
|
2941
|
-
};
|
|
2942
|
-
/**
|
|
2943
|
-
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
2944
|
-
* code in your target language. Each plugin that produces code from schemas
|
|
2945
|
-
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
2946
|
-
* with this helper.
|
|
2947
|
-
*
|
|
2948
|
-
* The builder receives resolved options and returns:
|
|
2949
|
-
*
|
|
2950
|
-
* - `name` unique identifier for the printer.
|
|
2951
|
-
* - `options` stored on the returned printer instance.
|
|
2952
|
-
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
|
|
2953
|
-
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
2954
|
-
* - `print` (optional), top-level override exposed as `printer.print`.
|
|
2955
|
-
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
2956
|
-
*
|
|
2957
|
-
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
2958
|
-
* (the node-level dispatcher).
|
|
2959
|
-
*
|
|
2960
|
-
* @example Tiny Zod printer
|
|
2961
|
-
* ```ts
|
|
2962
|
-
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
2963
|
-
*
|
|
2964
|
-
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
2965
|
-
*
|
|
2966
|
-
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
2967
|
-
* name: 'zod',
|
|
2968
|
-
* options: { strict: options.strict ?? true },
|
|
2969
|
-
* nodes: {
|
|
2970
|
-
* string: () => 'z.string()',
|
|
2971
|
-
* object(node) {
|
|
2972
|
-
* const props = node.properties
|
|
2973
|
-
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
2974
|
-
* .join(', ')
|
|
2975
|
-
* return `z.object({ ${props} })`
|
|
2976
|
-
* },
|
|
2977
|
-
* },
|
|
2978
|
-
* }))
|
|
2979
|
-
* ```
|
|
2980
|
-
*/
|
|
2981
|
-
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
|
|
2982
|
-
/**
|
|
2983
|
-
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
2984
|
-
**
|
|
2985
|
-
* @example
|
|
2986
|
-
* ```ts
|
|
2987
|
-
* export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
|
|
2988
|
-
* (node) => kindToHandlerKey[node.kind],
|
|
2989
|
-
* )
|
|
2990
|
-
* ```
|
|
2991
|
-
*/
|
|
2992
|
-
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"]) => {
|
|
2993
|
-
name: T["name"];
|
|
2994
|
-
options: T["options"];
|
|
2995
|
-
nodes: Partial<{ [K in TKey]: (this: {
|
|
2996
|
-
transform: (node: TNode) => T["output"] | null;
|
|
2997
|
-
options: T["options"];
|
|
2998
|
-
}, node: TNodeByKey[K]) => T["output"] | null }>;
|
|
2999
|
-
print?: (this: {
|
|
3000
|
-
transform: (node: TNode) => T["output"] | null;
|
|
3001
|
-
options: T["options"];
|
|
3002
|
-
}, node: TNode) => T["printOutput"] | null;
|
|
3003
|
-
}) => (options?: T["options"]) => {
|
|
3004
|
-
name: T["name"];
|
|
3005
|
-
options: T["options"];
|
|
3006
|
-
transform: (node: TNode) => T["output"] | null;
|
|
3007
|
-
print: (node: TNode) => T["printOutput"] | null;
|
|
3008
|
-
};
|
|
3009
|
-
//#endregion
|
|
3010
|
-
//#region src/visitor.d.ts
|
|
3011
|
-
/**
|
|
3012
|
-
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
3013
|
-
*
|
|
3014
|
-
* `ParentOf` uses this map to find parent types.
|
|
3015
|
-
*/
|
|
3016
|
-
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
|
|
3017
|
-
/**
|
|
3018
|
-
* Resolves the parent node type for a given AST node type.
|
|
3019
|
-
*
|
|
3020
|
-
* This is used by visitor context so `ctx.parent` is correctly typed
|
|
3021
|
-
* for each callback.
|
|
3022
|
-
*
|
|
3023
|
-
* @example
|
|
3024
|
-
* ```ts
|
|
3025
|
-
* type InputParent = ParentOf<InputNode>
|
|
3026
|
-
* // undefined
|
|
3027
|
-
* ```
|
|
3028
|
-
*
|
|
3029
|
-
* @example
|
|
3030
|
-
* ```ts
|
|
3031
|
-
* type PropertyParent = ParentOf<PropertyNode>
|
|
3032
|
-
* // SchemaNode
|
|
3033
|
-
* ```
|
|
3034
|
-
*
|
|
3035
|
-
* @example
|
|
3036
|
-
* ```ts
|
|
3037
|
-
* type SchemaParent = ParentOf<SchemaNode>
|
|
3038
|
-
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
|
|
3039
|
-
* ```
|
|
3040
|
-
*/
|
|
3041
|
-
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
|
|
3042
|
-
/**
|
|
3043
|
-
* Traversal context passed as the second argument to every visitor callback.
|
|
3044
|
-
* `parent` is typed from the current node type.
|
|
3045
|
-
*
|
|
3046
|
-
* @example
|
|
3047
|
-
* ```ts
|
|
3048
|
-
* const visitor: Visitor = {
|
|
3049
|
-
* schema(node, { parent }) {
|
|
3050
|
-
* // parent type is narrowed by node kind
|
|
3051
|
-
* },
|
|
3052
|
-
* }
|
|
3053
|
-
* ```
|
|
3054
|
-
*/
|
|
3055
|
-
type VisitorContext<T extends Node = Node> = {
|
|
3056
|
-
/**
|
|
3057
|
-
* Parent node of the currently visited node.
|
|
3058
|
-
* For `InputNode`, this is `undefined`.
|
|
3059
|
-
*/
|
|
3060
|
-
parent?: ParentOf<T>;
|
|
3061
|
-
};
|
|
3062
|
-
/**
|
|
3063
|
-
* Synchronous visitor consumed by `transform`. Each optional callback runs
|
|
3064
|
-
* for the matching node type. Return a new node to replace it, or `undefined`
|
|
3065
|
-
* to leave it untouched.
|
|
3066
|
-
*
|
|
3067
|
-
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
3068
|
-
* rewrites operation IDs, drops descriptions, or otherwise tweaks the AST
|
|
3069
|
-
* before printing.
|
|
3070
|
-
*
|
|
3071
|
-
* @example Prefix every operationId
|
|
3072
|
-
* ```ts
|
|
3073
|
-
* const visitor: Visitor = {
|
|
3074
|
-
* operation(node) {
|
|
3075
|
-
* return { ...node, operationId: `api_${node.operationId}` }
|
|
3076
|
-
* },
|
|
3077
|
-
* }
|
|
3078
|
-
* ```
|
|
3079
|
-
*
|
|
3080
|
-
* @example Strip schema descriptions
|
|
3081
|
-
* ```ts
|
|
3082
|
-
* const visitor: Visitor = {
|
|
3083
|
-
* schema(node) {
|
|
3084
|
-
* return { ...node, description: undefined }
|
|
3085
|
-
* },
|
|
3086
|
-
* }
|
|
3087
|
-
* ```
|
|
3088
|
-
*/
|
|
3089
|
-
type Visitor = {
|
|
3090
|
-
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
|
|
3091
|
-
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
|
|
3092
|
-
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
|
|
3093
|
-
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
|
|
3094
|
-
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
|
|
3095
|
-
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
|
|
3096
|
-
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
|
|
3097
|
-
};
|
|
3098
|
-
/**
|
|
3099
|
-
* Utility type for values that can be returned directly or asynchronously.
|
|
3100
|
-
*/
|
|
3101
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
3102
|
-
/**
|
|
3103
|
-
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
3104
|
-
*
|
|
3105
|
-
* @example
|
|
3106
|
-
* ```ts
|
|
3107
|
-
* const visitor: AsyncVisitor = {
|
|
3108
|
-
* async operation(node) {
|
|
3109
|
-
* await Promise.resolve(node.operationId)
|
|
3110
|
-
* },
|
|
3111
|
-
* }
|
|
3112
|
-
* ```
|
|
3113
|
-
*/
|
|
3114
|
-
type AsyncVisitor = {
|
|
3115
|
-
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
|
|
3116
|
-
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
|
|
3117
|
-
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
|
|
3118
|
-
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
|
|
3119
|
-
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
|
|
3120
|
-
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
|
|
3121
|
-
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
|
|
3122
|
-
};
|
|
3123
|
-
/**
|
|
3124
|
-
* Visitor used by `collect`.
|
|
3125
|
-
*
|
|
3126
|
-
* @example
|
|
3127
|
-
* ```ts
|
|
3128
|
-
* const visitor: CollectVisitor<string> = {
|
|
3129
|
-
* operation(node) {
|
|
3130
|
-
* return node.operationId
|
|
3131
|
-
* },
|
|
3132
|
-
* }
|
|
3133
|
-
* ```
|
|
3134
|
-
*/
|
|
3135
|
-
type CollectVisitor<T> = {
|
|
3136
|
-
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
|
|
3137
|
-
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
|
|
3138
|
-
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
|
|
3139
|
-
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
|
|
3140
|
-
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
|
|
3141
|
-
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
|
|
3142
|
-
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
|
|
3143
|
-
};
|
|
3144
|
-
/**
|
|
3145
|
-
* Options for `transform`.
|
|
3146
|
-
*
|
|
3147
|
-
* @example
|
|
3148
|
-
* ```ts
|
|
3149
|
-
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
3150
|
-
* ```
|
|
3151
|
-
*
|
|
3152
|
-
* @example
|
|
3153
|
-
* ```ts
|
|
3154
|
-
* // Only transform the current node, not nested children
|
|
3155
|
-
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
3156
|
-
* ```
|
|
3157
|
-
*/
|
|
3158
|
-
type TransformOptions = Visitor & {
|
|
3159
|
-
/**
|
|
3160
|
-
* Traversal depth (`'deep'` by default).
|
|
3161
|
-
* @default 'deep'
|
|
3162
|
-
*/
|
|
3163
|
-
depth?: VisitorDepth;
|
|
3164
|
-
/**
|
|
3165
|
-
* Internal parent override used during recursion.
|
|
3166
|
-
*/
|
|
3167
|
-
parent?: Node;
|
|
3168
|
-
};
|
|
3169
|
-
/**
|
|
3170
|
-
* Options for `walk`.
|
|
3171
|
-
*
|
|
3172
|
-
* @example
|
|
3173
|
-
* ```ts
|
|
3174
|
-
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
3175
|
-
* ```
|
|
3176
|
-
*/
|
|
3177
|
-
type WalkOptions = AsyncVisitor & {
|
|
3178
|
-
/**
|
|
3179
|
-
* Traversal depth (`'deep'` by default).
|
|
3180
|
-
* @default 'deep'
|
|
3181
|
-
*/
|
|
3182
|
-
depth?: VisitorDepth;
|
|
3183
|
-
/**
|
|
3184
|
-
* Maximum number of sibling nodes visited concurrently.
|
|
3185
|
-
* @default 30
|
|
3186
|
-
*/
|
|
3187
|
-
concurrency?: number;
|
|
3188
|
-
};
|
|
3189
|
-
/**
|
|
3190
|
-
* Options for `collect`.
|
|
3191
|
-
*
|
|
3192
|
-
* @example
|
|
3193
|
-
* ```ts
|
|
3194
|
-
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
3195
|
-
* ```
|
|
3196
|
-
*/
|
|
3197
|
-
type CollectOptions<T> = CollectVisitor<T> & {
|
|
3198
|
-
/**
|
|
3199
|
-
* Traversal depth (`'deep'` by default).
|
|
3200
|
-
* @default 'deep'
|
|
3201
|
-
*/
|
|
3202
|
-
depth?: VisitorDepth;
|
|
3203
|
-
/**
|
|
3204
|
-
* Internal parent override used during recursion.
|
|
3205
|
-
*/
|
|
3206
|
-
parent?: Node;
|
|
3207
|
-
};
|
|
3208
|
-
/**
|
|
3209
|
-
* Async depth-first traversal for side effects. Visitor return values are
|
|
3210
|
-
* ignored. Use `transform` when you want to rewrite nodes.
|
|
3211
|
-
*
|
|
3212
|
-
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
3213
|
-
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
3214
|
-
* work. Lower values reduce memory pressure.
|
|
3215
|
-
*
|
|
3216
|
-
* @example Log every operation
|
|
3217
|
-
* ```ts
|
|
3218
|
-
* await walk(root, {
|
|
3219
|
-
* operation(node) {
|
|
3220
|
-
* console.log(node.operationId)
|
|
3221
|
-
* },
|
|
3222
|
-
* })
|
|
3223
|
-
* ```
|
|
3224
|
-
*
|
|
3225
|
-
* @example Only visit the root node
|
|
3226
|
-
* ```ts
|
|
3227
|
-
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
3228
|
-
* ```
|
|
3229
|
-
*/
|
|
3230
|
-
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
3231
|
-
/**
|
|
3232
|
-
* Synchronous depth-first transform. Each visitor callback gets a chance to
|
|
3233
|
-
* return a replacement node; `undefined` keeps the original.
|
|
3234
|
-
*
|
|
3235
|
-
* The transform is immutable. The original tree is not mutated. A new tree
|
|
3236
|
-
* is returned. Use `depth: 'shallow'` to skip recursion into children.
|
|
3237
|
-
*
|
|
3238
|
-
* @example Prefix every operationId
|
|
3239
|
-
* ```ts
|
|
3240
|
-
* const next = transform(root, {
|
|
3241
|
-
* operation(node) {
|
|
3242
|
-
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
3243
|
-
* },
|
|
3244
|
-
* })
|
|
3245
|
-
* ```
|
|
3246
|
-
*
|
|
3247
|
-
* @example Replace only the root node
|
|
3248
|
-
* ```ts
|
|
3249
|
-
* const next = transform(root, {
|
|
3250
|
-
* depth: 'shallow',
|
|
3251
|
-
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
|
|
3252
|
-
* })
|
|
3253
|
-
* ```
|
|
3254
|
-
*/
|
|
3255
|
-
declare function transform(node: InputNode, options: TransformOptions): InputNode;
|
|
3256
|
-
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
|
|
3257
|
-
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
3258
|
-
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
3259
|
-
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
3260
|
-
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
3261
|
-
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
3262
|
-
declare function transform(node: Node, options: TransformOptions): Node;
|
|
3263
|
-
/**
|
|
3264
|
-
* Eager depth-first collection pass. Returns an array of every non-null value
|
|
3265
|
-
* the visitor callbacks return.
|
|
3266
|
-
*
|
|
3267
|
-
* @example Collect every operationId
|
|
3268
|
-
* ```ts
|
|
3269
|
-
* const ids = collect<string>(root, {
|
|
3270
|
-
* operation(node) {
|
|
3271
|
-
* return node.operationId
|
|
3272
|
-
* },
|
|
3273
|
-
* })
|
|
3274
|
-
* ```
|
|
3275
|
-
*/
|
|
3276
|
-
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
3277
|
-
//#endregion
|
|
3278
2468
|
//#region src/utils/ast.d.ts
|
|
3279
2469
|
/**
|
|
3280
2470
|
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
@@ -3314,6 +2504,19 @@ declare function createDiscriminantNode({
|
|
|
3314
2504
|
propertyName: string;
|
|
3315
2505
|
value: string;
|
|
3316
2506
|
}): SchemaNode;
|
|
2507
|
+
/**
|
|
2508
|
+
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
|
|
2509
|
+
*/
|
|
2510
|
+
type ParamGroupType = {
|
|
2511
|
+
/**
|
|
2512
|
+
* Type expression for the group, a plain group-name reference.
|
|
2513
|
+
*/
|
|
2514
|
+
type: TypeExpression;
|
|
2515
|
+
/**
|
|
2516
|
+
* Whether the parameter group is optional.
|
|
2517
|
+
*/
|
|
2518
|
+
optional: boolean;
|
|
2519
|
+
};
|
|
3317
2520
|
/**
|
|
3318
2521
|
* Resolver interface for {@link createOperationParams}.
|
|
3319
2522
|
*
|
|
@@ -3438,6 +2641,22 @@ type CreateOperationParamsOptions = {
|
|
|
3438
2641
|
*/
|
|
3439
2642
|
typeWrapper?: (type: string) => string;
|
|
3440
2643
|
};
|
|
2644
|
+
/**
|
|
2645
|
+
* Resolves the {@link TypeExpression} for an individual parameter.
|
|
2646
|
+
*
|
|
2647
|
+
* Without a resolver, falls back to the schema primitive (a plain type-name string).
|
|
2648
|
+
* When the parameter belongs to a named group, emits an {@link IndexedAccessTypeNode}
|
|
2649
|
+
* (`GroupParams['petId']`); otherwise the resolved individual name as a plain string.
|
|
2650
|
+
*/
|
|
2651
|
+
declare function resolveParamType({
|
|
2652
|
+
node,
|
|
2653
|
+
param,
|
|
2654
|
+
resolver
|
|
2655
|
+
}: {
|
|
2656
|
+
node: OperationNode;
|
|
2657
|
+
param: ParameterNode;
|
|
2658
|
+
resolver: OperationParamsResolver | undefined;
|
|
2659
|
+
}): TypeExpression;
|
|
3441
2660
|
/**
|
|
3442
2661
|
* Converts an `OperationNode` into function parameters for code generation.
|
|
3443
2662
|
*
|
|
@@ -3448,12 +2667,39 @@ type CreateOperationParamsOptions = {
|
|
|
3448
2667
|
*/
|
|
3449
2668
|
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
|
|
3450
2669
|
/**
|
|
3451
|
-
*
|
|
2670
|
+
* Shared arguments for building a query or header parameter group.
|
|
2671
|
+
*/
|
|
2672
|
+
type BuildGroupArgs = {
|
|
2673
|
+
name: string;
|
|
2674
|
+
node: OperationNode;
|
|
2675
|
+
params: Array<ParameterNode>;
|
|
2676
|
+
groupType: ParamGroupType | null;
|
|
2677
|
+
resolver: OperationParamsResolver | undefined;
|
|
2678
|
+
wrapType: (type: string) => string;
|
|
2679
|
+
};
|
|
2680
|
+
/**
|
|
2681
|
+
* Builds a single {@link FunctionParameterNode} for a query or header group.
|
|
2682
|
+
* Returns an empty array when there are no params to emit.
|
|
2683
|
+
*
|
|
2684
|
+
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
|
|
2685
|
+
* {@link TypeLiteralNode} from the individual params.
|
|
2686
|
+
*/
|
|
2687
|
+
declare function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode>;
|
|
2688
|
+
/**
|
|
2689
|
+
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
|
|
3452
2690
|
*
|
|
3453
|
-
*
|
|
3454
|
-
*
|
|
2691
|
+
* Used when query or header parameters have no dedicated group type name.
|
|
2692
|
+
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
|
|
3455
2693
|
*/
|
|
3456
|
-
declare function
|
|
2694
|
+
declare function buildTypeLiteral({
|
|
2695
|
+
node,
|
|
2696
|
+
params,
|
|
2697
|
+
resolver
|
|
2698
|
+
}: {
|
|
2699
|
+
node: OperationNode;
|
|
2700
|
+
params: Array<ParameterNode>;
|
|
2701
|
+
resolver: OperationParamsResolver | undefined;
|
|
2702
|
+
}): TypeLiteralNode;
|
|
3457
2703
|
declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
|
|
3458
2704
|
/**
|
|
3459
2705
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
@@ -3481,5 +2727,5 @@ declare function containsCircularRef(node: SchemaNode | undefined, {
|
|
|
3481
2727
|
excludeName?: string;
|
|
3482
2728
|
}): boolean;
|
|
3483
2729
|
//#endregion
|
|
3484
|
-
export {
|
|
3485
|
-
//# sourceMappingURL=
|
|
2730
|
+
export { functionParametersDef as $, createBreak as $t, responseDef as A, StringSchemaNode as At, FunctionParamNode as B, ParserOptions as Bt, HttpMethod as C, ObjectSchemaNode as Ct, operationDef as D, SchemaNode as Dt, createOperation as E, ScalarSchemaType as Et, ParameterLocation as F, schemaDef as Ft, TypeExpression as G, JSDocNode as Gt, FunctionParametersNode as H, CodeNode as Ht, ParameterNode as I, PropertyNode as It, createFunctionParameters as J, TypeNode as Jt, TypeLiteralNode as K, JsxNode as Kt, createParameter as L, createProperty as Lt, RequestBodyNode as M, UnionSchemaNode as Mt, createRequestBody as N, UrlSchemaNode as Nt, ResponseNode as O, SchemaNodeByType as Ot, requestBodyDef as P, createSchema as Pt, functionParameterDef as Q, createArrowFunction as Qt, parameterDef as R, propertyDef as Rt, inputDef as S, NumberSchemaNode as St, OperationNode as T, RefSchemaNode as Tt, IndexedAccessTypeNode as U, ConstNode as Ut, FunctionParameterNode as V, ArrowFunctionNode as Vt, ObjectBindingPatternNode as W, FunctionNode as Wt, createObjectBindingPattern as X, breakDef as Xt, createIndexedAccessType as Y, arrowFunctionDef as Yt, createTypeLiteral as Z, constDef as Zt, createOutput as _, ArraySchemaNode as _t, buildTypeLiteral as a, functionDef as an, ImportNode as at, InputNode as b, EnumSchemaNode as bt, containsCircularRef as c, typeDef as cn, createImport as ct, findCircularSchemas as d, defineNode as dn, fileDef as dt, createConst as en, indexedAccessTypeDef as et, isStringType as f, syncOptionality as fn, importDef as ft, OutputNode as g, createContent as gt, Node as h, contentDef as ht, buildGroupParam as i, createType as in, FileNode as it, StatusCode as j, TimeSchemaNode as jt, createResponse as k, SchemaType as kt, createDiscriminantNode as l, DistributiveOmit as ln, createSource as lt, syncSchemaRef as m, ContentNode as mt, OperationParamsResolver as n, createJsx as nn, typeLiteralDef as nt, caseParams as o, jsxDef as on, SourceNode as ot, resolveParamType as p, NodeKind as pn, sourceDef as pt, createFunctionParameter as q, TextNode as qt, ParamGroupType as r, createText as rn, ExportNode as rt, collectUsedSchemaNames as s, textDef as sn, createExport as st, BuildGroupArgs as t, createFunction as tn, objectBindingPatternDef as tt, createOperationParams as u, NodeDef as un, exportDef as ut, outputDef as v, DateSchemaNode as vt, HttpOperationNode as w, PrimitiveSchemaType as wt, createInput as x, IntersectionSchemaNode as xt, InputMeta as y, DatetimeSchemaNode as yt, FunctionParamKind as z, InferSchemaNode as zt };
|
|
2731
|
+
//# sourceMappingURL=ast-ClnJg9BN.d.ts.map
|