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

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.
@@ -1,9 +1,17 @@
1
- import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
1
+ import type { SchemaNode, SchemaNodeByType, SchemaType } from '../nodes/index.ts'
2
2
 
3
3
  /**
4
- * Handler context for `definePrinter` mirrors `PluginContext` from `@kubb/core`.
5
- * Available as `this` inside each node handler and the optional root-level `print`.
6
- * `this.print` always dispatches to the `nodes` handlers (node-level printer).
4
+ * Runtime context passed as `this` to printer handlers.
5
+ *
6
+ * `this.print` always dispatches to node-level handlers from `nodes`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const context: PrinterHandlerContext<string, {}> = {
11
+ * options: {},
12
+ * print: () => 'value',
13
+ * }
14
+ * ```
7
15
  */
8
16
  export type PrinterHandlerContext<TOutput, TOptions extends object> = {
9
17
  /**
@@ -17,8 +25,16 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
17
25
  }
18
26
 
19
27
  /**
20
- * Handler for a specific `SchemaNode` variant identified by `SchemaType` key `T`.
21
- * Use a regular function (not an arrow function) so that `this` is available.
28
+ * Handler for one schema node type.
29
+ *
30
+ * Use a regular function (not an arrow function) if you need `this`.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const handler: PrinterHandler<string, {}, 'string'> = function () {
35
+ * return 'string'
36
+ * }
37
+ * ```
22
38
  */
23
39
  export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
24
40
  this: PrinterHandlerContext<TOutput, TOptions>,
@@ -26,13 +42,17 @@ export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaTyp
26
42
  ) => TOutput | null | undefined
27
43
 
28
44
  /**
29
- * Shape of the type parameter passed to `definePrinter`.
30
- * Mirrors `AdapterFactoryOptions` / `PluginFactoryOptions` from `@kubb/core`.
45
+ * Generic shape used by `definePrinter`.
31
46
  *
32
47
  * - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
33
- * - `TOptions` — options passed to and stored on the printer
48
+ * - `TOptions` — options passed to and stored on the printer instance
34
49
  * - `TOutput` — the type emitted by node handlers
35
- * - `TPrintOutput` — the type emitted by the public `print` override (defaults to `TOutput`)
50
+ * - `TPrintOutput` — type returned by public `print` (defaults to `TOutput`)
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
55
+ * ```
36
56
  */
37
57
  export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
38
58
  name: TName
@@ -42,7 +62,12 @@ export type PrinterFactoryOptions<TName extends string = string, TOptions extend
42
62
  }
43
63
 
44
64
  /**
45
- * The object returned by calling a `definePrinter` instance.
65
+ * Printer instance returned by a printer factory.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
70
+ * ```
46
71
  */
47
72
  export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
48
73
  /**
@@ -55,16 +80,25 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
55
80
  options: T['options']
56
81
  /**
57
82
  * Public printer. If the builder provides a root-level `print`, this calls that
58
- * higher-level function (which may produce full declarations). Otherwise falls back
59
- * to the node-level dispatcher
83
+ * higher-level function (which may produce full declarations).
84
+ * Otherwise, falls back to the node-level dispatcher.
60
85
  */
61
86
  print: (node: SchemaNode) => T['printOutput'] | null | undefined
62
87
  }
63
88
 
64
89
  /**
65
- * Builder function passed to `definePrinter`. Receives the resolved options and returns the
66
- * printer configuration: a unique `name`, the stored `options`, node-level `nodes` handlers,
67
- * and an optional root-level `print` override.
90
+ * Builder function passed to `definePrinter`.
91
+ *
92
+ * It receives resolved options and returns:
93
+ * - `name`
94
+ * - `options`
95
+ * - `nodes` handlers
96
+ * - optional top-level `print` override
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
101
+ * ```
68
102
  */
69
103
  type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
70
104
  name: T['name']
@@ -84,16 +118,17 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
84
118
  }
85
119
 
86
120
  /**
87
- * Creates a named printer factory. Mirrors the `createPlugin` / `createAdapter` pattern
88
- * from `@kubb/core` — wraps a builder to make options optional and separates raw options
89
- * from resolved options.
121
+ * Creates a schema printer factory.
122
+ *
123
+ * This function wraps a builder and makes options optional at call sites.
90
124
  *
91
125
  * The builder receives resolved options and returns:
92
126
  * - `name` — a unique identifier for the printer
93
127
  * - `options` — options stored on the returned printer instance
94
128
  * - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
95
- * - `print` _(optional)_ — a root-level override that becomes the public `printer.print`.
96
- * Inside it, `this.print(node)` still dispatches to the `nodes` map — safe recursion, no infinite loop.
129
+ * - `print` _(optional)_ — top-level override exposed as `printer.print`
130
+ * - Inside this function, `this.print(node)` still dispatches to the `nodes` map
131
+ * - This keeps recursion safe and avoids self-calls
97
132
  *
98
133
  * When no `print` override is provided, `printer.print` is the node-level dispatcher directly.
99
134
  *
@@ -113,33 +148,14 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
113
148
  * },
114
149
  * }))
115
150
  * ```
116
- *
117
- * @example With a root-level `print` override to wrap output in a full declaration
118
- * ```ts
119
- * type TsPrinter = PrinterFactoryOptions<'ts', { typeName?: string }, ts.TypeNode, ts.Node>
120
- *
121
- * export const printerTs = definePrinter<TsPrinter>((options) => ({
122
- * name: 'ts',
123
- * options,
124
- * nodes: { string: () => factory.keywordTypeNodes.string },
125
- * print(node) {
126
- * const type = this.print(node) // calls the node-level dispatcher
127
- * if (!type || !this.options.typeName) return type
128
- * return factory.createTypeAliasDeclaration(this.options.typeName, type)
129
- * },
130
- * }))
131
- * ```
132
151
  */
133
152
  export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
134
153
  return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
135
154
  }
136
155
 
137
156
  /**
138
- * Generic printer factory. Extracts the core dispatch + context logic so it can be reused
139
- * for any node type — not just `SchemaNode`. `definePrinter` is built on top of this.
140
- *
141
- * @param getKey — derives the handler-map key from a node. Return `undefined` to skip.
142
- *
157
+ * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
158
+ **
143
159
  * @example
144
160
  * ```ts
145
161
  * export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
package/src/refs.ts CHANGED
@@ -2,20 +2,34 @@ import type { RootNode } from './nodes/root.ts'
2
2
  import type { SchemaNode } from './nodes/schema.ts'
3
3
 
4
4
  /**
5
- * Schema name to `SchemaNode` mapping.
5
+ * Lookup map from schema name to `SchemaNode`.
6
6
  */
7
7
  export type RefMap = Map<string, SchemaNode>
8
8
 
9
9
  /**
10
- * Extracts the final segment from a reference string.
11
- * Falls back to the original string when no slash exists.
10
+ * Returns the last path segment of a reference string.
11
+ *
12
+ * Example: `#/components/schemas/Pet` becomes `Pet`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * extractRefName('#/components/schemas/Pet') // 'Pet'
17
+ * ```
12
18
  */
13
19
  export function extractRefName(ref: string): string {
14
20
  return ref.split('/').at(-1) ?? ref
15
21
  }
16
22
 
17
23
  /**
18
- * Indexes named schemas from `root.schemas` by name. Unnamed schemas are skipped.
24
+ * Builds a `RefMap` from `root.schemas` using each schema's `name`.
25
+ *
26
+ * Unnamed schemas are skipped.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const refMap = buildRefMap(root)
31
+ * const pet = refMap.get('Pet')
32
+ * ```
19
33
  */
20
34
  export function buildRefMap(root: RootNode): RefMap {
21
35
  const map: RefMap = new Map()
@@ -29,14 +43,24 @@ export function buildRefMap(root: RootNode): RefMap {
29
43
  }
30
44
 
31
45
  /**
32
- * Looks up a schema by name. Prefer over `RefMap.get()` to keep the resolution strategy swappable.
46
+ * Resolves a schema by name from a `RefMap`.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const petSchema = resolveRef(refMap, 'Pet')
51
+ * ```
33
52
  */
34
53
  export function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined {
35
54
  return refMap.get(ref)
36
55
  }
37
56
 
38
57
  /**
39
- * Converts a `RefMap` to a plain object.
58
+ * Converts a `RefMap` into a plain object.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const refsObject = refMapToObject(refMap)
63
+ * ```
40
64
  */
41
65
  export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
42
66
  return Object.fromEntries(refMap)
@@ -0,0 +1,45 @@
1
+ import { pascalCase } from '@internals/utils'
2
+ import { narrowSchema } from './guards.ts'
3
+ import type { SchemaNode } from './nodes/schema.ts'
4
+ import { extractRefName } from './refs.ts'
5
+ import { collect } from './visitor.ts'
6
+
7
+ export function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | undefined {
8
+ if (!mapping || !ref) return undefined
9
+ return Object.entries(mapping).find(([, value]) => value === ref)?.[0]
10
+ }
11
+
12
+ export function childName(parentName: string | undefined, propName: string): string | undefined {
13
+ return parentName ? pascalCase([parentName, propName].join(' ')) : undefined
14
+ }
15
+
16
+ export function enumPropName(parentName: string | undefined, propName: string, enumSuffix: string): string {
17
+ return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
18
+ }
19
+
20
+ /**
21
+ * Collects import entries for all `ref` schema nodes in `node`.
22
+ */
23
+ export function collectImports<TImport>({
24
+ node,
25
+ nameMapping,
26
+ resolve,
27
+ }: {
28
+ node: SchemaNode
29
+ nameMapping: Map<string, string>
30
+ resolve: (schemaName: string) => TImport | undefined
31
+ }): Array<TImport> {
32
+ return collect<TImport>(node, {
33
+ schema(schemaNode): TImport | undefined {
34
+ const schemaRef = narrowSchema(schemaNode, 'ref')
35
+ if (!schemaRef?.ref) return
36
+
37
+ const rawName = extractRefName(schemaRef.ref)
38
+ const schemaName = nameMapping.get(rawName) ?? rawName
39
+ const result = resolve(schemaName)
40
+ if (!result) return
41
+
42
+ return result
43
+ },
44
+ })
45
+ }
@@ -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 | 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,
@@ -42,6 +43,6 @@ export type {
42
43
  UnionSchemaNode,
43
44
  UrlSchemaNode,
44
45
  } from './nodes/index.ts'
45
- export type { Printer, PrinterFactoryOptions } from './printer.ts'
46
+ export type { Printer, PrinterFactoryOptions } from './printers/printer.ts'
46
47
  export type { RefMap } from './refs.ts'
47
48
  export type { AsyncVisitor, CollectOptions, CollectVisitor, ParentOf, TransformOptions, Visitor, VisitorContext, WalkOptions } from './visitor.ts'
package/src/utils.ts CHANGED
@@ -7,12 +7,18 @@ import type { SchemaType } from './nodes/schema.ts'
7
7
  const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
8
8
 
9
9
  /**
10
- * Returns `true` when a schema node will be represented as a plain string in generated code.
10
+ * Returns `true` when a schema is emitted as a plain TypeScript `string`.
11
11
  *
12
12
  * - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
13
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
+ * ```
14
20
  */
15
- export function isPlainStringType(node: SchemaNode): boolean {
21
+ export function isStringType(node: SchemaNode): boolean {
16
22
  if (plainStringTypes.has(node.type)) {
17
23
  return true
18
24
  }
@@ -26,16 +32,23 @@ export function isPlainStringType(node: SchemaNode): boolean {
26
32
  }
27
33
 
28
34
  /**
29
- * Transforms the `name` field of each parameter node according to the given casing strategy.
35
+ * Applies casing rules to parameter names and returns a new parameter array.
30
36
  *
31
- * The original `params` array is never mutated — a new array of cloned nodes is returned.
32
- * When no `casing` is provided the original array is returned as-is.
37
+ * The input array is not mutated.
38
+ * If `casing` is not set, the original array is returned unchanged.
33
39
  *
34
40
  * Use this before passing parameters to schema builders so that property keys
35
- * in the generated output match the desired casing while the original
36
- * `OperationNode.parameters` array remains untouched for other consumers.
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
+ * ```
37
50
  */
38
- export function applyParamsCasing(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
51
+ export function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
39
52
  if (!casing) {
40
53
  return params
41
54
  }
@@ -46,3 +59,19 @@ export function applyParamsCasing(params: Array<ParameterNode>, casing: 'camelca
46
59
  return { ...param, name: transformed }
47
60
  })
48
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
+ }