@kubb/ast 5.12.8 → 5.13.1
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 +147 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +67 -1
- package/dist/index.d.ts +67 -1
- package/dist/index.js +144 -19
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -145,6 +145,12 @@ type BaseNode = {
|
|
|
145
145
|
* Node discriminator.
|
|
146
146
|
*/
|
|
147
147
|
kind: NodeKind;
|
|
148
|
+
/**
|
|
149
|
+
* Where the node comes from in the input, in the form the adapter's `locate` reads. Document
|
|
150
|
+
* adapters such as OpenAPI and AsyncAPI use an RFC 6901 JSON pointer, for example
|
|
151
|
+
* `#/components/schemas/Pet`. Plugins can copy it to the code nodes they emit.
|
|
152
|
+
*/
|
|
153
|
+
pointer?: string;
|
|
148
154
|
};
|
|
149
155
|
/** `Omit` applied to each member of a union, so the union survives. */
|
|
150
156
|
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
@@ -362,6 +368,21 @@ type ObjectSchemaNode = SchemaNodeBase & {
|
|
|
362
368
|
* Pattern-based property schemas.
|
|
363
369
|
*/
|
|
364
370
|
patternProperties?: Record<string, SchemaNode>;
|
|
371
|
+
/**
|
|
372
|
+
* Schema that each property name must match.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
377
|
+
* kind: 'Schema',
|
|
378
|
+
* type: 'object',
|
|
379
|
+
* primitive: 'object',
|
|
380
|
+
* properties: [],
|
|
381
|
+
* propertyNames: { kind: 'Schema', type: 'string', primitive: 'string', pattern: '^[a-z]+$' },
|
|
382
|
+
* }
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
propertyNames?: SchemaNode;
|
|
365
386
|
/**
|
|
366
387
|
* Minimum number of properties allowed.
|
|
367
388
|
*/
|
|
@@ -2097,6 +2118,42 @@ type TransformOptions = Visitor & TraversalOptions;
|
|
|
2097
2118
|
* ```
|
|
2098
2119
|
*/
|
|
2099
2120
|
type CollectOptions<T> = VisitorOf<T> & TraversalOptions;
|
|
2121
|
+
/**
|
|
2122
|
+
* Visits each direct child node in the AST's traversal order.
|
|
2123
|
+
*
|
|
2124
|
+
* @example
|
|
2125
|
+
* ```ts
|
|
2126
|
+
* forEachChild(operationNode, (child) => {
|
|
2127
|
+
* console.log(child.kind)
|
|
2128
|
+
* })
|
|
2129
|
+
* ```
|
|
2130
|
+
*/
|
|
2131
|
+
export declare function forEachChild(node: Node, visit: (child: Node) => void): void;
|
|
2132
|
+
/**
|
|
2133
|
+
* Visits each schema in a node's child tree in depth-first order. Includes `node` when it is a schema.
|
|
2134
|
+
*
|
|
2135
|
+
* @example
|
|
2136
|
+
* ```ts
|
|
2137
|
+
* forEachSchema(operationNode, (schema) => {
|
|
2138
|
+
* console.log(schema.type)
|
|
2139
|
+
* })
|
|
2140
|
+
* ```
|
|
2141
|
+
*/
|
|
2142
|
+
export declare function forEachSchema(node: Node, visit: (schema: SchemaNode) => void): void;
|
|
2143
|
+
/**
|
|
2144
|
+
* Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
|
|
2145
|
+
*
|
|
2146
|
+
* @example
|
|
2147
|
+
* ```ts
|
|
2148
|
+
* const firstString = findSchema(operationNode, {
|
|
2149
|
+
* predicate: (schema) => schema.type === 'string',
|
|
2150
|
+
* })
|
|
2151
|
+
* ```
|
|
2152
|
+
*/
|
|
2153
|
+
export declare function findSchema(node: Node, { predicate, followRefs }: {
|
|
2154
|
+
predicate: (schema: SchemaNode) => boolean;
|
|
2155
|
+
followRefs?: boolean;
|
|
2156
|
+
}): SchemaNode | undefined;
|
|
2100
2157
|
/**
|
|
2101
2158
|
* Synchronous depth-first transform. Each visitor callback can return a
|
|
2102
2159
|
* replacement node. Returning `undefined` keeps the original.
|
|
@@ -2459,6 +2516,15 @@ export declare function refName(ref: string): string;
|
|
|
2459
2516
|
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Order', targetName: 'OrderSchema' }) // 'OrderSchema'`
|
|
2460
2517
|
*/
|
|
2461
2518
|
export declare function resolveRefName(node: SchemaNode | null | undefined): string | null;
|
|
2519
|
+
/**
|
|
2520
|
+
* Follows a resolved ref chain to its target schema. Returns an unresolved or cyclic ref as is.
|
|
2521
|
+
*
|
|
2522
|
+
* @example
|
|
2523
|
+
* ```ts
|
|
2524
|
+
* const target = resolveRef(refSchema)
|
|
2525
|
+
* ```
|
|
2526
|
+
*/
|
|
2527
|
+
export declare function resolveRef(node: SchemaNode): SchemaNode;
|
|
2462
2528
|
/**
|
|
2463
2529
|
* Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
|
|
2464
2530
|
*
|
|
@@ -2684,7 +2750,7 @@ type ParserOptions = {
|
|
|
2684
2750
|
enumSuffix: 'enum' | (string & {});
|
|
2685
2751
|
};
|
|
2686
2752
|
declare namespace index_d_exports {
|
|
2687
|
-
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2753
|
+
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, findSchema, forEachChild, forEachSchema, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRef, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2688
2754
|
}
|
|
2689
2755
|
//#endregion
|
|
2690
2756
|
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateOnlyTypeValue, type DateSchemaNode, type DateTimeTypeValue, type DateTypeOptions, type DatetimeSchemaNode, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type InputMeta, type InputNode, type InputSecurityScheme, type InputServer, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeKind, type NotSchemaNode, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type ParameterLocation, type ParameterNode, type ParameterStyle, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, index_d_exports as ast, index_d_exports$1 as factory };
|
package/dist/index.d.ts
CHANGED
|
@@ -145,6 +145,12 @@ type BaseNode = {
|
|
|
145
145
|
* Node discriminator.
|
|
146
146
|
*/
|
|
147
147
|
kind: NodeKind;
|
|
148
|
+
/**
|
|
149
|
+
* Where the node comes from in the input, in the form the adapter's `locate` reads. Document
|
|
150
|
+
* adapters such as OpenAPI and AsyncAPI use an RFC 6901 JSON pointer, for example
|
|
151
|
+
* `#/components/schemas/Pet`. Plugins can copy it to the code nodes they emit.
|
|
152
|
+
*/
|
|
153
|
+
pointer?: string;
|
|
148
154
|
};
|
|
149
155
|
/** `Omit` applied to each member of a union, so the union survives. */
|
|
150
156
|
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
@@ -362,6 +368,21 @@ type ObjectSchemaNode = SchemaNodeBase & {
|
|
|
362
368
|
* Pattern-based property schemas.
|
|
363
369
|
*/
|
|
364
370
|
patternProperties?: Record<string, SchemaNode>;
|
|
371
|
+
/**
|
|
372
|
+
* Schema that each property name must match.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```ts
|
|
376
|
+
* const objectSchema: ObjectSchemaNode = {
|
|
377
|
+
* kind: 'Schema',
|
|
378
|
+
* type: 'object',
|
|
379
|
+
* primitive: 'object',
|
|
380
|
+
* properties: [],
|
|
381
|
+
* propertyNames: { kind: 'Schema', type: 'string', primitive: 'string', pattern: '^[a-z]+$' },
|
|
382
|
+
* }
|
|
383
|
+
* ```
|
|
384
|
+
*/
|
|
385
|
+
propertyNames?: SchemaNode;
|
|
365
386
|
/**
|
|
366
387
|
* Minimum number of properties allowed.
|
|
367
388
|
*/
|
|
@@ -2097,6 +2118,42 @@ type TransformOptions = Visitor & TraversalOptions;
|
|
|
2097
2118
|
* ```
|
|
2098
2119
|
*/
|
|
2099
2120
|
type CollectOptions<T> = VisitorOf<T> & TraversalOptions;
|
|
2121
|
+
/**
|
|
2122
|
+
* Visits each direct child node in the AST's traversal order.
|
|
2123
|
+
*
|
|
2124
|
+
* @example
|
|
2125
|
+
* ```ts
|
|
2126
|
+
* forEachChild(operationNode, (child) => {
|
|
2127
|
+
* console.log(child.kind)
|
|
2128
|
+
* })
|
|
2129
|
+
* ```
|
|
2130
|
+
*/
|
|
2131
|
+
export declare function forEachChild(node: Node, visit: (child: Node) => void): void;
|
|
2132
|
+
/**
|
|
2133
|
+
* Visits each schema in a node's child tree in depth-first order. Includes `node` when it is a schema.
|
|
2134
|
+
*
|
|
2135
|
+
* @example
|
|
2136
|
+
* ```ts
|
|
2137
|
+
* forEachSchema(operationNode, (schema) => {
|
|
2138
|
+
* console.log(schema.type)
|
|
2139
|
+
* })
|
|
2140
|
+
* ```
|
|
2141
|
+
*/
|
|
2142
|
+
export declare function forEachSchema(node: Node, visit: (schema: SchemaNode) => void): void;
|
|
2143
|
+
/**
|
|
2144
|
+
* Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
|
|
2145
|
+
*
|
|
2146
|
+
* @example
|
|
2147
|
+
* ```ts
|
|
2148
|
+
* const firstString = findSchema(operationNode, {
|
|
2149
|
+
* predicate: (schema) => schema.type === 'string',
|
|
2150
|
+
* })
|
|
2151
|
+
* ```
|
|
2152
|
+
*/
|
|
2153
|
+
export declare function findSchema(node: Node, { predicate, followRefs }: {
|
|
2154
|
+
predicate: (schema: SchemaNode) => boolean;
|
|
2155
|
+
followRefs?: boolean;
|
|
2156
|
+
}): SchemaNode | undefined;
|
|
2100
2157
|
/**
|
|
2101
2158
|
* Synchronous depth-first transform. Each visitor callback can return a
|
|
2102
2159
|
* replacement node. Returning `undefined` keeps the original.
|
|
@@ -2459,6 +2516,15 @@ export declare function refName(ref: string): string;
|
|
|
2459
2516
|
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Order', targetName: 'OrderSchema' }) // 'OrderSchema'`
|
|
2460
2517
|
*/
|
|
2461
2518
|
export declare function resolveRefName(node: SchemaNode | null | undefined): string | null;
|
|
2519
|
+
/**
|
|
2520
|
+
* Follows a resolved ref chain to its target schema. Returns an unresolved or cyclic ref as is.
|
|
2521
|
+
*
|
|
2522
|
+
* @example
|
|
2523
|
+
* ```ts
|
|
2524
|
+
* const target = resolveRef(refSchema)
|
|
2525
|
+
* ```
|
|
2526
|
+
*/
|
|
2527
|
+
export declare function resolveRef(node: SchemaNode): SchemaNode;
|
|
2462
2528
|
/**
|
|
2463
2529
|
* Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
|
|
2464
2530
|
*
|
|
@@ -2684,7 +2750,7 @@ type ParserOptions = {
|
|
|
2684
2750
|
enumSuffix: 'enum' | (string & {});
|
|
2685
2751
|
};
|
|
2686
2752
|
declare namespace index_d_exports {
|
|
2687
|
-
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2753
|
+
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateOnlyTypeValue, DateSchemaNode, DateTimeTypeValue, DateTypeOptions, DatetimeSchemaNode, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InputMeta, InputNode, InputSecurityScheme, InputServer, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeKind, NotSchemaNode, NumberSchemaNode, ObjectSchemaNode, OperationNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, index_d_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, index_d_exports$1 as factory, findCircularSchemas, findCircularSchemasFromGraph, findSchema, forEachChild, forEachSchema, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRef, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
2688
2754
|
}
|
|
2689
2755
|
//#endregion
|
|
2690
2756
|
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateOnlyTypeValue, type DateSchemaNode, type DateTimeTypeValue, type DateTypeOptions, type DatetimeSchemaNode, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type InputMeta, type InputNode, type InputSecurityScheme, type InputServer, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeKind, type NotSchemaNode, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type ParameterLocation, type ParameterNode, type ParameterStyle, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, index_d_exports as ast, index_d_exports$1 as factory };
|
package/dist/index.js
CHANGED
|
@@ -174,6 +174,7 @@ const VISITOR_KEYS = {
|
|
|
174
174
|
"members",
|
|
175
175
|
"additionalProperties",
|
|
176
176
|
"patternProperties",
|
|
177
|
+
"propertyNames",
|
|
177
178
|
"rest"
|
|
178
179
|
],
|
|
179
180
|
Property: ["schema"],
|
|
@@ -220,24 +221,102 @@ function isNodeRecord(value) {
|
|
|
220
221
|
* // returns parameters, the request body, and responses
|
|
221
222
|
* ```
|
|
222
223
|
*/
|
|
223
|
-
function
|
|
224
|
-
|
|
225
|
-
|
|
224
|
+
function getChildren(node, recurse) {
|
|
225
|
+
if (node.kind === "Schema" && !recurse) return [];
|
|
226
|
+
const children = [];
|
|
227
|
+
visitChildNodes(node, (child) => {
|
|
228
|
+
children.push(child);
|
|
229
|
+
return false;
|
|
230
|
+
});
|
|
231
|
+
return children;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Visits each direct child node in the AST's traversal order.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```ts
|
|
238
|
+
* forEachChild(operationNode, (child) => {
|
|
239
|
+
* console.log(child.kind)
|
|
240
|
+
* })
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
function forEachChild(node, visit) {
|
|
244
|
+
visitChildNodes(node, (child) => {
|
|
245
|
+
visit(child);
|
|
246
|
+
return false;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function visitChildNodes(node, visit) {
|
|
250
|
+
const keys = VISITOR_KEYS[node.kind];
|
|
251
|
+
if (!keys) return false;
|
|
226
252
|
const record = node;
|
|
227
253
|
for (const key of keys) {
|
|
228
254
|
const value = record[key];
|
|
229
255
|
if (Array.isArray(value)) {
|
|
230
|
-
for (const item of value) if (isNode(item))
|
|
256
|
+
for (const item of value) if (isNode(item) && visit(item)) return true;
|
|
231
257
|
continue;
|
|
232
258
|
}
|
|
233
259
|
if (isNode(value)) {
|
|
234
|
-
|
|
260
|
+
if (visit(value)) return true;
|
|
235
261
|
continue;
|
|
236
262
|
}
|
|
237
|
-
if (isNodeRecord(value)) {
|
|
238
|
-
|
|
263
|
+
if (isNodeRecord(value)) for (const name in value) {
|
|
264
|
+
if (!Object.hasOwn(value, name)) continue;
|
|
265
|
+
const item = value[name];
|
|
266
|
+
if (isNode(item) && visit(item)) return true;
|
|
239
267
|
}
|
|
240
268
|
}
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Visits each schema in a node's child tree in depth-first order. Includes `node` when it is a schema.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* forEachSchema(operationNode, (schema) => {
|
|
277
|
+
* console.log(schema.type)
|
|
278
|
+
* })
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
281
|
+
function forEachSchema(node, visit) {
|
|
282
|
+
const walk = (current) => {
|
|
283
|
+
if (current.kind === "Schema") visit(current);
|
|
284
|
+
forEachChild(current, walk);
|
|
285
|
+
};
|
|
286
|
+
walk(node);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* const firstString = findSchema(operationNode, {
|
|
294
|
+
* predicate: (schema) => schema.type === 'string',
|
|
295
|
+
* })
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
function findSchema(node, { predicate, followRefs = false }) {
|
|
299
|
+
const seen = followRefs ? /* @__PURE__ */ new WeakSet() : void 0;
|
|
300
|
+
const walk = (current) => {
|
|
301
|
+
if (current.kind === "Schema") {
|
|
302
|
+
if (predicate(current)) return current;
|
|
303
|
+
if (seen) {
|
|
304
|
+
if (seen.has(current)) return void 0;
|
|
305
|
+
seen.add(current);
|
|
306
|
+
if (current.type === "ref" && current.schema) {
|
|
307
|
+
const found = walk(current.schema);
|
|
308
|
+
if (found) return found;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
let found;
|
|
313
|
+
visitChildNodes(current, (child) => {
|
|
314
|
+
found = walk(child);
|
|
315
|
+
return found !== void 0;
|
|
316
|
+
});
|
|
317
|
+
if (found) return found;
|
|
318
|
+
};
|
|
319
|
+
return walk(node);
|
|
241
320
|
}
|
|
242
321
|
/**
|
|
243
322
|
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
@@ -374,7 +453,15 @@ function* collectNode(node, visitor, recurse, parent) {
|
|
|
374
453
|
* ```
|
|
375
454
|
*/
|
|
376
455
|
function collectSync(node, options) {
|
|
377
|
-
|
|
456
|
+
const { depth, parent, ...visitor } = options;
|
|
457
|
+
const result = [];
|
|
458
|
+
collectInto(node, visitor, (depth ?? "deep") === "deep", parent, result);
|
|
459
|
+
return result;
|
|
460
|
+
}
|
|
461
|
+
function collectInto(node, visitor, recurse, parent, result) {
|
|
462
|
+
const v = applyVisitor(node, visitor, parent);
|
|
463
|
+
if (v != null) result.push(v);
|
|
464
|
+
for (const child of getChildren(node, recurse)) collectInto(child, visitor, recurse, node, result);
|
|
378
465
|
}
|
|
379
466
|
//#endregion
|
|
380
467
|
//#region src/defineMacro.ts
|
|
@@ -614,6 +701,33 @@ function resolveRefName(node) {
|
|
|
614
701
|
return node.name ?? node.schema?.name ?? null;
|
|
615
702
|
}
|
|
616
703
|
/**
|
|
704
|
+
* Follows a resolved ref chain to its target schema. Returns an unresolved or cyclic ref as is.
|
|
705
|
+
*
|
|
706
|
+
* @example
|
|
707
|
+
* ```ts
|
|
708
|
+
* const target = resolveRef(refSchema)
|
|
709
|
+
* ```
|
|
710
|
+
*/
|
|
711
|
+
function resolveRef(node) {
|
|
712
|
+
if (node.type !== "ref" || !node.schema) return node;
|
|
713
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
714
|
+
let current = node;
|
|
715
|
+
while (current.type === "ref" && current.schema) {
|
|
716
|
+
if (seen.has(current)) return current;
|
|
717
|
+
seen.add(current);
|
|
718
|
+
current = current.schema;
|
|
719
|
+
}
|
|
720
|
+
return current;
|
|
721
|
+
}
|
|
722
|
+
/** Fields of a ref node that describe the ref itself, so they never override the resolved schema. */
|
|
723
|
+
const REF_OWN_KEYS = /* @__PURE__ */ new Set([
|
|
724
|
+
"kind",
|
|
725
|
+
"type",
|
|
726
|
+
"name",
|
|
727
|
+
"ref",
|
|
728
|
+
"schema"
|
|
729
|
+
]);
|
|
730
|
+
/**
|
|
617
731
|
* Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
|
|
618
732
|
*
|
|
619
733
|
* Every field on the ref except `kind`, `type`, `name`, `ref`, and `schema` overrides the same field
|
|
@@ -629,12 +743,12 @@ function resolveRefName(node) {
|
|
|
629
743
|
*/
|
|
630
744
|
function mergeRefWithSchema(node) {
|
|
631
745
|
if (!node.schema) return node;
|
|
632
|
-
const
|
|
633
|
-
const
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
746
|
+
const merged = { ...node.schema };
|
|
747
|
+
for (const key of Object.keys(node)) {
|
|
748
|
+
const value = node[key];
|
|
749
|
+
if (value !== void 0 && !REF_OWN_KEYS.has(key)) merged[key] = value;
|
|
750
|
+
}
|
|
751
|
+
return merged;
|
|
638
752
|
}
|
|
639
753
|
/**
|
|
640
754
|
* Returns `true` when a schema is a bare ref without modifiers, metadata, or omit keys.
|
|
@@ -1088,16 +1202,23 @@ function mergeByPath(result) {
|
|
|
1088
1202
|
return ({ node, names }) => {
|
|
1089
1203
|
const key = `${node.path}:${node.isTypeOnly ?? false}`;
|
|
1090
1204
|
const existing = byPath.get(key);
|
|
1091
|
-
if (existing
|
|
1092
|
-
|
|
1205
|
+
if (existing) {
|
|
1206
|
+
for (const name of names) {
|
|
1207
|
+
if (existing.seen.has(name)) continue;
|
|
1208
|
+
existing.seen.add(name);
|
|
1209
|
+
existing.names.push(name);
|
|
1210
|
+
}
|
|
1093
1211
|
return;
|
|
1094
1212
|
}
|
|
1095
1213
|
const item = {
|
|
1096
1214
|
...node,
|
|
1097
|
-
name: names
|
|
1215
|
+
name: [...names]
|
|
1098
1216
|
};
|
|
1099
1217
|
result.push(item);
|
|
1100
|
-
byPath.set(key,
|
|
1218
|
+
byPath.set(key, {
|
|
1219
|
+
names: item.name,
|
|
1220
|
+
seen: new Set(names)
|
|
1221
|
+
});
|
|
1101
1222
|
};
|
|
1102
1223
|
}
|
|
1103
1224
|
/**
|
|
@@ -1492,6 +1613,9 @@ var src_exports = /* @__PURE__ */ __exportAll({
|
|
|
1492
1613
|
factory: () => nodes_exports,
|
|
1493
1614
|
findCircularSchemas: () => findCircularSchemas,
|
|
1494
1615
|
findCircularSchemasFromGraph: () => findCircularSchemasFromGraph,
|
|
1616
|
+
findSchema: () => findSchema,
|
|
1617
|
+
forEachChild: () => forEachChild,
|
|
1618
|
+
forEachSchema: () => forEachSchema,
|
|
1495
1619
|
getSchemaLiteralValues: () => getSchemaLiteralValues,
|
|
1496
1620
|
inferDiscriminatorPropertyName: () => inferDiscriminatorPropertyName,
|
|
1497
1621
|
isBareRef: () => isBareRef,
|
|
@@ -1500,12 +1624,13 @@ var src_exports = /* @__PURE__ */ __exportAll({
|
|
|
1500
1624
|
narrowSchema: () => narrowSchema,
|
|
1501
1625
|
optionality: () => optionality,
|
|
1502
1626
|
refName: () => refName,
|
|
1627
|
+
resolveRef: () => resolveRef,
|
|
1503
1628
|
resolveRefName: () => resolveRefName,
|
|
1504
1629
|
resolveSchemaProperties: () => resolveSchemaProperties,
|
|
1505
1630
|
schemaTypes: () => schemaTypes,
|
|
1506
1631
|
transform: () => transform
|
|
1507
1632
|
});
|
|
1508
1633
|
//#endregion
|
|
1509
|
-
export { applyMacros, src_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, nodes_exports as factory, findCircularSchemas, findCircularSchemasFromGraph, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
1634
|
+
export { applyMacros, src_exports as ast, collect, collectImportedRefNames, collectSchemaRefs, collectSync, collectUsedSchemaNames, composeMacros, createPrinter, defineMacro, extractStringsFromNodes, nodes_exports as factory, findCircularSchemas, findCircularSchemasFromGraph, findSchema, forEachChild, forEachSchema, getSchemaLiteralValues, inferDiscriminatorPropertyName, isBareRef, isHttpOperationNode, mergeRefWithSchema, narrowSchema, optionality, refName, resolveRef, resolveRefName, resolveSchemaProperties, schemaTypes, transform };
|
|
1510
1635
|
|
|
1511
1636
|
//# sourceMappingURL=index.js.map
|