@kubb/ast 5.0.0-beta.5 → 5.0.0-beta.51

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.
Files changed (48) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +28 -19
  3. package/dist/index.cjs +878 -788
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +33 -3326
  6. package/dist/index.js +863 -750
  7. package/dist/index.js.map +1 -1
  8. package/dist/types-BaaNZbSi.d.ts +3581 -0
  9. package/dist/types.cjs +0 -0
  10. package/dist/types.d.ts +2 -0
  11. package/dist/types.js +1 -0
  12. package/dist/utils-BIcKgbbc.js +626 -0
  13. package/dist/utils-BIcKgbbc.js.map +1 -0
  14. package/dist/utils-CMRZrT-w.cjs +794 -0
  15. package/dist/utils-CMRZrT-w.cjs.map +1 -0
  16. package/dist/utils.cjs +18 -0
  17. package/dist/utils.d.ts +205 -0
  18. package/dist/utils.js +2 -0
  19. package/package.json +13 -5
  20. package/src/constants.ts +10 -49
  21. package/src/dedupe.ts +200 -0
  22. package/src/dialect.ts +58 -0
  23. package/src/dispatch.ts +53 -0
  24. package/src/factory.ts +154 -21
  25. package/src/guards.ts +18 -48
  26. package/src/index.ts +11 -8
  27. package/src/infer.ts +16 -14
  28. package/src/nodes/base.ts +2 -0
  29. package/src/nodes/code.ts +22 -28
  30. package/src/nodes/content.ts +37 -0
  31. package/src/nodes/file.ts +17 -15
  32. package/src/nodes/function.ts +11 -11
  33. package/src/nodes/http.ts +1 -35
  34. package/src/nodes/index.ts +10 -12
  35. package/src/nodes/operation.ts +98 -62
  36. package/src/nodes/response.ts +21 -14
  37. package/src/nodes/root.ts +72 -10
  38. package/src/nodes/schema.ts +18 -15
  39. package/src/printer.ts +44 -38
  40. package/src/resolvers.ts +5 -19
  41. package/src/signature.ts +232 -0
  42. package/src/transformers.ts +21 -16
  43. package/src/types.ts +8 -18
  44. package/src/{utils.ts → utils/ast.ts} +125 -84
  45. package/src/utils/index.ts +295 -0
  46. package/src/visitor.ts +239 -281
  47. package/src/refs.ts +0 -67
  48. /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
@@ -1,16 +1,20 @@
1
1
  import type { BaseNode } from './base.ts'
2
- import type { MediaType, StatusCode } from './http.ts'
3
- import type { SchemaNode } from './schema.ts'
2
+ import type { ContentNode } from './content.ts'
3
+ import type { StatusCode } from './http.ts'
4
4
 
5
5
  /**
6
6
  * AST node representing one operation response variant.
7
7
  *
8
+ * Mirrors {@link OperationNode.requestBody}: the response body schemas live exclusively inside
9
+ * the `content` array (one entry per content type), so the same schema is never duplicated at the
10
+ * node root and inside `content`.
11
+ *
8
12
  * @example
9
13
  * ```ts
10
14
  * const response: ResponseNode = {
11
15
  * kind: 'Response',
12
16
  * statusCode: '200',
13
- * schema: createSchema({ type: 'string' }),
17
+ * content: [{ contentType: 'application/json', schema: createSchema({ type: 'string' }) }],
14
18
  * }
15
19
  * ```
16
20
  */
@@ -28,16 +32,19 @@ export type ResponseNode = BaseNode & {
28
32
  */
29
33
  description?: string
30
34
  /**
31
- * Response body schema.
32
- */
33
- schema: SchemaNode
34
- /**
35
- * Response media type.
36
- */
37
- mediaType?: MediaType | null
38
- /**
39
- * Property keys to exclude from the generated type via `Omit<Type, Keys>`.
40
- * Set when a referenced schema has `writeOnly` fields that should not appear in response types.
35
+ * All available content type entries for this response.
36
+ *
37
+ * When the adapter `contentType` option is set, this array contains exactly one entry for that
38
+ * content type. Otherwise it contains one entry per content type declared in the spec, so that
39
+ * plugins can generate a union of response types (e.g. `application/json` and `application/xml`).
40
+ * Body-less responses keep a single entry whose `schema` is the empty/`void` placeholder.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * // spec response declares both application/json and application/xml
45
+ * response.content[0].contentType // 'application/json'
46
+ * response.content[1].contentType // 'application/xml'
47
+ * ```
41
48
  */
42
- keysToOmit?: Array<string>
49
+ content?: Array<ContentNode>
43
50
  }
package/src/nodes/root.ts CHANGED
@@ -3,32 +3,62 @@ import type { OperationNode } from './operation.ts'
3
3
  import type { SchemaNode } from './schema.ts'
4
4
 
5
5
  /**
6
- * Basic metadata for an API document.
7
- * Adapters fill fields that exist in their source format.
6
+ * Metadata for an API document, populated by the adapter and available to every generator.
7
+ *
8
+ * All fields are plain JSON-serializable values, no `Set`, no `Map`, no class instances.
9
+ * Computed fields (`circularNames`, `enumNames`) are pre-calculated once during the adapter
10
+ * pre-scan so generators never need to iterate the full schema list themselves.
8
11
  *
9
12
  * @example
10
13
  * ```ts
11
- * const meta: InputMeta = { title: 'Pet API', version: '1.0.0' }
14
+ * const meta: InputMeta = { title: 'Pet Store', version: '1.0.0', baseURL: 'https://petstore.swagger.io/v2', circularNames: [], enumNames: [] }
12
15
  * ```
13
16
  */
14
17
  export type InputMeta = {
15
18
  /**
16
- * API title (from `info.title` in OAS/AsyncAPI).
19
+ * API title from `info.title` in the source document.
17
20
  */
18
21
  title?: string
19
22
  /**
20
- * API description (from `info.description` in OAS/AsyncAPI).
23
+ * API description from `info.description` in the source document.
21
24
  */
22
25
  description?: string
23
26
  /**
24
- * API version string (from `info.version` in OAS/AsyncAPI).
27
+ * API version string from `info.version` in the source document.
25
28
  */
26
29
  version?: string
27
30
  /**
28
- * Resolved API base URL.
29
- * For OpenAPI and AsyncAPI, this comes from the selected server URL.
31
+ * Resolved base URL from the first matching server entry in the source document.
32
+ */
33
+ baseURL?: string | null
34
+ /**
35
+ * Names of schemas that participate in a circular reference chain.
36
+ * Computed once during the adapter pre-scan, use this instead of calling
37
+ * `findCircularSchemas` per generator call.
38
+ *
39
+ * Convert to a `Set` once at the start of a generator, not per-schema,
40
+ * to keep lookup O(1) without repeated allocations.
41
+ *
42
+ * @example Wrap a circular schema in z.lazy()
43
+ * ```ts
44
+ * const circular = new Set(meta.circularNames)
45
+ * if (circular.has(schema.name)) { ... }
46
+ * ```
30
47
  */
31
- baseURL?: string
48
+ circularNames: ReadonlyArray<string>
49
+ /**
50
+ * Names of schemas whose type is `enum`.
51
+ * Computed once during the adapter pre-scan, use this instead of filtering
52
+ * schemas per generator call.
53
+ *
54
+ * Convert to a `Set` once at the start of a generator when you need repeated
55
+ * membership checks, rather than calling `.includes()` per schema.
56
+ *
57
+ * @example Check if a referenced schema is an enum
58
+ * `const enums = new Set(meta.enumNames)`
59
+ * `const isEnum = enums.has(schemaName)`
60
+ */
61
+ enumNames: ReadonlyArray<string>
32
62
  }
33
63
 
34
64
  /**
@@ -58,7 +88,39 @@ export type InputNode = BaseNode & {
58
88
  */
59
89
  operations: Array<OperationNode>
60
90
  /**
61
- * Optional document metadata populated by the adapter.
91
+ * Document metadata populated by the adapter.
92
+ */
93
+ meta: InputMeta
94
+ }
95
+
96
+ /**
97
+ * Streaming variant of `InputNode` for memory-efficient processing of large API specs.
98
+ *
99
+ * `schemas` and `operations` are `AsyncIterable` rather than arrays, each `for await`
100
+ * loop creates a fresh parse pass from the cached in-memory document, so multiple
101
+ * consumers (plugins) can iterate independently without keeping all nodes in memory.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * for await (const schema of inputStreamNode.schemas) {
106
+ * // only this one SchemaNode is live here. Previous ones are GC-eligible
107
+ * }
108
+ * ```
109
+ */
110
+ export type InputStreamNode = {
111
+ kind: 'Input'
112
+ /**
113
+ * Lazily parsed schema nodes. Each `for await` creates a fresh parse pass, so
114
+ * multiple plugins can iterate independently without sharing state.
115
+ */
116
+ schemas: AsyncIterable<SchemaNode>
117
+ /**
118
+ * Lazily parsed operation nodes. Each `for await` creates a fresh parse pass, so
119
+ * multiple plugins can iterate independently without sharing state.
120
+ */
121
+ operations: AsyncIterable<OperationNode>
122
+ /**
123
+ * Document metadata available immediately, before the first yielded node.
62
124
  */
63
125
  meta?: InputMeta
64
126
  }
@@ -58,12 +58,12 @@ export type PrimitiveSchemaType =
58
58
  /**
59
59
  * Composite schema types.
60
60
  */
61
- export type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum'
61
+ type ComplexSchemaType = 'tuple' | 'union' | 'intersection' | 'enum'
62
62
 
63
63
  /**
64
64
  * Schema types that need special handling in generators.
65
65
  */
66
- export type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob'
66
+ type SpecialSchemaType = 'ref' | 'datetime' | 'time' | 'uuid' | 'email' | 'url' | 'ipv4' | 'ipv6' | 'blob'
67
67
 
68
68
  /**
69
69
  * All schema type strings.
@@ -154,6 +154,10 @@ type SchemaNodeBase = BaseNode & {
154
154
  * For example, this is `'string'` for a `uuid` schema.
155
155
  */
156
156
  primitive?: PrimitiveSchemaType
157
+ /**
158
+ * Schema `format` value.
159
+ */
160
+ format?: string
157
161
  }
158
162
 
159
163
  /**
@@ -174,7 +178,7 @@ export type ObjectSchemaNode = SchemaNodeBase & {
174
178
  */
175
179
  type: 'object'
176
180
  /**
177
- * Primitive type always `'object'` for object schemas.
181
+ * Primitive type, always `'object'` for object schemas.
178
182
  */
179
183
  primitive: 'object'
180
184
  /**
@@ -302,7 +306,7 @@ export type IntersectionSchemaNode = CompositeSchemaNodeBase & {
302
306
  /**
303
307
  * One named enum item.
304
308
  */
305
- export type EnumValueNode = {
309
+ type EnumValueNode = {
306
310
  /**
307
311
  * Enum item name.
308
312
  */
@@ -364,8 +368,9 @@ export type RefSchemaNode = SchemaNodeBase & {
364
368
  type: 'ref'
365
369
  /**
366
370
  * Referenced schema name.
371
+ * `null` means Kubb has processed this and determined there is no applicable name.
367
372
  */
368
- name?: string
373
+ name?: string | null
369
374
  /**
370
375
  * Original `$ref` path, for example, `#/components/schemas/Order`.
371
376
  * Used to resolve names later.
@@ -376,14 +381,12 @@ export type RefSchemaNode = SchemaNodeBase & {
376
381
  */
377
382
  pattern?: string
378
383
  /**
379
- * The fully-parsed schema that this ref resolves to.
380
- * Populated during OAS parsing when the referenced definition can be resolved.
381
- * `undefined` when the ref cannot be resolved or is part of a circular chain.
382
- *
383
- * Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
384
- * without following the reference manually.
384
+ * The fully-parsed schema this ref resolves to, so its structure (`primitive`, `properties`)
385
+ * can be read without following the reference. Populated during OAS parsing when the
386
+ * definition resolves, `null` when it can't or the ref is circular, and `undefined` when
387
+ * resolution has not been attempted.
385
388
  */
386
- schema?: SchemaNode
389
+ schema?: SchemaNode | null
387
390
  }
388
391
 
389
392
  /**
@@ -556,7 +559,7 @@ export type UrlSchemaNode = SchemaNodeBase & {
556
559
  * const uuidSchema: FormatStringSchemaNode = { kind: 'Schema', type: 'uuid', min: 36, max: 36 }
557
560
  * ```
558
561
  */
559
- export type FormatStringSchemaNode = SchemaNodeBase & {
562
+ type FormatStringSchemaNode = SchemaNodeBase & {
560
563
  /**
561
564
  * Schema type discriminator.
562
565
  */
@@ -579,7 +582,7 @@ export type FormatStringSchemaNode = SchemaNodeBase & {
579
582
  * const ipv4Schema: Ipv4SchemaNode = { kind: 'Schema', type: 'ipv4' }
580
583
  * ```
581
584
  */
582
- export type Ipv4SchemaNode = SchemaNodeBase & {
585
+ type Ipv4SchemaNode = SchemaNodeBase & {
583
586
  /**
584
587
  * Schema type discriminator.
585
588
  */
@@ -594,7 +597,7 @@ export type Ipv4SchemaNode = SchemaNodeBase & {
594
597
  * const ipv6Schema: Ipv6SchemaNode = { kind: 'Schema', type: 'ipv6' }
595
598
  * ```
596
599
  */
597
- export type Ipv6SchemaNode = SchemaNodeBase & {
600
+ type Ipv6SchemaNode = SchemaNodeBase & {
598
601
  /**
599
602
  * Schema type discriminator.
600
603
  */
package/src/printer.ts CHANGED
@@ -13,12 +13,12 @@ import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'
13
13
  * }
14
14
  * ```
15
15
  */
16
- export type PrinterHandlerContext<TOutput, TOptions extends object> = {
16
+ type PrinterHandlerContext<TOutput, TOptions extends object> = {
17
17
  /**
18
18
  * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
19
19
  * Use `this.transform` inside `nodes` handlers and inside the `print` override.
20
20
  */
21
- transform: (node: SchemaNode) => TOutput | null | undefined
21
+ transform: (node: SchemaNode) => TOutput | null
22
22
  /**
23
23
  * Options for this printer instance.
24
24
  */
@@ -37,16 +37,16 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
37
37
  * }
38
38
  * ```
39
39
  */
40
- export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
40
+ type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
41
41
  this: PrinterHandlerContext<TOutput, TOptions>,
42
42
  node: SchemaNodeByType[T],
43
- ) => TOutput | null | undefined
43
+ ) => TOutput | null
44
44
 
45
45
  /**
46
46
  * Partial map of per-node-type handler overrides for a printer.
47
47
  *
48
48
  * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
49
- * Supply only the handlers you want to replace; the printer's built-in
49
+ * Supply only the handlers you want to replace. The printer's built-in
50
50
  * defaults fill in the rest.
51
51
  *
52
52
  * @example
@@ -69,10 +69,10 @@ export type PrinterPartial<TOutput, TOptions extends object> = Partial<{
69
69
  /**
70
70
  * Generic shape used by `definePrinter`.
71
71
  *
72
- * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
73
- * - `TOptions` options passed to and stored on the printer instance
74
- * - `TOutput` the type emitted by node handlers
75
- * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
72
+ * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
73
+ * - `TOptions` options passed to and stored on the printer instance
74
+ * - `TOutput` the type emitted by node handlers
75
+ * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
76
76
  *
77
77
  * @example
78
78
  * ```ts
@@ -104,17 +104,17 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
104
104
  */
105
105
  options: T['options']
106
106
  /**
107
- * Node-level dispatcher converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
108
- * Always dispatches through the `nodes` map; never calls the `print` override.
107
+ * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
108
+ * Always dispatches through the `nodes` map. Never calls the `print` override.
109
109
  * Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
110
110
  */
111
- transform: (node: SchemaNode) => T['output'] | null | undefined
111
+ transform: (node: SchemaNode) => T['output'] | null
112
112
  /**
113
113
  * Public printer. If the builder provides a root-level `print`, this calls that
114
114
  * higher-level function (which may produce full declarations).
115
115
  * Otherwise, falls back to the node-level dispatcher.
116
116
  */
117
- print: (node: SchemaNode) => T['printOutput'] | null | undefined
117
+ print: (node: SchemaNode) => T['printOutput'] | null
118
118
  }
119
119
 
120
120
  /**
@@ -143,28 +143,32 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
143
143
  /**
144
144
  * Optional root-level print override. When provided, becomes the public `printer.print`.
145
145
  * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
146
- * not the override itself so recursion is safe.
146
+ * not the override itself, so recursion is safe.
147
147
  */
148
148
  print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
149
149
  }
150
-
151
150
  /**
152
- * Creates a schema printer factory.
153
- *
154
- * This function wraps a builder and makes options optional at call sites.
151
+ * Defines a schema printer: a function that takes a `SchemaNode` and emits
152
+ * code in your target language. Each plugin that produces code from schemas
153
+ * (TypeScript types, Zod schemas, Faker factories) ships a printer built
154
+ * with this helper.
155
155
  *
156
156
  * The builder receives resolved options and returns:
157
- * - `name` — a unique identifier for the printer
158
- * - `options` — options stored on the returned printer instance
159
- * - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
160
- * - `print` _(optional)_ — top-level override exposed as `printer.print`
161
- * - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
162
- * - This keeps recursion safe and avoids self-calls
163
157
  *
164
- * When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
158
+ * - `name` unique identifier for the printer.
159
+ * - `options` stored on the returned printer instance.
160
+ * - `nodes` map of `SchemaType` → handler. Handlers return the rendered
161
+ * output (a string, a TypeScript AST node, ...) for that schema type.
162
+ * - `print` (optional), top-level override exposed as `printer.print`.
163
+ * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
165
164
  *
166
- * @example Basic usage Zod schema printer
165
+ * Without a `print` override, `printer.print` falls back to `printer.transform`
166
+ * (the node-level dispatcher).
167
+ *
168
+ * @example Tiny Zod printer
167
169
  * ```ts
170
+ * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
171
+ *
168
172
  * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
169
173
  *
170
174
  * export const zodPrinter = definePrinter<PrinterZod>((options) => ({
@@ -173,7 +177,9 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
173
177
  * nodes: {
174
178
  * string: () => 'z.string()',
175
179
  * object(node) {
176
- * const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
180
+ * const props = node.properties
181
+ * .map((p) => `${p.name}: ${this.transform(p.schema)}`)
182
+ * .join(', ')
177
183
  * return `z.object({ ${props} })`
178
184
  * },
179
185
  * },
@@ -194,7 +200,7 @@ export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOp
194
200
  * )
195
201
  * ```
196
202
  */
197
- export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined) {
203
+ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {
198
204
  return function <T extends PrinterFactoryOptions>(
199
205
  build: (options: T['options']) => {
200
206
  name: T['name']
@@ -202,40 +208,40 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
202
208
  nodes: Partial<{
203
209
  [K in TKey]: (
204
210
  this: {
205
- transform: (node: TNode) => T['output'] | null | undefined
211
+ transform: (node: TNode) => T['output'] | null
206
212
  options: T['options']
207
213
  },
208
214
  node: TNodeByKey[K],
209
- ) => T['output'] | null | undefined
215
+ ) => T['output'] | null
210
216
  }>
211
217
  print?: (
212
218
  this: {
213
- transform: (node: TNode) => T['output'] | null | undefined
219
+ transform: (node: TNode) => T['output'] | null
214
220
  options: T['options']
215
221
  },
216
222
  node: TNode,
217
- ) => T['printOutput'] | null | undefined
223
+ ) => T['printOutput'] | null
218
224
  },
219
225
  ): (options?: T['options']) => {
220
226
  name: T['name']
221
227
  options: T['options']
222
- transform: (node: TNode) => T['output'] | null | undefined
223
- print: (node: TNode) => T['printOutput'] | null | undefined
228
+ transform: (node: TNode) => T['output'] | null
229
+ print: (node: TNode) => T['printOutput'] | null
224
230
  } {
225
231
  return (options) => {
226
232
  const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
227
233
 
228
234
  const context = {
229
235
  options: resolvedOptions,
230
- transform: (node: TNode): T['output'] | null | undefined => {
236
+ transform: (node: TNode): T['output'] | null => {
231
237
  const key = getKey(node)
232
- if (key === undefined) return null
238
+ if (key === null) return null
233
239
 
234
240
  const handler = nodes[key]
235
241
 
236
242
  if (!handler) return null
237
243
 
238
- return (handler as (this: typeof context, node: TNode) => T['output'] | null | undefined).call(context, node)
244
+ return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)
239
245
  },
240
246
  }
241
247
 
@@ -243,7 +249,7 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
243
249
  name,
244
250
  options: resolvedOptions,
245
251
  transform: context.transform,
246
- print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null | undefined,
252
+ print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,
247
253
  }
248
254
  }
249
255
  }
package/src/resolvers.ts CHANGED
@@ -1,21 +1,7 @@
1
- import { pascalCase } from '@internals/utils'
2
1
  import { narrowSchema } from './guards.ts'
3
2
  import type { SchemaNode } from './nodes/schema.ts'
4
- import { extractRefName } from './refs.ts'
5
3
  import { collect } from './visitor.ts'
6
-
7
- export function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {
8
- if (!mapping || !ref) return null
9
- return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null
10
- }
11
-
12
- export function childName(parentName: string | null | undefined, propName: string): string | null {
13
- return parentName ? pascalCase([parentName, propName].join(' ')) : null
14
- }
15
-
16
- export function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {
17
- return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
18
- }
4
+ import { extractRefName } from './utils/index.ts'
19
5
 
20
6
  /**
21
7
  * Collects import entries for all `ref` schema nodes in `node`.
@@ -27,17 +13,17 @@ export function collectImports<TImport>({
27
13
  }: {
28
14
  node: SchemaNode
29
15
  nameMapping: Map<string, string>
30
- resolve: (schemaName: string) => TImport | undefined
16
+ resolve: (schemaName: string) => TImport | null
31
17
  }): Array<TImport> {
32
18
  return collect<TImport>(node, {
33
- schema(schemaNode): TImport | undefined {
19
+ schema(schemaNode): TImport | null {
34
20
  const schemaRef = narrowSchema(schemaNode, 'ref')
35
- if (!schemaRef?.ref) return
21
+ if (!schemaRef?.ref) return null
36
22
 
37
23
  const rawName = extractRefName(schemaRef.ref)
38
24
  const schemaName = nameMapping.get(rawName) ?? rawName
39
25
  const result = resolve(schemaName)
40
- if (!result) return
26
+ if (!result) return null
41
27
 
42
28
  return result
43
29
  },