@kubb/ast 5.0.0-alpha.16 → 5.0.0-alpha.18

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/dist/index.d.ts CHANGED
@@ -1,18 +1,141 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { A as createSchema, C as createFunctionParameters, Ct as schemaTypes, D as createProperty, E as createParameter, H as ParameterNode, I as OperationNode, L as ResponseNode, O as createResponse, P as RootNode, S as createFunctionParameter, St as nodeKinds, T as createOperation, _ as resolveRef, b as definePrinter, bt as httpMethods, d as transform, dt as FunctionNode, f as walk, g as refMapToObject, h as extractRefName, ht as ObjectBindingParameterNode, j as syncPropertySchema, k as createRoot, l as collect, m as buildRefMap, mt as FunctionParametersNode, nt as SchemaNode, pt as FunctionParameterNode, rt as SchemaNodeByType, u as composeTransformers, ut as PropertyNode, vt as SCALAR_PRIMITIVE_TYPES, w as createObjectBindingParameter, xt as mediaTypes } from "./visitor-YMltBj6w.js";
2
+ import { A as createRoot, B as ResponseNode, C as createFunctionParameter, Ct as httpMethods, D as createParameter, E as createOperation, Et as schemaTypes, G as ParameterNode, L as RootNode, M as InferSchema, N as InferSchemaNode, O as createProperty, P as ParserOptions, S as createDiscriminantNode, T as createObjectBindingParameter, Tt as nodeKinds, _ as resolveRef, _t as FunctionParametersNode, at as SchemaNode, b as definePrinter, d as transform, f as walk, g as refMapToObject, gt as FunctionParameterNode, h as extractRefName, j as createSchema, k as createResponse, l as collect, m as buildRefMap, mt as FunctionNode, ot as SchemaNodeByType, pt as PropertyNode, u as composeTransformers, vt as ObjectBindingParameterNode, w as createFunctionParameters, wt as mediaTypes, xt as SCALAR_PRIMITIVE_TYPES, y as PrinterFactoryOptions, z as OperationNode } from "./visitor-DCQyoFvH.js";
3
3
 
4
- //#region src/functionPrinter.d.ts
4
+ //#region src/guards.d.ts
5
+ /**
6
+ * Narrows a `SchemaNode` to the variant that matches `type`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const schema = createSchema({ type: 'string' })
11
+ * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
12
+ * ```
13
+ */
14
+ declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
15
+ /**
16
+ * Returns `true` when the input is a `RootNode`.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * if (isRootNode(node)) {
21
+ * console.log(node.schemas.length)
22
+ * }
23
+ * ```
24
+ */
25
+ declare const isRootNode: (node: unknown) => node is RootNode;
26
+ /**
27
+ * Returns `true` when the input is an `OperationNode`.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * if (isOperationNode(node)) {
32
+ * console.log(node.operationId)
33
+ * }
34
+ * ```
35
+ */
36
+ declare const isOperationNode: (node: unknown) => node is OperationNode;
37
+ /**
38
+ * Returns `true` when the input is a `SchemaNode`.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * if (isSchemaNode(node)) {
43
+ * console.log(node.type)
44
+ * }
45
+ * ```
46
+ */
47
+ declare const isSchemaNode: (node: unknown) => node is SchemaNode;
48
+ /**
49
+ * Returns `true` when the input is a `PropertyNode`.
50
+ */
51
+ declare const isPropertyNode: (node: unknown) => node is PropertyNode;
52
+ /**
53
+ * Returns `true` when the input is a `ParameterNode`.
54
+ */
55
+ declare const isParameterNode: (node: unknown) => node is ParameterNode;
56
+ /**
57
+ * Returns `true` when the input is a `ResponseNode`.
58
+ */
59
+ declare const isResponseNode: (node: unknown) => node is ResponseNode;
60
+ /**
61
+ * Returns `true` when the input is a `FunctionParameterNode`.
62
+ */
63
+ declare const isFunctionParameterNode: (node: unknown) => node is FunctionParameterNode;
64
+ /**
65
+ * Returns `true` when the input is an `ObjectBindingParameterNode`.
66
+ */
67
+ declare const isObjectBindingParameterNode: (node: unknown) => node is ObjectBindingParameterNode;
68
+ /**
69
+ * Returns `true` when the input is a `FunctionParametersNode`.
70
+ */
71
+ declare const isFunctionParametersNode: (node: unknown) => node is FunctionParametersNode;
72
+ //#endregion
73
+ //#region src/printers/functionPrinter.d.ts
5
74
  /**
6
- * The four rendering modes for a `FunctionParametersNode`.
75
+ * Creates a function-parameter printer factory.
76
+ *
77
+ * This wrapper uses `createPrinterFactory` and dispatches handlers by `node.kind`
78
+ * (for function nodes) rather than by `node.type` (for schema nodes).
7
79
  *
8
- * | Mode | Output example | Use case |
9
- * |---------------|---------------------------------------------|---------------------------------|
10
- * | `declaration` | `id: string, config: Config = {}` | Function parameter declarations |
11
- * | `call` | `id, { method, url }` | Function call arguments |
12
- * | `keys` | `{ id, config }` | Key names only (destructuring) |
13
- * | `values` | `{ id: id, config: config }` | Key → value pairs |
80
+ * @example
81
+ * ```ts
82
+ * type MyPrinter = PrinterFactoryOptions<'my', { mode: 'declaration' | 'call' }, string>
83
+ *
84
+ * export const myPrinter = defineFunctionPrinter<MyPrinter>((options) => ({
85
+ * name: 'my',
86
+ * options,
87
+ * nodes: {
88
+ * functionParameter(node) {
89
+ * return options.mode === 'declaration' && node.type ? `${node.name}: ${node.type}` : node.name
90
+ * },
91
+ * objectBindingParameter(node) {
92
+ * const inner = node.properties.map(p => this.print(p)).filter(Boolean).join(', ')
93
+ * return `{ ${inner} }`
94
+ * },
95
+ * functionParameters(node) {
96
+ * return node.params.map(p => this.print(p)).filter(Boolean).join(', ')
97
+ * },
98
+ * },
99
+ * }))
100
+ * ```
14
101
  */
102
+ declare const defineFunctionPrinter: <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
103
+ name: T["name"];
104
+ options: T["options"];
105
+ nodes: Partial<{
106
+ functionParameter: (this: {
107
+ print: (node: FunctionNode) => T["output"] | null | undefined;
108
+ options: T["options"];
109
+ }, node: FunctionParameterNode) => T["output"] | null | undefined;
110
+ objectBindingParameter: (this: {
111
+ print: (node: FunctionNode) => T["output"] | null | undefined;
112
+ options: T["options"];
113
+ }, node: ObjectBindingParameterNode) => T["output"] | null | undefined;
114
+ functionParameters: (this: {
115
+ print: (node: FunctionNode) => T["output"] | null | undefined;
116
+ options: T["options"];
117
+ }, node: FunctionParametersNode) => T["output"] | null | undefined;
118
+ }>;
119
+ print?: ((this: {
120
+ print: (node: FunctionNode) => T["output"] | null | undefined;
121
+ options: T["options"];
122
+ }, node: FunctionNode) => T["printOutput"] | null | undefined) | undefined;
123
+ }) => (options?: T["options"] | undefined) => {
124
+ name: T["name"];
125
+ options: T["options"];
126
+ print: (node: FunctionNode) => T["printOutput"] | null | undefined;
127
+ };
15
128
  type FunctionPrinterOptions = {
129
+ /**
130
+ * Rendering modes supported by `functionPrinter`.
131
+ *
132
+ * | Mode | Output example | Use case |
133
+ * |---------------|---------------------------------------------|---------------------------------|
134
+ * | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
135
+ * | `call` | `id, { method, url }` | Function call arguments |
136
+ * | `keys` | `{ id, config }` | Key names only (destructuring) |
137
+ * | `values` | `{ id: id, config: config }` | Key/value object entries |
138
+ */
16
139
  mode: 'declaration' | 'call' | 'keys' | 'values';
17
140
  /**
18
141
  * Optional transformation applied to every parameter name before printing.
@@ -24,12 +147,12 @@ type FunctionPrinterOptions = {
24
147
  transformType?: (type: string) => string;
25
148
  };
26
149
  /**
27
- * Default function-signature printer. Covers the four standard output modes
28
- * used throughout Kubb plugins.
150
+ * Default function-signature printer.
151
+ * Covers the four standard output modes used across Kubb plugins.
29
152
  *
30
153
  * @example
31
154
  * ```ts
32
- * const printer = functionSignaturePrinter({ mode: 'declaration' })
155
+ * const printer = functionPrinter({ mode: 'declaration' })
33
156
  *
34
157
  * const sig = createFunctionParameters({
35
158
  * params: [
@@ -47,54 +170,40 @@ declare const functionPrinter: (options?: FunctionPrinterOptions | undefined) =>
47
170
  print: (node: FunctionNode) => string | null | undefined;
48
171
  };
49
172
  //#endregion
50
- //#region src/guards.d.ts
51
- /**
52
- * Narrows a `SchemaNode` to the specific variant matching `type`.
53
- */
54
- declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
173
+ //#region src/resolvers.d.ts
174
+ declare function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null;
175
+ declare function childName(parentName: string | null | undefined, propName: string): string | null;
176
+ declare function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string;
55
177
  /**
56
- * Type guard for `RootNode`.
178
+ * Collects import entries for all `ref` schema nodes in `node`.
57
179
  */
58
- declare const isRootNode: (node: unknown) => node is RootNode;
59
- /**
60
- * Type guard for `OperationNode`.
61
- */
62
- declare const isOperationNode: (node: unknown) => node is OperationNode;
63
- /**
64
- * Type guard for `SchemaNode`.
65
- */
66
- declare const isSchemaNode: (node: unknown) => node is SchemaNode;
67
- /**
68
- * Type guard for `PropertyNode`.
69
- */
70
- declare const isPropertyNode: (node: unknown) => node is PropertyNode;
71
- /**
72
- * Type guard for `ParameterNode`.
73
- */
74
- declare const isParameterNode: (node: unknown) => node is ParameterNode;
75
- /**
76
- * Type guard for `ResponseNode`.
77
- */
78
- declare const isResponseNode: (node: unknown) => node is ResponseNode;
79
- /**
80
- * Type guard for `FunctionParameterNode`.
81
- */
82
- declare const isFunctionParameterNode: (node: unknown) => node is FunctionParameterNode;
83
- /**
84
- * Type guard for `ObjectBindingParameterNode`.
85
- */
86
- declare const isObjectBindingParameterNode: (node: unknown) => node is ObjectBindingParameterNode;
87
- /**
88
- * Type guard for `FunctionParametersNode`.
89
- */
90
- declare const isFunctionParametersNode: (node: unknown) => node is FunctionParametersNode;
180
+ declare function collectImports<TImport>({
181
+ node,
182
+ nameMapping,
183
+ resolve
184
+ }: {
185
+ node: SchemaNode;
186
+ nameMapping: Map<string, string>;
187
+ resolve: (schemaName: string) => TImport | undefined;
188
+ }): Array<TImport>;
91
189
  //#endregion
92
- //#region src/transforms.d.ts
190
+ //#region src/transformers.d.ts
93
191
  /**
94
- * Replaces the discriminator property's schema inside an object node with
95
- * an enum of the provided values.
192
+ * Replaces a discriminator property's schema with a string enum of allowed values.
193
+ *
194
+ * If `node` is not an object schema, or if the property does not exist, the input
195
+ * node is returned as-is.
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * const schema = createSchema({
200
+ * type: 'object',
201
+ * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
202
+ * })
203
+ * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
204
+ * ```
96
205
  */
97
- declare function applyDiscriminatorEnum({
206
+ declare function setDiscriminatorEnum({
98
207
  node,
99
208
  propertyName,
100
209
  values,
@@ -106,33 +215,85 @@ declare function applyDiscriminatorEnum({
106
215
  enumName?: string;
107
216
  }): SchemaNode;
108
217
  /**
109
- * Merges adjacent anonymous object members into a single anonymous object.
218
+ * Merges adjacent anonymous object members into a single anonymous object member.
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * const merged = mergeAdjacentObjects([
223
+ * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
224
+ * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
225
+ * ])
226
+ * ```
110
227
  */
111
- declare function mergeAdjacentAnonymousObjects(members: Array<SchemaNode>): Array<SchemaNode>;
228
+ declare function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode>;
112
229
  /**
113
- * Removes enum members subsumed by broader scalar members in the same union.
230
+ * Removes enum members that are covered by broader scalar primitives in the same union.
231
+ *
232
+ * @example
233
+ * ```ts
234
+ * const simplified = simplifyUnion([
235
+ * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
236
+ * createSchema({ type: 'string' }),
237
+ * ])
238
+ * // keeps only string member
239
+ * ```
114
240
  */
115
- declare function simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode>;
241
+ declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
242
+ declare function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode;
243
+ /**
244
+ * Walks a schema tree and resolves `ref`/`enum` names through callbacks.
245
+ */
246
+ declare function resolveNames({
247
+ node,
248
+ nameMapping,
249
+ resolveName,
250
+ resolveEnumName
251
+ }: {
252
+ node: SchemaNode;
253
+ nameMapping: Map<string, string>;
254
+ resolveName: (ref: string) => string | undefined;
255
+ resolveEnumName?: (name: string) => string | undefined;
256
+ }): SchemaNode;
116
257
  //#endregion
117
258
  //#region src/utils.d.ts
118
259
  /**
119
- * Returns `true` when a schema node will be represented as a plain string in generated code.
260
+ * Returns `true` when a schema is emitted as a plain TypeScript `string`.
120
261
  *
121
262
  * - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
122
263
  * - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
264
+ *
265
+ * @example
266
+ * ```ts
267
+ * isStringType(createSchema({ type: 'uuid' })) // true
268
+ * isStringType(createSchema({ type: 'date', representation: 'date' })) // false
269
+ * ```
123
270
  */
124
- declare function isPlainStringType(node: SchemaNode): boolean;
271
+ declare function isStringType(node: SchemaNode): boolean;
125
272
  /**
126
- * Transforms the `name` field of each parameter node according to the given casing strategy.
273
+ * Applies casing rules to parameter names and returns a new parameter array.
127
274
  *
128
- * The original `params` array is never mutated — a new array of cloned nodes is returned.
129
- * When no `casing` is provided the original array is returned as-is.
275
+ * The input array is not mutated.
276
+ * If `casing` is not set, the original array is returned unchanged.
130
277
  *
131
278
  * Use this before passing parameters to schema builders so that property keys
132
- * in the generated output match the desired casing while the original
133
- * `OperationNode.parameters` array remains untouched for other consumers.
279
+ * in generated output match the desired casing while preserving
280
+ * `OperationNode.parameters` for other consumers.
281
+ *
282
+ * @example
283
+ * ```ts
284
+ * const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
285
+ * const cased = caseParams(params, 'camelcase')
286
+ * // cased[0].name === 'petId'
287
+ * ```
288
+ */
289
+ declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
290
+ /**
291
+ * Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
292
+ *
293
+ * - `optional` is set for non-required, non-nullable schemas.
294
+ * - `nullish` is set for non-required, nullable schemas.
134
295
  */
135
- declare function applyParamsCasing(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
296
+ declare function syncOptionality(required: boolean, schema: SchemaNode): SchemaNode;
136
297
  //#endregion
137
- export { SCALAR_PRIMITIVE_TYPES, applyDiscriminatorEnum, applyParamsCasing, buildRefMap, collect, composeTransformers, createFunctionParameter, createFunctionParameters, createObjectBindingParameter, createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, definePrinter, extractRefName, functionPrinter, httpMethods, isFunctionParameterNode, isFunctionParametersNode, isObjectBindingParameterNode, isOperationNode, isParameterNode, isPlainStringType, isPropertyNode, isResponseNode, isRootNode, isSchemaNode, mediaTypes, mergeAdjacentAnonymousObjects, narrowSchema, nodeKinds, refMapToObject, resolveRef, schemaTypes, simplifyUnionMembers, syncPropertySchema, transform, walk };
298
+ export { type InferSchema, type InferSchemaNode, type ParserOptions, SCALAR_PRIMITIVE_TYPES, buildRefMap, caseParams, childName, collect, collectImports, composeTransformers, createDiscriminantNode, createFunctionParameter, createFunctionParameters, createObjectBindingParameter, createOperation, createParameter, createProperty, createResponse, createRoot, createSchema, defineFunctionPrinter, definePrinter, enumPropName, extractRefName, findDiscriminator, functionPrinter, httpMethods, isFunctionParameterNode, isFunctionParametersNode, isObjectBindingParameterNode, isOperationNode, isParameterNode, isPropertyNode, isResponseNode, isRootNode, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, refMapToObject, resolveNames, resolveRef, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, transform, walk };
138
299
  //# sourceMappingURL=index.d.ts.map