@kubb/ast 5.0.0-alpha.2 → 5.0.0-alpha.20
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.cjs +1031 -128
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +273 -9
- package/dist/index.js +1008 -129
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/visitor-DCQyoFvH.d.ts +1976 -0
- package/package.json +3 -2
- package/src/constants.ts +97 -4
- package/src/factory.ts +276 -18
- package/src/guards.ts +63 -8
- package/src/index.ts +32 -6
- package/src/infer.ts +130 -0
- package/src/mocks.ts +12 -5
- package/src/nodes/base.ts +31 -4
- package/src/nodes/function.ts +131 -0
- package/src/nodes/http.ts +17 -5
- package/src/nodes/index.ts +18 -5
- package/src/nodes/operation.ts +61 -4
- package/src/nodes/parameter.ts +27 -1
- package/src/nodes/property.ts +23 -1
- package/src/nodes/response.ts +29 -3
- package/src/nodes/root.ts +41 -10
- package/src/nodes/schema.ts +328 -38
- package/src/printers/functionPrinter.ts +196 -0
- package/src/printers/index.ts +3 -0
- package/src/printers/printer.ts +204 -0
- package/src/refs.ts +36 -4
- package/src/resolvers.ts +45 -0
- package/src/transformers.ts +196 -0
- package/src/types.ts +9 -2
- package/src/utils.ts +77 -0
- package/src/visitor.ts +376 -81
- package/dist/visitor-CmsfJzro.d.ts +0 -649
- package/src/printer.ts +0 -129
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { SCALAR_PRIMITIVE_TYPES } from './constants.ts'
|
|
2
|
+
import { createProperty, createSchema } from './factory.ts'
|
|
3
|
+
import { narrowSchema } from './guards.ts'
|
|
4
|
+
import type { SchemaNode } from './nodes/schema.ts'
|
|
5
|
+
import { enumPropName } from './resolvers.ts'
|
|
6
|
+
import { transform } from './visitor.ts'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Replaces a discriminator property's schema with a string enum of allowed values.
|
|
10
|
+
*
|
|
11
|
+
* If `node` is not an object schema, or if the property does not exist, the input
|
|
12
|
+
* node is returned as-is.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const schema = createSchema({
|
|
17
|
+
* type: 'object',
|
|
18
|
+
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
|
|
19
|
+
* })
|
|
20
|
+
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function setDiscriminatorEnum({
|
|
24
|
+
node,
|
|
25
|
+
propertyName,
|
|
26
|
+
values,
|
|
27
|
+
enumName,
|
|
28
|
+
}: {
|
|
29
|
+
node: SchemaNode
|
|
30
|
+
propertyName: string
|
|
31
|
+
values: Array<string>
|
|
32
|
+
enumName?: string
|
|
33
|
+
}): SchemaNode {
|
|
34
|
+
const objectNode = narrowSchema(node, 'object')
|
|
35
|
+
if (!objectNode?.properties?.length) {
|
|
36
|
+
return node
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const hasProperty = objectNode.properties.some((prop) => prop.name === propertyName)
|
|
40
|
+
if (!hasProperty) {
|
|
41
|
+
return node
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return createSchema({
|
|
45
|
+
...objectNode,
|
|
46
|
+
properties: objectNode.properties.map((prop) => {
|
|
47
|
+
if (prop.name !== propertyName) {
|
|
48
|
+
return prop
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return createProperty({
|
|
52
|
+
...prop,
|
|
53
|
+
schema: createSchema({
|
|
54
|
+
type: 'enum',
|
|
55
|
+
primitive: 'string',
|
|
56
|
+
enumValues: values,
|
|
57
|
+
name: enumName,
|
|
58
|
+
readOnly: prop.schema.readOnly,
|
|
59
|
+
writeOnly: prop.schema.writeOnly,
|
|
60
|
+
}),
|
|
61
|
+
})
|
|
62
|
+
}),
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Merges adjacent anonymous object members into a single anonymous object member.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const merged = mergeAdjacentObjects([
|
|
72
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
|
|
73
|
+
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
|
|
74
|
+
* ])
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
|
|
78
|
+
return members.reduce<Array<SchemaNode>>((acc, member) => {
|
|
79
|
+
const objectMember = narrowSchema(member, 'object')
|
|
80
|
+
if (objectMember && !objectMember.name) {
|
|
81
|
+
const previous = acc.at(-1)
|
|
82
|
+
const previousObject = previous ? narrowSchema(previous, 'object') : undefined
|
|
83
|
+
|
|
84
|
+
if (previousObject && !previousObject.name) {
|
|
85
|
+
acc[acc.length - 1] = createSchema({
|
|
86
|
+
...previousObject,
|
|
87
|
+
properties: [...(previousObject.properties ?? []), ...(objectMember.properties ?? [])],
|
|
88
|
+
})
|
|
89
|
+
return acc
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
acc.push(member)
|
|
94
|
+
return acc
|
|
95
|
+
}, [])
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const simplified = simplifyUnion([
|
|
104
|
+
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
|
|
105
|
+
* createSchema({ type: 'string' }),
|
|
106
|
+
* ])
|
|
107
|
+
* // keeps only string member
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode> {
|
|
111
|
+
const scalarPrimitives = new Set(
|
|
112
|
+
members.filter((member) => SCALAR_PRIMITIVE_TYPES.has(member.type as typeof SCALAR_PRIMITIVE_TYPES extends Set<infer T> ? T : never)).map((m) => m.type),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
if (!scalarPrimitives.size) {
|
|
116
|
+
return members
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return members.filter((member) => {
|
|
120
|
+
const enumNode = narrowSchema(member, 'enum')
|
|
121
|
+
if (!enumNode) {
|
|
122
|
+
return true
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const primitive = enumNode.primitive
|
|
126
|
+
if (!primitive) {
|
|
127
|
+
return true
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0
|
|
131
|
+
if (enumValueCount <= 1) {
|
|
132
|
+
return true
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (scalarPrimitives.has(primitive)) {
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) {
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return true
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode {
|
|
148
|
+
const enumNode = narrowSchema(propNode, 'enum')
|
|
149
|
+
|
|
150
|
+
if (enumNode?.primitive === 'boolean') {
|
|
151
|
+
return { ...propNode, name: undefined }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (enumNode) {
|
|
155
|
+
return { ...propNode, name: enumPropName(parentName, propName, enumSuffix) }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return propNode
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Walks a schema tree and resolves `ref`/`enum` names through callbacks.
|
|
163
|
+
*/
|
|
164
|
+
export function resolveNames({
|
|
165
|
+
node,
|
|
166
|
+
nameMapping,
|
|
167
|
+
resolveName,
|
|
168
|
+
resolveEnumName,
|
|
169
|
+
}: {
|
|
170
|
+
node: SchemaNode
|
|
171
|
+
nameMapping: Map<string, string>
|
|
172
|
+
resolveName: (ref: string) => string | undefined
|
|
173
|
+
resolveEnumName?: (name: string) => string | undefined
|
|
174
|
+
}): SchemaNode {
|
|
175
|
+
return transform(node, {
|
|
176
|
+
schema(schemaNode) {
|
|
177
|
+
const schemaRef = narrowSchema(schemaNode, 'ref')
|
|
178
|
+
|
|
179
|
+
if (schemaRef && (schemaRef.ref || schemaRef.name)) {
|
|
180
|
+
const rawRef = schemaRef.ref ?? schemaRef.name!
|
|
181
|
+
const resolved = resolveName(nameMapping.get(rawRef) ?? rawRef)
|
|
182
|
+
if (resolved) {
|
|
183
|
+
return { ...schemaNode, name: resolved }
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const schemaEnum = narrowSchema(schemaNode, 'enum')
|
|
188
|
+
if (schemaEnum?.name) {
|
|
189
|
+
const resolved = (resolveEnumName ?? resolveName)(schemaEnum.name)
|
|
190
|
+
if (resolved) {
|
|
191
|
+
return { ...schemaNode, name: resolved }
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
}) as SchemaNode
|
|
196
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type { VisitorDepth } from './constants.ts'
|
|
2
2
|
export type { DistributiveOmit } from './factory.ts'
|
|
3
|
+
export type { InferSchema, InferSchemaNode, ParserOptions } from './infer.ts'
|
|
3
4
|
export type {
|
|
4
5
|
ArraySchemaNode,
|
|
5
6
|
BaseNode,
|
|
@@ -8,6 +9,10 @@ export type {
|
|
|
8
9
|
DatetimeSchemaNode,
|
|
9
10
|
EnumSchemaNode,
|
|
10
11
|
EnumValueNode,
|
|
12
|
+
FunctionNode,
|
|
13
|
+
FunctionNodeType,
|
|
14
|
+
FunctionParameterNode,
|
|
15
|
+
FunctionParametersNode,
|
|
11
16
|
HttpMethod,
|
|
12
17
|
HttpStatusCode,
|
|
13
18
|
IntersectionSchemaNode,
|
|
@@ -15,6 +20,7 @@ export type {
|
|
|
15
20
|
Node,
|
|
16
21
|
NodeKind,
|
|
17
22
|
NumberSchemaNode,
|
|
23
|
+
ObjectBindingParameterNode,
|
|
18
24
|
ObjectSchemaNode,
|
|
19
25
|
OperationNode,
|
|
20
26
|
ParameterLocation,
|
|
@@ -35,7 +41,8 @@ export type {
|
|
|
35
41
|
StringSchemaNode,
|
|
36
42
|
TimeSchemaNode,
|
|
37
43
|
UnionSchemaNode,
|
|
44
|
+
UrlSchemaNode,
|
|
38
45
|
} from './nodes/index.ts'
|
|
39
|
-
export type { Printer, PrinterFactoryOptions
|
|
46
|
+
export type { Printer, PrinterFactoryOptions } from './printers/printer.ts'
|
|
40
47
|
export type { RefMap } from './refs.ts'
|
|
41
|
-
export type { AsyncVisitor, CollectVisitor, Visitor } from './visitor.ts'
|
|
48
|
+
export type { AsyncVisitor, CollectOptions, CollectVisitor, ParentOf, TransformOptions, Visitor, VisitorContext, WalkOptions } from './visitor.ts'
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { camelCase, isValidVarName } from '@internals/utils'
|
|
2
|
+
|
|
3
|
+
import { narrowSchema } from './guards.ts'
|
|
4
|
+
import type { ParameterNode, SchemaNode } from './nodes/index.ts'
|
|
5
|
+
import type { SchemaType } from './nodes/schema.ts'
|
|
6
|
+
|
|
7
|
+
const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Returns `true` when a schema is emitted as a plain TypeScript `string`.
|
|
11
|
+
*
|
|
12
|
+
* - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
|
|
13
|
+
* - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* isStringType(createSchema({ type: 'uuid' })) // true
|
|
18
|
+
* isStringType(createSchema({ type: 'date', representation: 'date' })) // false
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function isStringType(node: SchemaNode): boolean {
|
|
22
|
+
if (plainStringTypes.has(node.type)) {
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')
|
|
27
|
+
if (temporal) {
|
|
28
|
+
return temporal.representation !== 'date'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return false
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Applies casing rules to parameter names and returns a new parameter array.
|
|
36
|
+
*
|
|
37
|
+
* The input array is not mutated.
|
|
38
|
+
* If `casing` is not set, the original array is returned unchanged.
|
|
39
|
+
*
|
|
40
|
+
* Use this before passing parameters to schema builders so that property keys
|
|
41
|
+
* in generated output match the desired casing while preserving
|
|
42
|
+
* `OperationNode.parameters` for other consumers.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
|
|
47
|
+
* const cased = caseParams(params, 'camelcase')
|
|
48
|
+
* // cased[0].name === 'petId'
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
|
|
52
|
+
if (!casing) {
|
|
53
|
+
return params
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return params.map((param) => {
|
|
57
|
+
const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name
|
|
58
|
+
|
|
59
|
+
return { ...param, name: transformed }
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Syncs property/parameter schema optionality flags from `required` and `schema.nullable`.
|
|
65
|
+
*
|
|
66
|
+
* - `optional` is set for non-required, non-nullable schemas.
|
|
67
|
+
* - `nullish` is set for non-required, nullable schemas.
|
|
68
|
+
*/
|
|
69
|
+
export function syncOptionality(required: boolean, schema: SchemaNode): SchemaNode {
|
|
70
|
+
const nullable = schema.nullable ?? false
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
...schema,
|
|
74
|
+
optional: !required && !nullable ? true : undefined,
|
|
75
|
+
nullish: !required && nullable ? true : undefined,
|
|
76
|
+
}
|
|
77
|
+
}
|