@kubb/ast 5.0.0-beta.3 → 5.0.0-beta.31
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 +1 -1
- package/dist/index.cjs +694 -331
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1433 -1007
- package/dist/index.js +682 -332
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/dedupe.ts +202 -0
- package/src/dialect.ts +64 -0
- package/src/dispatch.ts +53 -0
- package/src/factory.ts +127 -11
- package/src/guards.ts +18 -3
- package/src/index.ts +11 -3
- package/src/infer.ts +16 -5
- package/src/nodes/base.ts +2 -0
- package/src/nodes/code.ts +21 -21
- package/src/nodes/content.ts +37 -0
- package/src/nodes/file.ts +16 -14
- package/src/nodes/index.ts +7 -3
- package/src/nodes/operation.ts +98 -62
- package/src/nodes/response.ts +21 -14
- package/src/nodes/root.ts +72 -10
- package/src/nodes/schema.ts +9 -3
- package/src/printer.ts +34 -28
- package/src/refs.ts +4 -2
- package/src/resolvers.ts +4 -4
- package/src/signature.ts +135 -0
- package/src/transformers.ts +20 -15
- package/src/types.ts +8 -0
- package/src/utils.ts +109 -68
- package/src/visitor.ts +229 -275
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
|
-
*
|
|
7
|
-
*
|
|
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
|
|
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
|
|
19
|
+
* API title from `info.title` in the source document.
|
|
17
20
|
*/
|
|
18
21
|
title?: string
|
|
19
22
|
/**
|
|
20
|
-
* API description
|
|
23
|
+
* API description from `info.description` in the source document.
|
|
21
24
|
*/
|
|
22
25
|
description?: string
|
|
23
26
|
/**
|
|
24
|
-
* API version string
|
|
27
|
+
* API version string from `info.version` in the source document.
|
|
25
28
|
*/
|
|
26
29
|
version?: string
|
|
27
30
|
/**
|
|
28
|
-
* Resolved
|
|
29
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
}
|
package/src/nodes/schema.ts
CHANGED
|
@@ -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
|
/**
|
|
@@ -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.
|
|
@@ -378,12 +383,13 @@ export type RefSchemaNode = SchemaNodeBase & {
|
|
|
378
383
|
/**
|
|
379
384
|
* The fully-parsed schema that this ref resolves to.
|
|
380
385
|
* Populated during OAS parsing when the referenced definition can be resolved.
|
|
381
|
-
* `
|
|
386
|
+
* `null` when the ref cannot be resolved or is part of a circular chain.
|
|
387
|
+
* `undefined` when resolution has not been attempted.
|
|
382
388
|
*
|
|
383
389
|
* Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
|
|
384
390
|
* without following the reference manually.
|
|
385
391
|
*/
|
|
386
|
-
schema?: SchemaNode
|
|
392
|
+
schema?: SchemaNode | null
|
|
387
393
|
}
|
|
388
394
|
|
|
389
395
|
/**
|
package/src/printer.ts
CHANGED
|
@@ -18,7 +18,7 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
|
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
|
|
21
|
+
transform: (node: SchemaNode) => TOutput | null
|
|
22
22
|
/**
|
|
23
23
|
* Options for this printer instance.
|
|
24
24
|
*/
|
|
@@ -40,7 +40,7 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
|
|
|
40
40
|
export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
|
|
41
41
|
this: PrinterHandlerContext<TOutput, TOptions>,
|
|
42
42
|
node: SchemaNodeByType[T],
|
|
43
|
-
) => TOutput | null
|
|
43
|
+
) => TOutput | null
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* Partial map of per-node-type handler overrides for a printer.
|
|
@@ -108,13 +108,13 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
|
|
|
108
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
|
|
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
|
|
117
|
+
print: (node: SchemaNode) => T['printOutput'] | null
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
/**
|
|
@@ -147,24 +147,28 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
|
|
|
147
147
|
*/
|
|
148
148
|
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
|
|
149
149
|
}
|
|
150
|
-
|
|
151
150
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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 |
|
|
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
|
|
211
|
+
transform: (node: TNode) => T['output'] | null
|
|
206
212
|
options: T['options']
|
|
207
213
|
},
|
|
208
214
|
node: TNodeByKey[K],
|
|
209
|
-
) => T['output'] | null
|
|
215
|
+
) => T['output'] | null
|
|
210
216
|
}>
|
|
211
217
|
print?: (
|
|
212
218
|
this: {
|
|
213
|
-
transform: (node: TNode) => T['output'] | null
|
|
219
|
+
transform: (node: TNode) => T['output'] | null
|
|
214
220
|
options: T['options']
|
|
215
221
|
},
|
|
216
222
|
node: TNode,
|
|
217
|
-
) => T['printOutput'] | null
|
|
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
|
|
223
|
-
print: (node: TNode) => T['printOutput'] | null
|
|
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
|
|
236
|
+
transform: (node: TNode): T['output'] | null => {
|
|
231
237
|
const key = getKey(node)
|
|
232
|
-
if (key ===
|
|
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
|
|
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
|
|
252
|
+
print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,
|
|
247
253
|
}
|
|
248
254
|
}
|
|
249
255
|
}
|
package/src/refs.ts
CHANGED
|
@@ -45,13 +45,15 @@ export function buildRefMap(input: InputNode): RefMap {
|
|
|
45
45
|
/**
|
|
46
46
|
* Resolves a schema by name from a `RefMap`.
|
|
47
47
|
*
|
|
48
|
+
* Returns `null` when the ref is not found.
|
|
49
|
+
*
|
|
48
50
|
* @example
|
|
49
51
|
* ```ts
|
|
50
52
|
* const petSchema = resolveRef(refMap, 'Pet')
|
|
51
53
|
* ```
|
|
52
54
|
*/
|
|
53
|
-
export function resolveRef(refMap: RefMap, ref: string): SchemaNode |
|
|
54
|
-
return refMap.get(ref)
|
|
55
|
+
export function resolveRef(refMap: RefMap, ref: string): SchemaNode | null {
|
|
56
|
+
return refMap.get(ref) ?? null
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
/**
|
package/src/resolvers.ts
CHANGED
|
@@ -27,17 +27,17 @@ export function collectImports<TImport>({
|
|
|
27
27
|
}: {
|
|
28
28
|
node: SchemaNode
|
|
29
29
|
nameMapping: Map<string, string>
|
|
30
|
-
resolve: (schemaName: string) => TImport |
|
|
30
|
+
resolve: (schemaName: string) => TImport | null
|
|
31
31
|
}): Array<TImport> {
|
|
32
32
|
return collect<TImport>(node, {
|
|
33
|
-
schema(schemaNode): TImport |
|
|
33
|
+
schema(schemaNode): TImport | null {
|
|
34
34
|
const schemaRef = narrowSchema(schemaNode, 'ref')
|
|
35
|
-
if (!schemaRef?.ref) return
|
|
35
|
+
if (!schemaRef?.ref) return null
|
|
36
36
|
|
|
37
37
|
const rawName = extractRefName(schemaRef.ref)
|
|
38
38
|
const schemaName = nameMapping.get(rawName) ?? rawName
|
|
39
39
|
const result = resolve(schemaName)
|
|
40
|
-
if (!result) return
|
|
40
|
+
if (!result) return null
|
|
41
41
|
|
|
42
42
|
return result
|
|
43
43
|
},
|
package/src/signature.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import type { SchemaNode } from './nodes/index.ts'
|
|
3
|
+
import { extractRefName } from './refs.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The shape-affecting flags shared by every node kind: base primitive, format, and `nullable`.
|
|
7
|
+
* Documentation and usage-slot flags (`optional`/`nullish`/`readOnly`/`writeOnly`) are
|
|
8
|
+
* intentionally excluded — they describe the property slot, not the type.
|
|
9
|
+
*/
|
|
10
|
+
function flagsDescriptor(node: SchemaNode): string {
|
|
11
|
+
return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {
|
|
15
|
+
if (node.ref) return extractRefName(node.ref)
|
|
16
|
+
return node.name ?? ''
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
|
|
21
|
+
* children's signatures. {@link signatureOf} hashes this string; children contribute their
|
|
22
|
+
* fixed-length signature rather than their own full descriptor, which keeps the result bounded.
|
|
23
|
+
*/
|
|
24
|
+
function describeShape(node: SchemaNode, signatures: Map<SchemaNode, string>): string {
|
|
25
|
+
const flags = flagsDescriptor(node)
|
|
26
|
+
|
|
27
|
+
switch (node.type) {
|
|
28
|
+
case 'object': {
|
|
29
|
+
const props = (node.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema, signatures)}`).join(',')
|
|
30
|
+
let additional = ''
|
|
31
|
+
if (typeof node.additionalProperties === 'boolean') {
|
|
32
|
+
additional = `ab:${node.additionalProperties}`
|
|
33
|
+
} else if (node.additionalProperties) {
|
|
34
|
+
additional = `as:${signatureOf(node.additionalProperties, signatures)}`
|
|
35
|
+
}
|
|
36
|
+
const pattern = node.patternProperties
|
|
37
|
+
? Object.keys(node.patternProperties)
|
|
38
|
+
.sort()
|
|
39
|
+
.map((key) => `${key}=${signatureOf(node.patternProperties![key]!, signatures)}`)
|
|
40
|
+
.join(',')
|
|
41
|
+
: ''
|
|
42
|
+
return `object|${flags}|p[${props}]|${additional}|pp[${pattern}]|mn:${node.minProperties ?? ''}|mx:${node.maxProperties ?? ''}`
|
|
43
|
+
}
|
|
44
|
+
case 'array':
|
|
45
|
+
case 'tuple': {
|
|
46
|
+
const items = (node.items ?? []).map((item) => signatureOf(item, signatures)).join(',')
|
|
47
|
+
const rest = node.rest ? signatureOf(node.rest, signatures) : ''
|
|
48
|
+
return `${node.type}|${flags}|i[${items}]|r:${rest}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|u:${node.unique ? 1 : 0}`
|
|
49
|
+
}
|
|
50
|
+
case 'union': {
|
|
51
|
+
const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(',')
|
|
52
|
+
return `union|${flags}|s:${node.strategy ?? ''}|d:${node.discriminatorPropertyName ?? ''}|m[${members}]`
|
|
53
|
+
}
|
|
54
|
+
case 'intersection': {
|
|
55
|
+
const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(',')
|
|
56
|
+
return `intersection|${flags}|m[${members}]`
|
|
57
|
+
}
|
|
58
|
+
case 'enum': {
|
|
59
|
+
let values = ''
|
|
60
|
+
if (node.namedEnumValues?.length) {
|
|
61
|
+
values = node.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')
|
|
62
|
+
} else if (node.enumValues?.length) {
|
|
63
|
+
values = node.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')
|
|
64
|
+
}
|
|
65
|
+
return `enum|${flags}|v[${values}]`
|
|
66
|
+
}
|
|
67
|
+
case 'ref':
|
|
68
|
+
return `ref|${flags}|->${refTargetName(node)}`
|
|
69
|
+
case 'string':
|
|
70
|
+
return `string|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|pt:${node.pattern ?? ''}`
|
|
71
|
+
case 'number':
|
|
72
|
+
case 'integer':
|
|
73
|
+
case 'bigint':
|
|
74
|
+
return `${node.type}|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|emn:${node.exclusiveMinimum ?? ''}|emx:${node.exclusiveMaximum ?? ''}|mo:${node.multipleOf ?? ''}`
|
|
75
|
+
case 'url':
|
|
76
|
+
return `url|${flags}|path:${node.path ?? ''}|mn:${node.min ?? ''}|mx:${node.max ?? ''}`
|
|
77
|
+
case 'uuid':
|
|
78
|
+
case 'email':
|
|
79
|
+
return `${node.type}|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}`
|
|
80
|
+
case 'datetime':
|
|
81
|
+
return `datetime|${flags}|o:${node.offset ? 1 : 0}|l:${node.local ? 1 : 0}`
|
|
82
|
+
case 'date':
|
|
83
|
+
case 'time':
|
|
84
|
+
return `${node.type}|${flags}|rep:${node.representation}`
|
|
85
|
+
default:
|
|
86
|
+
return `${node.type}|${flags}`
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
|
|
92
|
+
* children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
|
|
93
|
+
* full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
|
|
94
|
+
* digest is identical across calls because it depends only on content — never on traversal
|
|
95
|
+
* order. This keeps the keys built during planning consistent with the ones recomputed later
|
|
96
|
+
* during streaming. `signatures` memoizes node → digest within a single computation.
|
|
97
|
+
*/
|
|
98
|
+
export function signatureOf(node: SchemaNode, signatures: Map<SchemaNode, string>): string {
|
|
99
|
+
const cached = signatures.get(node)
|
|
100
|
+
if (cached !== undefined) return cached
|
|
101
|
+
const signature = createHash('sha256').update(describeShape(node, signatures)).digest('hex')
|
|
102
|
+
signatures.set(node, signature)
|
|
103
|
+
return signature
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Computes a deterministic, shape-only signature (a fixed-length content hash) for a schema node.
|
|
108
|
+
*
|
|
109
|
+
* Two schemas share a signature when they are structurally identical, ignoring
|
|
110
|
+
* documentation (`name`, `title`, `description`, `example`, `default`, `deprecated`)
|
|
111
|
+
* and usage-slot flags (`optional`, `nullish`, `readOnly`, `writeOnly`). `nullable`
|
|
112
|
+
* is kept because it changes the produced type. `ref` nodes compare by target name,
|
|
113
|
+
* which also keeps the algorithm terminating on circular shapes.
|
|
114
|
+
*
|
|
115
|
+
* @example Two enums with different descriptions share a signature
|
|
116
|
+
* ```ts
|
|
117
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
|
|
118
|
+
* schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
export function schemaSignature(node: SchemaNode): string {
|
|
122
|
+
return signatureOf(node, new Map())
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns `true` when two schema nodes are structurally identical under shape-only equality.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* isSchemaEqual(a, b) // a and b produce the same TypeScript type
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {
|
|
134
|
+
return schemaSignature(a) === schemaSignature(b)
|
|
135
|
+
}
|
package/src/transformers.ts
CHANGED
|
@@ -73,25 +73,30 @@ export function setDiscriminatorEnum({
|
|
|
73
73
|
* ])
|
|
74
74
|
* ```
|
|
75
75
|
*/
|
|
76
|
-
export function
|
|
77
|
-
|
|
76
|
+
export function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {
|
|
77
|
+
let acc: SchemaNode | undefined
|
|
78
|
+
|
|
79
|
+
for (const member of members) {
|
|
78
80
|
const objectMember = narrowSchema(member, 'object')
|
|
79
|
-
if (objectMember && !objectMember.name) {
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
...previousObject,
|
|
86
|
-
properties: [...(previousObject.properties ?? []), ...(objectMember.properties ?? [])],
|
|
81
|
+
if (objectMember && !objectMember.name && acc !== undefined) {
|
|
82
|
+
const accObject = narrowSchema(acc, 'object')
|
|
83
|
+
if (accObject && !accObject.name) {
|
|
84
|
+
acc = createSchema({
|
|
85
|
+
...accObject,
|
|
86
|
+
properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],
|
|
87
87
|
})
|
|
88
|
-
|
|
88
|
+
continue
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
|
+
if (acc !== undefined) yield acc
|
|
92
|
+
acc = member
|
|
93
|
+
}
|
|
91
94
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
+
if (acc !== undefined) yield acc
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
|
|
99
|
+
return [...mergeAdjacentObjectsLazy(members)]
|
|
95
100
|
}
|
|
96
101
|
|
|
97
102
|
/**
|
|
@@ -145,7 +150,7 @@ export function setEnumName(propNode: SchemaNode, parentName: string | null | un
|
|
|
145
150
|
const enumNode = narrowSchema(propNode, 'enum')
|
|
146
151
|
|
|
147
152
|
if (enumNode?.primitive === 'boolean') {
|
|
148
|
-
return { ...propNode, name:
|
|
153
|
+
return { ...propNode, name: null }
|
|
149
154
|
}
|
|
150
155
|
|
|
151
156
|
if (enumNode) {
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export type { VisitorDepth } from './constants.ts'
|
|
2
|
+
export type { BuildDedupePlanOptions, DedupeCanonical, DedupePlan } from './dedupe.ts'
|
|
3
|
+
export type { SchemaDialect } from './dialect.ts'
|
|
4
|
+
export type { DispatchRule } from './dispatch.ts'
|
|
2
5
|
export type { DistributiveOmit } from './factory.ts'
|
|
3
6
|
export type { InferSchema, InferSchemaNode, ParserOptions } from './infer.ts'
|
|
4
7
|
export type {
|
|
@@ -21,11 +24,14 @@ export type {
|
|
|
21
24
|
FunctionParameterNode,
|
|
22
25
|
FunctionParametersNode,
|
|
23
26
|
FunctionParamNode,
|
|
27
|
+
GenericOperationNode,
|
|
24
28
|
HttpMethod,
|
|
29
|
+
HttpOperationNode,
|
|
25
30
|
HttpStatusCode,
|
|
26
31
|
ImportNode,
|
|
27
32
|
InputMeta,
|
|
28
33
|
InputNode,
|
|
34
|
+
InputStreamNode,
|
|
29
35
|
IntersectionSchemaNode,
|
|
30
36
|
Ipv4SchemaNode,
|
|
31
37
|
Ipv6SchemaNode,
|
|
@@ -37,6 +43,8 @@ export type {
|
|
|
37
43
|
NumberSchemaNode,
|
|
38
44
|
ObjectSchemaNode,
|
|
39
45
|
OperationNode,
|
|
46
|
+
OperationNodeBase,
|
|
47
|
+
OperationProtocol,
|
|
40
48
|
OutputNode,
|
|
41
49
|
ParameterGroupNode,
|
|
42
50
|
ParameterLocation,
|