@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
package/src/index.ts
CHANGED
|
@@ -1,62 +1,28 @@
|
|
|
1
1
|
export { httpMethods, schemaTypes } from './constants.ts'
|
|
2
2
|
export { applyDedupe, buildDedupePlan } from './dedupe.ts'
|
|
3
3
|
export { defineSchemaDialect } from './dialect.ts'
|
|
4
|
-
|
|
4
|
+
// Node constructors, also published as the `@kubb/ast/factory` subpath, mirroring `ts.factory.createX`.
|
|
5
|
+
export * as factory from './factory.ts'
|
|
5
6
|
export { isHttpOperationNode, narrowSchema } from './guards.ts'
|
|
6
7
|
export { defineNode } from './node.ts'
|
|
7
8
|
export type { NodeDef } from './node.ts'
|
|
8
9
|
export { syncOptionality } from './node.ts'
|
|
9
|
-
export {
|
|
10
|
-
arrowFunctionDef,
|
|
11
|
-
breakDef,
|
|
12
|
-
constDef,
|
|
13
|
-
createArrowFunction,
|
|
14
|
-
createBreak,
|
|
15
|
-
createConst,
|
|
16
|
-
createFunction,
|
|
17
|
-
createJsx,
|
|
18
|
-
createText,
|
|
19
|
-
createType,
|
|
20
|
-
functionDef,
|
|
21
|
-
jsxDef,
|
|
22
|
-
textDef,
|
|
23
|
-
typeDef,
|
|
24
|
-
} from './nodes/code.ts'
|
|
10
|
+
export { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'
|
|
25
11
|
export { contentDef } from './nodes/content.ts'
|
|
26
|
-
export {
|
|
27
|
-
export {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
functionParameterDef,
|
|
34
|
-
functionParametersDef,
|
|
35
|
-
indexedAccessTypeDef,
|
|
36
|
-
objectBindingPatternDef,
|
|
37
|
-
typeLiteralDef,
|
|
38
|
-
} from './nodes/function.ts'
|
|
39
|
-
export { createInput, createStreamInput, inputDef } from './nodes/input.ts'
|
|
40
|
-
export { createOperation, operationDef } from './nodes/operation.ts'
|
|
41
|
-
export { createOutput, outputDef } from './nodes/output.ts'
|
|
42
|
-
export { createParameter, parameterDef } from './nodes/parameter.ts'
|
|
43
|
-
export { createProperty, propertyDef } from './nodes/property.ts'
|
|
12
|
+
export { exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts'
|
|
13
|
+
export { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'
|
|
14
|
+
export { inputDef } from './nodes/input.ts'
|
|
15
|
+
export { operationDef } from './nodes/operation.ts'
|
|
16
|
+
export { outputDef } from './nodes/output.ts'
|
|
17
|
+
export { parameterDef } from './nodes/parameter.ts'
|
|
18
|
+
export { propertyDef } from './nodes/property.ts'
|
|
44
19
|
export { requestBodyDef } from './nodes/requestBody.ts'
|
|
45
|
-
export {
|
|
46
|
-
export {
|
|
20
|
+
export { responseDef } from './nodes/response.ts'
|
|
21
|
+
export { schemaDef } from './nodes/schema.ts'
|
|
47
22
|
export { createPrinterFactory, definePrinter } from './printer.ts'
|
|
48
23
|
export { signatureOf } from './signature.ts'
|
|
49
24
|
export { mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
|
|
50
25
|
export type * from './types.ts'
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
collectUsedSchemaNames,
|
|
54
|
-
containsCircularRef,
|
|
55
|
-
createDiscriminantNode,
|
|
56
|
-
createOperationParams,
|
|
57
|
-
extractStringsFromNodes,
|
|
58
|
-
findCircularSchemas,
|
|
59
|
-
isStringType,
|
|
60
|
-
syncSchemaRef,
|
|
61
|
-
} from './utils/ast.ts'
|
|
26
|
+
// The node/AST helpers in ./utils/ast.ts live on the `@kubb/ast/utils` subpath, not the root barrel.
|
|
27
|
+
export { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'
|
|
62
28
|
export { collect, transform, walk } from './visitor.ts'
|
package/src/node.ts
CHANGED
|
@@ -23,7 +23,7 @@ export type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omi
|
|
|
23
23
|
/**
|
|
24
24
|
* Builds a type guard that matches nodes of the given `kind`.
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
function isKind<T extends BaseNode>(kind: NodeKind) {
|
|
27
27
|
return (node: unknown): node is T => (node as BaseNode).kind === kind
|
|
28
28
|
}
|
|
29
29
|
|
package/src/nodes/base.ts
CHANGED
package/src/nodes/function.ts
CHANGED
|
@@ -174,9 +174,9 @@ export type FunctionParametersNode = BaseNode & {
|
|
|
174
174
|
export type FunctionParamNode = FunctionParameterNode | FunctionParametersNode | TypeLiteralNode | IndexedAccessTypeNode | ObjectBindingPatternNode
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
|
-
* Handler
|
|
177
|
+
* Handler-map keys for the function-parameter printer, one per {@link FunctionParamNode} kind.
|
|
178
178
|
*/
|
|
179
|
-
export type
|
|
179
|
+
export type FunctionParamKind = FunctionParamNode['kind']
|
|
180
180
|
|
|
181
181
|
/**
|
|
182
182
|
* Definition for the {@link TypeLiteralNode}.
|
package/src/nodes/index.ts
CHANGED
|
@@ -16,7 +16,7 @@ export type { ArrowFunctionNode, BreakNode, CodeNode, ConstNode, FunctionNode, J
|
|
|
16
16
|
export type { ContentNode } from './content.ts'
|
|
17
17
|
export type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
|
|
18
18
|
export type {
|
|
19
|
-
|
|
19
|
+
FunctionParamKind,
|
|
20
20
|
FunctionParameterNode,
|
|
21
21
|
FunctionParametersNode,
|
|
22
22
|
FunctionParamNode,
|
package/src/nodes/input.ts
CHANGED
|
@@ -114,26 +114,27 @@ export const inputDef = defineNode<InputNode, Partial<Omit<InputNode, 'kind'>>>(
|
|
|
114
114
|
})
|
|
115
115
|
|
|
116
116
|
/**
|
|
117
|
-
* Creates an `InputNode
|
|
117
|
+
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
|
|
118
|
+
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
|
|
119
|
+
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
|
|
118
120
|
*
|
|
119
|
-
* @example
|
|
121
|
+
* @example Eager
|
|
120
122
|
* ```ts
|
|
121
123
|
* const input = createInput()
|
|
122
124
|
* // { kind: 'Input', schemas: [], operations: [] }
|
|
123
125
|
* ```
|
|
124
|
-
*/
|
|
125
|
-
export function createInput(overrides: Partial<Omit<InputNode, 'kind'>> = {}): InputNode {
|
|
126
|
-
return inputDef.create(overrides)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Creates a streaming `InputNode<true>` from pre-built `AsyncIterable` sources.
|
|
131
126
|
*
|
|
132
|
-
* @example
|
|
127
|
+
* @example Streaming
|
|
133
128
|
* ```ts
|
|
134
|
-
* const node =
|
|
129
|
+
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
|
|
135
130
|
* ```
|
|
136
131
|
*/
|
|
137
|
-
export function
|
|
138
|
-
|
|
132
|
+
export function createInput<Stream extends boolean = false>(options: Partial<Omit<InputNode<Stream>, 'kind'>> & { stream?: Stream } = {}): InputNode<Stream> {
|
|
133
|
+
const { stream, ...overrides } = options
|
|
134
|
+
// Streaming inputs carry AsyncIterable sources, so skip the array/meta defaults that
|
|
135
|
+
// inputDef.create applies for the eager variant.
|
|
136
|
+
if (stream) {
|
|
137
|
+
return { kind: 'Input', ...overrides } as InputNode<Stream>
|
|
138
|
+
}
|
|
139
|
+
return inputDef.create(overrides as Partial<Omit<InputNode, 'kind'>>) as InputNode<Stream>
|
|
139
140
|
}
|
package/src/printer.ts
CHANGED
|
@@ -192,11 +192,11 @@ export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOp
|
|
|
192
192
|
|
|
193
193
|
/**
|
|
194
194
|
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
|
|
195
|
-
|
|
195
|
+
*
|
|
196
196
|
* @example
|
|
197
197
|
* ```ts
|
|
198
|
-
* export const defineFunctionPrinter = createPrinterFactory<
|
|
199
|
-
* (node) =>
|
|
198
|
+
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
|
|
199
|
+
* (node) => node.kind,
|
|
200
200
|
* )
|
|
201
201
|
* ```
|
|
202
202
|
*/
|
package/src/types.ts
CHANGED
package/src/utils/ast.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { createFunctionParameter, createFunctionParameters, createIndexedAccessT
|
|
|
5
5
|
import { createProperty } from '../nodes/property.ts'
|
|
6
6
|
import { createSchema } from '../nodes/schema.ts'
|
|
7
7
|
import type {
|
|
8
|
-
CodeNode,
|
|
9
8
|
ExportNode,
|
|
10
9
|
FunctionParameterNode,
|
|
11
10
|
FunctionParametersNode,
|
|
@@ -18,7 +17,7 @@ import type {
|
|
|
18
17
|
TypeLiteralNode,
|
|
19
18
|
} from '../nodes/index.ts'
|
|
20
19
|
import type { SchemaType } from '../nodes/schema.ts'
|
|
21
|
-
import { extractRefName } from './index.ts'
|
|
20
|
+
import { extractRefName, extractStringsFromNodes, resolveGroupType } from './index.ts'
|
|
22
21
|
import { collect, collectLazy } from '../visitor.ts'
|
|
23
22
|
|
|
24
23
|
const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
|
|
@@ -120,7 +119,7 @@ export function createDiscriminantNode({ propertyName, value }: { propertyName:
|
|
|
120
119
|
/**
|
|
121
120
|
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
|
|
122
121
|
*/
|
|
123
|
-
type ParamGroupType = {
|
|
122
|
+
export type ParamGroupType = {
|
|
124
123
|
/**
|
|
125
124
|
* Type expression for the group, a plain group-name reference.
|
|
126
125
|
*/
|
|
@@ -383,7 +382,7 @@ export function createOperationParams(node: OperationNode, options: CreateOperat
|
|
|
383
382
|
/**
|
|
384
383
|
* Shared arguments for building a query or header parameter group.
|
|
385
384
|
*/
|
|
386
|
-
type BuildGroupArgs = {
|
|
385
|
+
export type BuildGroupArgs = {
|
|
387
386
|
name: string
|
|
388
387
|
node: OperationNode
|
|
389
388
|
params: Array<ParameterNode>
|
|
@@ -421,35 +420,6 @@ export function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNo
|
|
|
421
420
|
return buildGroupProperty(args).map((p) => createFunctionParameter(p))
|
|
422
421
|
}
|
|
423
422
|
|
|
424
|
-
/**
|
|
425
|
-
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
|
|
426
|
-
*
|
|
427
|
-
* Returns `null` when there is no resolver, no params, or the group name equals the
|
|
428
|
-
* individual param name (so there is no real group to emit).
|
|
429
|
-
*/
|
|
430
|
-
export function resolveGroupType({
|
|
431
|
-
node,
|
|
432
|
-
params,
|
|
433
|
-
group,
|
|
434
|
-
resolver,
|
|
435
|
-
}: {
|
|
436
|
-
node: OperationNode
|
|
437
|
-
params: Array<ParameterNode>
|
|
438
|
-
group: 'query' | 'header'
|
|
439
|
-
resolver: OperationParamsResolver | undefined
|
|
440
|
-
}): ParamGroupType | null {
|
|
441
|
-
if (!resolver || !params.length) {
|
|
442
|
-
return null
|
|
443
|
-
}
|
|
444
|
-
const firstParam = params[0]!
|
|
445
|
-
const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName
|
|
446
|
-
const groupName = groupMethod.call(resolver, node, firstParam)
|
|
447
|
-
if (groupName === resolver.resolveParamName(node, firstParam)) {
|
|
448
|
-
return null
|
|
449
|
-
}
|
|
450
|
-
return { type: groupName, optional: params.every((p) => !p.required) }
|
|
451
|
-
}
|
|
452
|
-
|
|
453
423
|
/**
|
|
454
424
|
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
|
|
455
425
|
*
|
|
@@ -651,39 +621,6 @@ export function combineImports(imports: Array<ImportNode>, exports: Array<Export
|
|
|
651
621
|
return result
|
|
652
622
|
}
|
|
653
623
|
|
|
654
|
-
/**
|
|
655
|
-
* Extracts all string content from a `CodeNode` tree recursively.
|
|
656
|
-
*
|
|
657
|
-
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
658
|
-
* and nested node content. Used internally to build the full source string for import filtering.
|
|
659
|
-
*/
|
|
660
|
-
export function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {
|
|
661
|
-
if (!nodes?.length) return ''
|
|
662
|
-
return nodes
|
|
663
|
-
.map((node) => {
|
|
664
|
-
// Backward-compat: compiled plugins may still pass bare strings at runtime
|
|
665
|
-
if (typeof node === 'string') return node as string
|
|
666
|
-
if (node.kind === 'Text') return node.value
|
|
667
|
-
if (node.kind === 'Break') return ''
|
|
668
|
-
if (node.kind === 'Jsx') return node.value
|
|
669
|
-
|
|
670
|
-
const parts: Array<string> = []
|
|
671
|
-
|
|
672
|
-
if ('params' in node && node.params) parts.push(node.params)
|
|
673
|
-
if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)
|
|
674
|
-
if ('returnType' in node && node.returnType) parts.push(node.returnType)
|
|
675
|
-
if ('type' in node && typeof node.type === 'string') parts.push(node.type)
|
|
676
|
-
|
|
677
|
-
const nested = extractStringsFromNodes(node.nodes)
|
|
678
|
-
|
|
679
|
-
if (nested) parts.push(nested)
|
|
680
|
-
|
|
681
|
-
return parts.join('\n')
|
|
682
|
-
})
|
|
683
|
-
.filter(Boolean)
|
|
684
|
-
.join('\n')
|
|
685
|
-
}
|
|
686
|
-
|
|
687
624
|
/**
|
|
688
625
|
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
|
|
689
626
|
*
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CodeNode } from '../nodes/code.ts'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
5
|
+
*
|
|
6
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
7
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
8
|
+
*/
|
|
9
|
+
export function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {
|
|
10
|
+
if (!nodes?.length) return ''
|
|
11
|
+
return nodes
|
|
12
|
+
.map((node) => {
|
|
13
|
+
// Backward-compat: compiled plugins may still pass bare strings at runtime
|
|
14
|
+
if (typeof node === 'string') return node as string
|
|
15
|
+
if (node.kind === 'Text') return node.value
|
|
16
|
+
if (node.kind === 'Break') return ''
|
|
17
|
+
if (node.kind === 'Jsx') return node.value
|
|
18
|
+
|
|
19
|
+
const parts: Array<string> = []
|
|
20
|
+
|
|
21
|
+
if ('params' in node && node.params) parts.push(node.params)
|
|
22
|
+
if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)
|
|
23
|
+
if ('returnType' in node && node.returnType) parts.push(node.returnType)
|
|
24
|
+
if ('type' in node && typeof node.type === 'string') parts.push(node.type)
|
|
25
|
+
|
|
26
|
+
const nested = extractStringsFromNodes(node.nodes)
|
|
27
|
+
|
|
28
|
+
if (nested) parts.push(nested)
|
|
29
|
+
|
|
30
|
+
return parts.join('\n')
|
|
31
|
+
})
|
|
32
|
+
.filter(Boolean)
|
|
33
|
+
.join('\n')
|
|
34
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import { isIdentifier, pascalCase, singleQuote } from '@internals/utils'
|
|
2
2
|
import { INDENT } from '../constants.ts'
|
|
3
|
+
import type { OperationNode, ParameterNode } from '../nodes/index.ts'
|
|
4
|
+
import type { OperationParamsResolver, ParamGroupType } from './ast.ts'
|
|
3
5
|
|
|
4
6
|
export { isValidVarName } from '@internals/utils'
|
|
7
|
+
export { extractStringsFromNodes } from './extractStringsFromNodes.ts'
|
|
8
|
+
export {
|
|
9
|
+
buildGroupParam,
|
|
10
|
+
buildTypeLiteral,
|
|
11
|
+
caseParams,
|
|
12
|
+
collectUsedSchemaNames,
|
|
13
|
+
containsCircularRef,
|
|
14
|
+
findCircularSchemas,
|
|
15
|
+
isStringType,
|
|
16
|
+
resolveParamType,
|
|
17
|
+
syncSchemaRef,
|
|
18
|
+
} from './ast.ts'
|
|
19
|
+
export type { BuildGroupArgs, ParamGroupType } from './ast.ts'
|
|
5
20
|
|
|
6
21
|
/**
|
|
7
22
|
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
@@ -295,3 +310,32 @@ export function findDiscriminator(mapping: Record<string, string> | undefined, r
|
|
|
295
310
|
if (!mapping || !ref) return null
|
|
296
311
|
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null
|
|
297
312
|
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
|
|
316
|
+
*
|
|
317
|
+
* Returns `null` when there is no resolver, no params, or the group name equals the
|
|
318
|
+
* individual param name (so there is no real group to emit).
|
|
319
|
+
*/
|
|
320
|
+
export function resolveGroupType({
|
|
321
|
+
node,
|
|
322
|
+
params,
|
|
323
|
+
group,
|
|
324
|
+
resolver,
|
|
325
|
+
}: {
|
|
326
|
+
node: OperationNode
|
|
327
|
+
params: Array<ParameterNode>
|
|
328
|
+
group: 'query' | 'header'
|
|
329
|
+
resolver: OperationParamsResolver | undefined
|
|
330
|
+
}): ParamGroupType | null {
|
|
331
|
+
if (!resolver || !params.length) {
|
|
332
|
+
return null
|
|
333
|
+
}
|
|
334
|
+
const firstParam = params[0]!
|
|
335
|
+
const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName
|
|
336
|
+
const groupName = groupMethod.call(resolver, node, firstParam)
|
|
337
|
+
if (groupName === resolver.resolveParamName(node, firstParam)) {
|
|
338
|
+
return null
|
|
339
|
+
}
|
|
340
|
+
return { type: groupName, optional: params.every((p) => !p.required) }
|
|
341
|
+
}
|