🚀. Socket Launch Week Day 2:Introducing Manifest Alerts.Learn more
Sign In

@kubb/ast

Package Overview
Dependencies
Maintainers
1
Versions
171
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kubb/ast - npm Package Compare versions

Comparing version
5.0.0-beta.58
to
5.0.0-beta.59
+88
dist/casing-BE2R1RXg.cjs
//#region \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
value,
configurable: true
});
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __exportAll = (all, no_symbols) => {
let target = {};
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
return target;
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
Object.defineProperty(exports, "__exportAll", {
enumerable: true,
get: function() {
return __exportAll;
}
});
Object.defineProperty(exports, "__name", {
enumerable: true,
get: function() {
return __name;
}
});
Object.defineProperty(exports, "__toESM", {
enumerable: true,
get: function() {
return __toESM;
}
});
Object.defineProperty(exports, "pascalCase", {
enumerable: true,
get: function() {
return pascalCase;
}
});
//# sourceMappingURL=casing-BE2R1RXg.cjs.map
{"version":3,"file":"casing-BE2R1RXg.cjs","names":[],"sources":["../../../internals/utils/src/casing.ts"],"sourcesContent":["type Options = {\n /**\n * Text prepended before casing is applied.\n */\n prefix?: string\n /**\n * Text appended before casing is applied.\n */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n return text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n .split(/[\\s\\-_./\\\\:]+/)\n .filter(Boolean)\n .map((word, i) => {\n if (word.length > 1 && word === word.toUpperCase()) return word\n const head = i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()\n return head + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Converts `text` to camelCase.\n *\n * @example Word boundaries\n * `camelCase('hello-world') // 'helloWorld'`\n *\n * @example With a prefix\n * `camelCase('tag', { prefix: 'create' }) // 'createTag'`\n */\nexport function camelCase(text: string, { prefix = '', suffix = '' }: Options = {}): string {\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n *\n * @example Word boundaries\n * `pascalCase('hello-world') // 'HelloWorld'`\n *\n * @example With a suffix\n * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`\n */\nexport function pascalCase(text: string, { prefix = '', suffix = '' }: Options = {}): string {\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA,SAAS,gBAAgB,MAAc,QAAyB;CAC9D,OAAO,KACJ,KAAK,CAAC,CACN,QAAQ,qBAAqB,OAAO,CAAC,CACrC,QAAQ,yBAAyB,OAAO,CAAC,CACzC,QAAQ,gBAAgB,OAAO,CAAC,CAChC,MAAM,eAAe,CAAC,CACtB,OAAO,OAAO,CAAC,CACf,KAAK,MAAM,MAAM;EAChB,IAAI,KAAK,SAAS,KAAK,SAAS,KAAK,YAAY,GAAG,OAAO;EAE3D,QADa,MAAM,KAAK,CAAC,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,KAC9E,KAAK,MAAM,CAAC;CAC5B,CAAC,CAAC,CACD,KAAK,EAAE,CAAC,CACR,QAAQ,iBAAiB,EAAE;AAChC;;;;;;;;;;AAwBA,SAAgB,WAAW,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC3F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,IAAI;AAC5D"}
import { n as __name } from "./chunk-CNktS9qV.js";
import { Pt as CodeNode, _t as SchemaNode } from "./index-BzjwdK2M.js";
//#region src/transformers.d.ts
/**
* Replaces a discriminator property's schema with a string enum of allowed values.
*
* If `node` is not an object schema, or if the property does not exist, the input
* node is returned as-is.
*
* @example
* ```ts
* const schema = createSchema({
* type: 'object',
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
* })
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
* ```
*/
declare function setDiscriminatorEnum({
node,
propertyName,
values,
enumName
}: {
node: SchemaNode;
propertyName: string;
values: Array<string>;
enumName?: string;
}): SchemaNode;
/**
* Merges adjacent anonymous object members into a single anonymous object member.
*
* @example
* ```ts
* const merged = mergeAdjacentObjects([
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
* ])
* ```
*/
declare function mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined>;
/**
* Removes enum members that are covered by broader scalar primitives in the same union.
*
* @example
* ```ts
* const simplified = simplifyUnion([
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
* createSchema({ type: 'string' }),
* ])
* // keeps only string member
* ```
*/
declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
declare function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode;
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* // Ref with description override
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
declare function syncSchemaRef(node: SchemaNode): SchemaNode;
//#endregion
//#region src/utils/extractStringsFromNodes.d.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
//#endregion
export { simplifyUnion as a, setEnumName as i, mergeAdjacentObjectsLazy as n, syncSchemaRef as o, setDiscriminatorEnum as r, extractStringsFromNodes as t };
//# sourceMappingURL=extractStringsFromNodes-WMYJ8nQL.d.ts.map
const require_casing = require("./casing-BE2R1RXg.cjs");
let node_crypto = require("node:crypto");
let node_path = require("node:path");
node_path = require_casing.__toESM(node_path, 1);
//#region src/node.ts
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
* object properties combine "required" and "nullable" into a single AST.
*
* - Non-required + non-nullable → `optional: true`.
* - Non-required + nullable → `nullish: true`.
* - Required → both flags cleared.
*/
function syncOptionality(schema, required) {
const nullable = schema.nullable ?? false;
return {
...schema,
optional: !required && !nullable ? true : void 0,
nullish: !required && nullable ? true : void 0
};
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* Set `rebuild: true` when the `build` hook derives fields from children. After a
* transform rewrites those children, the registry reruns `create` so the derived
* fields stay correct.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook that is rerun on transform
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* rebuild: true,
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey, rebuild } = config;
function create(input) {
const base = build ? build(input) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey,
rebuild
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
build: (props) => ({
...props,
content: props.content?.map(createContent)
}),
children: ["content"]
});
/**
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "parameter",
rebuild: true
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "property",
rebuild: true
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [{
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
}] : void 0);
return {
...rest,
content: entries?.map(createContent)
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
//#region src/utils/fileMerge.ts
function sourceKey(source) {
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
//#endregion
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ require_casing.__exportAll({
createArrowFunction: () => createArrowFunction,
createBreak: () => createBreak,
createConst: () => createConst,
createContent: () => createContent,
createExport: () => createExport,
createFile: () => createFile,
createFunction: () => createFunction,
createFunctionParameter: () => createFunctionParameter,
createFunctionParameters: () => createFunctionParameters,
createImport: () => createImport,
createIndexedAccessType: () => createIndexedAccessType,
createInput: () => createInput,
createJsx: () => createJsx,
createObjectBindingPattern: () => createObjectBindingPattern,
createOperation: () => createOperation,
createOutput: () => createOutput,
createParameter: () => createParameter,
createProperty: () => createProperty,
createRequestBody: () => createRequestBody,
createResponse: () => createResponse,
createSchema: () => createSchema,
createSource: () => createSource,
createText: () => createText,
createType: () => createType,
createTypeLiteral: () => createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
Object.defineProperty(exports, "arrowFunctionDef", {
enumerable: true,
get: function() {
return arrowFunctionDef;
}
});
Object.defineProperty(exports, "breakDef", {
enumerable: true,
get: function() {
return breakDef;
}
});
Object.defineProperty(exports, "constDef", {
enumerable: true,
get: function() {
return constDef;
}
});
Object.defineProperty(exports, "contentDef", {
enumerable: true,
get: function() {
return contentDef;
}
});
Object.defineProperty(exports, "createArrowFunction", {
enumerable: true,
get: function() {
return createArrowFunction;
}
});
Object.defineProperty(exports, "createBreak", {
enumerable: true,
get: function() {
return createBreak;
}
});
Object.defineProperty(exports, "createConst", {
enumerable: true,
get: function() {
return createConst;
}
});
Object.defineProperty(exports, "createContent", {
enumerable: true,
get: function() {
return createContent;
}
});
Object.defineProperty(exports, "createExport", {
enumerable: true,
get: function() {
return createExport;
}
});
Object.defineProperty(exports, "createFile", {
enumerable: true,
get: function() {
return createFile;
}
});
Object.defineProperty(exports, "createFunction", {
enumerable: true,
get: function() {
return createFunction;
}
});
Object.defineProperty(exports, "createFunctionParameter", {
enumerable: true,
get: function() {
return createFunctionParameter;
}
});
Object.defineProperty(exports, "createFunctionParameters", {
enumerable: true,
get: function() {
return createFunctionParameters;
}
});
Object.defineProperty(exports, "createImport", {
enumerable: true,
get: function() {
return createImport;
}
});
Object.defineProperty(exports, "createIndexedAccessType", {
enumerable: true,
get: function() {
return createIndexedAccessType;
}
});
Object.defineProperty(exports, "createInput", {
enumerable: true,
get: function() {
return createInput;
}
});
Object.defineProperty(exports, "createJsx", {
enumerable: true,
get: function() {
return createJsx;
}
});
Object.defineProperty(exports, "createObjectBindingPattern", {
enumerable: true,
get: function() {
return createObjectBindingPattern;
}
});
Object.defineProperty(exports, "createOperation", {
enumerable: true,
get: function() {
return createOperation;
}
});
Object.defineProperty(exports, "createOutput", {
enumerable: true,
get: function() {
return createOutput;
}
});
Object.defineProperty(exports, "createParameter", {
enumerable: true,
get: function() {
return createParameter;
}
});
Object.defineProperty(exports, "createProperty", {
enumerable: true,
get: function() {
return createProperty;
}
});
Object.defineProperty(exports, "createRequestBody", {
enumerable: true,
get: function() {
return createRequestBody;
}
});
Object.defineProperty(exports, "createResponse", {
enumerable: true,
get: function() {
return createResponse;
}
});
Object.defineProperty(exports, "createSchema", {
enumerable: true,
get: function() {
return createSchema;
}
});
Object.defineProperty(exports, "createSource", {
enumerable: true,
get: function() {
return createSource;
}
});
Object.defineProperty(exports, "createText", {
enumerable: true,
get: function() {
return createText;
}
});
Object.defineProperty(exports, "createType", {
enumerable: true,
get: function() {
return createType;
}
});
Object.defineProperty(exports, "createTypeLiteral", {
enumerable: true,
get: function() {
return createTypeLiteral;
}
});
Object.defineProperty(exports, "defineNode", {
enumerable: true,
get: function() {
return defineNode;
}
});
Object.defineProperty(exports, "exportDef", {
enumerable: true,
get: function() {
return exportDef;
}
});
Object.defineProperty(exports, "extractStringsFromNodes", {
enumerable: true,
get: function() {
return extractStringsFromNodes;
}
});
Object.defineProperty(exports, "factory_exports", {
enumerable: true,
get: function() {
return factory_exports;
}
});
Object.defineProperty(exports, "fileDef", {
enumerable: true,
get: function() {
return fileDef;
}
});
Object.defineProperty(exports, "functionDef", {
enumerable: true,
get: function() {
return functionDef;
}
});
Object.defineProperty(exports, "functionParameterDef", {
enumerable: true,
get: function() {
return functionParameterDef;
}
});
Object.defineProperty(exports, "functionParametersDef", {
enumerable: true,
get: function() {
return functionParametersDef;
}
});
Object.defineProperty(exports, "importDef", {
enumerable: true,
get: function() {
return importDef;
}
});
Object.defineProperty(exports, "indexedAccessTypeDef", {
enumerable: true,
get: function() {
return indexedAccessTypeDef;
}
});
Object.defineProperty(exports, "inputDef", {
enumerable: true,
get: function() {
return inputDef;
}
});
Object.defineProperty(exports, "jsxDef", {
enumerable: true,
get: function() {
return jsxDef;
}
});
Object.defineProperty(exports, "objectBindingPatternDef", {
enumerable: true,
get: function() {
return objectBindingPatternDef;
}
});
Object.defineProperty(exports, "operationDef", {
enumerable: true,
get: function() {
return operationDef;
}
});
Object.defineProperty(exports, "outputDef", {
enumerable: true,
get: function() {
return outputDef;
}
});
Object.defineProperty(exports, "parameterDef", {
enumerable: true,
get: function() {
return parameterDef;
}
});
Object.defineProperty(exports, "propertyDef", {
enumerable: true,
get: function() {
return propertyDef;
}
});
Object.defineProperty(exports, "requestBodyDef", {
enumerable: true,
get: function() {
return requestBodyDef;
}
});
Object.defineProperty(exports, "responseDef", {
enumerable: true,
get: function() {
return responseDef;
}
});
Object.defineProperty(exports, "schemaDef", {
enumerable: true,
get: function() {
return schemaDef;
}
});
Object.defineProperty(exports, "sourceDef", {
enumerable: true,
get: function() {
return sourceDef;
}
});
Object.defineProperty(exports, "syncOptionality", {
enumerable: true,
get: function() {
return syncOptionality;
}
});
Object.defineProperty(exports, "textDef", {
enumerable: true,
get: function() {
return textDef;
}
});
Object.defineProperty(exports, "typeDef", {
enumerable: true,
get: function() {
return typeDef;
}
});
Object.defineProperty(exports, "typeLiteralDef", {
enumerable: true,
get: function() {
return typeLiteralDef;
}
});
Object.defineProperty(exports, "update", {
enumerable: true,
get: function() {
return update;
}
});
//# sourceMappingURL=factory-BmcGBdeg.cjs.map

Sorry, the diff of this file is too big to display

import { t as __exportAll } from "./chunk-CNktS9qV.js";
import { B as createText, C as createExport, F as createArrowFunction, I as createBreak, J as extractStringsFromNodes, K as createProperty, L as createConst, R as createFunction, T as createSource, V as createType, Z as createSchema, _ as createTypeLiteral, a as createOutput, d as createInput, g as createObjectBindingPattern, h as createIndexedAccessType, j as createContent, l as createRequestBody, m as createFunctionParameters, p as createFunctionParameter, r as createParameter, s as createOperation, t as createResponse, w as createImport, z as createJsx } from "./response-DKxTr522.js";
import { hash } from "node:crypto";
import path from "node:path";
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/utils/fileMerge.ts
function sourceKey(source) {
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
//#endregion
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ __exportAll({
createArrowFunction: () => createArrowFunction,
createBreak: () => createBreak,
createConst: () => createConst,
createContent: () => createContent,
createExport: () => createExport,
createFile: () => createFile,
createFunction: () => createFunction,
createFunctionParameter: () => createFunctionParameter,
createFunctionParameters: () => createFunctionParameters,
createImport: () => createImport,
createIndexedAccessType: () => createIndexedAccessType,
createInput: () => createInput,
createJsx: () => createJsx,
createObjectBindingPattern: () => createObjectBindingPattern,
createOperation: () => createOperation,
createOutput: () => createOutput,
createParameter: () => createParameter,
createProperty: () => createProperty,
createRequestBody: () => createRequestBody,
createResponse: () => createResponse,
createSchema: () => createSchema,
createSource: () => createSource,
createText: () => createText,
createType: () => createType,
createTypeLiteral: () => createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: hash("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
export { factory_exports as n, update as r, createFile as t };
//# sourceMappingURL=factory-Du7nEP4B.js.map
{"version":3,"file":"factory-Du7nEP4B.js","names":[],"sources":["../../../internals/utils/src/fs.ts","../src/utils/fileMerge.ts","../src/factory.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname, join, posix, resolve } from 'node:path'\nimport { camelCase } from './casing.ts'\nimport { runtime } from './runtime.ts'\n\n/**\n * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and\n * returns the absolute path of the nearest `package.json`, or `null` when none\n * is found before reaching the filesystem root.\n *\n * @example\n * ```ts\n * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'\n * ```\n */\nexport function findPackageJSON(cwd?: string): string | null {\n let dir = cwd ? resolve(cwd) : process.cwd()\n while (true) {\n const pkgPath = join(dir, 'package.json')\n if (existsSync(pkgPath)) return pkgPath\n const parent = dirname(dir)\n if (parent === dir) return null\n dir = parent\n }\n}\n\n/**\n * Converts all backslashes to forward slashes.\n * Extended-length Windows paths (`\\\\?\\...`) are left unchanged.\n */\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Resolves to `true` when the file or directory at `path` exists.\n * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.\n *\n * @example\n * ```ts\n * if (await exists('./kubb.config.ts')) {\n * const content = await read('./kubb.config.ts')\n * }\n * ```\n */\nexport async function exists(path: string): Promise<boolean> {\n if (runtime.isBun) {\n return Bun.file(path).exists()\n }\n return access(path).then(\n () => true,\n () => false,\n )\n}\n\n/**\n * Reads the file at `path` as a UTF-8 string.\n * Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.\n *\n * @example\n * ```ts\n * const source = await read('./src/Pet.ts')\n * ```\n */\nexport async function read(path: string): Promise<string> {\n if (runtime.isBun) {\n return Bun.file(path).text()\n }\n return readFile(path, { encoding: 'utf8' })\n}\n\ntype WriteOptions = {\n /**\n * When `true`, re-reads the file immediately after writing and throws if the\n * content does not match — useful for catching write failures on unreliable file systems.\n */\n sanity?: boolean\n}\n\n/**\n * Writes `data` to `path`, trimming leading/trailing whitespace before saving.\n * Skips the write when the trimmed content is empty or identical to what is already on disk.\n * Creates any missing parent directories automatically.\n * When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.\n *\n * @example\n * ```ts\n * await write('./src/Pet.ts', source) // writes and returns trimmed content\n * await write('./src/Pet.ts', source) // null — file unchanged\n * await write('./src/Pet.ts', ' ') // null — empty content skipped\n * ```\n */\nexport async function write(path: string, data: string, options: WriteOptions = {}): Promise<string | null> {\n const trimmed = data.trim()\n if (trimmed === '') return null\n\n const resolved = resolve(path)\n\n if (runtime.isBun) {\n const file = Bun.file(resolved)\n const oldContent = (await file.exists()) ? await file.text() : null\n if (oldContent === trimmed) return null\n await Bun.write(resolved, trimmed)\n return trimmed\n }\n\n try {\n const oldContent = await readFile(resolved, { encoding: 'utf-8' })\n if (oldContent === trimmed) return null\n } catch {\n /* file doesn't exist yet */\n }\n\n await mkdir(dirname(resolved), { recursive: true })\n await writeFile(resolved, trimmed, { encoding: 'utf-8' })\n\n if (options.sanity) {\n const savedData = await readFile(resolved, { encoding: 'utf-8' })\n if (savedData !== trimmed) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n return savedData\n }\n\n return trimmed\n}\n\n/**\n * Recursively removes `path`. Silently succeeds when `path` does not exist.\n *\n * @example\n * ```ts\n * await clean('./dist')\n * ```\n */\nexport async function clean(path: string): Promise<void> {\n return rm(path, { recursive: true, force: true })\n}\n\n/**\n * Converts a filesystem path to use POSIX (`/`) separators.\n *\n * Most of the codebase compares and composes paths as strings (prefix matching, joining for\n * import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated\n * paths, but on Windows it returns `\\`-separated paths, which breaks every such comparison.\n *\n * Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the\n * code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is\n * exercisable from POSIX CI.\n *\n * @example\n * toPosixPath('C:\\\\repo\\\\src\\\\pet.ts') // 'C:/repo/src/pet.ts'\n */\nexport function toPosixPath(filePath: string): string {\n return filePath.replaceAll('\\\\', '/')\n}\n\n/**\n * Strips the file extension from a path or file name.\n * Only removes the last `.ext` segment when the dot is not part of a directory name.\n *\n * @example\n * trimExtName('petStore.ts') // 'petStore'\n * trimExtName('/src/models/pet.ts') // '/src/models/pet'\n * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'\n * trimExtName('noExtension') // 'noExtension'\n */\nexport function trimExtName(text: string): string {\n const dotIndex = text.lastIndexOf('.')\n if (dotIndex > 0 && !text.includes('/', dotIndex)) {\n return text.slice(0, dotIndex)\n }\n return text\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","/**\n * File-member merging. `combineImports`, `combineExports`, and `combineSources` deduplicate and sort\n * the import, export, and source entries of one file, and drop imports nothing references. This works\n * on a file's members, not on schema content.\n *\n * For collapsing duplicate schema shapes by structural signature, see `dedupe.ts`.\n */\nimport type { ExportNode, ImportNode, SourceNode } from '../nodes/index.ts'\nimport { extractStringsFromNodes } from './extractStringsFromNodes.ts'\n\nfunction sourceKey(source: SourceNode): string {\n const nameKey = source.name ?? extractStringsFromNodes(source.nodes)\n return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`\n}\n\nfunction pathTypeKey(path: string, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${isTypeOnly ?? false}`\n}\n\nfunction exportKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined, asAlias: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`\n}\n\nfunction importKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}`\n}\n\n/**\n * Computes a multi-level sort key for exports and imports:\n * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.\n */\nfunction sortKey(node: { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string }): string {\n const isArray = Array.isArray(node.name) ? '1' : '0'\n const typeOnly = node.isTypeOnly ? '0' : '1'\n const hasName = node.name != null ? '1' : '0'\n const name = Array.isArray(node.name) ? node.name.toSorted().join('\\0') : (node.name ?? '')\n return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`\n}\n\n/**\n * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.\n *\n * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.\n */\nexport function combineSources(sources: Array<SourceNode>): Array<SourceNode> {\n const seen = new Map<string, SourceNode>()\n for (const source of sources) {\n const key = sourceKey(source)\n if (!seen.has(key)) seen.set(key, source)\n }\n return [...seen.values()]\n}\n\n/**\n * Merges `incoming` names into `existing`, preserving order and dropping duplicates.\n *\n * Shared by `combineExports` and `combineImports` for the same-path name-merge case.\n */\nfunction mergeNameArrays<TName>(existing: Array<TName>, incoming: Array<TName>): Array<TName> {\n const merged = new Set(existing)\n for (const name of incoming) merged.add(name)\n return [...merged]\n}\n\n/**\n * Deduplicates and merges `ExportNode` objects by path and type.\n *\n * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.\n * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.\n */\nexport function combineExports(exports: Array<ExportNode>): Array<ExportNode> {\n const result: Array<ExportNode> = []\n // Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ExportNode>()\n // Deduplicates non-array exports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = exports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n const { name, path, isTypeOnly, asAlias } = curr\n\n if (Array.isArray(name)) {\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ExportNode = { ...curr, name: [...new Set(name)] }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n const key = exportKey(path, name, isTypeOnly, asAlias)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n\n/**\n * Deduplicates and merges `ImportNode` objects, filtering out unused imports.\n *\n * Retains imports that are referenced in `source` or re-exported. Imports with the same path and\n * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.\n */\nexport function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {\n // Build a lookup of all exported names to retain imports that are re-exported\n const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))\n const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)\n\n // Memoize object import names so the same logical (propertyName, name) pair always\n // reuses the same object reference. Set-based deduplication then works correctly.\n const importNameMemo = new Map<string, { propertyName: string; name?: string }>()\n const canonicalizeName = (n: string | { propertyName: string; name?: string }): string | { propertyName: string; name?: string } => {\n if (typeof n === 'string') return n\n const key = `${n.propertyName}:${n.name ?? ''}`\n if (!importNameMemo.has(key)) importNameMemo.set(key, n)\n return importNameMemo.get(key)!\n }\n\n // Paths that keep at least one used named import. A default import from such a path is retained\n // even when its binding can't be found in `source` e.g. a generated `client` default import\n // alongside `import type { Client } from <same path>`, where merged grouped output omits the body.\n const pathsWithUsedNamedImport = new Set<string>()\n for (const node of imports) {\n if (!Array.isArray(node.name)) continue\n if (node.name.some((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))) {\n pathsWithUsedNamedImport.add(node.path)\n }\n }\n\n const result: Array<ImportNode> = []\n // Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ImportNode>()\n // Deduplicates non-array imports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = imports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n if (curr.path === curr.root) continue\n\n const { path, isTypeOnly } = curr\n let { name } = curr\n\n if (Array.isArray(name)) {\n name = [...new Set(name.map(canonicalizeName))].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ImportNode = { ...curr, name }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue\n\n const key = importKey(path, name, isTypeOnly)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { trimExtName } from '@internals/utils'\nimport type { FileNode, Node } from './nodes/index.ts'\nimport { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'\nimport { combineExports, combineImports, combineSources } from './utils/fileMerge.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createImport, createSource } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const rawExtname = path.extname(input.baseName)\n // Handle dotfile basename like '.ts' where path.extname returns ''\n const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const source = (input.sources ?? [])\n .flatMap((item) => item.nodes ?? [])\n .map((node) => extractStringsFromNodes([node]))\n .filter(Boolean)\n .join('\\n\\n')\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []\n const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))\n const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))\n // Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's\n // definition and a cross-file import of it in the same file. The first pass catches imports that\n // resolve to this file's own path. The second drops imports of names the file already defines,\n // the case consolidation produces when the import path no longer matches `input.path`. Sources\n // stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.\n const resolvedImports = combinedImports\n .filter((imp) => imp.path !== input.path)\n .flatMap((imp) => {\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n return {\n kind: 'File',\n ...input,\n id: hash('sha256', input.path, 'hex'),\n name: trimExtName(input.baseName),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: input.meta ?? ({} as TMeta),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA8LA,SAAgB,YAAY,MAAsB;CAChD,MAAM,WAAW,KAAK,YAAY,GAAG;CACrC,IAAI,WAAW,KAAK,CAAC,KAAK,SAAS,KAAK,QAAQ,GAC9C,OAAO,KAAK,MAAM,GAAG,QAAQ;CAE/B,OAAO;AACT;;;AC1LA,SAAS,UAAU,QAA4B;CAE7C,OAAO,GADS,OAAO,QAAQ,wBAAwB,OAAO,KAAK,EACjD,GAAG,OAAO,gBAAgB,MAAM,GAAG,OAAO,cAAc;AAC5E;AAEA,SAAS,YAAY,MAAc,YAAgD;CACjF,OAAO,GAAG,KAAK,GAAG,cAAc;AAClC;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAwC,SAA6C;CACrJ,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc,MAAM,GAAG,WAAW;AACpE;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAgD;CAChH,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc;AAChD;;;;;AAMA,SAAS,QAAQ,MAAoG;CACnH,MAAM,UAAU,MAAM,QAAQ,KAAK,IAAI,IAAI,MAAM;CACjD,MAAM,WAAW,KAAK,aAAa,MAAM;CACzC,MAAM,UAAU,KAAK,QAAQ,OAAO,MAAM;CAC1C,MAAM,OAAO,MAAM,QAAQ,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,KAAK,IAAI,IAAK,KAAK,QAAQ;CACxF,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,KAAK,KAAK,GAAG,QAAQ,GAAG;AAC3D;;;;;;AAOA,SAAgB,eAAe,SAA+C;CAC5E,MAAM,uBAAO,IAAI,IAAwB;CACzC,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,MAAM,UAAU,MAAM;EAC5B,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG,KAAK,IAAI,KAAK,MAAM;CAC1C;CACA,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC;AAC1B;;;;;;AAOA,SAAS,gBAAuB,UAAwB,UAAsC;CAC5F,MAAM,SAAS,IAAI,IAAI,QAAQ;CAC/B,KAAK,MAAM,QAAQ,UAAU,OAAO,IAAI,IAAI;CAC5C,OAAO,CAAC,GAAG,MAAM;AACnB;;;;;;;AAQA,SAAgB,eAAe,SAA+C;CAC5E,MAAM,SAA4B,CAAC;CAEnC,MAAM,8BAAc,IAAI,IAAwB;CAEhD,MAAM,uBAAO,IAAI,IAAY;CAG7B,MAAM,QAAQ,QAAQ,KAAK,UAAU;EAAE;EAAM,KAAK,QAAQ,IAAI;CAAE,EAAE;CAClE,MAAM,MAAM,GAAG,MAAO,EAAE,MAAM,EAAE,MAAM,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAE;CAEjE,KAAK,MAAM,EAAE,MAAM,UAAU,OAAO;EAClC,MAAM,EAAE,MAAM,MAAM,YAAY,YAAY;EAE5C,IAAI,MAAM,QAAQ,IAAI,GAAG;GACvB,IAAI,CAAC,KAAK,QAAQ;GAElB,MAAM,MAAM,YAAY,MAAM,UAAU;GACxC,MAAM,WAAW,YAAY,IAAI,GAAG;GAEpC,IAAI,YAAY,MAAM,QAAQ,SAAS,IAAI,GACzC,SAAS,OAAO,gBAAgB,SAAS,MAAM,IAAI;QAC9C;IACL,MAAM,UAAsB;KAAE,GAAG;KAAM,MAAM,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC;IAAE;IAChE,OAAO,KAAK,OAAO;IACnB,YAAY,IAAI,KAAK,OAAO;GAC9B;EACF,OAAO;GACL,MAAM,MAAM,UAAU,MAAM,MAAM,YAAY,OAAO;GACrD,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG;IAClB,OAAO,KAAK,IAAI;IAChB,KAAK,IAAI,GAAG;GACd;EACF;CACF;CAEA,OAAO;AACT;;;;;;;AAQA,SAAgB,eAAe,SAA4B,SAA4B,QAAoC;CAEzH,MAAM,gBAAgB,IAAI,IAAI,QAAQ,SAAS,MAAO,MAAM,QAAQ,EAAE,IAAI,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,IAAI,IAAI,CAAC,CAAE,CAAC;CAC/G,MAAM,UAAU,eAAgC,CAAC,UAAU,OAAO,SAAS,UAAU,KAAK,cAAc,IAAI,UAAU;CAItH,MAAM,iCAAiB,IAAI,IAAqD;CAChF,MAAM,oBAAoB,MAA0G;EAClI,IAAI,OAAO,MAAM,UAAU,OAAO;EAClC,MAAM,MAAM,GAAG,EAAE,aAAa,GAAG,EAAE,QAAQ;EAC3C,IAAI,CAAC,eAAe,IAAI,GAAG,GAAG,eAAe,IAAI,KAAK,CAAC;EACvD,OAAO,eAAe,IAAI,GAAG;CAC/B;CAKA,MAAM,2CAA2B,IAAI,IAAY;CACjD,KAAK,MAAM,QAAQ,SAAS;EAC1B,IAAI,CAAC,MAAM,QAAQ,KAAK,IAAI,GAAG;EAC/B,IAAI,KAAK,KAAK,MAAM,SAAU,OAAO,SAAS,WAAW,OAAO,IAAI,IAAI,OAAO,KAAK,QAAQ,KAAK,YAAY,CAAE,GAC7G,yBAAyB,IAAI,KAAK,IAAI;CAE1C;CAEA,MAAM,SAA4B,CAAC;CAEnC,MAAM,8BAAc,IAAI,IAAwB;CAEhD,MAAM,uBAAO,IAAI,IAAY;CAG7B,MAAM,QAAQ,QAAQ,KAAK,UAAU;EAAE;EAAM,KAAK,QAAQ,IAAI;CAAE,EAAE;CAClE,MAAM,MAAM,GAAG,MAAO,EAAE,MAAM,EAAE,MAAM,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAE;CAEjE,KAAK,MAAM,EAAE,MAAM,UAAU,OAAO;EAClC,IAAI,KAAK,SAAS,KAAK,MAAM;EAE7B,MAAM,EAAE,MAAM,eAAe;EAC7B,IAAI,EAAE,SAAS;EAEf,IAAI,MAAM,QAAQ,IAAI,GAAG;GACvB,OAAO,CAAC,GAAG,IAAI,IAAI,KAAK,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC,QAAQ,SAAU,OAAO,SAAS,WAAW,OAAO,IAAI,IAAI,OAAO,KAAK,QAAQ,KAAK,YAAY,CAAE;GACnJ,IAAI,CAAC,KAAK,QAAQ;GAElB,MAAM,MAAM,YAAY,MAAM,UAAU;GACxC,MAAM,WAAW,YAAY,IAAI,GAAG;GAEpC,IAAI,YAAY,MAAM,QAAQ,SAAS,IAAI,GACzC,SAAS,OAAO,gBAAgB,SAAS,MAAM,IAAI;QAC9C;IACL,MAAM,UAAsB;KAAE,GAAG;KAAM;IAAK;IAC5C,OAAO,KAAK,OAAO;IACnB,YAAY,IAAI,KAAK,OAAO;GAC9B;EACF,OAAO;GACL,IAAI,QAAQ,CAAC,OAAO,IAAI,KAAK,CAAC,yBAAyB,IAAI,IAAI,GAAG;GAElE,MAAM,MAAM,UAAU,MAAM,MAAM,UAAU;GAC5C,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG;IAClB,OAAO,KAAK,IAAI;IAChB,KAAK,IAAI,GAAG;GACd;EACF;CACF;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AChJA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,WAA0C,OAA6C;CAGrG,MAAM,UAFa,KAAK,QAAQ,MAAM,QAEZ,MAAM,MAAM,SAAS,WAAW,GAAG,IAAI,MAAM,WAAW;CAClF,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,wBAAwB,MAAM,UAAU;CAG1D,MAAM,UAAU,MAAM,WAAW,CAAC,EAAA,CAC/B,SAAS,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,CACnC,KAAK,SAAS,wBAAwB,CAAC,IAAI,CAAC,CAAC,CAAC,CAC9C,OAAO,OAAO,CAAC,CACf,KAAK,MAAM;CACd,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,OAAO,IAAI,CAAC;CACjF,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,SAAS,iBAAiB,UAAU,KAAA,CAAS,IAAI,CAAC;CACvH,MAAM,aAAa,IAAI,KAAK,MAAM,WAAW,CAAC,EAAA,CAAG,KAAK,SAAS,KAAK,IAAI,CAAC,CAAC,QAAQ,SAAyB,QAAQ,IAAI,CAAC,CAAC;CACzH,MAAM,UAAU,SAAoE,OAAO,SAAS,WAAW,OAAQ,KAAK,QAAQ,KAAK;CAMzI,MAAM,kBAAkB,gBACrB,QAAQ,QAAQ,IAAI,SAAS,MAAM,IAAI,CAAC,CACxC,SAAS,QAAQ;EAChB,IAAI,CAAC,MAAM,QAAQ,IAAI,IAAI,GACzB,OAAO,OAAO,IAAI,SAAS,YAAY,WAAW,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG;EAE7E,MAAM,OAAO,IAAI,KAAK,QAAQ,SAAS,CAAC,WAAW,IAAI,OAAO,IAAI,CAAC,CAAC;EACpE,IAAI,CAAC,KAAK,QAAQ,OAAO,CAAC;EAC1B,OAAO,CAAC,KAAK,WAAW,IAAI,KAAK,SAAS,MAAM;GAAE,GAAG;GAAK,MAAM;EAAK,CAAC;CACxE,CAAC;CACH,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,OAAO,IAAI,CAAC;CAEjF,OAAO;EACL,MAAM;EACN,GAAG;EACH,IAAI,KAAK,UAAU,MAAM,MAAM,KAAK;EACpC,MAAM,YAAY,MAAM,QAAQ;EAChC;EACA,SAAS;EACT,SAAS;EACT,SAAS;EACT,MAAM,MAAM,QAAS,CAAC;CACxB;AACF"}

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { A as FunctionParametersNode, N as TypeExpression, P as TypeLiteralNode, f as OperationNode, k as FunctionParameterNode, w as ParameterNode } from "./index-BzjwdK2M.js";
//#region src/utils/operationParams.d.ts
declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
/**
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
*/
type ParamGroupType = {
/**
* Type expression for the group, a plain group-name reference.
*/
type: TypeExpression;
/**
* Whether the parameter group is optional.
*/
optional: boolean;
};
/**
* Resolver interface for {@link createOperationParams}.
*
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
*/
type OperationParamsResolver = {
/**
* Resolves the type name for an individual parameter.
*
* @example Individual path parameter name
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
*/
resolveParamName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the request body type name.
*
* @example Request body type name
* `resolver.resolveDataName(node) // → 'CreatePetData'`
*/
resolveDataName(node: OperationNode): string;
/**
* Resolves the grouped path parameters type name.
* When the return value equals `resolveParamName`, no indexed access is emitted.
*
* @example Grouped path params type name
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
*/
resolvePathParamsName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the grouped query parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped query params type name
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
*/
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string;
/**
* Resolves the grouped header parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped header params type name
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
*/
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string;
};
/**
* Options for {@link createOperationParams}.
*/
type CreateOperationParamsOptions = {
/**
* How all operation parameters are grouped in the function signature.
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
* - `'inline'` emits each param category as a separate top-level parameter
*/
paramsType: 'object' | 'inline';
/**
* How path parameters are emitted when `paramsType` is `'inline'`.
* - `'object'` groups them as `{ petId, storeId }: PathParams`
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
*/
pathParamsType: 'object' | 'inline' | 'inlineSpread';
/**
* Converts parameter names to camelCase before output.
*/
paramsCasing?: 'camelcase';
/**
* Resolver for parameter and request body type names.
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
* When omitted, falls back to the schema primitive or `'unknown'`.
*/
resolver?: OperationParamsResolver;
/**
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
* Falls back to `'{}'` when all path params are optional.
*/
pathParamsDefault?: string;
/**
* Extra parameters appended after the standard operation parameters.
*
* @example Plugin-specific trailing parameter
* ```ts
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
* ```
*/
extraParams?: Array<FunctionParameterNode>;
/**
* Override the default parameter names used for body, query, header, and rest-path groups.
*
* Useful when targeting languages or frameworks with different naming conventions.
*
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
*/
paramNames?: {
/**
* Name for the request body parameter.
* @default 'data'
*/
data?: string;
/**
* Name for the query parameters group parameter.
* @default 'params'
*/
params?: string;
/**
* Name for the header parameters group parameter.
* @default 'headers'
*/
headers?: string;
/**
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
* @default 'pathParams'
*/
path?: string;
};
/**
* Transforms every resolved type name before it lands in a parameter node, for framework-level
* type wrappers.
*
* @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
*/
typeWrapper?: (type: string) => string;
};
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
declare function resolveParamType({
node,
param,
resolver
}: {
node: OperationNode;
param: ParameterNode;
resolver: OperationParamsResolver | undefined;
}): TypeExpression;
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
declare function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode;
/**
* Shared arguments for building a query or header parameter group.
*/
type BuildGroupArgs = {
name: string;
node: OperationNode;
params: Array<ParameterNode>;
groupType: ParamGroupType | null;
resolver: OperationParamsResolver | undefined;
wrapType: (type: string) => string;
};
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
declare function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode>;
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
declare function buildTypeLiteral({
node,
params,
resolver
}: {
node: OperationNode;
params: Array<ParameterNode>;
resolver: OperationParamsResolver | undefined;
}): TypeLiteralNode;
//#endregion
export { buildTypeLiteral as a, resolveParamType as c, buildGroupParam as i, OperationParamsResolver as n, caseParams as o, ParamGroupType as r, createOperationParams as s, BuildGroupArgs as t };
//# sourceMappingURL=operationParams-BZ07xDm0.d.ts.map
import "./chunk-CNktS9qV.js";
//#region src/node.ts
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
* object properties combine "required" and "nullable" into a single AST.
*
* - Non-required + non-nullable → `optional: true`.
* - Non-required + nullable → `nullish: true`.
* - Required → both flags cleared.
*/
function syncOptionality(schema, required) {
const nullable = schema.nullable ?? false;
return {
...schema,
optional: !required && !nullable ? true : void 0,
nullish: !required && nullable ? true : void 0
};
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* Set `rebuild: true` when the `build` hook derives fields from children. After a
* transform rewrites those children, the registry reruns `create` so the derived
* fields stay correct.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook that is rerun on transform
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* rebuild: true,
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey, rebuild } = config;
function create(input) {
const base = build ? build(input) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey,
rebuild
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to camelCase.
*
* @example Word boundaries
* `camelCase('hello-world') // 'helloWorld'`
*
* @example With a prefix
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
*/
function camelCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "property",
rebuild: true
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
build: (props) => ({
...props,
content: props.content?.map(createContent)
}),
children: ["content"]
});
/**
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "parameter",
rebuild: true
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [{
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
}] : void 0);
return {
...rest,
content: entries?.map(createContent)
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
export { defineNode as $, contentDef as A, createText as B, createExport as C, fileDef as D, exportDef as E, createArrowFunction as F, typeDef as G, functionDef as H, createBreak as I, extractStringsFromNodes as J, createProperty as K, createConst as L, arrowFunctionDef as M, breakDef as N, importDef as O, constDef as P, schemaDef as Q, createFunction as R, typeLiteralDef as S, createSource as T, jsxDef as U, createType as V, textDef as W, pascalCase as X, camelCase as Y, createSchema as Z, createTypeLiteral as _, createOutput as a, indexedAccessTypeDef as b, operationDef as c, createInput as d, syncOptionality as et, inputDef as f, createObjectBindingPattern as g, createIndexedAccessType as h, parameterDef as i, createContent as j, sourceDef as k, createRequestBody as l, createFunctionParameters as m, responseDef as n, outputDef as o, createFunctionParameter as p, propertyDef as q, createParameter as r, createOperation as s, createResponse as t, requestBodyDef as u, functionParameterDef as v, createImport as w, objectBindingPatternDef as x, functionParametersDef as y, createJsx as z };
//# sourceMappingURL=response-DKxTr522.js.map

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { Et as PropertyNode, _t as SchemaNode, f as OperationNode, h as ResponseNode, n as OutputNode, nt as ContentNode, o as InputNode, t as Node, vt as SchemaNodeByType, w as ParameterNode, y as RequestBodyNode, yt as SchemaType } from "./index-BzjwdK2M.js";
//#region src/constants.d.ts
/**
* Traversal depth for AST visitor utilities.
*
* - `'shallow'` visits only the immediate node, skipping children.
* - `'deep'` recursively visits all descendant nodes.
*/
type VisitorDepth = 'shallow' | 'deep';
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
declare const schemaTypes: {
/**
* Text value.
*/
readonly string: "string";
/**
* Floating-point number (`float`, `double`).
*/
readonly number: "number";
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
readonly integer: "integer";
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
readonly bigint: "bigint";
/**
* Boolean value.
*/
readonly boolean: "boolean";
/**
* Explicit null value.
*/
readonly null: "null";
/**
* Any value (no type restriction).
*/
readonly any: "any";
/**
* Unknown value (must be narrowed before usage).
*/
readonly unknown: "unknown";
/**
* No return value (`void`).
*/
readonly void: "void";
/**
* Object with named properties.
*/
readonly object: "object";
/**
* Sequential list of items.
*/
readonly array: "array";
/**
* Fixed-length list with position-specific items.
*/
readonly tuple: "tuple";
/**
* "One of" multiple schema members.
*/
readonly union: "union";
/**
* "All of" multiple schema members.
*/
readonly intersection: "intersection";
/**
* Enum schema.
*/
readonly enum: "enum";
/**
* Reference to another schema.
*/
readonly ref: "ref";
/**
* Calendar date (for example `2026-03-24`).
*/
readonly date: "date";
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
readonly datetime: "datetime";
/**
* Time-only value (for example `09:00:00`).
*/
readonly time: "time";
/**
* UUID value.
*/
readonly uuid: "uuid";
/**
* Email address value.
*/
readonly email: "email";
/**
* URL value.
*/
readonly url: "url";
/**
* IPv4 address value.
*/
readonly ipv4: "ipv4";
/**
* IPv6 address value.
*/
readonly ipv6: "ipv6";
/**
* Binary/blob value.
*/
readonly blob: "blob";
/**
* Impossible value (`never`).
*/
readonly never: "never";
};
/**
* HTTP method identifiers used by operation nodes.
*/
declare const httpMethods: {
readonly get: "GET";
readonly post: "POST";
readonly put: "PUT";
readonly patch: "PATCH";
readonly delete: "DELETE";
readonly head: "HEAD";
readonly options: "OPTIONS";
readonly trace: "TRACE";
};
//#endregion
//#region src/dedupe.d.ts
/**
* A canonical destination for a deduplicated shape: the shared schema name and
* the synthetic `$ref` path that points at it.
*/
type DedupeCanonical = {
/**
* Canonical schema name every duplicate occurrence refers to.
*/
name: string;
/**
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
*/
ref: string;
};
/**
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
* canonical target, plus the freshly hoisted definitions that must be added to
* the schema list.
*/
type DedupePlan = {
/**
* Maps a structural signature to the canonical schema that represents it.
*/
canonicalBySignature: Map<string, DedupeCanonical>;
/**
* Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
* references to the duplicate can be repointed at the first schema with the same content.
*/
aliasNames: Map<string, DedupeCanonical>;
/**
* New top-level schema definitions created for inline shapes that had no existing
* named component. Nested duplicates inside each definition are already collapsed.
*/
hoisted: Array<SchemaNode>;
};
/**
* The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
*/
type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>;
/**
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
* The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
*/
type BuildDedupePlanOptions = {
/**
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
* reject both ineligible kinds (return `false` for anything other than, say, enums and
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
*/
isCandidate: (node: SchemaNode) => boolean;
/**
* Produces the canonical name for an inline shape with no existing named component.
* Return `null` to leave the shape inline (for example when no contextual name exists).
*/
nameFor: (node: SchemaNode, signature: string) => string | null;
/**
* Builds the `$ref` path for a canonical name.
*/
refFor: (name: string) => string;
/**
* Minimum number of occurrences before a shape is deduplicated.
*
* @default 2
*/
minOccurrences?: number;
};
/**
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
* so nested duplicates inside a replaced shape are not visited again. A `ref` that points
* at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
* the same content.
*
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
* turned into a reference to itself. Nested duplicates are still collapsed.
*
* @example
* ```ts
* const next = applyDedupe(operationNode, plan)
* ```
*/
declare function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode;
declare function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode;
/**
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
*
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
* is a named top-level schema, the first one becomes the canonical (so other top-level
* duplicates and inline copies turn into references to it). Every other top-level name with
* the same content is recorded in `aliasNames`, so refs to it can be repointed at the
* canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
* per node with {@link applyDedupe}.
*
* @example
* ```ts
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
* nameFor: (node) => node.name ?? null,
* refFor: (name) => `#/components/schemas/${name}`,
* })
* ```
*/
declare function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan;
//#endregion
//#region src/dialect.d.ts
/**
* The spec-specific questions a schema parser answers while turning a source document into Kubb
* AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where
* OpenAPI, AsyncAPI, and plain JSON Schema differ.
*/
type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
/**
* Identifies the dialect in logs and diagnostics.
*/
name: string;
/**
* Whether the schema is nullable.
*/
isNullable: (schema?: TSchema) => boolean;
/**
* Whether the value is a `$ref` pointer.
*/
isReference: (value?: unknown) => value is TRef;
/**
* Whether the schema carries a discriminator for polymorphism.
*/
isDiscriminator: (value?: unknown) => value is TDiscriminated;
/**
* Whether the schema is binary data, converted to a `blob` node.
*/
isBinary: (schema: TSchema) => boolean;
/**
* Resolves a local `$ref` against the document, or nullish when it cannot.
*/
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined;
};
/**
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
* dialect's type for inference.
*
* @example
* ```ts
* export const oasDialect = defineSchemaDialect({
* name: 'oas',
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* })
* ```
*/
declare function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
//#endregion
//#region src/visitor.d.ts
/**
* Ordered mapping of `[NodeType, ParentType]` pairs.
*
* `ParentOf` uses this map to find parent types.
*/
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
/**
* Resolves the parent node type for a given AST node type.
*
* Visitor context relies on this so `ctx.parent` is typed for each callback.
*
* @example
* ```ts
* type InputParent = ParentOf<InputNode>
* // undefined
* ```
*
* @example
* ```ts
* type PropertyParent = ParentOf<PropertyNode>
* // SchemaNode
* ```
*
* @example
* ```ts
* type SchemaParent = ParentOf<SchemaNode>
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
* ```
*/
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
/**
* Traversal context passed as the second argument to every visitor callback.
* `parent` is typed from the current node type.
*
* @example
* ```ts
* const visitor: Visitor = {
* schema(node, { parent }) {
* // parent type is narrowed by node kind
* },
* }
* ```
*/
type VisitorContext<T extends Node = Node> = {
/**
* Parent node of the currently visited node.
* For `InputNode`, this is `undefined`.
*/
parent?: ParentOf<T>;
};
/**
* Synchronous visitor consumed by `transform`. Each optional callback runs
* for the matching node type. Return a new node to replace it, or `undefined`
* to leave it untouched.
*
* Plugins typically expose `transformer` so users can supply a `Visitor` that
* rewrites the AST before printing.
*
* @example Prefix every operationId
* ```ts
* const visitor: Visitor = {
* operation(node) {
* return { ...node, operationId: `api_${node.operationId}` }
* },
* }
* ```
*
* @example Strip schema descriptions
* ```ts
* const visitor: Visitor = {
* schema(node) {
* return { ...node, description: undefined }
* },
* }
* ```
*/
type Visitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
};
/**
* A visitor callback result that may be sync or async.
*/
type MaybePromise<T> = T | Promise<T>;
/**
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
*
* @example
* ```ts
* const visitor: AsyncVisitor = {
* async operation(node) {
* await Promise.resolve(node.operationId)
* },
* }
* ```
*/
type AsyncVisitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
};
/**
* Visitor used by `collect`.
*
* @example
* ```ts
* const visitor: CollectVisitor<string> = {
* operation(node) {
* return node.operationId
* },
* }
* ```
*/
type CollectVisitor<T> = {
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
};
/**
* Options for `transform`.
*
* @example
* ```ts
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
* ```
*
* @example
* ```ts
* // Only transform the current node, not nested children
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
* ```
*/
type TransformOptions = Visitor & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Options for `walk`.
*
* @example
* ```ts
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
* ```
*/
type WalkOptions = AsyncVisitor & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Maximum number of sibling nodes visited concurrently.
* @default 30
*/
concurrency?: number;
};
/**
* Options for `collect`.
*
* @example
* ```ts
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
* ```
*/
type CollectOptions<T> = CollectVisitor<T> & {
/**
* Traversal depth.
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
declare function walk(node: Node, options: WalkOptions): Promise<void>;
/**
* Synchronous depth-first transform. Each visitor callback can return a
* replacement node. Returning `undefined` keeps the original.
*
* The original tree is never mutated, a new tree is returned. Pass
* `depth: 'shallow'` to skip recursion into children.
*
* @example Prefix every operationId
* ```ts
* const next = transform(root, {
* operation(node) {
* return { ...node, operationId: `prefixed_${node.operationId}` }
* },
* })
* ```
*
* @example Replace only the root node
* ```ts
* const next = transform(root, {
* depth: 'shallow',
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
* })
* ```
*/
declare function transform(node: InputNode, options: TransformOptions): InputNode;
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
declare function transform(node: Node, options: TransformOptions): Node;
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
//#endregion
//#region src/printer.d.ts
/**
* Runtime context passed as `this` to printer handlers.
*
* `this.transform` dispatches to node-level handlers from `nodes`.
*
* @example
* ```ts
* const context: PrinterHandlerContext<string, {}> = {
* options: {},
* transform: () => 'value',
* }
* ```
*/
type PrinterHandlerContext<TOutput, TOptions extends object> = {
/**
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
*/
transform: (node: SchemaNode) => TOutput | null;
/**
* Options for this printer instance.
*/
options: TOptions;
};
/**
* Handler for one schema node type.
*
* Use a regular function (not an arrow function) if you need `this`.
*
* @example
* ```ts
* const handler: PrinterHandler<string, {}, 'string'> = function () {
* return 'string'
* }
* ```
*/
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
/**
* Partial map of per-node-type handler overrides for a printer.
*
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
* Supply only the handlers you want to replace. The printer's built-in
* defaults fill in the rest.
*
* @example
* ```ts
* pluginZod({
* printer: {
* nodes: {
* date(): string {
* return 'z.string().date()'
* },
* } satisfies PrinterPartial<string, PrinterZodOptions>,
* },
* })
* ```
*/
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
/**
* Generic shape used by `definePrinter`.
*
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
* - `TOptions` options passed to and stored on the printer instance
* - `TOutput` the type emitted by node handlers
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
*
* @example
* ```ts
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
* ```
*/
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
name: TName;
options: TOptions;
output: TOutput;
printOutput: TPrintOutput;
};
/**
* Printer instance returned by a printer factory.
*
* @example
* ```ts
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
* ```
*/
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
/**
* Unique identifier supplied at creation time.
*/
name: T['name'];
/**
* Options for this printer instance.
*/
options: T['options'];
/**
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
* Always dispatches through the `nodes` map. Never calls the `print` override.
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/
transform: (node: SchemaNode) => T['output'] | null;
/**
* Public printer. If the builder provides a root-level `print`, this calls that
* higher-level function (which may produce full declarations).
* Otherwise, falls back to the node-level dispatcher.
*/
print: (node: SchemaNode) => T['printOutput'] | null;
};
/**
* Builder function passed to `definePrinter`.
*
* It receives resolved options and returns:
* - `name`
* - `options`
* - `nodes` handlers
* - optional top-level `print` override
*
* @example
* ```ts
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
* ```
*/
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
name: T['name'];
/**
* Options to store on the printer.
*/
options: T['options'];
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
/**
* Optional root-level print override. When provided, becomes the public `printer.print`.
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
* not the override itself, so recursion is safe.
*/
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
};
/**
* Defines a schema printer: a function that takes a `SchemaNode` and emits
* code in your target language. Each plugin that produces code from schemas
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
* with this helper.
*
* The builder receives resolved options and returns:
*
* - `name` unique identifier for the printer.
* - `options` stored on the returned printer instance.
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
* output (a string, a TypeScript AST node, ...) for that schema type.
* - `print` (optional), top-level override exposed as `printer.print`.
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
*
* Without a `print` override, `printer.print` falls back to `printer.transform`
* (the node-level dispatcher).
*
* @example Tiny Zod printer
* ```ts
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
*
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
*
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
* name: 'zod',
* options: { strict: options.strict ?? true },
* nodes: {
* string: () => 'z.string()',
* object(node) {
* const props = node.properties
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
* .join(', ')
* return `z.object({ ${props} })`
* },
* },
* }))
* ```
*/
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
/**
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
*
* @example
* ```ts
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
* (node) => node.kind,
* )
* ```
*/
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
name: T["name"];
options: T["options"];
nodes: Partial<{ [K in TKey]: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNodeByKey[K]) => T["output"] | null }>;
print?: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNode) => T["printOutput"] | null;
}) => (options?: T["options"]) => {
name: T["name"];
options: T["options"];
transform: (node: TNode) => T["output"] | null;
print: (node: TNode) => T["printOutput"] | null;
};
//#endregion
export { applyDedupe as _, definePrinter as a, schemaTypes as b, VisitorContext as c, walk as d, SchemaDialect as f, DedupePlan as g, DedupeLookups as h, createPrinterFactory as i, collect as l, DedupeCanonical as m, PrinterFactoryOptions as n, ParentOf as o, defineSchemaDialect as p, PrinterPartial as r, Visitor as s, Printer as t, transform as u, buildDedupePlan as v, httpMethods as y };
//# sourceMappingURL=types-olVl9v5p.d.ts.map
const require_casing = require("./casing-BE2R1RXg.cjs");
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/utils/refs.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? require_casing.pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return require_casing.pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
Object.defineProperty(exports, "buildGroupParam", {
enumerable: true,
get: function() {
return buildGroupParam;
}
});
Object.defineProperty(exports, "buildJSDoc", {
enumerable: true,
get: function() {
return buildJSDoc;
}
});
Object.defineProperty(exports, "buildList", {
enumerable: true,
get: function() {
return buildList;
}
});
Object.defineProperty(exports, "buildObject", {
enumerable: true,
get: function() {
return buildObject;
}
});
Object.defineProperty(exports, "buildTypeLiteral", {
enumerable: true,
get: function() {
return buildTypeLiteral;
}
});
Object.defineProperty(exports, "caseParams", {
enumerable: true,
get: function() {
return caseParams;
}
});
Object.defineProperty(exports, "childName", {
enumerable: true,
get: function() {
return childName;
}
});
Object.defineProperty(exports, "collectUsedSchemaNames", {
enumerable: true,
get: function() {
return collectUsedSchemaNames;
}
});
Object.defineProperty(exports, "containsCircularRef", {
enumerable: true,
get: function() {
return containsCircularRef;
}
});
Object.defineProperty(exports, "createOperationParams", {
enumerable: true,
get: function() {
return createOperationParams;
}
});
Object.defineProperty(exports, "enumPropName", {
enumerable: true,
get: function() {
return enumPropName;
}
});
Object.defineProperty(exports, "extractRefName", {
enumerable: true,
get: function() {
return extractRefName;
}
});
Object.defineProperty(exports, "extractStringsFromNodes", {
enumerable: true,
get: function() {
return extractStringsFromNodes;
}
});
Object.defineProperty(exports, "findCircularSchemas", {
enumerable: true,
get: function() {
return findCircularSchemas;
}
});
Object.defineProperty(exports, "getNestedAccessor", {
enumerable: true,
get: function() {
return getNestedAccessor;
}
});
Object.defineProperty(exports, "isHttpOperationNode", {
enumerable: true,
get: function() {
return isHttpOperationNode;
}
});
Object.defineProperty(exports, "isStringType", {
enumerable: true,
get: function() {
return isStringType;
}
});
Object.defineProperty(exports, "isValidVarName", {
enumerable: true,
get: function() {
return isValidVarName;
}
});
Object.defineProperty(exports, "jsStringEscape", {
enumerable: true,
get: function() {
return jsStringEscape;
}
});
Object.defineProperty(exports, "narrowSchema", {
enumerable: true,
get: function() {
return narrowSchema;
}
});
Object.defineProperty(exports, "objectKey", {
enumerable: true,
get: function() {
return objectKey;
}
});
Object.defineProperty(exports, "resolveGroupType", {
enumerable: true,
get: function() {
return resolveGroupType;
}
});
Object.defineProperty(exports, "resolveParamType", {
enumerable: true,
get: function() {
return resolveParamType;
}
});
Object.defineProperty(exports, "stringify", {
enumerable: true,
get: function() {
return stringify;
}
});
Object.defineProperty(exports, "stringifyObject", {
enumerable: true,
get: function() {
return stringifyObject;
}
});
Object.defineProperty(exports, "syncSchemaRef", {
enumerable: true,
get: function() {
return syncSchemaRef;
}
});
Object.defineProperty(exports, "toRegExpString", {
enumerable: true,
get: function() {
return toRegExpString;
}
});
Object.defineProperty(exports, "trimQuotes", {
enumerable: true,
get: function() {
return trimQuotes;
}
});
//# sourceMappingURL=utils-BCtRXfhI.cjs.map
{"version":3,"file":"utils-BCtRXfhI.cjs","names":["pascalCase"],"sources":["../src/guards.ts","../src/utils/refs.ts"],"sourcesContent":["import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'\n\n/**\n * Narrows a `SchemaNode` to the variant that matches `type`.\n *\n * @example\n * ```ts\n * const schema = createSchema({ type: 'string' })\n * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null\n * ```\n */\nexport function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {\n return node?.type === type ? (node as SchemaNodeByType[T]) : null\n}\n\n/**\n * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.\n *\n * @example\n * ```ts\n * if (isHttpOperationNode(node)) {\n * console.log(node.method, node.path)\n * }\n * ```\n */\nexport function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {\n return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)\n}\n","import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'\nimport type { SchemaType } from '../nodes/schema.ts'\nimport type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * ```ts\n * extractRefName('#/components/schemas/Pet') // 'Pet'\n * ```\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * ```ts\n * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })\n * // => 'Pet'\n * ```\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example\n * ```ts\n * childName('Order', 'shipping_address') // 'OrderShippingAddress'\n * childName(undefined, 'params') // null\n * ```\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * ```ts\n * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'\n * ```\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n\n/**\n * Derives a {@link ParamGroupType} for a query or header group from the resolver.\n *\n * Returns `null` when there is no resolver, no params, or the group name equals the\n * individual param name (so there is no real group to emit).\n */\nexport function resolveGroupType({\n node,\n params,\n group,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n group: 'query' | 'header'\n resolver: OperationParamsResolver | undefined\n}): ParamGroupType | null {\n if (!resolver || !params.length) {\n return null\n }\n const firstParam = params[0]!\n const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName\n const groupName = groupMethod.call(resolver, node, firstParam)\n if (groupName === resolver.resolveParamName(node, firstParam)) {\n return null\n }\n return { type: groupName, optional: params.every((p) => !p.required) }\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAgB,aAA2C,MAA8B,MAAqC;CAC5H,OAAO,MAAM,SAAS,OAAQ,OAA+B;AAC/D;;;;;;;;;;;AAYA,SAAgB,oBAAoB,MAAgD;CAClF,OAAO,KAAK,aAAa,UAAW,KAAK,WAAW,KAAA,KAAa,KAAK,SAAS,KAAA;AACjF;;;ACrBA,MAAM,mBAAmB,IAAI,IAAgB;CAAC;CAAU;CAAQ;CAAS;CAAO;AAAU,CAAU;;;;;;;;;AAUpG,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;AAClC;;;;;;;;;;;AA8BA,SAAgB,UAAU,YAAuC,UAAiC;CAChG,OAAO,aAAaA,eAAAA,WAAW,CAAC,YAAY,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI;AACrE;;;;;;;;;;AAWA,SAAgB,aAAa,YAAuC,UAAkB,YAA4B;CAChH,OAAOA,eAAAA,WAAW;EAAC;EAAY;EAAU;CAAU,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;AAChF;;;;;;;AAQA,SAAgB,aAAa,MAA2B;CACtD,IAAI,iBAAiB,IAAI,KAAK,IAAI,GAChC,OAAO;CAGT,MAAM,WAAW,aAAa,MAAM,MAAM,KAAK,aAAa,MAAM,MAAM;CACxE,IAAI,UACF,OAAO,SAAS,mBAAmB;CAGrC,OAAO;AACT;;;;;;;AAQA,SAAgB,iBAAiB,EAC/B,MACA,QACA,OACA,YAMwB;CACxB,IAAI,CAAC,YAAY,CAAC,OAAO,QACvB,OAAO;CAET,MAAM,aAAa,OAAO;CAE1B,MAAM,aADc,UAAU,UAAU,SAAS,yBAAyB,SAAS,wBAAA,CACrD,KAAK,UAAU,MAAM,UAAU;CAC7D,IAAI,cAAc,SAAS,iBAAiB,MAAM,UAAU,GAC1D,OAAO;CAET,OAAO;EAAE,MAAM;EAAW,UAAU,OAAO,OAAO,MAAM,CAAC,EAAE,QAAQ;CAAE;AACvE"}
import "./chunk-CNktS9qV.js";
import { A as contentDef, D as fileDef, E as exportDef, G as typeDef, H as functionDef, K as createProperty, M as arrowFunctionDef, N as breakDef, O as importDef, P as constDef, Q as schemaDef, S as typeLiteralDef, U as jsxDef, W as textDef, X as pascalCase, Y as camelCase, Z as createSchema, _ as createTypeLiteral, b as indexedAccessTypeDef, c as operationDef, f as inputDef, h as createIndexedAccessType, i as parameterDef, k as sourceDef, m as createFunctionParameters, n as responseDef, o as outputDef, p as createFunctionParameter, q as propertyDef, u as requestBodyDef, v as functionParameterDef, x as objectBindingPatternDef, y as functionParametersDef } from "./response-DKxTr522.js";
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value.
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
/**
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
* (for example `string | number | boolean`).
*/
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* HTTP method identifiers used by operation nodes.
*/
const httpMethods = {
get: "GET",
post: "POST",
put: "PUT",
patch: "PATCH",
delete: "DELETE",
head: "HEAD",
options: "OPTIONS",
trace: "TRACE"
};
/**
* One indentation level, derived from {@link INDENT_SIZE}.
*/
const INDENT = Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region ../../internals/utils/src/promise.ts
/**
* Wraps `factory` with a keyed cache backed by the provided store.
*
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
* collected) or a `Map` for primitive keys. For multi-argument functions,
* nest two `memoize` calls — the outer keyed by the first argument, the
* inner (created once per outer miss) keyed by the second.
*
* Because the cache is owned by the caller, it can be shared, inspected, or
* cleared independently of the memoized function.
*
* @example Single WeakMap key
* ```ts
* const cache = new WeakMap<SchemaNode, Set<string>>()
* const getRefs = memoize(cache, (node) => collectRefs(node))
* ```
*
* @example Single Map key (primitive)
* ```ts
* const cache = new Map<string, Resolver>()
* const getResolver = memoize(cache, (name) => buildResolver(name))
* ```
*
* @example Two-level (object + primitive)
* ```ts
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
* fn(params)('camelcase')
* ```
*/
function memoize(store, factory) {
return (key) => {
if (store.has(key)) return store.get(key);
const value = factory(key);
store.set(key, value);
return value;
};
}
//#endregion
//#region ../../internals/utils/src/reserved.ts
/**
* JavaScript and Java reserved words.
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
*/
const reservedWords = new Set([
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
"Array",
"Date",
"hasOwnProperty",
"Infinity",
"isFinite",
"isNaN",
"isPrototypeOf",
"length",
"Math",
"name",
"NaN",
"Number",
"Object",
"prototype",
"String",
"toString",
"undefined",
"valueOf"
]);
/**
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
*
* @example
* ```ts
* isValidVarName('status') // true
* isValidVarName('class') // false (reserved word)
* isValidVarName('42foo') // false (starts with digit)
* ```
*/
function isValidVarName(name) {
if (!name || reservedWords.has(name)) return false;
return isIdentifier(name);
}
/**
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
*
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
* deciding whether an object key needs quoting.
*
* @example
* ```ts
* isIdentifier('name') // true
* isIdentifier('x-total')// false
* ```
*/
function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}
//#endregion
//#region ../../internals/utils/src/string.ts
/**
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
* any backslash or single quote in the content.
*
* @example
* ```ts
* singleQuote('foo') // "'foo'"
* singleQuote("o'clock") // "'o\\'clock'"
* ```
*/
function singleQuote(value) {
if (value === void 0 || value === null) return "''";
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
}
//#endregion
//#region src/utils/codegen.ts
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
function buildJSDoc(comments, options = {}) {
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
if (comments.length === 0) return fallback;
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text) {
if (!text) return "";
return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n");
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
function objectKey(name) {
return isIdentifier(name) ? name : singleQuote(name);
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
function buildObject(entries) {
if (entries.length === 0) return "{}";
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
function buildList(items, brackets = ["[", "]"]) {
const [open, close] = brackets;
if (items.length === 0) return `${open}${close}`;
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
}
//#endregion
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/utils/refs.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name resolves.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
function resolveRefName(node) {
if (!node || node.type !== "ref") return null;
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
return node.name ?? node.schema?.name ?? null;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
//#region src/transformers.ts
/**
* Replaces a discriminator property's schema with a string enum of allowed values.
*
* If `node` is not an object schema, or if the property does not exist, the input
* node is returned as-is.
*
* @example
* ```ts
* const schema = createSchema({
* type: 'object',
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
* })
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
* ```
*/
function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
const objectNode = narrowSchema(node, "object");
if (!objectNode?.properties?.length) return node;
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
return createSchema({
...objectNode,
properties: objectNode.properties.map((prop) => {
if (prop.name !== propertyName) return prop;
return createProperty({
...prop,
schema: createSchema({
type: "enum",
primitive: "string",
enumValues: values,
name: enumName,
readOnly: prop.schema.readOnly,
writeOnly: prop.schema.writeOnly
})
});
})
});
}
/**
* Merges adjacent anonymous object members into a single anonymous object member.
*
* @example
* ```ts
* const merged = mergeAdjacentObjects([
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
* ])
* ```
*/
function* mergeAdjacentObjectsLazy(members) {
let acc;
for (const member of members) {
const objectMember = narrowSchema(member, "object");
if (objectMember && !objectMember.name && acc !== void 0) {
const accObject = narrowSchema(acc, "object");
if (accObject && !accObject.name) {
acc = createSchema({
...accObject,
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
});
continue;
}
}
if (acc !== void 0) yield acc;
acc = member;
}
if (acc !== void 0) yield acc;
}
/**
* Removes enum members that are covered by broader scalar primitives in the same union.
*
* @example
* ```ts
* const simplified = simplifyUnion([
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
* createSchema({ type: 'string' }),
* ])
* // keeps only string member
* ```
*/
function simplifyUnion(members) {
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;
return members.filter((member) => {
const enumNode = narrowSchema(member, "enum");
if (!enumNode) return true;
const primitive = enumNode.primitive;
if (!primitive) return true;
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
if (scalarPrimitives.has(primitive)) return false;
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
return true;
});
}
function setEnumName(propNode, parentName, propName, enumSuffix) {
const enumNode = narrowSchema(propNode, "enum");
if (enumNode?.primitive === "boolean") return {
...propNode,
name: null
};
if (enumNode) return {
...propNode,
name: enumPropName(parentName, propName, enumSuffix)
};
return propNode;
}
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* // Ref with description override
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
function syncSchemaRef(node) {
const ref = narrowSchema(node, "ref");
if (!ref) return node;
if (!ref.schema) return node;
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
return createSchema({
...ref.schema,
...definedOverrides
});
}
//#endregion
//#region src/utils/strings.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
function trimQuotes(text) {
if (text.length >= 2) {
const first = text[0];
const last = text[text.length - 1];
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
}
return text;
}
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
function stringify(value) {
if (value === void 0 || value === null) return "''";
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
function jsStringEscape(input) {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case "\"":
case "'":
case "\\": return `\\${character}`;
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
default: return "";
}
});
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
function toRegExpString(text, func = "RegExp") {
const raw = trimQuotes(text);
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
const replacementTarget = match?.[1] ?? "";
const matchedFlags = match?.[2];
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
const { source, flags } = new RegExp(cleaned, matchedFlags);
if (func === null) return `/${source}/${flags}`;
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
function stringifyObject(value) {
return Object.entries(value).map(([key, val]) => {
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
return `${key}: ${val}`;
}).filter(Boolean).join(",\n");
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
function getNestedAccessor(param, accessor) {
const parts = Array.isArray(param) ? param : param.split(".");
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
}
//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
const nodeDefs = [
inputDef,
outputDef,
operationDef,
requestBodyDef,
contentDef,
responseDef,
schemaDef,
propertyDef,
parameterDef,
functionParameterDef,
functionParametersDef,
typeLiteralDef,
indexedAccessTypeDef,
objectBindingPatternDef,
constDef,
typeDef,
functionDef,
arrowFunctionDef,
textDef,
breakDef,
jsxDef,
importDef,
exportDef,
sourceDef,
fileDef
];
//#endregion
//#region src/visitor.ts
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
if (rebuilt === node) return node;
const rebuild = nodeRebuilders[rebuilt.kind];
return rebuild ? rebuild(rebuilt) : rebuilt;
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
//#region src/utils/schemaGraph.ts
/**
* Collects every named schema referenced transitively from a node through its ref edges.
*
* Refs are followed by name only, so the resolved `node.schema` is never traversed inline.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
const refs = /* @__PURE__ */ new Set();
collect(node, { schema(child) {
if (child.type === "ref") {
const name = resolveRefName(child);
if (name) refs.add(name);
}
} });
return refs;
});
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
if (!node) return out;
for (const name of collectSchemaRefs(node)) out.add(name);
return out;
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when its parameters, request body, or responses reference it, directly
* or through other named schemas. The walk is iterative, so reference cycles are safe.
*
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*/
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
function computeUsedSchemaNames(operations, schemas) {
const schemaMap = /* @__PURE__ */ new Map();
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
const result = /* @__PURE__ */ new Set();
function visitSchema(schema) {
const directRefs = collectReferencedSchemaNames(schema);
for (const name of directRefs) if (!result.has(name)) {
result.add(name);
const namedSchema = schemaMap.get(name);
if (namedSchema) visitSchema(namedSchema);
}
}
for (const op of operations) for (const schema of collectLazy(op, {
depth: "shallow",
schema: (node) => node
})) visitSchema(schema);
return result;
}
function collectUsedSchemaNames(operations, schemas) {
return collectUsedSchemaNamesMemo(operations)(schemas);
}
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
const graph = /* @__PURE__ */ new Map();
for (const schema of schemas) {
if (!schema.name) continue;
graph.set(schema.name, collectReferencedSchemaNames(schema));
}
const circular = /* @__PURE__ */ new Set();
for (const start of graph.keys()) {
const visited = /* @__PURE__ */ new Set();
const stack = [...graph.get(start) ?? []];
while (stack.length > 0) {
const node = stack.pop();
if (node === start) {
circular.add(start);
break;
}
if (visited.has(node)) continue;
visited.add(node);
const next = graph.get(node);
if (next) for (const r of next) stack.push(r);
}
}
return circular;
});
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
function findCircularSchemas(schemas) {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
return findCircularSchemasMemo(schemas);
}
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
function containsCircularRef(node, { circularSchemas, excludeName }) {
if (!node || circularSchemas.size === 0) return false;
for (const _ of collectLazy(node, { schema(child) {
if (child.type !== "ref") return null;
const name = resolveRefName(child);
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
} })) return true;
return false;
}
//#endregion
//#region src/utils/operationParams.ts
/**
* Applies casing rules to parameter names and returns a new array without mutating the input.
*
* Run it before handing parameters to schema builders so output property keys get the right casing
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
* original array is returned unchanged.
*/
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
return {
...param,
name: transformed
};
})));
function caseParams(params, casing) {
if (!casing) return params;
return caseParamsMemo(params)(casing);
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
function resolveParamType({ node, param, resolver }) {
if (!resolver) return param.schema.primitive ?? "unknown";
const individualName = resolver.resolveParamName(node, param);
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName
};
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
if (groupName && groupName !== individualName) return createIndexedAccessType({
objectType: groupName,
indexType: param.name
});
return individualName;
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
function createOperationParams(node, options) {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
const dataName = paramNames?.data ?? "data";
const paramsName = paramNames?.params ?? "params";
const headersName = paramNames?.headers ?? "headers";
const pathName = paramNames?.path ?? "pathParams";
const wrapType = (type) => typeWrapper ? typeWrapper(type) : type;
const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type;
const casedParams = caseParams(node.parameters, paramsCasing);
const pathParams = casedParams.filter((p) => p.in === "path");
const queryParams = casedParams.filter((p) => p.in === "query");
const headerParams = casedParams.filter((p) => p.in === "header");
const toProperty = (param) => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({
node,
param,
resolver
})),
optional: !param.required
});
const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0;
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
const bodyProperty = bodyType ? [{
name: dataName,
type: bodyType,
optional: !(node.requestBody?.required ?? false)
}] : [];
const trailingGroups = [{
name: paramsName,
node,
params: queryParams,
groupType: resolveGroupType({
node,
params: queryParams,
group: "query",
resolver
}),
resolver,
wrapType
}, {
name: headersName,
node,
params: headerParams,
groupType: resolveGroupType({
node,
params: headerParams,
group: "header",
resolver
}),
resolver,
wrapType
}];
const params = [];
if (paramsType === "object") {
const children = [
...pathParams.map(toProperty),
...bodyProperty,
...trailingGroups.flatMap(buildGroupProperty)
];
if (children.length) params.push(createFunctionParameter({
properties: children,
default: emptyObjectDefault(children)
}));
} else {
if (pathParamsType === "inlineSpread" && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
params.push(createFunctionParameter({
name: pathName,
type: spreadType ? wrapType(spreadType) : void 0,
rest: true
}));
} else if (pathParamsType === "inline") params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))));
else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty);
params.push(createFunctionParameter({
properties: pathChildren,
default: pathParamsDefault ?? emptyObjectDefault(pathChildren)
}));
}
params.push(...bodyProperty.map((p) => createFunctionParameter(p)));
params.push(...trailingGroups.flatMap(buildGroupParam));
}
params.push(...extraParams);
return createFunctionParameters({ params });
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) {
if (groupType) return [{
name,
type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type,
optional: groupType.optional
}];
if (params.length) return [{
name,
type: buildTypeLiteral({
node,
params,
resolver
}),
optional: params.every((p) => !p.required)
}];
return [];
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupParam(args) {
return buildGroupProperty(args).map((p) => createFunctionParameter(p));
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
function buildTypeLiteral({ node, params, resolver }) {
return createTypeLiteral({ members: params.map((p) => ({
name: p.name,
type: resolveParamType({
node,
param: p,
resolver
}),
optional: !p.required
})) });
}
//#endregion
export { isHttpOperationNode as A, simplifyUnion as C, extractRefName as D, enumPropName as E, objectKey as F, isValidVarName as I, httpMethods as L, buildJSDoc as M, buildList as N, isStringType as O, buildObject as P, schemaTypes as R, setEnumName as S, childName as T, stringifyObject as _, resolveParamType as a, mergeAdjacentObjectsLazy as b, findCircularSchemas as c, transform as d, walk as f, stringify as g, jsStringEscape as h, createOperationParams as i, narrowSchema as j, resolveGroupType as k, collect as l, getNestedAccessor as m, buildTypeLiteral as n, collectUsedSchemaNames as o, nodeDefs as p, caseParams as r, containsCircularRef as s, buildGroupParam as t, collectLazy as u, toRegExpString as v, syncSchemaRef as w, setDiscriminatorEnum as x, trimQuotes as y };
//# sourceMappingURL=utils-SdZU0F3H.js.map

Sorry, the diff of this file is too big to display

import { isIdentifier, singleQuote } from '@internals/utils'
import { INDENT } from '../constants.ts'
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
export function buildJSDoc(
comments: Array<string>,
options: {
/**
* String used to indent each comment line.
* @default ' * '
*/
indent?: string
/**
* String appended after the closing tag.
* @default '\n '
*/
suffix?: string
/**
* Returned as-is when `comments` is empty.
* @default ' '
*/
fallback?: string
} = {},
): string {
const { indent = ' * ', suffix = '\n ', fallback = ' ' } = options
if (comments.length === 0) return fallback
return `/**\n${comments.map((c) => `${indent}${c}`).join('\n')}\n */${suffix}`
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text: string): string {
if (!text) return ''
return text
.split('\n')
.map((line) => (line.trim() ? `${INDENT}${line}` : ''))
.join('\n')
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
export function objectKey(name: string): string {
return isIdentifier(name) ? name : singleQuote(name)
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
export function buildObject(entries: Array<string>): string {
if (entries.length === 0) return '{}'
const body = entries.map((entry) => `${indentLines(entry)},`).join('\n')
return `{\n${body}\n}`
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
export function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {
const [open, close] = brackets
if (items.length === 0) return `${open}${close}`
if (!items.some((item) => item.includes('\n'))) return `${open}${items.join(', ')}${close}`
const body = items.map((item) => `${indentLines(item)},`).join('\n')
return `${open}\n${body}\n${close}`
}
/**
* File-member merging. `combineImports`, `combineExports`, and `combineSources` deduplicate and sort
* the import, export, and source entries of one file, and drop imports nothing references. This works
* on a file's members, not on schema content.
*
* For collapsing duplicate schema shapes by structural signature, see `dedupe.ts`.
*/
import type { ExportNode, ImportNode, SourceNode } from '../nodes/index.ts'
import { extractStringsFromNodes } from './extractStringsFromNodes.ts'
function sourceKey(source: SourceNode): string {
const nameKey = source.name ?? extractStringsFromNodes(source.nodes)
return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`
}
function pathTypeKey(path: string, isTypeOnly: boolean | null | undefined): string {
return `${path}:${isTypeOnly ?? false}`
}
function exportKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined, asAlias: boolean | null | undefined): string {
return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`
}
function importKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined): string {
return `${path}:${name ?? ''}:${isTypeOnly ?? false}`
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node: { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string }): string {
const isArray = Array.isArray(node.name) ? '1' : '0'
const typeOnly = node.isTypeOnly ? '0' : '1'
const hasName = node.name != null ? '1' : '0'
const name = Array.isArray(node.name) ? node.name.toSorted().join('\0') : (node.name ?? '')
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
export function combineSources(sources: Array<SourceNode>): Array<SourceNode> {
const seen = new Map<string, SourceNode>()
for (const source of sources) {
const key = sourceKey(source)
if (!seen.has(key)) seen.set(key, source)
}
return [...seen.values()]
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays<TName>(existing: Array<TName>, incoming: Array<TName>): Array<TName> {
const merged = new Set(existing)
for (const name of incoming) merged.add(name)
return [...merged]
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
export function combineExports(exports: Array<ExportNode>): Array<ExportNode> {
const result: Array<ExportNode> = []
// Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging
const namedByPath = new Map<string, ExportNode>()
// Deduplicates non-array exports by their exact identity
const seen = new Set<string>()
// Precompute sort keys once, avoids recomputing per comparison.
const keyed = exports.map((node) => ({ node, key: sortKey(node) }))
keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr
if (Array.isArray(name)) {
if (!name.length) continue
const key = pathTypeKey(path, isTypeOnly)
const existing = namedByPath.get(key)
if (existing && Array.isArray(existing.name)) {
existing.name = mergeNameArrays(existing.name, name)
} else {
const newItem: ExportNode = { ...curr, name: [...new Set(name)] }
result.push(newItem)
namedByPath.set(key, newItem)
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias)
if (!seen.has(key)) {
result.push(curr)
seen.add(key)
}
}
}
return result
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*/
export function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {
// Build a lookup of all exported names to retain imports that are re-exported
const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))
const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)
// Memoize object import names so the same logical (propertyName, name) pair always
// reuses the same object reference. Set-based deduplication then works correctly.
const importNameMemo = new Map<string, { propertyName: string; name?: string }>()
const canonicalizeName = (n: string | { propertyName: string; name?: string }): string | { propertyName: string; name?: string } => {
if (typeof n === 'string') return n
const key = `${n.propertyName}:${n.name ?? ''}`
if (!importNameMemo.has(key)) importNameMemo.set(key, n)
return importNameMemo.get(key)!
}
// Paths that keep at least one used named import. A default import from such a path is retained
// even when its binding can't be found in `source` e.g. a generated `client` default import
// alongside `import type { Client } from <same path>`, where merged grouped output omits the body.
const pathsWithUsedNamedImport = new Set<string>()
for (const node of imports) {
if (!Array.isArray(node.name)) continue
if (node.name.some((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))) {
pathsWithUsedNamedImport.add(node.path)
}
}
const result: Array<ImportNode> = []
// Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging
const namedByPath = new Map<string, ImportNode>()
// Deduplicates non-array imports by their exact identity
const seen = new Set<string>()
// Precompute sort keys once, avoids recomputing per comparison.
const keyed = imports.map((node) => ({ node, key: sortKey(node) }))
keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue
const { path, isTypeOnly } = curr
let { name } = curr
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))
if (!name.length) continue
const key = pathTypeKey(path, isTypeOnly)
const existing = namedByPath.get(key)
if (existing && Array.isArray(existing.name)) {
existing.name = mergeNameArrays(existing.name, name)
} else {
const newItem: ImportNode = { ...curr, name }
result.push(newItem)
namedByPath.set(key, newItem)
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue
const key = importKey(path, name, isTypeOnly)
if (!seen.has(key)) {
result.push(curr)
seen.add(key)
}
}
}
return result
}
import { camelCase, isValidVarName, memoize } from '@internals/utils'
import { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createTypeLiteral } from '../nodes/function.ts'
import type { FunctionParameterNode, FunctionParametersNode, OperationNode, ParameterNode, TypeExpression, TypeLiteralNode } from '../nodes/index.ts'
import { resolveGroupType } from './refs.ts'
/**
* Applies casing rules to parameter names and returns a new array without mutating the input.
*
* Run it before handing parameters to schema builders so output property keys get the right casing
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
* original array is returned unchanged.
*/
const caseParamsMemo = memoize(new WeakMap<Array<ParameterNode>, (casing: string) => Array<ParameterNode>>(), (params) =>
memoize(new Map<string, Array<ParameterNode>>(), (casing: string) =>
params.map((param) => {
const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name
return { ...param, name: transformed }
}),
),
)
export function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
if (!casing) return params
return caseParamsMemo(params)(casing)
}
/**
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
*/
export type ParamGroupType = {
/**
* Type expression for the group, a plain group-name reference.
*/
type: TypeExpression
/**
* Whether the parameter group is optional.
*/
optional: boolean
}
/**
* A single member of a destructured parameter group, fed to `createFunctionParameter({ properties })`.
*/
type GroupProperty = {
name: string
type: TypeExpression
optional?: boolean
}
/**
* Resolver interface for {@link createOperationParams}.
*
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
*/
export type OperationParamsResolver = {
/**
* Resolves the type name for an individual parameter.
*
* @example Individual path parameter name
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
*/
resolveParamName(node: OperationNode, param: ParameterNode): string
/**
* Resolves the request body type name.
*
* @example Request body type name
* `resolver.resolveDataName(node) // → 'CreatePetData'`
*/
resolveDataName(node: OperationNode): string
/**
* Resolves the grouped path parameters type name.
* When the return value equals `resolveParamName`, no indexed access is emitted.
*
* @example Grouped path params type name
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
*/
resolvePathParamsName(node: OperationNode, param: ParameterNode): string
/**
* Resolves the grouped query parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped query params type name
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
*/
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
/**
* Resolves the grouped header parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped header params type name
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
*/
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
}
/**
* Options for {@link createOperationParams}.
*/
export type CreateOperationParamsOptions = {
/**
* How all operation parameters are grouped in the function signature.
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
* - `'inline'` emits each param category as a separate top-level parameter
*/
paramsType: 'object' | 'inline'
/**
* How path parameters are emitted when `paramsType` is `'inline'`.
* - `'object'` groups them as `{ petId, storeId }: PathParams`
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
*/
pathParamsType: 'object' | 'inline' | 'inlineSpread'
/**
* Converts parameter names to camelCase before output.
*/
paramsCasing?: 'camelcase'
/**
* Resolver for parameter and request body type names.
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
* When omitted, falls back to the schema primitive or `'unknown'`.
*/
resolver?: OperationParamsResolver
/**
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
* Falls back to `'{}'` when all path params are optional.
*/
pathParamsDefault?: string
/**
* Extra parameters appended after the standard operation parameters.
*
* @example Plugin-specific trailing parameter
* ```ts
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
* ```
*/
extraParams?: Array<FunctionParameterNode>
/**
* Override the default parameter names used for body, query, header, and rest-path groups.
*
* Useful when targeting languages or frameworks with different naming conventions.
*
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
*/
paramNames?: {
/**
* Name for the request body parameter.
* @default 'data'
*/
data?: string
/**
* Name for the query parameters group parameter.
* @default 'params'
*/
params?: string
/**
* Name for the header parameters group parameter.
* @default 'headers'
*/
headers?: string
/**
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
* @default 'pathParams'
*/
path?: string
}
/**
* Transforms every resolved type name before it lands in a parameter node, for framework-level
* type wrappers.
*
* @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
*/
typeWrapper?: (type: string) => string
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, it falls back to the schema primitive (a plain type-name string). When the
* parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like
* `GroupParams['petId']`, otherwise the resolved individual name.
*/
export function resolveParamType({
node,
param,
resolver,
}: {
node: OperationNode
param: ParameterNode
resolver: OperationParamsResolver | undefined
}): TypeExpression {
if (!resolver) {
return param.schema.primitive ?? 'unknown'
}
const individualName = resolver.resolveParamName(node, param)
const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName,
} as const
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined
if (groupName && groupName !== individualName) {
return createIndexedAccessType({ objectType: groupName, indexType: param.name })
}
return individualName
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
export function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options
const dataName = paramNames?.data ?? 'data'
const paramsName = paramNames?.params ?? 'params'
const headersName = paramNames?.headers ?? 'headers'
const pathName = paramNames?.path ?? 'pathParams'
const wrapType = (type: string): string => (typeWrapper ? typeWrapper(type) : type)
// Only plain type-name references are wrapped, they need casing applied.
// TypeLiteral and IndexedAccessType expressions are pre-resolved and pass through unchanged.
const wrapTypeExpression = (type: TypeExpression): TypeExpression => (typeof type === 'string' ? wrapType(type) : type)
const casedParams = caseParams(node.parameters, paramsCasing)
const pathParams = casedParams.filter((p) => p.in === 'path')
const queryParams = casedParams.filter((p) => p.in === 'query')
const headerParams = casedParams.filter((p) => p.in === 'header')
const toProperty = (param: ParameterNode): GroupProperty => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({ node, param, resolver })),
optional: !param.required,
})
const emptyObjectDefault = (props: Array<GroupProperty>): string | undefined => (props.every((p) => p.optional) ? '{}' : undefined)
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined
const bodyProperty: Array<GroupProperty> = bodyType ? [{ name: dataName, type: bodyType, optional: !(node.requestBody?.required ?? false) }] : []
const trailingGroups: Array<BuildGroupArgs> = [
{ name: paramsName, node, params: queryParams, groupType: resolveGroupType({ node, params: queryParams, group: 'query', resolver }), resolver, wrapType },
{
name: headersName,
node,
params: headerParams,
groupType: resolveGroupType({ node, params: headerParams, group: 'header', resolver }),
resolver,
wrapType,
},
]
const params: Array<FunctionParameterNode> = []
if (paramsType === 'object') {
const children = [...pathParams.map(toProperty), ...bodyProperty, ...trailingGroups.flatMap(buildGroupProperty)]
if (children.length) {
params.push(createFunctionParameter({ properties: children, default: emptyObjectDefault(children) }))
}
} else {
if (pathParamsType === 'inlineSpread' && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!)
params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))
} else if (pathParamsType === 'inline') {
params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))))
} else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty)
params.push(createFunctionParameter({ properties: pathChildren, default: pathParamsDefault ?? emptyObjectDefault(pathChildren) }))
}
params.push(...bodyProperty.map((p) => createFunctionParameter(p)))
params.push(...trailingGroups.flatMap(buildGroupParam))
}
params.push(...extraParams)
return createFunctionParameters({ params })
}
/**
* Shared arguments for building a query or header parameter group.
*/
export type BuildGroupArgs = {
name: string
node: OperationNode
params: Array<ParameterNode>
groupType: ParamGroupType | null
resolver: OperationParamsResolver | undefined
wrapType: (type: string) => string
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }: BuildGroupArgs): Array<GroupProperty> {
if (groupType) {
const type = typeof groupType.type === 'string' ? wrapType(groupType.type) : groupType.type
return [{ name, type, optional: groupType.optional }]
}
if (params.length) {
return [{ name, type: buildTypeLiteral({ node, params, resolver }), optional: params.every((p) => !p.required) }]
}
return []
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
export function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode> {
return buildGroupProperty(args).map((p) => createFunctionParameter(p))
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
export function buildTypeLiteral({
node,
params,
resolver,
}: {
node: OperationNode
params: Array<ParameterNode>
resolver: OperationParamsResolver | undefined
}): TypeLiteralNode {
return createTypeLiteral({
members: params.map((p) => ({
name: p.name,
type: resolveParamType({ node, param: p, resolver }),
optional: !p.required,
})),
})
}
import { pascalCase } from '@internals/utils'
import { narrowSchema } from '../guards.ts'
import type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'
import type { SchemaType } from '../nodes/schema.ts'
import type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'
const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
export function extractRefName(ref: string): string {
return ref.split('/').at(-1) ?? ref
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name resolves.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
export function resolveRefName(node: SchemaNode | undefined): string | null {
if (!node || node.type !== 'ref') return null
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null
return node.name ?? node.schema?.name ?? null
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
export function childName(parentName: string | null | undefined, propName: string): string | null {
return parentName ? pascalCase([parentName, propName].join(' ')) : null
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
export function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {
return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
export function isStringType(node: SchemaNode): boolean {
if (plainStringTypes.has(node.type)) {
return true
}
const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')
if (temporal) {
return temporal.representation !== 'date'
}
return false
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
export function resolveGroupType({
node,
params,
group,
resolver,
}: {
node: OperationNode
params: Array<ParameterNode>
group: 'query' | 'header'
resolver: OperationParamsResolver | undefined
}): ParamGroupType | null {
if (!resolver || !params.length) {
return null
}
const firstParam = params[0]!
const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName
const groupName = groupMethod.call(resolver, node, firstParam)
if (groupName === resolver.resolveParamName(node, firstParam)) {
return null
}
return { type: groupName, optional: params.every((p) => !p.required) }
}
import { memoize } from '@internals/utils'
import type { OperationNode, SchemaNode } from '../nodes/index.ts'
import { collect, collectLazy } from '../visitor.ts'
import { resolveRefName } from './refs.ts'
/**
* Collects every named schema referenced transitively from a node through its ref edges.
*
* Refs are followed by name only, so the resolved `node.schema` is never traversed inline.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(new WeakMap<SchemaNode, ReadonlySet<string>>(), (node: SchemaNode): ReadonlySet<string> => {
const refs = new Set<string>()
collect<void>(node, {
schema(child) {
if (child.type === 'ref') {
const name = resolveRefName(child)
if (name) refs.add(name)
}
},
})
return refs
})
export function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {
if (!node) return out
for (const name of collectSchemaRefs(node)) out.add(name)
return out
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when its parameters, request body, or responses reference it, directly
* or through other named schemas. The walk is iterative, so reference cycles are safe.
*
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*/
const collectUsedSchemaNamesMemo = memoize(new WeakMap<ReadonlyArray<OperationNode>, (schemas: ReadonlyArray<SchemaNode>) => Set<string>>(), (ops) =>
memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas) => computeUsedSchemaNames(ops, schemas)),
)
function computeUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {
const schemaMap = new Map<string, SchemaNode>()
for (const schema of schemas) {
if (schema.name) schemaMap.set(schema.name, schema)
}
const result = new Set<string>()
function visitSchema(schema: SchemaNode): void {
const directRefs = collectReferencedSchemaNames(schema)
for (const name of directRefs) {
if (!result.has(name)) {
result.add(name)
const namedSchema = schemaMap.get(name)
if (namedSchema) visitSchema(namedSchema)
}
}
}
for (const op of operations) {
for (const schema of collectLazy<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {
visitSchema(schema)
}
}
return result
}
export function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {
return collectUsedSchemaNamesMemo(operations)(schemas)
}
const EMPTY_CIRCULAR_SET = new Set<string>()
const findCircularSchemasMemo = memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas: ReadonlyArray<SchemaNode>): Set<string> => {
const graph = new Map<string, Set<string>>()
for (const schema of schemas) {
if (!schema.name) continue
graph.set(schema.name, collectReferencedSchemaNames(schema))
}
const circular = new Set<string>()
for (const start of graph.keys()) {
const visited = new Set<string>()
const stack: Array<string> = [...(graph.get(start) ?? [])]
while (stack.length > 0) {
const node = stack.pop()!
if (node === start) {
circular.add(start)
break
}
if (visited.has(node)) continue
visited.add(node)
const next = graph.get(node)
if (next) for (const r of next) stack.push(r)
}
}
return circular
})
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
export function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET
return findCircularSchemasMemo(schemas)
}
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
export function containsCircularRef(
node: SchemaNode | undefined,
{ circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },
): boolean {
if (!node || circularSchemas.size === 0) return false
for (const _ of collectLazy<true>(node, {
schema(child) {
if (child.type !== 'ref') return null
const name = resolveRefName(child)
return name && name !== excludeName && circularSchemas.has(name) ? true : null
},
})) {
return true
}
return false
}
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
export function trimQuotes(text: string): string {
if (text.length >= 2) {
const first = text[0]
const last = text[text.length - 1]
if ((first === '"' && last === '"') || (first === "'" && last === "'") || (first === '`' && last === '`')) {
return text.slice(1, -1)
}
}
return text
}
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
export function stringify(value: string | number | boolean | undefined): string {
if (value === undefined || value === null) return "''"
const json = JSON.stringify(trimQuotes(value.toString()))
const inner = json.slice(1, -1).replace(/\\"/g, '"').replace(/'/g, "\\'")
return `'${inner}'`
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
export function jsStringEscape(input: unknown): string {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case '"':
case "'":
case '\\':
return `\\${character}`
case '\n':
return '\\n'
case '\r':
return '\\r'
case '\u2028':
return '\\u2028'
case '\u2029':
return '\\u2029'
default:
return ''
}
})
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
export function toRegExpString(text: string, func: string | null = 'RegExp'): string {
const raw = trimQuotes(text)
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i)
const replacementTarget = match?.[1] ?? ''
const matchedFlags = match?.[2]
const cleaned = raw
.replace(/^\\?\//, '')
.replace(/\\?\/$/, '')
.replace(replacementTarget, '')
const { source, flags } = new RegExp(cleaned, matchedFlags)
if (func === null) return `/${source}/${flags}`
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
export function stringifyObject(value: Record<string, unknown>): string {
const items = Object.entries(value)
.map(([key, val]) => {
if (val !== null && typeof val === 'object') {
return `${key}: {\n ${stringifyObject(val as Record<string, unknown>)}\n }`
}
return `${key}: ${val}`
})
.filter(Boolean)
return items.join(',\n')
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
export function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {
const parts = Array.isArray(param) ? param : param.split('.')
if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null
return `${accessor}?.['${`${parts.join("']?.['")}']`}`
}
+25
-28
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils = require("./utils-C8bWAzhv.cjs");
const require_factory = require("./factory-JN-Ylfl6.cjs");
exports.createArrowFunction = require_utils.createArrowFunction;
exports.createBreak = require_utils.createBreak;
exports.createConst = require_utils.createConst;
exports.createContent = require_utils.createContent;
exports.createDiscriminantNode = require_utils.createDiscriminantNode;
exports.createExport = require_utils.createExport;
const require_factory = require("./factory-BmcGBdeg.cjs");
exports.createArrowFunction = require_factory.createArrowFunction;
exports.createBreak = require_factory.createBreak;
exports.createConst = require_factory.createConst;
exports.createContent = require_factory.createContent;
exports.createExport = require_factory.createExport;
exports.createFile = require_factory.createFile;
exports.createFunction = require_utils.createFunction;
exports.createFunctionParameter = require_utils.createFunctionParameter;
exports.createFunctionParameters = require_utils.createFunctionParameters;
exports.createImport = require_utils.createImport;
exports.createIndexedAccessType = require_utils.createIndexedAccessType;
exports.createInput = require_utils.createInput;
exports.createJsx = require_utils.createJsx;
exports.createObjectBindingPattern = require_utils.createObjectBindingPattern;
exports.createOperation = require_utils.createOperation;
exports.createOperationParams = require_utils.createOperationParams;
exports.createOutput = require_utils.createOutput;
exports.createParameter = require_utils.createParameter;
exports.createProperty = require_utils.createProperty;
exports.createRequestBody = require_utils.createRequestBody;
exports.createResponse = require_utils.createResponse;
exports.createSchema = require_utils.createSchema;
exports.createSource = require_utils.createSource;
exports.createText = require_utils.createText;
exports.createType = require_utils.createType;
exports.createTypeLiteral = require_utils.createTypeLiteral;
exports.createFunction = require_factory.createFunction;
exports.createFunctionParameter = require_factory.createFunctionParameter;
exports.createFunctionParameters = require_factory.createFunctionParameters;
exports.createImport = require_factory.createImport;
exports.createIndexedAccessType = require_factory.createIndexedAccessType;
exports.createInput = require_factory.createInput;
exports.createJsx = require_factory.createJsx;
exports.createObjectBindingPattern = require_factory.createObjectBindingPattern;
exports.createOperation = require_factory.createOperation;
exports.createOutput = require_factory.createOutput;
exports.createParameter = require_factory.createParameter;
exports.createProperty = require_factory.createProperty;
exports.createRequestBody = require_factory.createRequestBody;
exports.createResponse = require_factory.createResponse;
exports.createSchema = require_factory.createSchema;
exports.createSource = require_factory.createSource;
exports.createText = require_factory.createText;
exports.createType = require_factory.createType;
exports.createTypeLiteral = require_factory.createTypeLiteral;
exports.update = require_factory.update;
import { n as __name, t as __exportAll } from "./chunk-CNktS9qV.js";
import { $t as createBreak, E as createOperation, J as createFunctionParameters, L as createParameter, Lt as createProperty, N as createRequestBody, Pt as createSchema, Qt as createArrowFunction, X as createObjectBindingPattern, Y as createIndexedAccessType, Z as createTypeLiteral, _ as createOutput, ct as createImport, en as createConst, gt as createContent, h as Node, in as createType, it as FileNode, k as createResponse, l as createDiscriminantNode, lt as createSource, nn as createJsx, q as createFunctionParameter, rn as createText, st as createExport, tn as createFunction, u as createOperationParams, x as createInput } from "./ast-ClnJg9BN.js";
import { F as createFunctionParameter, Gt as createBreak, I as createFunctionParameters, Jt as createJsx, K as FileNode, Kt as createConst, L as createIndexedAccessType, Ot as createProperty, R as createObjectBindingPattern, T as createParameter, Wt as createArrowFunction, X as createImport, Xt as createType, Y as createExport, Yt as createText, Z as createSource, at as createContent, g as createResponse, p as createOperation, qt as createFunction, r as createOutput, s as createInput, t as Node, wt as createSchema, x as createRequestBody, z as createTypeLiteral } from "./index-BzjwdK2M.js";
//#region src/factory.d.ts
declare namespace factory_d_exports {
export { UserFileNode, createArrowFunction, createBreak, createConst, createContent, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOperationParams, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, update };
export { UserFileNode, createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, update };
}

@@ -61,3 +61,3 @@ /**

//#endregion
export { UserFileNode, createArrowFunction, createBreak, createConst, createContent, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOperationParams, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, factory_d_exports as t, update };
export { UserFileNode, createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, factory_d_exports as t, update };
//# sourceMappingURL=factory.d.ts.map

@@ -1,3 +0,3 @@

import { B as createOperation, H as createRequestBody, I as createParameter, J as createSource, K as createExport, P as createResponse, R as createOutput, T as createOperationParams, W as createInput, _t as createFunctionParameter, at as createBreak, bt as createObjectBindingPattern, ct as createJsx, et as createContent, ht as createProperty, it as createArrowFunction, jt as createSchema, lt as createText, ot as createConst, q as createImport, st as createFunction, ut as createType, vt as createFunctionParameters, w as createDiscriminantNode, xt as createTypeLiteral, yt as createIndexedAccessType } from "./utils-DN4XLVqz.js";
import { r as update, t as createFile } from "./factory-C5gHvtLU.js";
export { createArrowFunction, createBreak, createConst, createContent, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOperationParams, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, update };
import { B as createText, C as createExport, F as createArrowFunction, I as createBreak, K as createProperty, L as createConst, R as createFunction, T as createSource, V as createType, Z as createSchema, _ as createTypeLiteral, a as createOutput, d as createInput, g as createObjectBindingPattern, h as createIndexedAccessType, j as createContent, l as createRequestBody, m as createFunctionParameters, p as createFunctionParameter, r as createParameter, s as createOperation, t as createResponse, w as createImport, z as createJsx } from "./response-DKxTr522.js";
import { r as update, t as createFile } from "./factory-Du7nEP4B.js";
export { createArrowFunction, createBreak, createConst, createContent, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOutput, createParameter, createProperty, createRequestBody, createResponse, createSchema, createSource, createText, createType, createTypeLiteral, update };
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils = require("./utils-C8bWAzhv.cjs");
const require_factory = require("./factory-JN-Ylfl6.cjs");
require("./casing-BE2R1RXg.cjs");
const require_factory = require("./factory-BmcGBdeg.cjs");
const require_utils = require("./utils-BCtRXfhI.cjs");
let node_crypto = require("node:crypto");
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value.
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* Scalar primitive schema types used for union simplification and type narrowing.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
/**
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
* (for example `string | number | boolean`).
*/
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* HTTP method identifiers used by operation nodes.
*/
const httpMethods = {
get: "GET",
post: "POST",
put: "PUT",
patch: "PATCH",
delete: "DELETE",
head: "HEAD",
options: "OPTIONS",
trace: "TRACE"
};
Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region src/signature.ts

@@ -231,5 +381,5 @@ /**

/**
* Node → digest cache, keyed by identity. A `WeakMap` so entries die with the node, and so a tree
* hashed during dedupe planning is not walked again when it is rewritten during streaming. Reuse
* is safe because a signature depends only on content, and nodes are immutable once created.
* Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so
* a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.
* Reuse is safe because a signature depends only on content, and nodes are immutable once created.
*/

@@ -258,2 +408,250 @@ const signatureCache = /* @__PURE__ */ new WeakMap();

//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
const nodeDefs = [
require_factory.inputDef,
require_factory.outputDef,
require_factory.operationDef,
require_factory.requestBodyDef,
require_factory.contentDef,
require_factory.responseDef,
require_factory.schemaDef,
require_factory.propertyDef,
require_factory.parameterDef,
require_factory.functionParameterDef,
require_factory.functionParametersDef,
require_factory.typeLiteralDef,
require_factory.indexedAccessTypeDef,
require_factory.objectBindingPatternDef,
require_factory.constDef,
require_factory.typeDef,
require_factory.functionDef,
require_factory.arrowFunctionDef,
require_factory.textDef,
require_factory.breakDef,
require_factory.jsxDef,
require_factory.importDef,
require_factory.exportDef,
require_factory.sourceDef,
require_factory.fileDef
];
//#endregion
//#region src/visitor.ts
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
if (rebuilt === node) return node;
const rebuild = nodeRebuilders[rebuilt.kind];
return rebuild ? rebuild(rebuilt) : rebuilt;
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
//#region src/dedupe.ts

@@ -265,3 +663,3 @@ /**

function createRefNode(node, canonical) {
return require_utils.createSchema({
return require_factory.createSchema({
type: "ref",

@@ -284,3 +682,3 @@ name: canonical.name,

const root = node;
return require_utils.transform(node, { schema(schemaNode) {
return transform(node, { schema(schemaNode) {
if (schemaNode.type === "ref") {

@@ -353,3 +751,3 @@ const target = schemaNode.ref ? require_utils.extractRefName(schemaNode.ref) : schemaNode.name;

if (root.kind === "Schema") topLevelNodes.add(root);
for (const schemaNode of require_utils.collectLazy(root, { schema: (node) => node })) record(schemaNode);
for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode);
}

@@ -510,9 +908,9 @@ const canonicalBySignature = /* @__PURE__ */ new Map();

if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
return require_utils.createSchema({
return require_factory.createSchema({
...objectNode,
properties: objectNode.properties.map((prop) => {
if (prop.name !== propertyName) return prop;
return require_utils.createProperty({
return require_factory.createProperty({
...prop,
schema: require_utils.createSchema({
schema: require_factory.createSchema({
type: "enum",

@@ -547,3 +945,3 @@ primitive: "string",

if (accObject && !accObject.name) {
acc = require_utils.createSchema({
acc = require_factory.createSchema({
...accObject,

@@ -573,3 +971,3 @@ properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]

function simplifyUnion(members) {
const scalarPrimitives = new Set(members.filter((member) => require_utils.isScalarPrimitive(member.type)).map((m) => m.type));
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;

@@ -601,14 +999,14 @@ return members.filter((member) => {

exports.applyDedupe = applyDedupe;
exports.arrowFunctionDef = require_utils.arrowFunctionDef;
exports.breakDef = require_utils.breakDef;
exports.arrowFunctionDef = require_factory.arrowFunctionDef;
exports.breakDef = require_factory.breakDef;
exports.buildDedupePlan = buildDedupePlan;
exports.collect = require_utils.collect;
exports.constDef = require_utils.constDef;
exports.contentDef = require_utils.contentDef;
exports.collect = collect;
exports.constDef = require_factory.constDef;
exports.contentDef = require_factory.contentDef;
exports.createPrinterFactory = createPrinterFactory;
exports.defineNode = require_utils.defineNode;
exports.defineNode = require_factory.defineNode;
exports.definePrinter = definePrinter;
exports.defineSchemaDialect = defineSchemaDialect;
exports.exportDef = require_utils.exportDef;
exports.extractStringsFromNodes = require_utils.extractStringsFromNodes;
exports.exportDef = require_factory.exportDef;
exports.extractStringsFromNodes = require_factory.extractStringsFromNodes;
Object.defineProperty(exports, "factory", {

@@ -620,23 +1018,24 @@ enumerable: true,

});
exports.fileDef = require_utils.fileDef;
exports.functionDef = require_utils.functionDef;
exports.functionParameterDef = require_utils.functionParameterDef;
exports.functionParametersDef = require_utils.functionParametersDef;
exports.httpMethods = require_utils.httpMethods;
exports.importDef = require_utils.importDef;
exports.indexedAccessTypeDef = require_utils.indexedAccessTypeDef;
exports.inputDef = require_utils.inputDef;
exports.fileDef = require_factory.fileDef;
exports.functionDef = require_factory.functionDef;
exports.functionParameterDef = require_factory.functionParameterDef;
exports.functionParametersDef = require_factory.functionParametersDef;
exports.httpMethods = httpMethods;
exports.importDef = require_factory.importDef;
exports.indexedAccessTypeDef = require_factory.indexedAccessTypeDef;
exports.inputDef = require_factory.inputDef;
exports.isHttpOperationNode = require_utils.isHttpOperationNode;
exports.jsxDef = require_utils.jsxDef;
exports.jsxDef = require_factory.jsxDef;
exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
exports.narrowSchema = require_utils.narrowSchema;
exports.objectBindingPatternDef = require_utils.objectBindingPatternDef;
exports.operationDef = require_utils.operationDef;
exports.outputDef = require_utils.outputDef;
exports.parameterDef = require_utils.parameterDef;
exports.propertyDef = require_utils.propertyDef;
exports.requestBodyDef = require_utils.requestBodyDef;
exports.responseDef = require_utils.responseDef;
exports.schemaDef = require_utils.schemaDef;
exports.schemaTypes = require_utils.schemaTypes;
exports.nodeDefs = nodeDefs;
exports.objectBindingPatternDef = require_factory.objectBindingPatternDef;
exports.operationDef = require_factory.operationDef;
exports.outputDef = require_factory.outputDef;
exports.parameterDef = require_factory.parameterDef;
exports.propertyDef = require_factory.propertyDef;
exports.requestBodyDef = require_factory.requestBodyDef;
exports.responseDef = require_factory.responseDef;
exports.schemaDef = require_factory.schemaDef;
exports.schemaTypes = schemaTypes;
exports.setDiscriminatorEnum = setDiscriminatorEnum;

@@ -646,10 +1045,10 @@ exports.setEnumName = setEnumName;

exports.simplifyUnion = simplifyUnion;
exports.sourceDef = require_utils.sourceDef;
exports.syncOptionality = require_utils.syncOptionality;
exports.textDef = require_utils.textDef;
exports.transform = require_utils.transform;
exports.typeDef = require_utils.typeDef;
exports.typeLiteralDef = require_utils.typeLiteralDef;
exports.walk = require_utils.walk;
exports.sourceDef = require_factory.sourceDef;
exports.syncOptionality = require_factory.syncOptionality;
exports.textDef = require_factory.textDef;
exports.transform = transform;
exports.typeDef = require_factory.typeDef;
exports.typeLiteralDef = require_factory.typeLiteralDef;
exports.walk = walk;
//# sourceMappingURL=index.cjs.map
import { n as __name } from "./chunk-CNktS9qV.js";
import { $ as functionParametersDef, A as responseDef, At as StringSchemaNode, B as FunctionParamNode, Bt as ParserOptions, C as HttpMethod, Ct as ObjectSchemaNode, D as operationDef, Dt as SchemaNode, Et as ScalarSchemaType, F as ParameterLocation, Ft as schemaDef, G as TypeExpression, Gt as JSDocNode, H as FunctionParametersNode, Ht as CodeNode, I as ParameterNode, It as PropertyNode, Jt as TypeNode, K as TypeLiteralNode, Kt as JsxNode, Mt as UnionSchemaNode, Nt as UrlSchemaNode, O as ResponseNode, Ot as SchemaNodeByType, P as requestBodyDef, Q as functionParameterDef, R as parameterDef, Rt as propertyDef, S as inputDef, St as NumberSchemaNode, T as OperationNode, Tt as RefSchemaNode, U as IndexedAccessTypeNode, Ut as ConstNode, V as FunctionParameterNode, Vt as ArrowFunctionNode, W as ObjectBindingPatternNode, Wt as FunctionNode, Xt as breakDef, Yt as arrowFunctionDef, Zt as constDef, _t as ArraySchemaNode, an as functionDef, at as ImportNode, b as InputNode, bt as EnumSchemaNode, cn as typeDef, dn as defineNode, dt as fileDef, et as indexedAccessTypeDef, fn as syncOptionality, ft as importDef, g as OutputNode, h as Node, ht as contentDef, it as FileNode, j as StatusCode, jt as TimeSchemaNode, kt as SchemaType, ln as DistributiveOmit, n as OperationParamsResolver, nt as typeLiteralDef, on as jsxDef, ot as SourceNode, pn as NodeKind, pt as sourceDef, qt as TextNode, rt as ExportNode, sn as textDef, tt as objectBindingPatternDef, un as NodeDef, ut as exportDef, v as outputDef, vt as DateSchemaNode, w as HttpOperationNode, wt as PrimitiveSchemaType, xt as IntersectionSchemaNode, y as InputMeta, yt as DatetimeSchemaNode, z as FunctionParamKind, zt as InferSchemaNode } from "./ast-ClnJg9BN.js";
import { $ as fileDef, $t as textDef, A as FunctionParametersNode, At as InferSchemaNode, B as functionParameterDef, Bt as TypeNode, C as ParameterLocation, Ct as UrlSchemaNode, D as FunctionParamKind, Dt as UserPropertyNode, E as parameterDef, Et as PropertyNode, Ft as ConstNode, G as ExportNode, H as indexedAccessTypeDef, Ht as breakDef, It as FunctionNode, J as SourceNode, K as FileNode, Lt as JSDocNode, M as ObjectBindingPatternNode, Mt as ArrowFunctionNode, N as TypeExpression, Nt as BreakNode, O as FunctionParamNode, P as TypeLiteralNode, Pt as CodeNode, Q as exportDef, Qt as jsxDef, Rt as JsxNode, S as requestBodyDef, St as UnionSchemaNode, Tt as schemaDef, U as objectBindingPatternDef, Ut as constDef, V as functionParametersDef, Vt as arrowFunctionDef, W as typeLiteralDef, Zt as functionDef, _ as responseDef, _t as SchemaNode, a as InputMeta, an as BaseNode, b as UserRequestBody, bt as StringSchemaNode, c as inputDef, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, en as typeDef, et as importDef, f as OperationNode, ft as ObjectSchemaNode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, i as outputDef, in as syncOptionality, it as contentDef, j as IndexedAccessTypeNode, jt as ParserOptions, k as FunctionParameterNode, kt as propertyDef, l as GenericOperationNode, lt as EnumSchemaNode, m as operationDef, mt as RefSchemaNode, n as OutputNode, nn as NodeDef, nt as ContentNode, o as InputNode, on as NodeKind, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as ImportNode, rn as defineNode, rt as UserContent, st as DateSchemaNode, t as Node, tn as DistributiveOmit, tt as sourceDef, u as HttpMethod, ut as IntersectionSchemaNode, v as StatusCode, vt as SchemaNodeByType, w as ParameterNode, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, zt as TextNode } from "./index-BzjwdK2M.js";
import { UserFileNode, t as factory_d_exports } from "./factory.js";
import { _ as applyDedupe, a as transform, b as schemaTypes, c as PrinterFactoryOptions, d as definePrinter, f as SchemaDialect, g as DedupePlan, h as DedupeLookups, i as collect, l as PrinterPartial, m as DedupeCanonical, n as Visitor, o as walk, p as defineSchemaDialect, r as VisitorContext, s as Printer, t as ParentOf, u as createPrinterFactory, v as buildDedupePlan, y as httpMethods } from "./types-CB2oY8Dw.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-Bn9cOos9.js";
import { _ as applyDedupe, a as definePrinter, b as schemaTypes, c as VisitorContext, d as walk, f as SchemaDialect, g as DedupePlan, h as DedupeLookups, i as createPrinterFactory, l as collect, m as DedupeCanonical, n as PrinterFactoryOptions, o as ParentOf, p as defineSchemaDialect, r as PrinterPartial, s as Visitor, t as Printer, u as transform, v as buildDedupePlan, y as httpMethods } from "./types-olVl9v5p.js";
import { n as OperationParamsResolver } from "./operationParams-BZ07xDm0.js";
import { a as simplifyUnion, i as setEnumName, n as mergeAdjacentObjectsLazy, r as setDiscriminatorEnum, t as extractStringsFromNodes } from "./extractStringsFromNodes-WMYJ8nQL.js";

@@ -19,3 +20,3 @@ //#region src/guards.d.ts

/**
* Narrows an `OperationNode` to an `HttpOperationNode`, guaranteeing `method` and `path`.
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*

@@ -31,2 +32,90 @@ * @example

//#endregion
//#region src/registry.d.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/
declare const nodeDefs: (NodeDef<ConstNode, Omit<ConstNode, "kind">> | NodeDef<TypeNode, Omit<TypeNode, "kind">> | NodeDef<FunctionNode, Omit<FunctionNode, "kind">> | NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">> | NodeDef<TextNode, string> | NodeDef<BreakNode, void> | NodeDef<JsxNode, string> | NodeDef<SchemaNode, (Omit<ObjectSchemaNode, "kind" | "properties" | "primitive"> & {
properties?: Array<PropertyNode>;
primitive?: "object";
}) | DistributiveOmit<ArraySchemaNode | UnionSchemaNode | IntersectionSchemaNode | EnumSchemaNode | RefSchemaNode | DatetimeSchemaNode | DateSchemaNode | TimeSchemaNode | StringSchemaNode | NumberSchemaNode | UrlSchemaNode | (BaseNode & {
kind: "Schema";
name?: string | null;
title?: string;
description?: string;
nullable?: boolean;
optional?: boolean;
nullish?: boolean;
deprecated?: boolean;
readOnly?: boolean;
writeOnly?: boolean;
default?: unknown;
example?: unknown;
primitive?: PrimitiveSchemaType;
format?: string;
} & {
type: "uuid" | "email";
min?: number;
max?: number;
}) | (BaseNode & {
kind: "Schema";
name?: string | null;
title?: string;
description?: string;
nullable?: boolean;
optional?: boolean;
nullish?: boolean;
deprecated?: boolean;
readOnly?: boolean;
writeOnly?: boolean;
default?: unknown;
example?: unknown;
primitive?: PrimitiveSchemaType;
format?: string;
} & {
type: "ipv4";
}) | (BaseNode & {
kind: "Schema";
name?: string | null;
title?: string;
description?: string;
nullable?: boolean;
optional?: boolean;
nullish?: boolean;
deprecated?: boolean;
readOnly?: boolean;
writeOnly?: boolean;
default?: unknown;
example?: unknown;
primitive?: PrimitiveSchemaType;
format?: string;
} & {
type: "ipv6";
}) | ScalarSchemaNode, "kind">> | NodeDef<TypeLiteralNode, Pick<TypeLiteralNode, "members">> | NodeDef<IndexedAccessTypeNode, Omit<IndexedAccessTypeNode, "kind">> | NodeDef<ObjectBindingPatternNode, Pick<ObjectBindingPatternNode, "elements">> | NodeDef<FunctionParameterNode, {
name: string;
type?: TypeExpression;
optional?: boolean;
default?: string;
rest?: boolean;
} | {
properties: Array<{
name: string;
type: TypeExpression;
optional?: boolean;
}>;
optional?: boolean;
default?: string;
}> | NodeDef<FunctionParametersNode, Partial<Omit<FunctionParametersNode, "kind">>> | NodeDef<ContentNode, UserContent> | NodeDef<ResponseNode, Pick<ResponseNode, "statusCode"> & Partial<Omit<ResponseNode, "kind" | "statusCode" | "content">> & {
content?: Array<UserContent>;
schema?: SchemaNode;
mediaType?: string | null;
keysToOmit?: Array<string> | null;
}> | NodeDef<InputNode<false>, Partial<Omit<InputNode<false>, "kind">>> | NodeDef<ImportNode, Omit<ImportNode, "kind">> | NodeDef<ExportNode, Omit<ExportNode, "kind">> | NodeDef<SourceNode, Omit<SourceNode, "kind">> | NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">> | NodeDef<ParameterNode, Pick<ParameterNode, "name" | "schema" | "in"> & Partial<Omit<ParameterNode, "kind" | "name" | "schema" | "in">>> | NodeDef<PropertyNode, UserPropertyNode> | NodeDef<RequestBodyNode, UserRequestBody> | NodeDef<OperationNode, {
[key: string]: unknown;
operationId: string;
method?: HttpOperationNode["method"];
path?: HttpOperationNode["path"];
requestBody?: UserRequestBody;
}> | NodeDef<OutputNode, Partial<Omit<OutputNode, "kind">>>)[];
//#endregion
//#region src/signature.d.ts

@@ -48,57 +137,3 @@ /**

//#endregion
//#region src/transformers.d.ts
/**
* Replaces a discriminator property's schema with a string enum of allowed values.
*
* If `node` is not an object schema, or if the property does not exist, the input
* node is returned as-is.
*
* @example
* ```ts
* const schema = createSchema({
* type: 'object',
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
* })
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
* ```
*/
declare function setDiscriminatorEnum({
node,
propertyName,
values,
enumName
}: {
node: SchemaNode;
propertyName: string;
values: Array<string>;
enumName?: string;
}): SchemaNode;
/**
* Merges adjacent anonymous object members into a single anonymous object member.
*
* @example
* ```ts
* const merged = mergeAdjacentObjects([
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
* ])
* ```
*/
declare function mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined>;
/**
* Removes enum members that are covered by broader scalar primitives in the same union.
*
* @example
* ```ts
* const simplified = simplifyUnion([
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
* createSchema({ type: 'string' }),
* ])
* // keeps only string member
* ```
*/
declare function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode>;
declare function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode;
//#endregion
export { type ArraySchemaNode, type ArrowFunctionNode, type CodeNode, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, type DedupeCanonical, type DedupeLookups, type DedupePlan, type DistributiveOmit, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type FunctionParamKind, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type HttpMethod, type HttpOperationNode, type ImportNode, type IndexedAccessTypeNode, type InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Node, type NodeDef, type NodeKind, type NumberSchemaNode, type ObjectBindingPatternNode, type ObjectSchemaNode, type OperationNode, type OperationParamsResolver, type OutputNode, type ParameterLocation, type ParameterNode, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type ResponseNode, type ScalarSchemaType, type SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeExpression, type TypeLiteralNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, applyDedupe, arrowFunctionDef, breakDef, buildDedupePlan, collect, constDef, contentDef, createPrinterFactory, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, factory_d_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, mergeAdjacentObjectsLazy, narrowSchema, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, setDiscriminatorEnum, setEnumName, signatureOf, simplifyUnion, sourceDef, syncOptionality, textDef, transform, typeDef, typeLiteralDef, walk };
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateSchemaNode, type DatetimeSchemaNode, type DedupeCanonical, type DedupeLookups, type DedupePlan, type DistributiveOmit, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type FunctionParamKind, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type IndexedAccessTypeNode, type InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Node, type NodeDef, type NodeKind, type NumberSchemaNode, type ObjectBindingPatternNode, type ObjectSchemaNode, type OperationNode, type OperationParamsResolver, type OutputNode, type ParameterLocation, type ParameterNode, 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 SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeExpression, type TypeLiteralNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, applyDedupe, arrowFunctionDef, breakDef, buildDedupePlan, collect, constDef, contentDef, createPrinterFactory, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, factory_d_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, mergeAdjacentObjectsLazy, narrowSchema, nodeDefs, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, setDiscriminatorEnum, setEnumName, signatureOf, simplifyUnion, sourceDef, syncOptionality, textDef, transform, typeDef, typeLiteralDef, walk };
//# sourceMappingURL=index.d.ts.map
import "./chunk-CNktS9qV.js";
import { $ as contentDef, A as collect, Ct as functionParametersDef, Dt as isHttpOperationNode, Et as typeLiteralDef, F as responseDef, Ft as httpMethods, G as inputDef, It as isScalarPrimitive, L as parameterDef, Lt as schemaTypes, M as transform, Mt as schemaDef, N as walk, Nt as defineNode, Ot as narrowSchema, Pt as syncOptionality, Q as sourceDef, St as functionParameterDef, Tt as objectBindingPatternDef, U as requestBodyDef, V as operationDef, X as fileDef, Y as exportDef, Z as importDef, a as enumPropName, dt as functionDef, ft as jsxDef, gt as propertyDef, ht as createProperty, j as collectLazy, jt as createSchema, kt as extractStringsFromNodes, mt as typeDef, nt as breakDef, o as extractRefName, pt as textDef, rt as constDef, tt as arrowFunctionDef, wt as indexedAccessTypeDef, z as outputDef } from "./utils-DN4XLVqz.js";
import { n as factory_exports } from "./factory-C5gHvtLU.js";
import { A as isHttpOperationNode, C as simplifyUnion, D as extractRefName, L as httpMethods, R as schemaTypes, S as setEnumName, b as mergeAdjacentObjectsLazy, d as transform, f as walk, j as narrowSchema, l as collect, p as nodeDefs, u as collectLazy, x as setDiscriminatorEnum } from "./utils-SdZU0F3H.js";
import { $ as defineNode, A as contentDef, D as fileDef, E as exportDef, G as typeDef, H as functionDef, J as extractStringsFromNodes, M as arrowFunctionDef, N as breakDef, O as importDef, P as constDef, Q as schemaDef, S as typeLiteralDef, U as jsxDef, W as textDef, Z as createSchema, b as indexedAccessTypeDef, c as operationDef, et as syncOptionality, f as inputDef, i as parameterDef, k as sourceDef, n as responseDef, o as outputDef, q as propertyDef, u as requestBodyDef, v as functionParameterDef, x as objectBindingPatternDef, y as functionParametersDef } from "./response-DKxTr522.js";
import { n as factory_exports } from "./factory-Du7nEP4B.js";
import { hash } from "node:crypto";

@@ -231,5 +232,5 @@ //#region src/signature.ts

/**
* Node → digest cache, keyed by identity. A `WeakMap` so entries die with the node, and so a tree
* hashed during dedupe planning is not walked again when it is rewritten during streaming. Reuse
* is safe because a signature depends only on content, and nodes are immutable once created.
* Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so
* a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.
* Reuse is safe because a signature depends only on content, and nodes are immutable once created.
*/

@@ -486,111 +487,4 @@ const signatureCache = /* @__PURE__ */ new WeakMap();

//#endregion
//#region src/transformers.ts
/**
* Replaces a discriminator property's schema with a string enum of allowed values.
*
* If `node` is not an object schema, or if the property does not exist, the input
* node is returned as-is.
*
* @example
* ```ts
* const schema = createSchema({
* type: 'object',
* properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],
* })
* const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })
* ```
*/
function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
const objectNode = narrowSchema(node, "object");
if (!objectNode?.properties?.length) return node;
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node;
return createSchema({
...objectNode,
properties: objectNode.properties.map((prop) => {
if (prop.name !== propertyName) return prop;
return createProperty({
...prop,
schema: createSchema({
type: "enum",
primitive: "string",
enumValues: values,
name: enumName,
readOnly: prop.schema.readOnly,
writeOnly: prop.schema.writeOnly
})
});
})
});
}
/**
* Merges adjacent anonymous object members into a single anonymous object member.
*
* @example
* ```ts
* const merged = mergeAdjacentObjects([
* createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),
* createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),
* ])
* ```
*/
function* mergeAdjacentObjectsLazy(members) {
let acc;
for (const member of members) {
const objectMember = narrowSchema(member, "object");
if (objectMember && !objectMember.name && acc !== void 0) {
const accObject = narrowSchema(acc, "object");
if (accObject && !accObject.name) {
acc = createSchema({
...accObject,
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
});
continue;
}
}
if (acc !== void 0) yield acc;
acc = member;
}
if (acc !== void 0) yield acc;
}
/**
* Removes enum members that are covered by broader scalar primitives in the same union.
*
* @example
* ```ts
* const simplified = simplifyUnion([
* createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),
* createSchema({ type: 'string' }),
* ])
* // keeps only string member
* ```
*/
function simplifyUnion(members) {
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
if (!scalarPrimitives.size) return members;
return members.filter((member) => {
const enumNode = narrowSchema(member, "enum");
if (!enumNode) return true;
const primitive = enumNode.primitive;
if (!primitive) return true;
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
if (scalarPrimitives.has(primitive)) return false;
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
return true;
});
}
function setEnumName(propNode, parentName, propName, enumSuffix) {
const enumNode = narrowSchema(propNode, "enum");
if (enumNode?.primitive === "boolean") return {
...propNode,
name: null
};
if (enumNode) return {
...propNode,
name: enumPropName(parentName, propName, enumSuffix)
};
return propNode;
}
//#endregion
export { applyDedupe, arrowFunctionDef, breakDef, buildDedupePlan, collect, constDef, contentDef, createPrinterFactory, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, factory_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, mergeAdjacentObjectsLazy, narrowSchema, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, setDiscriminatorEnum, setEnumName, signatureOf, simplifyUnion, sourceDef, syncOptionality, textDef, transform, typeDef, typeLiteralDef, walk };
export { applyDedupe, arrowFunctionDef, breakDef, buildDedupePlan, collect, constDef, contentDef, createPrinterFactory, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, factory_exports as factory, fileDef, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, jsxDef, mergeAdjacentObjectsLazy, narrowSchema, nodeDefs, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, setDiscriminatorEnum, setEnumName, signatureOf, simplifyUnion, sourceDef, syncOptionality, textDef, transform, typeDef, typeLiteralDef, walk };
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"index.js","names":["obj"],"sources":["../src/signature.ts","../src/dedupe.ts","../src/dialect.ts","../src/printer.ts","../src/transformers.ts"],"sourcesContent":["import { hash } from 'node:crypto'\nimport type { SchemaNode } from './nodes/index.ts'\nimport { extractRefName } from './utils/index.ts'\n\n/**\n * The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.\n */\nfunction flagsDescriptor(node: SchemaNode): string {\n return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`\n}\n\nfunction refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {\n if (node.ref) return extractRefName(node.ref)\n return node.name ?? ''\n}\n\ntype ScalarField = { kind: 'scalar'; key: string; prefix: string }\ntype BoolField = { kind: 'bool'; key: string; prefix: string }\ntype ChildField = { kind: 'child'; key: string; prefix: string }\ntype ChildrenField = { kind: 'children'; key: string; prefix: string }\ntype ObjectPropsField = { kind: 'objectProps' }\ntype AdditionalPropsField = { kind: 'additionalProps' }\ntype PatternPropsField = { kind: 'patternProps' }\ntype EnumValuesField = { kind: 'enumValues' }\ntype RefTargetField = { kind: 'refTarget' }\n\ntype ShapeField =\n | ScalarField\n | BoolField\n | ChildField\n | ChildrenField\n | ObjectPropsField\n | AdditionalPropsField\n | PatternPropsField\n | EnumValuesField\n | RefTargetField\n\nconst arrayTupleFields: ReadonlyArray<ShapeField> = [\n { kind: 'children', key: 'items', prefix: 'i' },\n { kind: 'child', key: 'rest', prefix: 'r' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'bool', key: 'unique', prefix: 'u' },\n]\n\nconst numericFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },\n { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },\n { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },\n]\n\nconst rangeFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n]\n\n/**\n * Maps each node `type` to its ordered shape-contributing fields. Types absent from the map\n * (boolean, null, any, and other scalars) fall back to `${type}|${flags}`.\n */\nconst SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {\n object: [\n { kind: 'objectProps' },\n { kind: 'additionalProps' },\n { kind: 'patternProps' },\n { kind: 'scalar', key: 'minProperties', prefix: 'mn' },\n { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },\n ],\n array: arrayTupleFields,\n tuple: arrayTupleFields,\n union: [\n { kind: 'scalar', key: 'strategy', prefix: 's' },\n { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },\n { kind: 'children', key: 'members', prefix: 'm' },\n ],\n intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],\n enum: [{ kind: 'enumValues' }],\n ref: [{ kind: 'refTarget' }],\n string: [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'pattern', prefix: 'pt' },\n ],\n number: numericFields,\n integer: numericFields,\n bigint: numericFields,\n url: [\n { kind: 'scalar', key: 'path', prefix: 'path' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n ],\n uuid: rangeFields,\n email: rangeFields,\n datetime: [\n { kind: 'bool', key: 'offset', prefix: 'o' },\n { kind: 'bool', key: 'local', prefix: 'l' },\n ],\n date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n}\n\nfunction serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {\n switch (field.kind) {\n case 'scalar':\n return `${field.prefix}:${record[field.key] ?? ''}`\n case 'bool':\n return `${field.prefix}:${record[field.key] ? 1 : 0}`\n case 'child': {\n const child = record[field.key] as SchemaNode | undefined\n return `${field.prefix}:${child ? signatureOf(child) : ''}`\n }\n case 'children': {\n const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []\n return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`\n }\n case 'objectProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')\n return `p[${props}]`\n }\n case 'additionalProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`\n if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`\n return ''\n }\n case 'patternProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const pattern = obj.patternProperties\n ? Object.keys(obj.patternProperties)\n .sort()\n .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)\n .join(',')\n : ''\n return `pp[${pattern}]`\n }\n case 'enumValues': {\n const en = node as Extract<SchemaNode, { type: 'enum' }>\n let values = ''\n if (en.namedEnumValues?.length) {\n values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')\n } else if (en.enumValues?.length) {\n values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')\n }\n return `v[${values}]`\n }\n case 'refTarget': {\n return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`\n }\n }\n}\n\n/**\n * Builds the local shape descriptor that {@link signatureOf} hashes: the node's kind, flags,\n * constraints, and its children's signatures.\n */\nfunction describeShape(node: SchemaNode): string {\n const flags = flagsDescriptor(node)\n const fields = SHAPE_KEYS[node.type]\n if (!fields) return `${node.type}|${flags}`\n\n const record = node as unknown as Record<string, unknown>\n const parts: Array<string> = [`${node.type}|${flags}`]\n for (const field of fields) {\n parts.push(serializeShapeField(field, node, record))\n }\n return parts.join('|')\n}\n\n/**\n * Node → digest cache, keyed by identity. A `WeakMap` so entries die with the node, and so a tree\n * hashed during dedupe planning is not walked again when it is rewritten during streaming. Reuse\n * is safe because a signature depends only on content, and nodes are immutable once created.\n */\nconst signatureCache = new WeakMap<SchemaNode, string>()\n\n/**\n * Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas\n * share a signature when they are structurally identical, ignoring documentation (`name`, `title`,\n * `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,\n * `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`\n * nodes compare by target name, which also terminates on circular shapes.\n *\n * @example Two enums with different descriptions share a signature\n * ```ts\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))\n * ```\n */\nexport function signatureOf(node: SchemaNode): string {\n const cached = signatureCache.get(node)\n if (cached !== undefined) return cached\n const signature = hash('sha256', describeShape(node), 'hex')\n signatureCache.set(node, signature)\n\n return signature\n}\n\n/**\n * Returns `true` when two schema nodes are structurally identical under shape-only equality,\n * meaning they produce the same TypeScript type.\n */\nexport function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {\n return signatureOf(a) === signatureOf(b)\n}\n","import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'\nimport { createSchema } from './nodes/schema.ts'\nimport { signatureOf } from './signature.ts'\nimport { extractRefName } from './utils/index.ts'\nimport { collectLazy, transform } from './visitor.ts'\n\n/**\n * A canonical destination for a deduplicated shape: the shared schema name and\n * the synthetic `$ref` path that points at it.\n */\nexport type DedupeCanonical = {\n /**\n * Canonical schema name every duplicate occurrence refers to.\n */\n name: string\n /**\n * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).\n */\n ref: string\n}\n\n/**\n * The result of {@link buildDedupePlan}: a lookup from structural signature to its\n * canonical target, plus the freshly hoisted definitions that must be added to\n * the schema list.\n */\nexport type DedupePlan = {\n /**\n * Maps a structural signature to the canonical schema that represents it.\n */\n canonicalBySignature: Map<string, DedupeCanonical>\n /**\n * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so\n * references to the duplicate can be repointed at the first schema with the same content.\n */\n aliasNames: Map<string, DedupeCanonical>\n /**\n * New top-level schema definitions created for inline shapes that had no existing\n * named component. Nested duplicates inside each definition are already collapsed.\n */\n hoisted: Array<SchemaNode>\n}\n\n/**\n * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.\n */\nexport type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>\n\n/**\n * Options that inject the naming and candidate policy into {@link buildDedupePlan}.\n * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.\n */\nexport type BuildDedupePlanOptions = {\n /**\n * Returns `true` when a node should be deduplicated. This is the only gate, so it must\n * reject both ineligible kinds (return `false` for anything other than, say, enums and\n * objects) and unsafe shapes (e.g. nodes that reference a circular schema).\n */\n isCandidate: (node: SchemaNode) => boolean\n /**\n * Produces the canonical name for an inline shape with no existing named component.\n * Return `null` to leave the shape inline (for example when no contextual name exists).\n */\n nameFor: (node: SchemaNode, signature: string) => string | null\n /**\n * Builds the `$ref` path for a canonical name.\n */\n refFor: (name: string) => string\n /**\n * Minimum number of occurrences before a shape is deduplicated.\n *\n * @default 2\n */\n minOccurrences?: number\n}\n\n/**\n * Builds the shared `ref` replacement for a duplicate occurrence, carrying the\n * usage-slot and documentation fields that are not part of the canonical type.\n */\nfunction createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {\n return createSchema({\n type: 'ref',\n name: canonical.name,\n ref: canonical.ref,\n optional: node.optional,\n nullish: node.nullish,\n readOnly: node.readOnly,\n writeOnly: node.writeOnly,\n deprecated: node.deprecated,\n description: node.description,\n default: node.default,\n example: node.example,\n })\n}\n\n/**\n * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical\n * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,\n * so nested duplicates inside a replaced shape are not visited again. A `ref` that points\n * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with\n * the same content.\n *\n * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not\n * turned into a reference to itself. Nested duplicates are still collapsed.\n *\n * @example\n * ```ts\n * const next = applyDedupe(operationNode, plan)\n * ```\n */\nexport function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode\nexport function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode\nexport function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {\n const { canonicalBySignature, aliasNames } = plan\n if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node\n\n const root = node\n\n return transform(node, {\n schema(schemaNode) {\n if (schemaNode.type === 'ref') {\n const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name\n const canonical = target ? aliasNames.get(target) : undefined\n\n return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined\n }\n\n const signature = signatureOf(schemaNode)\n if (skipRootMatch && schemaNode === root) return undefined\n\n const canonical = canonicalBySignature.get(signature)\n if (!canonical) return undefined\n\n return createRefNode(schemaNode, canonical)\n },\n })\n}\n\n/**\n * Strips usage-slot flags from a hoisted definition and applies its canonical name.\n * A standalone definition is never optional, so `optional`/`nullish` are cleared.\n */\nfunction cleanDefinition(node: SchemaNode, name: string): SchemaNode {\n return { ...node, name, optional: undefined, nullish: undefined }\n}\n\n/**\n * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.\n *\n * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence\n * is a named top-level schema, the first one becomes the canonical (so other top-level\n * duplicates and inline copies turn into references to it). Every other top-level name with\n * the same content is recorded in `aliasNames`, so refs to it can be repointed at the\n * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied\n * per node with {@link applyDedupe}.\n *\n * @example\n * ```ts\n * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {\n * isCandidate: (node) => node.type === 'enum' || node.type === 'object',\n * nameFor: (node) => node.name ?? null,\n * refFor: (name) => `#/components/schemas/${name}`,\n * })\n * ```\n */\nexport function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {\n const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options\n\n const topLevelNodes = new Set<SchemaNode>()\n\n type Group = {\n count: number\n representative: SchemaNode\n topLevelNames: Array<string>\n }\n const groups = new Map<string, Group>()\n\n function record(schemaNode: SchemaNode): void {\n const signature = signatureOf(schemaNode)\n if (!isCandidate(schemaNode)) return\n\n const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name\n const group = groups.get(signature)\n if (group) {\n group.count++\n if (isTopLevel) group.topLevelNames.push(schemaNode.name!)\n } else {\n groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })\n }\n }\n\n for (const root of roots) {\n if (root.kind === 'Schema') topLevelNodes.add(root)\n for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {\n record(schemaNode)\n }\n }\n\n const canonicalBySignature = new Map<string, DedupeCanonical>()\n const aliasNames = new Map<string, DedupeCanonical>()\n const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []\n\n for (const [signature, group] of groups) {\n if (group.count < minOccurrences) continue\n\n const [firstName, ...duplicateNames] = group.topLevelNames\n if (firstName) {\n const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }\n canonicalBySignature.set(signature, canonical)\n for (const duplicate of duplicateNames) {\n aliasNames.set(duplicate, canonical)\n }\n continue\n }\n\n const name = nameFor(group.representative, signature)\n if (!name) continue\n\n canonicalBySignature.set(signature, { name, ref: refFor(name) })\n pendingHoists.push({ name, representative: group.representative })\n }\n\n // Build hoisted definitions only after every canonical name is known, so nested\n // duplicates inside a definition also resolve to refs.\n const hoisted = pendingHoists.map(({ name, representative }) =>\n cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),\n )\n\n return { canonicalBySignature, aliasNames, hoisted }\n}\n","/**\n * The spec-specific questions a schema parser answers while turning a source document into Kubb\n * AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where\n * OpenAPI, AsyncAPI, and plain JSON Schema differ.\n */\nexport type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Identifies the dialect in logs and diagnostics.\n */\n name: string\n /**\n * Whether the schema is nullable.\n */\n isNullable: (schema?: TSchema) => boolean\n /**\n * Whether the value is a `$ref` pointer.\n */\n isReference: (value?: unknown) => value is TRef\n /**\n * Whether the schema carries a discriminator for polymorphism.\n */\n isDiscriminator: (value?: unknown) => value is TDiscriminated\n /**\n * Whether the schema is binary data, converted to a `blob` node.\n */\n isBinary: (schema: TSchema) => boolean\n /**\n * Resolves a local `$ref` against the document, or nullish when it cannot.\n */\n resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined\n}\n\n/**\n * Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the\n * dialect's type for inference.\n *\n * @example\n * ```ts\n * export const oasDialect = defineSchemaDialect({\n * name: 'oas',\n * isNullable,\n * isReference,\n * isDiscriminator,\n * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',\n * resolveRef,\n * })\n * ```\n */\nexport function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(\n dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>,\n): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument> {\n return dialect\n}\n","import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'\n\n/**\n * Runtime context passed as `this` to printer handlers.\n *\n * `this.transform` dispatches to node-level handlers from `nodes`.\n *\n * @example\n * ```ts\n * const context: PrinterHandlerContext<string, {}> = {\n * options: {},\n * transform: () => 'value',\n * }\n * ```\n */\ntype PrinterHandlerContext<TOutput, TOptions extends object> = {\n /**\n * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.\n * Use `this.transform` inside `nodes` handlers and inside the `print` override.\n */\n transform: (node: SchemaNode) => TOutput | null\n /**\n * Options for this printer instance.\n */\n options: TOptions\n}\n\n/**\n * Handler for one schema node type.\n *\n * Use a regular function (not an arrow function) if you need `this`.\n *\n * @example\n * ```ts\n * const handler: PrinterHandler<string, {}, 'string'> = function () {\n * return 'string'\n * }\n * ```\n */\ntype PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (\n this: PrinterHandlerContext<TOutput, TOptions>,\n node: SchemaNodeByType[T],\n) => TOutput | null\n\n/**\n * Partial map of per-node-type handler overrides for a printer.\n *\n * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).\n * Supply only the handlers you want to replace. The printer's built-in\n * defaults fill in the rest.\n *\n * @example\n * ```ts\n * pluginZod({\n * printer: {\n * nodes: {\n * date(): string {\n * return 'z.string().date()'\n * },\n * } satisfies PrinterPartial<string, PrinterZodOptions>,\n * },\n * })\n * ```\n */\nexport type PrinterPartial<TOutput, TOptions extends object> = Partial<{\n [K in SchemaType]: PrinterHandler<TOutput, TOptions, K>\n}>\n\n/**\n * Generic shape used by `definePrinter`.\n *\n * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)\n * - `TOptions` options passed to and stored on the printer instance\n * - `TOutput` the type emitted by node handlers\n * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)\n *\n * @example\n * ```ts\n * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>\n * ```\n */\nexport type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {\n name: TName\n options: TOptions\n output: TOutput\n printOutput: TPrintOutput\n}\n\n/**\n * Printer instance returned by a printer factory.\n *\n * @example\n * ```ts\n * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})\n * ```\n */\nexport type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {\n /**\n * Unique identifier supplied at creation time.\n */\n name: T['name']\n /**\n * Options for this printer instance.\n */\n options: T['options']\n /**\n * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.\n * Always dispatches through the `nodes` map. Never calls the `print` override.\n * Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.\n */\n transform: (node: SchemaNode) => T['output'] | null\n /**\n * Public printer. If the builder provides a root-level `print`, this calls that\n * higher-level function (which may produce full declarations).\n * Otherwise, falls back to the node-level dispatcher.\n */\n print: (node: SchemaNode) => T['printOutput'] | null\n}\n\n/**\n * Builder function passed to `definePrinter`.\n *\n * It receives resolved options and returns:\n * - `name`\n * - `options`\n * - `nodes` handlers\n * - optional top-level `print` override\n *\n * @example\n * ```ts\n * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })\n * ```\n */\ntype PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {\n name: T['name']\n /**\n * Options to store on the printer.\n */\n options: T['options']\n nodes: Partial<{\n [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>\n }>\n /**\n * Optional root-level print override. When provided, becomes the public `printer.print`.\n * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),\n * not the override itself, so recursion is safe.\n */\n print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null\n}\n/**\n * Defines a schema printer: a function that takes a `SchemaNode` and emits\n * code in your target language. Each plugin that produces code from schemas\n * (TypeScript types, Zod schemas, Faker factories) ships a printer built\n * with this helper.\n *\n * The builder receives resolved options and returns:\n *\n * - `name` unique identifier for the printer.\n * - `options` stored on the returned printer instance.\n * - `nodes` map of `SchemaType` → handler. Handlers return the rendered\n * output (a string, a TypeScript AST node, ...) for that schema type.\n * - `print` (optional), top-level override exposed as `printer.print`.\n * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.\n *\n * Without a `print` override, `printer.print` falls back to `printer.transform`\n * (the node-level dispatcher).\n *\n * @example Tiny Zod printer\n * ```ts\n * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = definePrinter<PrinterZod>((options) => ({\n * name: 'zod',\n * options: { strict: options.strict ?? true },\n * nodes: {\n * string: () => 'z.string()',\n * object(node) {\n * const props = node.properties\n * .map((p) => `${p.name}: ${this.transform(p.schema)}`)\n * .join(', ')\n * return `z.object({ ${props} })`\n * },\n * },\n * }))\n * ```\n */\nexport function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\n return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>\n}\n\n/**\n * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.\n *\n * @example\n * ```ts\n * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(\n * (node) => node.kind,\n * )\n * ```\n */\nexport function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {\n return function <T extends PrinterFactoryOptions>(\n build: (options: T['options']) => {\n name: T['name']\n options: T['options']\n nodes: Partial<{\n [K in TKey]: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNodeByKey[K],\n ) => T['output'] | null\n }>\n print?: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNode,\n ) => T['printOutput'] | null\n },\n ): (options?: T['options']) => {\n name: T['name']\n options: T['options']\n transform: (node: TNode) => T['output'] | null\n print: (node: TNode) => T['printOutput'] | null\n } {\n return (options) => {\n const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))\n\n const context = {\n options: resolvedOptions,\n transform: (node: TNode): T['output'] | null => {\n const key = getKey(node)\n if (key === null) return null\n\n const handler = nodes[key]\n\n if (!handler) return null\n\n return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)\n },\n }\n\n return {\n name,\n options: resolvedOptions,\n transform: context.transform,\n print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,\n }\n }\n }\n}\n","import { isScalarPrimitive } from './constants.ts'\nimport { narrowSchema } from './guards.ts'\nimport { createProperty } from './nodes/property.ts'\nimport { createSchema, type SchemaNode } from './nodes/schema.ts'\nimport { enumPropName } from './utils/index.ts'\n\n/**\n * Replaces a discriminator property's schema with a string enum of allowed values.\n *\n * If `node` is not an object schema, or if the property does not exist, the input\n * node is returned as-is.\n *\n * @example\n * ```ts\n * const schema = createSchema({\n * type: 'object',\n * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })],\n * })\n * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] })\n * ```\n */\nexport function setDiscriminatorEnum({\n node,\n propertyName,\n values,\n enumName,\n}: {\n node: SchemaNode\n propertyName: string\n values: Array<string>\n enumName?: string\n}): SchemaNode {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) {\n return node\n }\n\n const hasProperty = objectNode.properties.some((prop) => prop.name === propertyName)\n if (!hasProperty) {\n return node\n }\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) {\n return prop\n }\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n}\n\n/**\n * Merges adjacent anonymous object members into a single anonymous object member.\n *\n * @example\n * ```ts\n * const merged = mergeAdjacentObjects([\n * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }),\n * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }),\n * ])\n * ```\n */\nexport function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {\n let acc: SchemaNode | undefined\n\n for (const member of members) {\n const objectMember = narrowSchema(member, 'object')\n if (objectMember && !objectMember.name && acc !== undefined) {\n const accObject = narrowSchema(acc, 'object')\n if (accObject && !accObject.name) {\n acc = createSchema({\n ...accObject,\n properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],\n })\n continue\n }\n }\n if (acc !== undefined) yield acc\n acc = member\n }\n\n if (acc !== undefined) yield acc\n}\n\nexport function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {\n return [...mergeAdjacentObjectsLazy(members)]\n}\n\n/**\n * Removes enum members that are covered by broader scalar primitives in the same union.\n *\n * @example\n * ```ts\n * const simplified = simplifyUnion([\n * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }),\n * createSchema({ type: 'string' }),\n * ])\n * // keeps only string member\n * ```\n */\nexport function simplifyUnion(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n\n if (!scalarPrimitives.size) {\n return members\n }\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) {\n return true\n }\n\n const primitive = enumNode.primitive\n if (!primitive) {\n return true\n }\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) {\n return true\n }\n\n if (scalarPrimitives.has(primitive)) {\n return false\n }\n\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) {\n return false\n }\n\n return true\n })\n}\n\nexport function setEnumName(propNode: SchemaNode, parentName: string | null | undefined, propName: string, enumSuffix: string): SchemaNode {\n const enumNode = narrowSchema(propNode, 'enum')\n\n if (enumNode?.primitive === 'boolean') {\n return { ...propNode, name: null }\n }\n\n if (enumNode) {\n return {\n ...propNode,\n name: enumPropName(parentName, propName, enumSuffix),\n }\n }\n\n return propNode\n}\n"],"mappings":";;;;;;;;AAOA,SAAS,gBAAgB,MAA0B;CACjD,OAAO,GAAG,KAAK,aAAa,GAAG,GAAG,KAAK,UAAU,GAAG,GAAG,KAAK,WAAW,IAAI;AAC7E;AAEA,SAAS,cAAc,MAAoD;CACzE,IAAI,KAAK,KAAK,OAAO,eAAe,KAAK,GAAG;CAC5C,OAAO,KAAK,QAAQ;AACtB;AAuBA,MAAM,mBAA8C;CAClD;EAAE,MAAM;EAAY,KAAK;EAAS,QAAQ;CAAI;CAC9C;EAAE,MAAM;EAAS,KAAK;EAAQ,QAAQ;CAAI;CAC1C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAQ,KAAK;EAAU,QAAQ;CAAI;AAC7C;AAEA,MAAM,gBAA2C;CAC/C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAoB,QAAQ;CAAM;CACzD;EAAE,MAAM;EAAU,KAAK;EAAoB,QAAQ;CAAM;CACzD;EAAE,MAAM;EAAU,KAAK;EAAc,QAAQ;CAAK;AACpD;AAEA,MAAM,cAAyC,CAC7C;CAAE,MAAM;CAAU,KAAK;CAAO,QAAQ;AAAK,GAC3C;CAAE,MAAM;CAAU,KAAK;CAAO,QAAQ;AAAK,CAC7C;;;;;AAMA,MAAM,aAA6E;CACjF,QAAQ;EACN,EAAE,MAAM,cAAc;EACtB,EAAE,MAAM,kBAAkB;EAC1B,EAAE,MAAM,eAAe;EACvB;GAAE,MAAM;GAAU,KAAK;GAAiB,QAAQ;EAAK;EACrD;GAAE,MAAM;GAAU,KAAK;GAAiB,QAAQ;EAAK;CACvD;CACA,OAAO;CACP,OAAO;CACP,OAAO;EACL;GAAE,MAAM;GAAU,KAAK;GAAY,QAAQ;EAAI;EAC/C;GAAE,MAAM;GAAU,KAAK;GAA6B,QAAQ;EAAI;EAChE;GAAE,MAAM;GAAY,KAAK;GAAW,QAAQ;EAAI;CAClD;CACA,cAAc,CAAC;EAAE,MAAM;EAAY,KAAK;EAAW,QAAQ;CAAI,CAAC;CAChE,MAAM,CAAC,EAAE,MAAM,aAAa,CAAC;CAC7B,KAAK,CAAC,EAAE,MAAM,YAAY,CAAC;CAC3B,QAAQ;EACN;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;EAC3C;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;EAC3C;GAAE,MAAM;GAAU,KAAK;GAAW,QAAQ;EAAK;CACjD;CACA,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,KAAK;EACH;GAAE,MAAM;GAAU,KAAK;GAAQ,QAAQ;EAAO;EAC9C;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;EAC3C;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;CAC7C;CACA,MAAM;CACN,OAAO;CACP,UAAU,CACR;EAAE,MAAM;EAAQ,KAAK;EAAU,QAAQ;CAAI,GAC3C;EAAE,MAAM;EAAQ,KAAK;EAAS,QAAQ;CAAI,CAC5C;CACA,MAAM,CAAC;EAAE,MAAM;EAAU,KAAK;EAAkB,QAAQ;CAAM,CAAC;CAC/D,MAAM,CAAC;EAAE,MAAM;EAAU,KAAK;EAAkB,QAAQ;CAAM,CAAC;AACjE;AAEA,SAAS,oBAAoB,OAAmB,MAAkB,QAAyC;CACzG,QAAQ,MAAM,MAAd;EACE,KAAK,UACH,OAAO,GAAG,MAAM,OAAO,GAAG,OAAO,MAAM,QAAQ;EACjD,KAAK,QACH,OAAO,GAAG,MAAM,OAAO,GAAG,OAAO,MAAM,OAAO,IAAI;EACpD,KAAK,SAAS;GACZ,MAAM,QAAQ,OAAO,MAAM;GAC3B,OAAO,GAAG,MAAM,OAAO,GAAG,QAAQ,YAAY,KAAK,IAAI;EACzD;EACA,KAAK,YAAY;GACf,MAAM,WAAY,OAAO,MAAM,QAA0C,CAAC;GAC1E,OAAO,GAAG,MAAM,OAAO,GAAG,SAAS,KAAK,MAAM,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;EAC1E;EACA,KAAK,eAGH,OAAO,MADQA,KAAI,cAAc,CAAC,EAAA,CAAG,KAAK,SAAS,GAAG,KAAK,OAAO,KAAK,WAAW,MAAM,MAAM,YAAY,KAAK,MAAM,GAAG,CAAC,CAAC,KAAK,GAC/G,EAAE;EAEpB,KAAK,mBAAmB;GACtB,MAAM,MAAM;GACZ,IAAI,OAAO,IAAI,yBAAyB,WAAW,OAAO,MAAM,IAAI;GACpE,IAAI,IAAI,sBAAsB,OAAO,MAAM,YAAY,IAAI,oBAAoB;GAC/E,OAAO;EACT;EACA,KAAK,gBAAgB;GACnB,MAAM,MAAM;GAOZ,OAAO,MANS,IAAI,oBAChB,OAAO,KAAK,IAAI,iBAAiB,CAAC,CAC/B,KAAK,CAAC,CACN,KAAK,QAAQ,GAAG,IAAI,GAAG,YAAY,IAAI,kBAAmB,IAAK,GAAG,CAAC,CACnE,KAAK,GAAG,IACX,GACiB;EACvB;EACA,KAAK,cAAc;GACjB,MAAM,KAAK;GACX,IAAI,SAAS;GACb,IAAI,GAAG,iBAAiB,QACtB,SAAS,GAAG,gBAAgB,KAAK,UAAU,GAAG,MAAM,KAAK,GAAG,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG;QACzG,IAAI,GAAG,YAAY,QACxB,SAAS,GAAG,WAAW,KAAK,UAAU,GAAG,UAAU,OAAO,SAAS,OAAO,MAAM,GAAG,OAAO,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG;GAE9G,OAAO,KAAK,OAAO;EACrB;EACA,KAAK,aACH,OAAO,KAAK,cAAc,IAA4C;CAE1E;AACF;;;;;AAMA,SAAS,cAAc,MAA0B;CAC/C,MAAM,QAAQ,gBAAgB,IAAI;CAClC,MAAM,SAAS,WAAW,KAAK;CAC/B,IAAI,CAAC,QAAQ,OAAO,GAAG,KAAK,KAAK,GAAG;CAEpC,MAAM,SAAS;CACf,MAAM,QAAuB,CAAC,GAAG,KAAK,KAAK,GAAG,OAAO;CACrD,KAAK,MAAM,SAAS,QAClB,MAAM,KAAK,oBAAoB,OAAO,MAAM,MAAM,CAAC;CAErD,OAAO,MAAM,KAAK,GAAG;AACvB;;;;;;AAOA,MAAM,iCAAiB,IAAI,QAA4B;;;;;;;;;;;;;;AAevD,SAAgB,YAAY,MAA0B;CACpD,MAAM,SAAS,eAAe,IAAI,IAAI;CACtC,IAAI,WAAW,KAAA,GAAW,OAAO;CACjC,MAAM,YAAY,KAAK,UAAU,cAAc,IAAI,GAAG,KAAK;CAC3D,eAAe,IAAI,MAAM,SAAS;CAElC,OAAO;AACT;;;;;;;ACtHA,SAAS,cAAc,MAAkB,WAAwC;CAC/E,OAAO,aAAa;EAClB,MAAM;EACN,MAAM,UAAU;EAChB,KAAK,UAAU;EACf,UAAU,KAAK;EACf,SAAS,KAAK;EACd,UAAU,KAAK;EACf,WAAW,KAAK;EAChB,YAAY,KAAK;EACjB,aAAa,KAAK;EAClB,SAAS,KAAK;EACd,SAAS,KAAK;CAChB,CAAC;AACH;AAmBA,SAAgB,YAAY,MAAY,MAAqB,gBAAgB,OAAa;CACxF,MAAM,EAAE,sBAAsB,eAAe;CAC7C,IAAI,qBAAqB,SAAS,KAAK,WAAW,SAAS,GAAG,OAAO;CAErE,MAAM,OAAO;CAEb,OAAO,UAAU,MAAM,EACrB,OAAO,YAAY;EACjB,IAAI,WAAW,SAAS,OAAO;GAC7B,MAAM,SAAS,WAAW,MAAM,eAAe,WAAW,GAAG,IAAI,WAAW;GAC5E,MAAM,YAAY,SAAS,WAAW,IAAI,MAAM,IAAI,KAAA;GAEpD,OAAO,YAAY;IAAE,GAAG;IAAY,MAAM,UAAU;IAAM,KAAK,UAAU;GAAI,IAAI,KAAA;EACnF;EAEA,MAAM,YAAY,YAAY,UAAU;EACxC,IAAI,iBAAiB,eAAe,MAAM,OAAO,KAAA;EAEjD,MAAM,YAAY,qBAAqB,IAAI,SAAS;EACpD,IAAI,CAAC,WAAW,OAAO,KAAA;EAEvB,OAAO,cAAc,YAAY,SAAS;CAC5C,EACF,CAAC;AACH;;;;;AAMA,SAAS,gBAAgB,MAAkB,MAA0B;CACnE,OAAO;EAAE,GAAG;EAAM;EAAM,UAAU,KAAA;EAAW,SAAS,KAAA;CAAU;AAClE;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,gBAAgB,OAA4B,SAA6C;CACvG,MAAM,EAAE,aAAa,SAAS,QAAQ,iBAAiB,MAAM;CAE7D,MAAM,gCAAgB,IAAI,IAAgB;CAO1C,MAAM,yBAAS,IAAI,IAAmB;CAEtC,SAAS,OAAO,YAA8B;EAC5C,MAAM,YAAY,YAAY,UAAU;EACxC,IAAI,CAAC,YAAY,UAAU,GAAG;EAE9B,MAAM,aAAa,cAAc,IAAI,UAAU,KAAK,CAAC,CAAC,WAAW;EACjE,MAAM,QAAQ,OAAO,IAAI,SAAS;EAClC,IAAI,OAAO;GACT,MAAM;GACN,IAAI,YAAY,MAAM,cAAc,KAAK,WAAW,IAAK;EAC3D,OACE,OAAO,IAAI,WAAW;GAAE,OAAO;GAAG,gBAAgB;GAAY,eAAe,aAAa,CAAC,WAAW,IAAK,IAAI,CAAC;EAAE,CAAC;CAEvH;CAEA,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,KAAK,SAAS,UAAU,cAAc,IAAI,IAAI;EAClD,KAAK,MAAM,cAAc,YAAwB,MAAM,EAAE,SAAS,SAAS,KAAK,CAAC,GAC/E,OAAO,UAAU;CAErB;CAEA,MAAM,uCAAuB,IAAI,IAA6B;CAC9D,MAAM,6BAAa,IAAI,IAA6B;CACpD,MAAM,gBAAqE,CAAC;CAE5E,KAAK,MAAM,CAAC,WAAW,UAAU,QAAQ;EACvC,IAAI,MAAM,QAAQ,gBAAgB;EAElC,MAAM,CAAC,WAAW,GAAG,kBAAkB,MAAM;EAC7C,IAAI,WAAW;GACb,MAAM,YAA6B;IAAE,MAAM;IAAW,KAAK,OAAO,SAAS;GAAE;GAC7E,qBAAqB,IAAI,WAAW,SAAS;GAC7C,KAAK,MAAM,aAAa,gBACtB,WAAW,IAAI,WAAW,SAAS;GAErC;EACF;EAEA,MAAM,OAAO,QAAQ,MAAM,gBAAgB,SAAS;EACpD,IAAI,CAAC,MAAM;EAEX,qBAAqB,IAAI,WAAW;GAAE;GAAM,KAAK,OAAO,IAAI;EAAE,CAAC;EAC/D,cAAc,KAAK;GAAE;GAAM,gBAAgB,MAAM;EAAe,CAAC;CACnE;CAQA,OAAO;EAAE;EAAsB;EAAY,SAJ3B,cAAc,KAAK,EAAE,MAAM,qBACzC,gBAAgB,YAAY,gBAAgB;GAAE;GAAsB;EAAW,GAAG,IAAI,GAAG,IAAI,CAG9C;CAAE;AACrD;;;;;;;;;;;;;;;;;;;ACtLA,SAAgB,oBACd,SACyD;CACzD,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACwIA,SAAgB,cAAuE,OAAkE;CACvJ,OAAO,sBAAgE,SAAS,KAAK,IAAI,CAAC,CAAC,KAAK;AAClG;;;;;;;;;;;AAYA,SAAgB,qBAAkG,QAAsC;CACtJ,OAAO,SACL,OAyBA;EACA,QAAQ,YAAY;GAClB,MAAM,EAAE,MAAM,SAAS,iBAAiB,OAAO,OAAO,kBAAkB,MAAM,WAAY,CAAC,CAAkB;GAE7G,MAAM,UAAU;IACd,SAAS;IACT,YAAY,SAAoC;KAC9C,MAAM,MAAM,OAAO,IAAI;KACvB,IAAI,QAAQ,MAAM,OAAO;KAEzB,MAAM,UAAU,MAAM;KAEtB,IAAI,CAAC,SAAS,OAAO;KAErB,OAAQ,QAAsE,KAAK,SAAS,IAAI;IAClG;GACF;GAEA,OAAO;IACL;IACA,SAAS;IACT,WAAW,QAAQ;IACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;GAChE;EACF;CACF;AACF;;;;;;;;;;;;;;;;;;AC1OA,SAAgB,qBAAqB,EACnC,MACA,cACA,QACA,YAMa;CACb,MAAM,aAAa,aAAa,MAAM,QAAQ;CAC9C,IAAI,CAAC,YAAY,YAAY,QAC3B,OAAO;CAIT,IAAI,CADgB,WAAW,WAAW,MAAM,SAAS,KAAK,SAAS,YACxD,GACb,OAAO;CAGT,OAAO,aAAa;EAClB,GAAG;EACH,YAAY,WAAW,WAAW,KAAK,SAAS;GAC9C,IAAI,KAAK,SAAS,cAChB,OAAO;GAGT,OAAO,eAAe;IACpB,GAAG;IACH,QAAQ,aAAa;KACnB,MAAM;KACN,WAAW;KACX,YAAY;KACZ,MAAM;KACN,UAAU,KAAK,OAAO;KACtB,WAAW,KAAK,OAAO;IACzB,CAAC;GACH,CAAC;EACH,CAAC;CACH,CAAC;AACH;;;;;;;;;;;;AAaA,UAAiB,yBAAyB,SAAuE;CAC/G,IAAI;CAEJ,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,eAAe,aAAa,QAAQ,QAAQ;EAClD,IAAI,gBAAgB,CAAC,aAAa,QAAQ,QAAQ,KAAA,GAAW;GAC3D,MAAM,YAAY,aAAa,KAAK,QAAQ;GAC5C,IAAI,aAAa,CAAC,UAAU,MAAM;IAChC,MAAM,aAAa;KACjB,GAAG;KACH,YAAY,CAAC,GAAI,UAAU,cAAc,CAAC,GAAI,GAAI,aAAa,cAAc,CAAC,CAAE;IAClF,CAAC;IACD;GACF;EACF;EACA,IAAI,QAAQ,KAAA,GAAW,MAAM;EAC7B,MAAM;CACR;CAEA,IAAI,QAAQ,KAAA,GAAW,MAAM;AAC/B;;;;;;;;;;;;;AAkBA,SAAgB,cAAc,SAA+C;CAC3E,MAAM,mBAAmB,IAAI,IAAI,QAAQ,QAAQ,WAAW,kBAAkB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,IAAI,CAAC;CAE9G,IAAI,CAAC,iBAAiB,MACpB,OAAO;CAGT,OAAO,QAAQ,QAAQ,WAAW;EAChC,MAAM,WAAW,aAAa,QAAQ,MAAM;EAC5C,IAAI,CAAC,UACH,OAAO;EAGT,MAAM,YAAY,SAAS;EAC3B,IAAI,CAAC,WACH,OAAO;EAIT,KADuB,SAAS,iBAAiB,UAAU,SAAS,YAAY,UAAU,MACpE,GACpB,OAAO;EAGT,IAAI,iBAAiB,IAAI,SAAS,GAChC,OAAO;EAGT,KAAK,cAAc,aAAa,cAAc,cAAc,iBAAiB,IAAI,SAAS,KAAK,iBAAiB,IAAI,QAAQ,IAC1H,OAAO;EAGT,OAAO;CACT,CAAC;AACH;AAEA,SAAgB,YAAY,UAAsB,YAAuC,UAAkB,YAAgC;CACzI,MAAM,WAAW,aAAa,UAAU,MAAM;CAE9C,IAAI,UAAU,cAAc,WAC1B,OAAO;EAAE,GAAG;EAAU,MAAM;CAAK;CAGnC,IAAI,UACF,OAAO;EACL,GAAG;EACH,MAAM,aAAa,YAAY,UAAU,UAAU;CACrD;CAGF,OAAO;AACT"}
{"version":3,"file":"index.js","names":["obj"],"sources":["../src/signature.ts","../src/dedupe.ts","../src/dialect.ts","../src/printer.ts"],"sourcesContent":["import { hash } from 'node:crypto'\nimport type { SchemaNode } from './nodes/index.ts'\nimport { extractRefName } from './utils/index.ts'\n\n/**\n * The flags shared by every node kind that affect its type: `primitive`, `format`, `nullable`.\n */\nfunction flagsDescriptor(node: SchemaNode): string {\n return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`\n}\n\nfunction refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {\n if (node.ref) return extractRefName(node.ref)\n return node.name ?? ''\n}\n\ntype ScalarField = { kind: 'scalar'; key: string; prefix: string }\ntype BoolField = { kind: 'bool'; key: string; prefix: string }\ntype ChildField = { kind: 'child'; key: string; prefix: string }\ntype ChildrenField = { kind: 'children'; key: string; prefix: string }\ntype ObjectPropsField = { kind: 'objectProps' }\ntype AdditionalPropsField = { kind: 'additionalProps' }\ntype PatternPropsField = { kind: 'patternProps' }\ntype EnumValuesField = { kind: 'enumValues' }\ntype RefTargetField = { kind: 'refTarget' }\n\ntype ShapeField =\n | ScalarField\n | BoolField\n | ChildField\n | ChildrenField\n | ObjectPropsField\n | AdditionalPropsField\n | PatternPropsField\n | EnumValuesField\n | RefTargetField\n\nconst arrayTupleFields: ReadonlyArray<ShapeField> = [\n { kind: 'children', key: 'items', prefix: 'i' },\n { kind: 'child', key: 'rest', prefix: 'r' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'bool', key: 'unique', prefix: 'u' },\n]\n\nconst numericFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },\n { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },\n { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },\n]\n\nconst rangeFields: ReadonlyArray<ShapeField> = [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n]\n\n/**\n * Maps each node `type` to its ordered shape-contributing fields. Types absent from the map\n * (boolean, null, any, and other scalars) fall back to `${type}|${flags}`.\n */\nconst SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {\n object: [\n { kind: 'objectProps' },\n { kind: 'additionalProps' },\n { kind: 'patternProps' },\n { kind: 'scalar', key: 'minProperties', prefix: 'mn' },\n { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },\n ],\n array: arrayTupleFields,\n tuple: arrayTupleFields,\n union: [\n { kind: 'scalar', key: 'strategy', prefix: 's' },\n { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },\n { kind: 'children', key: 'members', prefix: 'm' },\n ],\n intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],\n enum: [{ kind: 'enumValues' }],\n ref: [{ kind: 'refTarget' }],\n string: [\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n { kind: 'scalar', key: 'pattern', prefix: 'pt' },\n ],\n number: numericFields,\n integer: numericFields,\n bigint: numericFields,\n url: [\n { kind: 'scalar', key: 'path', prefix: 'path' },\n { kind: 'scalar', key: 'min', prefix: 'mn' },\n { kind: 'scalar', key: 'max', prefix: 'mx' },\n ],\n uuid: rangeFields,\n email: rangeFields,\n datetime: [\n { kind: 'bool', key: 'offset', prefix: 'o' },\n { kind: 'bool', key: 'local', prefix: 'l' },\n ],\n date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],\n}\n\nfunction serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {\n switch (field.kind) {\n case 'scalar':\n return `${field.prefix}:${record[field.key] ?? ''}`\n case 'bool':\n return `${field.prefix}:${record[field.key] ? 1 : 0}`\n case 'child': {\n const child = record[field.key] as SchemaNode | undefined\n return `${field.prefix}:${child ? signatureOf(child) : ''}`\n }\n case 'children': {\n const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []\n return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`\n }\n case 'objectProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')\n return `p[${props}]`\n }\n case 'additionalProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`\n if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`\n return ''\n }\n case 'patternProps': {\n const obj = node as Extract<SchemaNode, { type: 'object' }>\n const pattern = obj.patternProperties\n ? Object.keys(obj.patternProperties)\n .sort()\n .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)\n .join(',')\n : ''\n return `pp[${pattern}]`\n }\n case 'enumValues': {\n const en = node as Extract<SchemaNode, { type: 'enum' }>\n let values = ''\n if (en.namedEnumValues?.length) {\n values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')\n } else if (en.enumValues?.length) {\n values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')\n }\n return `v[${values}]`\n }\n case 'refTarget': {\n return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`\n }\n }\n}\n\n/**\n * Builds the local shape descriptor that {@link signatureOf} hashes: the node's kind, flags,\n * constraints, and its children's signatures.\n */\nfunction describeShape(node: SchemaNode): string {\n const flags = flagsDescriptor(node)\n const fields = SHAPE_KEYS[node.type]\n if (!fields) return `${node.type}|${flags}`\n\n const record = node as unknown as Record<string, unknown>\n const parts: Array<string> = [`${node.type}|${flags}`]\n for (const field of fields) {\n parts.push(serializeShapeField(field, node, record))\n }\n return parts.join('|')\n}\n\n/**\n * Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so\n * a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.\n * Reuse is safe because a signature depends only on content, and nodes are immutable once created.\n */\nconst signatureCache = new WeakMap<SchemaNode, string>()\n\n/**\n * Computes a deterministic, shape-only signature (a content hash) for a schema node. Two schemas\n * share a signature when they are structurally identical, ignoring documentation (`name`, `title`,\n * `description`, `example`, `default`, `deprecated`) and usage-slot flags (`optional`, `nullish`,\n * `readOnly`, `writeOnly`). `nullable` is kept because it changes the produced type, and `ref`\n * nodes compare by target name, which also terminates on circular shapes.\n *\n * @example Two enums with different descriptions share a signature\n * ```ts\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===\n * signatureOf(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))\n * ```\n */\nexport function signatureOf(node: SchemaNode): string {\n const cached = signatureCache.get(node)\n if (cached !== undefined) return cached\n const signature = hash('sha256', describeShape(node), 'hex')\n signatureCache.set(node, signature)\n\n return signature\n}\n\n/**\n * Returns `true` when two schema nodes are structurally identical under shape-only equality,\n * meaning they produce the same TypeScript type.\n */\nexport function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {\n return signatureOf(a) === signatureOf(b)\n}\n","/**\n * Schema-shape deduplication. `buildDedupePlan` finds top-level and inline schemas that share a\n * structural signature, picks one canonical definition, and `applyDedupe` repoints every duplicate\n * at it. This works on `SchemaNode` content, not on files.\n *\n * For merging a file's imports, exports, and source nodes, see `utils/fileMerge.ts`. Same idea of\n * collapsing duplicates, but a different domain.\n */\nimport type { Node, OperationNode, SchemaNode } from './nodes/index.ts'\nimport { createSchema } from './nodes/schema.ts'\nimport { signatureOf } from './signature.ts'\nimport { extractRefName } from './utils/index.ts'\nimport { collectLazy, transform } from './visitor.ts'\n\n/**\n * A canonical destination for a deduplicated shape: the shared schema name and\n * the synthetic `$ref` path that points at it.\n */\nexport type DedupeCanonical = {\n /**\n * Canonical schema name every duplicate occurrence refers to.\n */\n name: string\n /**\n * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).\n */\n ref: string\n}\n\n/**\n * The result of {@link buildDedupePlan}: a lookup from structural signature to its\n * canonical target, plus the freshly hoisted definitions that must be added to\n * the schema list.\n */\nexport type DedupePlan = {\n /**\n * Maps a structural signature to the canonical schema that represents it.\n */\n canonicalBySignature: Map<string, DedupeCanonical>\n /**\n * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so\n * references to the duplicate can be repointed at the first schema with the same content.\n */\n aliasNames: Map<string, DedupeCanonical>\n /**\n * New top-level schema definitions created for inline shapes that had no existing\n * named component. Nested duplicates inside each definition are already collapsed.\n */\n hoisted: Array<SchemaNode>\n}\n\n/**\n * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.\n */\nexport type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>\n\n/**\n * Options that inject the naming and candidate policy into {@link buildDedupePlan}.\n * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.\n */\nexport type BuildDedupePlanOptions = {\n /**\n * Returns `true` when a node should be deduplicated. This is the only gate, so it must\n * reject both ineligible kinds (return `false` for anything other than, say, enums and\n * objects) and unsafe shapes (e.g. nodes that reference a circular schema).\n */\n isCandidate: (node: SchemaNode) => boolean\n /**\n * Produces the canonical name for an inline shape with no existing named component.\n * Return `null` to leave the shape inline (for example when no contextual name exists).\n */\n nameFor: (node: SchemaNode, signature: string) => string | null\n /**\n * Builds the `$ref` path for a canonical name.\n */\n refFor: (name: string) => string\n /**\n * Minimum number of occurrences before a shape is deduplicated.\n *\n * @default 2\n */\n minOccurrences?: number\n}\n\n/**\n * Builds the shared `ref` replacement for a duplicate occurrence, carrying the\n * usage-slot and documentation fields that are not part of the canonical type.\n */\nfunction createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {\n return createSchema({\n type: 'ref',\n name: canonical.name,\n ref: canonical.ref,\n optional: node.optional,\n nullish: node.nullish,\n readOnly: node.readOnly,\n writeOnly: node.writeOnly,\n deprecated: node.deprecated,\n description: node.description,\n default: node.default,\n example: node.example,\n })\n}\n\n/**\n * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical\n * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,\n * so nested duplicates inside a replaced shape are not visited again. A `ref` that points\n * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with\n * the same content.\n *\n * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not\n * turned into a reference to itself. Nested duplicates are still collapsed.\n *\n * @example\n * ```ts\n * const next = applyDedupe(operationNode, plan)\n * ```\n */\nexport function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode\nexport function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode\nexport function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {\n const { canonicalBySignature, aliasNames } = plan\n if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node\n\n const root = node\n\n return transform(node, {\n schema(schemaNode) {\n if (schemaNode.type === 'ref') {\n const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name\n const canonical = target ? aliasNames.get(target) : undefined\n\n return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined\n }\n\n const signature = signatureOf(schemaNode)\n if (skipRootMatch && schemaNode === root) return undefined\n\n const canonical = canonicalBySignature.get(signature)\n if (!canonical) return undefined\n\n return createRefNode(schemaNode, canonical)\n },\n })\n}\n\n/**\n * Strips usage-slot flags from a hoisted definition and applies its canonical name.\n * A standalone definition is never optional, so `optional`/`nullish` are cleared.\n */\nfunction cleanDefinition(node: SchemaNode, name: string): SchemaNode {\n return { ...node, name, optional: undefined, nullish: undefined }\n}\n\n/**\n * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.\n *\n * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence\n * is a named top-level schema, the first one becomes the canonical (so other top-level\n * duplicates and inline copies turn into references to it). Every other top-level name with\n * the same content is recorded in `aliasNames`, so refs to it can be repointed at the\n * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied\n * per node with {@link applyDedupe}.\n *\n * @example\n * ```ts\n * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {\n * isCandidate: (node) => node.type === 'enum' || node.type === 'object',\n * nameFor: (node) => node.name ?? null,\n * refFor: (name) => `#/components/schemas/${name}`,\n * })\n * ```\n */\nexport function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {\n const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options\n\n const topLevelNodes = new Set<SchemaNode>()\n\n type Group = {\n count: number\n representative: SchemaNode\n topLevelNames: Array<string>\n }\n const groups = new Map<string, Group>()\n\n function record(schemaNode: SchemaNode): void {\n const signature = signatureOf(schemaNode)\n if (!isCandidate(schemaNode)) return\n\n const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name\n const group = groups.get(signature)\n if (group) {\n group.count++\n if (isTopLevel) group.topLevelNames.push(schemaNode.name!)\n } else {\n groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })\n }\n }\n\n for (const root of roots) {\n if (root.kind === 'Schema') topLevelNodes.add(root)\n for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {\n record(schemaNode)\n }\n }\n\n const canonicalBySignature = new Map<string, DedupeCanonical>()\n const aliasNames = new Map<string, DedupeCanonical>()\n const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []\n\n for (const [signature, group] of groups) {\n if (group.count < minOccurrences) continue\n\n const [firstName, ...duplicateNames] = group.topLevelNames\n if (firstName) {\n const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }\n canonicalBySignature.set(signature, canonical)\n for (const duplicate of duplicateNames) {\n aliasNames.set(duplicate, canonical)\n }\n continue\n }\n\n const name = nameFor(group.representative, signature)\n if (!name) continue\n\n canonicalBySignature.set(signature, { name, ref: refFor(name) })\n pendingHoists.push({ name, representative: group.representative })\n }\n\n // Build hoisted definitions only after every canonical name is known, so nested\n // duplicates inside a definition also resolve to refs.\n const hoisted = pendingHoists.map(({ name, representative }) =>\n cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),\n )\n\n return { canonicalBySignature, aliasNames, hoisted }\n}\n","/**\n * The spec-specific questions a schema parser answers while turning a source document into Kubb\n * AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where\n * OpenAPI, AsyncAPI, and plain JSON Schema differ.\n */\nexport type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {\n /**\n * Identifies the dialect in logs and diagnostics.\n */\n name: string\n /**\n * Whether the schema is nullable.\n */\n isNullable: (schema?: TSchema) => boolean\n /**\n * Whether the value is a `$ref` pointer.\n */\n isReference: (value?: unknown) => value is TRef\n /**\n * Whether the schema carries a discriminator for polymorphism.\n */\n isDiscriminator: (value?: unknown) => value is TDiscriminated\n /**\n * Whether the schema is binary data, converted to a `blob` node.\n */\n isBinary: (schema: TSchema) => boolean\n /**\n * Resolves a local `$ref` against the document, or nullish when it cannot.\n */\n resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined\n}\n\n/**\n * Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the\n * dialect's type for inference.\n *\n * @example\n * ```ts\n * export const oasDialect = defineSchemaDialect({\n * name: 'oas',\n * isNullable,\n * isReference,\n * isDiscriminator,\n * isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',\n * resolveRef,\n * })\n * ```\n */\nexport function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(\n dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>,\n): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument> {\n return dialect\n}\n","import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'\n\n/**\n * Runtime context passed as `this` to printer handlers.\n *\n * `this.transform` dispatches to node-level handlers from `nodes`.\n *\n * @example\n * ```ts\n * const context: PrinterHandlerContext<string, {}> = {\n * options: {},\n * transform: () => 'value',\n * }\n * ```\n */\ntype PrinterHandlerContext<TOutput, TOptions extends object> = {\n /**\n * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.\n * Use `this.transform` inside `nodes` handlers and inside the `print` override.\n */\n transform: (node: SchemaNode) => TOutput | null\n /**\n * Options for this printer instance.\n */\n options: TOptions\n}\n\n/**\n * Handler for one schema node type.\n *\n * Use a regular function (not an arrow function) if you need `this`.\n *\n * @example\n * ```ts\n * const handler: PrinterHandler<string, {}, 'string'> = function () {\n * return 'string'\n * }\n * ```\n */\ntype PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (\n this: PrinterHandlerContext<TOutput, TOptions>,\n node: SchemaNodeByType[T],\n) => TOutput | null\n\n/**\n * Partial map of per-node-type handler overrides for a printer.\n *\n * Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).\n * Supply only the handlers you want to replace. The printer's built-in\n * defaults fill in the rest.\n *\n * @example\n * ```ts\n * pluginZod({\n * printer: {\n * nodes: {\n * date(): string {\n * return 'z.string().date()'\n * },\n * } satisfies PrinterPartial<string, PrinterZodOptions>,\n * },\n * })\n * ```\n */\nexport type PrinterPartial<TOutput, TOptions extends object> = Partial<{\n [K in SchemaType]: PrinterHandler<TOutput, TOptions, K>\n}>\n\n/**\n * Generic shape used by `definePrinter`.\n *\n * - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)\n * - `TOptions` options passed to and stored on the printer instance\n * - `TOutput` the type emitted by node handlers\n * - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)\n *\n * @example\n * ```ts\n * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>\n * ```\n */\nexport type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {\n name: TName\n options: TOptions\n output: TOutput\n printOutput: TPrintOutput\n}\n\n/**\n * Printer instance returned by a printer factory.\n *\n * @example\n * ```ts\n * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})\n * ```\n */\nexport type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {\n /**\n * Unique identifier supplied at creation time.\n */\n name: T['name']\n /**\n * Options for this printer instance.\n */\n options: T['options']\n /**\n * Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.\n * Always dispatches through the `nodes` map. Never calls the `print` override.\n * Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.\n */\n transform: (node: SchemaNode) => T['output'] | null\n /**\n * Public printer. If the builder provides a root-level `print`, this calls that\n * higher-level function (which may produce full declarations).\n * Otherwise, falls back to the node-level dispatcher.\n */\n print: (node: SchemaNode) => T['printOutput'] | null\n}\n\n/**\n * Builder function passed to `definePrinter`.\n *\n * It receives resolved options and returns:\n * - `name`\n * - `options`\n * - `nodes` handlers\n * - optional top-level `print` override\n *\n * @example\n * ```ts\n * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })\n * ```\n */\ntype PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {\n name: T['name']\n /**\n * Options to store on the printer.\n */\n options: T['options']\n nodes: Partial<{\n [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>\n }>\n /**\n * Optional root-level print override. When provided, becomes the public `printer.print`.\n * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),\n * not the override itself, so recursion is safe.\n */\n print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null\n}\n/**\n * Defines a schema printer: a function that takes a `SchemaNode` and emits\n * code in your target language. Each plugin that produces code from schemas\n * (TypeScript types, Zod schemas, Faker factories) ships a printer built\n * with this helper.\n *\n * The builder receives resolved options and returns:\n *\n * - `name` unique identifier for the printer.\n * - `options` stored on the returned printer instance.\n * - `nodes` map of `SchemaType` → handler. Handlers return the rendered\n * output (a string, a TypeScript AST node, ...) for that schema type.\n * - `print` (optional), top-level override exposed as `printer.print`.\n * Use `this.transform(node)` inside it to dispatch to `nodes` recursively.\n *\n * Without a `print` override, `printer.print` falls back to `printer.transform`\n * (the node-level dispatcher).\n *\n * @example Tiny Zod printer\n * ```ts\n * import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'\n *\n * type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>\n *\n * export const zodPrinter = definePrinter<PrinterZod>((options) => ({\n * name: 'zod',\n * options: { strict: options.strict ?? true },\n * nodes: {\n * string: () => 'z.string()',\n * object(node) {\n * const props = node.properties\n * .map((p) => `${p.name}: ${this.transform(p.schema)}`)\n * .join(', ')\n * return `z.object({ ${props} })`\n * },\n * },\n * }))\n * ```\n */\nexport function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {\n return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>\n}\n\n/**\n * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.\n *\n * @example\n * ```ts\n * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(\n * (node) => node.kind,\n * )\n * ```\n */\nexport function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {\n return function <T extends PrinterFactoryOptions>(\n build: (options: T['options']) => {\n name: T['name']\n options: T['options']\n nodes: Partial<{\n [K in TKey]: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNodeByKey[K],\n ) => T['output'] | null\n }>\n print?: (\n this: {\n transform: (node: TNode) => T['output'] | null\n options: T['options']\n },\n node: TNode,\n ) => T['printOutput'] | null\n },\n ): (options?: T['options']) => {\n name: T['name']\n options: T['options']\n transform: (node: TNode) => T['output'] | null\n print: (node: TNode) => T['printOutput'] | null\n } {\n return (options) => {\n const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))\n\n const context = {\n options: resolvedOptions,\n transform: (node: TNode): T['output'] | null => {\n const key = getKey(node)\n if (key === null) return null\n\n const handler = nodes[key]\n\n if (!handler) return null\n\n return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)\n },\n }\n\n return {\n name,\n options: resolvedOptions,\n transform: context.transform,\n print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;AAOA,SAAS,gBAAgB,MAA0B;CACjD,OAAO,GAAG,KAAK,aAAa,GAAG,GAAG,KAAK,UAAU,GAAG,GAAG,KAAK,WAAW,IAAI;AAC7E;AAEA,SAAS,cAAc,MAAoD;CACzE,IAAI,KAAK,KAAK,OAAO,eAAe,KAAK,GAAG;CAC5C,OAAO,KAAK,QAAQ;AACtB;AAuBA,MAAM,mBAA8C;CAClD;EAAE,MAAM;EAAY,KAAK;EAAS,QAAQ;CAAI;CAC9C;EAAE,MAAM;EAAS,KAAK;EAAQ,QAAQ;CAAI;CAC1C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAQ,KAAK;EAAU,QAAQ;CAAI;AAC7C;AAEA,MAAM,gBAA2C;CAC/C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAO,QAAQ;CAAK;CAC3C;EAAE,MAAM;EAAU,KAAK;EAAoB,QAAQ;CAAM;CACzD;EAAE,MAAM;EAAU,KAAK;EAAoB,QAAQ;CAAM;CACzD;EAAE,MAAM;EAAU,KAAK;EAAc,QAAQ;CAAK;AACpD;AAEA,MAAM,cAAyC,CAC7C;CAAE,MAAM;CAAU,KAAK;CAAO,QAAQ;AAAK,GAC3C;CAAE,MAAM;CAAU,KAAK;CAAO,QAAQ;AAAK,CAC7C;;;;;AAMA,MAAM,aAA6E;CACjF,QAAQ;EACN,EAAE,MAAM,cAAc;EACtB,EAAE,MAAM,kBAAkB;EAC1B,EAAE,MAAM,eAAe;EACvB;GAAE,MAAM;GAAU,KAAK;GAAiB,QAAQ;EAAK;EACrD;GAAE,MAAM;GAAU,KAAK;GAAiB,QAAQ;EAAK;CACvD;CACA,OAAO;CACP,OAAO;CACP,OAAO;EACL;GAAE,MAAM;GAAU,KAAK;GAAY,QAAQ;EAAI;EAC/C;GAAE,MAAM;GAAU,KAAK;GAA6B,QAAQ;EAAI;EAChE;GAAE,MAAM;GAAY,KAAK;GAAW,QAAQ;EAAI;CAClD;CACA,cAAc,CAAC;EAAE,MAAM;EAAY,KAAK;EAAW,QAAQ;CAAI,CAAC;CAChE,MAAM,CAAC,EAAE,MAAM,aAAa,CAAC;CAC7B,KAAK,CAAC,EAAE,MAAM,YAAY,CAAC;CAC3B,QAAQ;EACN;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;EAC3C;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;EAC3C;GAAE,MAAM;GAAU,KAAK;GAAW,QAAQ;EAAK;CACjD;CACA,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,KAAK;EACH;GAAE,MAAM;GAAU,KAAK;GAAQ,QAAQ;EAAO;EAC9C;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;EAC3C;GAAE,MAAM;GAAU,KAAK;GAAO,QAAQ;EAAK;CAC7C;CACA,MAAM;CACN,OAAO;CACP,UAAU,CACR;EAAE,MAAM;EAAQ,KAAK;EAAU,QAAQ;CAAI,GAC3C;EAAE,MAAM;EAAQ,KAAK;EAAS,QAAQ;CAAI,CAC5C;CACA,MAAM,CAAC;EAAE,MAAM;EAAU,KAAK;EAAkB,QAAQ;CAAM,CAAC;CAC/D,MAAM,CAAC;EAAE,MAAM;EAAU,KAAK;EAAkB,QAAQ;CAAM,CAAC;AACjE;AAEA,SAAS,oBAAoB,OAAmB,MAAkB,QAAyC;CACzG,QAAQ,MAAM,MAAd;EACE,KAAK,UACH,OAAO,GAAG,MAAM,OAAO,GAAG,OAAO,MAAM,QAAQ;EACjD,KAAK,QACH,OAAO,GAAG,MAAM,OAAO,GAAG,OAAO,MAAM,OAAO,IAAI;EACpD,KAAK,SAAS;GACZ,MAAM,QAAQ,OAAO,MAAM;GAC3B,OAAO,GAAG,MAAM,OAAO,GAAG,QAAQ,YAAY,KAAK,IAAI;EACzD;EACA,KAAK,YAAY;GACf,MAAM,WAAY,OAAO,MAAM,QAA0C,CAAC;GAC1E,OAAO,GAAG,MAAM,OAAO,GAAG,SAAS,KAAK,MAAM,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;EAC1E;EACA,KAAK,eAGH,OAAO,MADQA,KAAI,cAAc,CAAC,EAAA,CAAG,KAAK,SAAS,GAAG,KAAK,OAAO,KAAK,WAAW,MAAM,MAAM,YAAY,KAAK,MAAM,GAAG,CAAC,CAAC,KAAK,GAC/G,EAAE;EAEpB,KAAK,mBAAmB;GACtB,MAAM,MAAM;GACZ,IAAI,OAAO,IAAI,yBAAyB,WAAW,OAAO,MAAM,IAAI;GACpE,IAAI,IAAI,sBAAsB,OAAO,MAAM,YAAY,IAAI,oBAAoB;GAC/E,OAAO;EACT;EACA,KAAK,gBAAgB;GACnB,MAAM,MAAM;GAOZ,OAAO,MANS,IAAI,oBAChB,OAAO,KAAK,IAAI,iBAAiB,CAAC,CAC/B,KAAK,CAAC,CACN,KAAK,QAAQ,GAAG,IAAI,GAAG,YAAY,IAAI,kBAAmB,IAAK,GAAG,CAAC,CACnE,KAAK,GAAG,IACX,GACiB;EACvB;EACA,KAAK,cAAc;GACjB,MAAM,KAAK;GACX,IAAI,SAAS;GACb,IAAI,GAAG,iBAAiB,QACtB,SAAS,GAAG,gBAAgB,KAAK,UAAU,GAAG,MAAM,KAAK,GAAG,MAAM,UAAU,GAAG,OAAO,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG;QACzG,IAAI,GAAG,YAAY,QACxB,SAAS,GAAG,WAAW,KAAK,UAAU,GAAG,UAAU,OAAO,SAAS,OAAO,MAAM,GAAG,OAAO,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG;GAE9G,OAAO,KAAK,OAAO;EACrB;EACA,KAAK,aACH,OAAO,KAAK,cAAc,IAA4C;CAE1E;AACF;;;;;AAMA,SAAS,cAAc,MAA0B;CAC/C,MAAM,QAAQ,gBAAgB,IAAI;CAClC,MAAM,SAAS,WAAW,KAAK;CAC/B,IAAI,CAAC,QAAQ,OAAO,GAAG,KAAK,KAAK,GAAG;CAEpC,MAAM,SAAS;CACf,MAAM,QAAuB,CAAC,GAAG,KAAK,KAAK,GAAG,OAAO;CACrD,KAAK,MAAM,SAAS,QAClB,MAAM,KAAK,oBAAoB,OAAO,MAAM,MAAM,CAAC;CAErD,OAAO,MAAM,KAAK,GAAG;AACvB;;;;;;AAOA,MAAM,iCAAiB,IAAI,QAA4B;;;;;;;;;;;;;;AAevD,SAAgB,YAAY,MAA0B;CACpD,MAAM,SAAS,eAAe,IAAI,IAAI;CACtC,IAAI,WAAW,KAAA,GAAW,OAAO;CACjC,MAAM,YAAY,KAAK,UAAU,cAAc,IAAI,GAAG,KAAK;CAC3D,eAAe,IAAI,MAAM,SAAS;CAElC,OAAO;AACT;;;;;;;AC9GA,SAAS,cAAc,MAAkB,WAAwC;CAC/E,OAAO,aAAa;EAClB,MAAM;EACN,MAAM,UAAU;EAChB,KAAK,UAAU;EACf,UAAU,KAAK;EACf,SAAS,KAAK;EACd,UAAU,KAAK;EACf,WAAW,KAAK;EAChB,YAAY,KAAK;EACjB,aAAa,KAAK;EAClB,SAAS,KAAK;EACd,SAAS,KAAK;CAChB,CAAC;AACH;AAmBA,SAAgB,YAAY,MAAY,MAAqB,gBAAgB,OAAa;CACxF,MAAM,EAAE,sBAAsB,eAAe;CAC7C,IAAI,qBAAqB,SAAS,KAAK,WAAW,SAAS,GAAG,OAAO;CAErE,MAAM,OAAO;CAEb,OAAO,UAAU,MAAM,EACrB,OAAO,YAAY;EACjB,IAAI,WAAW,SAAS,OAAO;GAC7B,MAAM,SAAS,WAAW,MAAM,eAAe,WAAW,GAAG,IAAI,WAAW;GAC5E,MAAM,YAAY,SAAS,WAAW,IAAI,MAAM,IAAI,KAAA;GAEpD,OAAO,YAAY;IAAE,GAAG;IAAY,MAAM,UAAU;IAAM,KAAK,UAAU;GAAI,IAAI,KAAA;EACnF;EAEA,MAAM,YAAY,YAAY,UAAU;EACxC,IAAI,iBAAiB,eAAe,MAAM,OAAO,KAAA;EAEjD,MAAM,YAAY,qBAAqB,IAAI,SAAS;EACpD,IAAI,CAAC,WAAW,OAAO,KAAA;EAEvB,OAAO,cAAc,YAAY,SAAS;CAC5C,EACF,CAAC;AACH;;;;;AAMA,SAAS,gBAAgB,MAAkB,MAA0B;CACnE,OAAO;EAAE,GAAG;EAAM;EAAM,UAAU,KAAA;EAAW,SAAS,KAAA;CAAU;AAClE;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,gBAAgB,OAA4B,SAA6C;CACvG,MAAM,EAAE,aAAa,SAAS,QAAQ,iBAAiB,MAAM;CAE7D,MAAM,gCAAgB,IAAI,IAAgB;CAO1C,MAAM,yBAAS,IAAI,IAAmB;CAEtC,SAAS,OAAO,YAA8B;EAC5C,MAAM,YAAY,YAAY,UAAU;EACxC,IAAI,CAAC,YAAY,UAAU,GAAG;EAE9B,MAAM,aAAa,cAAc,IAAI,UAAU,KAAK,CAAC,CAAC,WAAW;EACjE,MAAM,QAAQ,OAAO,IAAI,SAAS;EAClC,IAAI,OAAO;GACT,MAAM;GACN,IAAI,YAAY,MAAM,cAAc,KAAK,WAAW,IAAK;EAC3D,OACE,OAAO,IAAI,WAAW;GAAE,OAAO;GAAG,gBAAgB;GAAY,eAAe,aAAa,CAAC,WAAW,IAAK,IAAI,CAAC;EAAE,CAAC;CAEvH;CAEA,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,KAAK,SAAS,UAAU,cAAc,IAAI,IAAI;EAClD,KAAK,MAAM,cAAc,YAAwB,MAAM,EAAE,SAAS,SAAS,KAAK,CAAC,GAC/E,OAAO,UAAU;CAErB;CAEA,MAAM,uCAAuB,IAAI,IAA6B;CAC9D,MAAM,6BAAa,IAAI,IAA6B;CACpD,MAAM,gBAAqE,CAAC;CAE5E,KAAK,MAAM,CAAC,WAAW,UAAU,QAAQ;EACvC,IAAI,MAAM,QAAQ,gBAAgB;EAElC,MAAM,CAAC,WAAW,GAAG,kBAAkB,MAAM;EAC7C,IAAI,WAAW;GACb,MAAM,YAA6B;IAAE,MAAM;IAAW,KAAK,OAAO,SAAS;GAAE;GAC7E,qBAAqB,IAAI,WAAW,SAAS;GAC7C,KAAK,MAAM,aAAa,gBACtB,WAAW,IAAI,WAAW,SAAS;GAErC;EACF;EAEA,MAAM,OAAO,QAAQ,MAAM,gBAAgB,SAAS;EACpD,IAAI,CAAC,MAAM;EAEX,qBAAqB,IAAI,WAAW;GAAE;GAAM,KAAK,OAAO,IAAI;EAAE,CAAC;EAC/D,cAAc,KAAK;GAAE;GAAM,gBAAgB,MAAM;EAAe,CAAC;CACnE;CAQA,OAAO;EAAE;EAAsB;EAAY,SAJ3B,cAAc,KAAK,EAAE,MAAM,qBACzC,gBAAgB,YAAY,gBAAgB;GAAE;GAAsB;EAAW,GAAG,IAAI,GAAG,IAAI,CAG9C;CAAE;AACrD;;;;;;;;;;;;;;;;;;;AC9LA,SAAgB,oBACd,SACyD;CACzD,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACwIA,SAAgB,cAAuE,OAAkE;CACvJ,OAAO,sBAAgE,SAAS,KAAK,IAAI,CAAC,CAAC,KAAK;AAClG;;;;;;;;;;;AAYA,SAAgB,qBAAkG,QAAsC;CACtJ,OAAO,SACL,OAyBA;EACA,QAAQ,YAAY;GAClB,MAAM,EAAE,MAAM,SAAS,iBAAiB,OAAO,OAAO,kBAAkB,MAAM,WAAY,CAAC,CAAkB;GAE7G,MAAM,UAAU;IACd,SAAS;IACT,YAAY,SAAoC;KAC9C,MAAM,MAAM,OAAO,IAAI;KACvB,IAAI,QAAQ,MAAM,OAAO;KAEzB,MAAM,UAAU,MAAM;KAEtB,IAAI,CAAC,SAAS,OAAO;KAErB,OAAQ,QAAsE,KAAK,SAAS,IAAI;IAClG;GACF;GAEA,OAAO;IACL;IACA,SAAS;IACT,WAAW,QAAQ;IACnB,OAAQ,gBAAgB,cAAc,KAAK,OAAO,IAAI,QAAQ;GAChE;EACF;CACF;AACF"}

@@ -1,4 +0,5 @@

import { At as StringSchemaNode, B as FunctionParamNode, Bt as ParserOptions, C as HttpMethod, Ct as ObjectSchemaNode, Dt as SchemaNode, Et as ScalarSchemaType, F as ParameterLocation, G as TypeExpression, Gt as JSDocNode, H as FunctionParametersNode, Ht as CodeNode, I as ParameterNode, It as PropertyNode, Jt as TypeNode, K as TypeLiteralNode, Kt as JsxNode, Mt as UnionSchemaNode, Nt as UrlSchemaNode, O as ResponseNode, Ot as SchemaNodeByType, St as NumberSchemaNode, T as OperationNode, Tt as RefSchemaNode, U as IndexedAccessTypeNode, Ut as ConstNode, V as FunctionParameterNode, Vt as ArrowFunctionNode, W as ObjectBindingPatternNode, Wt as FunctionNode, _t as ArraySchemaNode, at as ImportNode, b as InputNode, bt as EnumSchemaNode, g as OutputNode, h as Node, it as FileNode, j as StatusCode, jt as TimeSchemaNode, kt as SchemaType, ln as DistributiveOmit, n as OperationParamsResolver, ot as SourceNode, pn as NodeKind, qt as TextNode, rt as ExportNode, vt as DateSchemaNode, w as HttpOperationNode, wt as PrimitiveSchemaType, xt as IntersectionSchemaNode, y as InputMeta, yt as DatetimeSchemaNode, z as FunctionParamKind, zt as InferSchemaNode } from "./ast-ClnJg9BN.js";
import { A as FunctionParametersNode, At as InferSchemaNode, Bt as TypeNode, C as ParameterLocation, Ct as UrlSchemaNode, D as FunctionParamKind, Et as PropertyNode, Ft as ConstNode, G as ExportNode, It as FunctionNode, J as SourceNode, K as FileNode, Lt as JSDocNode, M as ObjectBindingPatternNode, Mt as ArrowFunctionNode, N as TypeExpression, Nt as BreakNode, O as FunctionParamNode, P as TypeLiteralNode, Pt as CodeNode, Rt as JsxNode, St as UnionSchemaNode, _t as SchemaNode, a as InputMeta, bt as StringSchemaNode, ct as DatetimeSchemaNode, d as HttpOperationNode, dt as NumberSchemaNode, f as OperationNode, ft as ObjectSchemaNode, gt as ScalarSchemaType, h as ResponseNode, ht as ScalarSchemaNode, j as IndexedAccessTypeNode, jt as ParserOptions, k as FunctionParameterNode, l as GenericOperationNode, lt as EnumSchemaNode, mt as RefSchemaNode, n as OutputNode, nt as ContentNode, o as InputNode, on as NodeKind, ot as ArraySchemaNode, pt as PrimitiveSchemaType, q as ImportNode, st as DateSchemaNode, t as Node, tn as DistributiveOmit, u as HttpMethod, ut as IntersectionSchemaNode, v as StatusCode, vt as SchemaNodeByType, w as ParameterNode, xt as TimeSchemaNode, y as RequestBodyNode, yt as SchemaType, zt as TextNode } from "./index-BzjwdK2M.js";
import { UserFileNode } from "./factory.js";
import { c as PrinterFactoryOptions, f as SchemaDialect, g as DedupePlan, h as DedupeLookups, l as PrinterPartial, m as DedupeCanonical, n as Visitor, r as VisitorContext, s as Printer, t as ParentOf } from "./types-CB2oY8Dw.js";
export type { ArraySchemaNode, ArrowFunctionNode, CodeNode, ConstNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupeLookups, DedupePlan, DistributiveOmit, EnumSchemaNode, ExportNode, FileNode, FunctionNode, FunctionParamKind, FunctionParamNode, FunctionParameterNode, FunctionParametersNode, HttpMethod, HttpOperationNode, ImportNode, IndexedAccessTypeNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Node, NodeKind, NumberSchemaNode, ObjectBindingPatternNode, ObjectSchemaNode, OperationNode, OperationParamsResolver, OutputNode, ParameterLocation, ParameterNode, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, ResponseNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeExpression, TypeLiteralNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext };
import { c as VisitorContext, f as SchemaDialect, g as DedupePlan, h as DedupeLookups, m as DedupeCanonical, n as PrinterFactoryOptions, o as ParentOf, r as PrinterPartial, s as Visitor, t as Printer } from "./types-olVl9v5p.js";
import { n as OperationParamsResolver } from "./operationParams-BZ07xDm0.js";
export type { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupeLookups, DedupePlan, DistributiveOmit, EnumSchemaNode, ExportNode, FileNode, FunctionNode, FunctionParamKind, FunctionParamNode, FunctionParameterNode, FunctionParametersNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, IndexedAccessTypeNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Node, NodeKind, NumberSchemaNode, ObjectBindingPatternNode, ObjectSchemaNode, OperationNode, OperationParamsResolver, OutputNode, ParameterLocation, ParameterNode, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeExpression, TypeLiteralNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext };
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const require_utils = require("./utils-C8bWAzhv.cjs");
exports.buildGroupParam = require_utils.buildGroupParam;
exports.buildJSDoc = require_utils.buildJSDoc;
exports.buildList = require_utils.buildList;
exports.buildObject = require_utils.buildObject;
exports.buildTypeLiteral = require_utils.buildTypeLiteral;
exports.caseParams = require_utils.caseParams;
const require_utils = require("./utils-BCtRXfhI.cjs");
Object.defineProperty(exports, "buildGroupParam", {
enumerable: true,
get: function() {
return require_utils.buildGroupParam;
}
});
Object.defineProperty(exports, "buildJSDoc", {
enumerable: true,
get: function() {
return require_utils.buildJSDoc;
}
});
Object.defineProperty(exports, "buildList", {
enumerable: true,
get: function() {
return require_utils.buildList;
}
});
Object.defineProperty(exports, "buildObject", {
enumerable: true,
get: function() {
return require_utils.buildObject;
}
});
Object.defineProperty(exports, "buildTypeLiteral", {
enumerable: true,
get: function() {
return require_utils.buildTypeLiteral;
}
});
Object.defineProperty(exports, "caseParams", {
enumerable: true,
get: function() {
return require_utils.caseParams;
}
});
exports.childName = require_utils.childName;
exports.collectUsedSchemaNames = require_utils.collectUsedSchemaNames;
exports.containsCircularRef = require_utils.containsCircularRef;
Object.defineProperty(exports, "collectUsedSchemaNames", {
enumerable: true,
get: function() {
return require_utils.collectUsedSchemaNames;
}
});
Object.defineProperty(exports, "containsCircularRef", {
enumerable: true,
get: function() {
return require_utils.containsCircularRef;
}
});
Object.defineProperty(exports, "createOperationParams", {
enumerable: true,
get: function() {
return require_utils.createOperationParams;
}
});
exports.enumPropName = require_utils.enumPropName;
exports.extractRefName = require_utils.extractRefName;
exports.extractStringsFromNodes = require_utils.extractStringsFromNodes;
exports.findCircularSchemas = require_utils.findCircularSchemas;
exports.findDiscriminator = require_utils.findDiscriminator;
exports.getNestedAccessor = require_utils.getNestedAccessor;
Object.defineProperty(exports, "extractStringsFromNodes", {
enumerable: true,
get: function() {
return require_utils.extractStringsFromNodes;
}
});
Object.defineProperty(exports, "findCircularSchemas", {
enumerable: true,
get: function() {
return require_utils.findCircularSchemas;
}
});
Object.defineProperty(exports, "getNestedAccessor", {
enumerable: true,
get: function() {
return require_utils.getNestedAccessor;
}
});
exports.isStringType = require_utils.isStringType;
exports.isValidVarName = require_utils.isValidVarName;
exports.jsStringEscape = require_utils.jsStringEscape;
exports.objectKey = require_utils.objectKey;
Object.defineProperty(exports, "isValidVarName", {
enumerable: true,
get: function() {
return require_utils.isValidVarName;
}
});
Object.defineProperty(exports, "jsStringEscape", {
enumerable: true,
get: function() {
return require_utils.jsStringEscape;
}
});
Object.defineProperty(exports, "objectKey", {
enumerable: true,
get: function() {
return require_utils.objectKey;
}
});
exports.resolveGroupType = require_utils.resolveGroupType;
exports.resolveParamType = require_utils.resolveParamType;
exports.stringify = require_utils.stringify;
exports.stringifyObject = require_utils.stringifyObject;
exports.syncSchemaRef = require_utils.syncSchemaRef;
exports.toRegExpString = require_utils.toRegExpString;
exports.trimQuotes = require_utils.trimQuotes;
Object.defineProperty(exports, "resolveParamType", {
enumerable: true,
get: function() {
return require_utils.resolveParamType;
}
});
Object.defineProperty(exports, "stringify", {
enumerable: true,
get: function() {
return require_utils.stringify;
}
});
Object.defineProperty(exports, "stringifyObject", {
enumerable: true,
get: function() {
return require_utils.stringifyObject;
}
});
Object.defineProperty(exports, "syncSchemaRef", {
enumerable: true,
get: function() {
return require_utils.syncSchemaRef;
}
});
Object.defineProperty(exports, "toRegExpString", {
enumerable: true,
get: function() {
return require_utils.toRegExpString;
}
});
Object.defineProperty(exports, "trimQuotes", {
enumerable: true,
get: function() {
return require_utils.trimQuotes;
}
});
import { n as __name } from "./chunk-CNktS9qV.js";
import { I as ParameterNode, T as OperationNode, a as buildTypeLiteral, c as containsCircularRef, d as findCircularSchemas, f as isStringType, i as buildGroupParam, m as syncSchemaRef, n as OperationParamsResolver, o as caseParams, p as resolveParamType, r as ParamGroupType, s as collectUsedSchemaNames, t as BuildGroupArgs } from "./ast-ClnJg9BN.js";
import { t as extractStringsFromNodes } from "./extractStringsFromNodes-Bn9cOos9.js";
import { _t as SchemaNode, f as OperationNode, w as ParameterNode } from "./index-BzjwdK2M.js";
import { a as buildTypeLiteral, c as resolveParamType, i as buildGroupParam, n as OperationParamsResolver, o as caseParams, r as ParamGroupType, s as createOperationParams, t as BuildGroupArgs } from "./operationParams-BZ07xDm0.js";
import { o as syncSchemaRef, t as extractStringsFromNodes } from "./extractStringsFromNodes-WMYJ8nQL.js";

@@ -18,74 +19,4 @@ //#region ../../internals/utils/src/reserved.d.ts

//#endregion
//#region src/utils/index.d.ts
//#region src/utils/codegen.d.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
declare function trimQuotes(text: string): string;
/**
* Serializes a primitive value to a single-quoted string literal, stripping any surrounding quote
* characters first. Escaping comes from `JSON.stringify`, then the quote style switches to single
* quotes so generated code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
declare function stringify(value: string | number | boolean | undefined): string;
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
declare function jsStringEscape(input: unknown): string;
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
declare function toRegExpString(text: string, func?: string | null): string;
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
declare function stringifyObject(value: Record<string, unknown>): string;
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
declare function getNestedAccessor(param: string | Array<string>, accessor: string): string | null;
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no

@@ -154,2 +85,4 @@ * comments so callers always get a usable string.

declare function buildList(items: Array<string>, brackets?: [open: string, close: string]): string;
//#endregion
//#region src/utils/refs.d.ts
/**

@@ -186,10 +119,8 @@ * Returns the last path segment of a reference string.

/**
* Returns the discriminator key whose mapping value matches `ref`, or `null` when there is no match.
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* @example
* ```ts
* findDiscriminator({ dog: '#/components/schemas/Dog' }, '#/components/schemas/Dog') // 'dog'
* ```
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
declare function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null;
declare function isStringType(node: SchemaNode): boolean;
/**

@@ -213,3 +144,104 @@ * Derives a {@link ParamGroupType} for a query or header group from the resolver.

//#endregion
export { type BuildGroupArgs, type ParamGroupType, buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, objectKey, resolveGroupType, resolveParamType, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes };
//#region src/utils/strings.d.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
declare function trimQuotes(text: string): string;
/**
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
*
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
* code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
declare function stringify(value: string | number | boolean | undefined): string;
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
declare function jsStringEscape(input: unknown): string;
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
declare function toRegExpString(text: string, func?: string | null): string;
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
declare function stringifyObject(value: Record<string, unknown>): string;
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
declare function getNestedAccessor(param: string | Array<string>, accessor: string): string | null;
//#endregion
//#region src/utils/schemaGraph.d.ts
declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
/**
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
*
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
* linear in the size of the schema graph.
*
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
*/
declare function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string>;
/**
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
*
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
*
* @note Stops at the first matching circular ref.
*/
declare function containsCircularRef(node: SchemaNode | undefined, {
circularSchemas,
excludeName
}: {
circularSchemas: ReadonlySet<string>;
excludeName?: string;
}): boolean;
//#endregion
export { type BuildGroupArgs, type ParamGroupType, buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, objectKey, resolveGroupType, resolveParamType, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes };
//# sourceMappingURL=utils.d.ts.map

@@ -1,2 +0,3 @@

import { At as isValidVarName, C as containsCircularRef, D as isStringType, E as findCircularSchemas, O as resolveParamType, _ as buildTypeLiteral, a as enumPropName, c as getNestedAccessor, d as resolveGroupType, f as stringify, g as buildGroupParam, h as trimQuotes, i as childName, k as syncSchemaRef, kt as extractStringsFromNodes, l as jsStringEscape, m as toRegExpString, n as buildList, o as extractRefName, p as stringifyObject, r as buildObject, s as findDiscriminator, t as buildJSDoc, u as objectKey, v as caseParams, y as collectUsedSchemaNames } from "./utils-DN4XLVqz.js";
export { buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, objectKey, resolveGroupType, resolveParamType, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes };
import { D as extractRefName, E as enumPropName, F as objectKey, I as isValidVarName, M as buildJSDoc, N as buildList, O as isStringType, P as buildObject, T as childName, _ as stringifyObject, a as resolveParamType, c as findCircularSchemas, g as stringify, h as jsStringEscape, i as createOperationParams, k as resolveGroupType, m as getNestedAccessor, n as buildTypeLiteral, o as collectUsedSchemaNames, r as caseParams, s as containsCircularRef, t as buildGroupParam, v as toRegExpString, w as syncSchemaRef, y as trimQuotes } from "./utils-SdZU0F3H.js";
import { J as extractStringsFromNodes } from "./response-DKxTr522.js";
export { buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, objectKey, resolveGroupType, resolveParamType, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes };
{
"name": "@kubb/ast",
"version": "5.0.0-beta.58",
"version": "5.0.0-beta.59",
"description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",

@@ -5,0 +5,0 @@ "keywords": [

@@ -137,2 +137,12 @@ <div align="center">

## Adding a node
Adding a node touches three files. The barrels and visitor tables derive the rest.
1. Define the node in its own `src/nodes/*.ts` file. Call `defineNode` and export the resulting `fooDef`, the `createFoo` constructor, and the node's type.
2. Add `fooDef` to the `nodeDefs` array in `src/registry.ts`.
3. Re-export `createFoo` from `src/factory.ts`.
Everything else follows from there. `@kubb/ast/types` picks up the node type through `export type *`, the `@kubb/ast` barrel picks up `fooDef` through `export * from './registry.ts'`, and the visitor tables (`VISITOR_KEYS`, `VISITOR_KEY_BY_KIND`, `nodeRebuilders`) come from the def's `children`, `visitorKey`, and `rebuild` fields. `registry.test.ts` fails when a def has no matching `factory.create*`, so missing wiring is caught in CI.
## Supporting Kubb

@@ -139,0 +149,0 @@

@@ -20,5 +20,4 @@ import type { HttpMethod } from './nodes/operation.ts'

*
* These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
* Call `isScalarPrimitive()` to check for the scalar types.
*/

@@ -43,3 +42,3 @@ export const schemaTypes = {

/**
* Boolean value
* Boolean value.
*/

@@ -137,4 +136,2 @@ boolean: 'boolean',

* Scalar primitive schema types used for union simplification and type narrowing.
*
* Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.
*/

@@ -144,5 +141,4 @@ const SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])

/**
* Type guard that returns `true` when `type` is a scalar primitive schema type.
*
* Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
* (for example `string | number | boolean`).
*/

@@ -155,4 +151,2 @@ export function isScalarPrimitive(type: string): type is ScalarPrimitive {

* HTTP method identifiers used by operation nodes.
*
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
*/

@@ -171,6 +165,6 @@ export const httpMethods = {

/**
* Default concurrency limit for `walk()` traversal utility.
* Default concurrency limit for the `walk()` traversal utility.
*
* Set to 30 to balance I/O-bound resolver parallelism against event loop pressure and memory usage during large spec traversals.
* Use `WALK_CONCURRENCY` when calling `walk()` or override for different hardware constraints.
* Set to 30 to balance I/O-bound resolver parallelism against event-loop and memory pressure
* during large spec traversals. Override it for different hardware constraints.
*

@@ -177,0 +171,0 @@ * @example

@@ -0,1 +1,9 @@

/**
* Schema-shape deduplication. `buildDedupePlan` finds top-level and inline schemas that share a
* structural signature, picks one canonical definition, and `applyDedupe` repoints every duplicate
* at it. This works on `SchemaNode` content, not on files.
*
* For merging a file's imports, exports, and source nodes, see `utils/fileMerge.ts`. Same idea of
* collapsing duplicates, but a different domain.
*/
import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'

@@ -2,0 +10,0 @@ import { createSchema } from './nodes/schema.ts'

@@ -5,4 +5,4 @@ import { hash } from 'node:crypto'

import type { FileNode, Node } from './nodes/index.ts'
import { combineExports, combineImports, combineSources } from './utils/ast.ts'
import { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'
import { combineExports, combineImports, combineSources } from './utils/fileMerge.ts'

@@ -24,3 +24,2 @@ // Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes

export { createSchema } from './nodes/schema.ts'
export { createDiscriminantNode, createOperationParams } from './utils/ast.ts'

@@ -27,0 +26,0 @@ /**

@@ -17,3 +17,3 @@ import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'

/**
* Narrows an `OperationNode` to an `HttpOperationNode`, guaranteeing `method` and `path`.
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
*

@@ -20,0 +20,0 @@ * @example

@@ -10,14 +10,5 @@ export { httpMethods, schemaTypes } from './constants.ts'

export { syncOptionality } from './node.ts'
export { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts'
export { contentDef } from './nodes/content.ts'
export { exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts'
export { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'
export { inputDef } from './nodes/input.ts'
export { operationDef } from './nodes/operation.ts'
export { outputDef } from './nodes/output.ts'
export { parameterDef } from './nodes/parameter.ts'
export { propertyDef } from './nodes/property.ts'
export { requestBodyDef } from './nodes/requestBody.ts'
export { responseDef } from './nodes/response.ts'
export { schemaDef } from './nodes/schema.ts'
// Every node def plus `nodeDefs` in one line, so adding a node never edits this barrel. The visitor
// tables derived from `nodeDefs` stay internal to `visitor.ts`.
export * from './registry.ts'
export { createPrinterFactory, definePrinter } from './printer.ts'

@@ -27,4 +18,4 @@ export { signatureOf } from './signature.ts'

export type * from './types.ts'
// The node/AST helpers in ./utils/ast.ts live on the `@kubb/ast/utils` subpath, not the root barrel.
// The node/AST helpers in ./utils/ live on the `@kubb/ast/utils` subpath, not the root barrel.
export { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'
export { collect, transform, walk } from './visitor.ts'

@@ -10,3 +10,7 @@ import { defineNode } from '../node.ts'

* JSDoc comment lines. `undefined` entries are filtered out during rendering.
* @example ['@description A pet resource', '@deprecated']
*
* @example
* ```ts
* ['@description A pet resource', '@deprecated']
* ```
*/

@@ -29,5 +33,2 @@ comments?: Array<string | undefined>

export type ConstNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Const'

@@ -40,8 +41,9 @@ /**

* Whether the declaration should be exported.
* @default false
*/
export?: boolean | null
/**
* Optional explicit type annotation.
* @example 'Pet'
* Explicit type annotation.
*
* @example Type reference
* `'Pet'`
*/

@@ -55,3 +57,2 @@ type?: string | null

* Whether to append `as const` to the declaration.
* @default false
*/

@@ -61,3 +62,3 @@ asConst?: boolean | null

* Child nodes representing the value of the constant (children of the `Const` component).
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
*/

@@ -80,5 +81,2 @@ nodes?: Array<CodeNode>

export type TypeNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Type'

@@ -91,3 +89,2 @@ /**

* Whether the declaration should be exported.
* @default false
*/

@@ -101,3 +98,3 @@ export?: boolean | null

* Child nodes representing the type body (children of the `Type` component).
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
*/

@@ -120,5 +117,2 @@ nodes?: Array<CodeNode>

export type FunctionNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Function'

@@ -131,3 +125,2 @@ /**

* Whether the function is a default export.
* @default false
*/

@@ -141,3 +134,2 @@ default?: boolean | null

* Whether the function should be exported.
* @default false
*/

@@ -147,3 +139,2 @@ export?: boolean | null

* Whether the function is async. When `true`, the return type is wrapped in `Promise<>`.
* @default false
*/

@@ -153,3 +144,5 @@ async?: boolean | null

* TypeScript generic type parameters.
* @example ['T', 'U extends string']
*
* @example Constrained generics
* `['T', 'U extends string']`
*/

@@ -159,3 +152,5 @@ generics?: string | Array<string> | null

* Return type annotation.
* @example 'Pet'
*
* @example Type reference
* `'Pet'`
*/

@@ -169,3 +164,3 @@ returnType?: string | null

* Child nodes representing the function body (children of the `Function` component).
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
*/

@@ -188,5 +183,2 @@ nodes?: Array<CodeNode>

export type ArrowFunctionNode = BaseNode & {
/**
* Node kind.
*/
kind: 'ArrowFunction'

@@ -199,3 +191,2 @@ /**

* Whether the function is a default export.
* @default false
*/

@@ -209,3 +200,2 @@ default?: boolean | null

* Whether the arrow function should be exported.
* @default false
*/

@@ -215,3 +205,2 @@ export?: boolean | null

* Whether the arrow function is async. When `true`, the return type is wrapped in `Promise<>`.
* @default false
*/

@@ -221,3 +210,5 @@ async?: boolean | null

* TypeScript generic type parameters.
* @example ['T', 'U extends string']
*
* @example Constrained generics
* `['T', 'U extends string']`
*/

@@ -227,3 +218,5 @@ generics?: string | Array<string> | null

* Return type annotation.
* @example 'Pet'
*
* @example Type reference
* `'Pet'`
*/

@@ -237,3 +230,2 @@ returnType?: string | null

* Render the arrow function body as a single-line expression.
* @default false
*/

@@ -243,3 +235,3 @@ singleLine?: boolean | null

* Child nodes representing the function body (children of the `Function.Arrow` component).
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
* Each entry is a {@link CodeNode}. Use {@link TextNode} for raw string content.
*/

@@ -262,5 +254,2 @@ nodes?: Array<CodeNode>

export type TextNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Text'

@@ -276,5 +265,4 @@ /**

*
* Corresponds to `<br/>` in JSX components. When printed, produces an empty
* string that, joined with `\n` by `printNodes` creates a blank line
* between surrounding code nodes.
* Corresponds to `<br/>` in JSX components. When printed it produces an empty string,
* so joining nodes with `\n` in `printNodes` leaves a blank line between the surrounding code.
*

@@ -289,5 +277,2 @@ * @example

export type BreakNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Break'

@@ -299,4 +284,4 @@ }

*
* Mirrors the `Jsx` component from `@kubb/renderer-jsx`. Use this to embed raw
* JSX/TSX markup (including fragments `<>…</>`) directly in generated code.
* Mirrors the `Jsx` component from `@kubb/renderer-jsx`. Embeds raw JSX/TSX markup
* (including fragments `<>…</>`) directly in generated code.
*

@@ -310,5 +295,2 @@ * @example

export type JsxNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Jsx'

@@ -315,0 +297,0 @@ /**

@@ -8,4 +8,4 @@ import { defineNode } from '../node.ts'

*
* One entry per content type declared in the spec (e.g. `application/json`,
* `multipart/form-data`), each carrying its own body schema.
* There is one entry per content type declared in the spec (e.g. `application/json`,
* `multipart/form-data`), and each entry holds its own body schema.
*

@@ -12,0 +12,0 @@ * @example

@@ -39,4 +39,8 @@ import { defineNode } from '../node.ts'

* Import name(s) to be used.
* @example ['useState']
* @example 'React'
*
* @example Named imports
* `['useState']`
*
* @example Default import
* `'React'`
*/

@@ -46,17 +50,17 @@ name: ImportName

* Path for the import.
* @example '@kubb/core'
*
* @example
* `'@kubb/core'`
*/
path: string
/**
* Add type-only import prefix.
* Add a type-only import prefix.
* - `true` generates `import type { Type } from './path'`
* - `false` generates `import { Type } from './path'`
* @default false
*/
isTypeOnly?: boolean | null
/**
* Import entire module as namespace.
* Import the entire module as a namespace.
* - `true` generates `import * as Name from './path'`
* - `false` generates standard import
* @default false
* - `false` generates a standard import
*/

@@ -97,4 +101,8 @@ isNameSpace?: boolean | null

* Export name(s) to be used. When omitted, generates a wildcard export.
* @example ['useState']
* @example 'React'
*
* @example Named exports
* `['useState']`
*
* @example Single export
* `'React'`
*/

@@ -104,10 +112,11 @@ name?: string | Array<string> | null

* Path for the export.
* @example '@kubb/core'
*
* @example
* `'@kubb/core'`
*/
path: string
/**
* Add type-only export prefix.
* Add a type-only export prefix.
* - `true` generates `export type { Type } from './path'`
* - `false` generates `export { Type } from './path'`
* @default false
*/

@@ -119,3 +128,2 @@ isTypeOnly?: boolean | null

* - `false` generates a standard export
* @default false
*/

@@ -146,8 +154,6 @@ asAlias?: boolean | null

* Mark this source as a type-only export.
* @default false
*/
isTypeOnly?: boolean | null
/**
* Include `export` keyword in the generated source.
* @default false
* Include the `export` keyword in the generated source.
*/

@@ -157,8 +163,7 @@ isExportable?: boolean | null

* Include this source in barrel/index file generation.
* @default false
*/
isIndexable?: boolean | null
/**
* Structured child nodes representing the content of this source fragment, in DOM order.
* Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
* Child nodes that make up this source fragment, in DOM order.
* Use a {@link TextNode} for raw string content.
*/

@@ -191,4 +196,4 @@ nodes?: Array<CodeNode>

/**
* Unique identifier derived from a SHA256 hash of the file path. Computed
* by `createFile`; callers do not need to provide it.
* Unique identifier derived from a SHA256 hash of the file path. `createFile`
* computes it, so callers do not need to provide it.
*/

@@ -228,3 +233,3 @@ id: string

/**
* Optional metadata attached to this file (used by plugins for barrel generation etc.).
* Optional metadata attached to this file, read by plugins during barrel generation.
*/

@@ -231,0 +236,0 @@ meta?: TMeta

@@ -24,5 +24,2 @@ import { defineNode } from '../node.ts'

export type TypeLiteralNode = BaseNode & {
/**
* Node kind.
*/
kind: 'TypeLiteral'

@@ -59,5 +56,2 @@ /**

export type IndexedAccessTypeNode = BaseNode & {
/**
* Node kind.
*/
kind: 'IndexedAccessType'

@@ -85,5 +79,2 @@ /**

export type ObjectBindingPatternNode = BaseNode & {
/**
* Node kind.
*/
kind: 'ObjectBindingPattern'

@@ -127,5 +118,2 @@ /**

export type FunctionParameterNode = BaseNode & {
/**
* Node kind.
*/
kind: 'FunctionParameter'

@@ -165,5 +153,2 @@ /**

export type FunctionParametersNode = BaseNode & {
/**
* Node kind.
*/
kind: 'FunctionParameters'

@@ -170,0 +155,0 @@ /**

@@ -38,7 +38,7 @@ import type { Streamable } from '@internals/utils'

* Names of schemas that participate in a circular reference chain.
* Computed once during the adapter pre-scan, use this instead of calling
* `findCircularSchemas` per generator call.
* Computed once during the adapter pre-scan, so a generator never has to
* call `findCircularSchemas` itself.
*
* Convert to a `Set` once at the start of a generator, not per-schema,
* to keep lookup O(1) without repeated allocations.
* so lookups stay O(1) without repeated allocations.
*

@@ -54,7 +54,7 @@ * @example Wrap a circular schema in z.lazy()

* Names of schemas whose type is `enum`.
* Computed once during the adapter pre-scan, use this instead of filtering
* schemas per generator call.
* Computed once during the adapter pre-scan, so a generator never has to
* filter the schema list itself.
*
* Convert to a `Set` once at the start of a generator when you need repeated
* membership checks, rather than calling `.includes()` per schema.
* membership checks, so each check stays O(1) instead of an array scan.
*

@@ -61,0 +61,0 @@ * @example Check if a referenced schema is an enum

@@ -44,3 +44,3 @@ import { defineNode } from '../node.ts'

/**
* Parameters that could be used, we have QueryParams, PathParams, HeaderParams and CookieParams
* Query, path, header, and cookie parameters for the operation.
*/

@@ -47,0 +47,0 @@ parameters: Array<ParameterNode>

@@ -22,5 +22,2 @@ import { defineNode, syncOptionality } from '../node.ts'

export type ParameterNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Parameter'

@@ -27,0 +24,0 @@ /**

@@ -19,5 +19,2 @@ import { defineNode, syncOptionality } from '../node.ts'

export type PropertyNode = BaseNode & {
/**
* Node kind.
*/
kind: 'Property'

@@ -24,0 +21,0 @@ /**

@@ -21,8 +21,5 @@ import { defineNode } from '../node.ts'

export type RequestBodyNode = BaseNode & {
/**
* Node kind.
*/
kind: 'RequestBody'
/**
* Human-readable request body description.
* Request body description carried over from the spec.
*/

@@ -36,7 +33,7 @@ description?: string

/**
* All available content type entries for this request body.
* Content type entries for this request body.
*
* When the adapter `contentType` option is set, this array contains exactly one entry for
* that content type. Otherwise it contains one entry per content type declared in the spec,
* so that plugins can generate code for every variant (e.g. separate hooks for
* so plugins can generate code for every variant (for example, separate hooks for
* `application/json` and `multipart/form-data`).

@@ -43,0 +40,0 @@ */

@@ -283,4 +283,5 @@ import type { InferSchemaNode } from '../infer.ts'

/**
* Logical strategy applied to union members: 'one' means exactly one member must be valid (from `oneOf`),
* 'any' means any number of members can be valid (from `anyOf`).
* How many union members must be valid.
* - `'one'`: exactly one member, from `oneOf`
* - `'any'`: any number of members, from `anyOf`
*/

@@ -287,0 +288,0 @@ strategy?: 'one' | 'any'

@@ -109,3 +109,3 @@ import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts'

* Always dispatches through the `nodes` map. Never calls the `print` override.
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
* Reach for it when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/

@@ -112,0 +112,0 @@ transform: (node: SchemaNode) => T['output'] | null

@@ -6,3 +6,2 @@ import type { NodeDef } from './node.ts'

import { functionParameterDef, functionParametersDef, indexedAccessTypeDef, objectBindingPatternDef, typeLiteralDef } from './nodes/function.ts'
import type { Node, NodeKind } from './nodes/index.ts'
import { inputDef } from './nodes/input.ts'

@@ -17,5 +16,35 @@ import { operationDef } from './nodes/operation.ts'

// Surface every def from one place so the package barrel re-exports them with `export * from './registry.ts'`.
// Adding a node means adding its `defineNode` to a `nodes/*.ts` file and listing it in `nodeDefs` below, nothing else.
export {
arrowFunctionDef,
breakDef,
constDef,
contentDef,
exportDef,
fileDef,
functionDef,
functionParameterDef,
functionParametersDef,
importDef,
indexedAccessTypeDef,
inputDef,
jsxDef,
objectBindingPatternDef,
operationDef,
outputDef,
parameterDef,
propertyDef,
requestBodyDef,
responseDef,
schemaDef,
sourceDef,
textDef,
typeDef,
typeLiteralDef,
}
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables below derive from it.
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
*/

@@ -49,25 +78,1 @@ export const nodeDefs = [

] satisfies ReadonlyArray<NodeDef>
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
export const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<
Record<NodeKind, ReadonlyArray<string>>
>
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
export const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<
Record<NodeKind, NonNullable<NodeDef['visitorKey']>>
>
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
export const nodeRebuilders = Object.fromEntries(
nodeDefs.flatMap((def) => (def.rebuild ? [[def.kind, def.create as unknown as (node: Node) => Node] as const] : [])),
) as Partial<Record<NodeKind, (node: Node) => Node>>

@@ -173,5 +173,5 @@ import { hash } from 'node:crypto'

/**
* Node → digest cache, keyed by identity. A `WeakMap` so entries die with the node, and so a tree
* hashed during dedupe planning is not walked again when it is rewritten during streaming. Reuse
* is safe because a signature depends only on content, and nodes are immutable once created.
* Caches the digest per node, keyed by identity. A `WeakMap` so entries die with the node, and so
* a tree hashed during dedupe planning is not walked again when it is rewritten during streaming.
* Reuse is safe because a signature depends only on content, and nodes are immutable once created.
*/

@@ -178,0 +178,0 @@ const signatureCache = new WeakMap<SchemaNode, string>()

@@ -5,3 +5,3 @@ import { isScalarPrimitive } from './constants.ts'

import { createSchema, type SchemaNode } from './nodes/schema.ts'
import { enumPropName } from './utils/index.ts'
import { enumPropName } from './utils/refs.ts'

@@ -166,1 +166,28 @@ /**

}
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* // Ref with description override
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
export function syncSchemaRef(node: SchemaNode): SchemaNode {
const ref = narrowSchema(node, 'ref')
if (!ref) return node
if (!ref.schema) return node
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref
// Filter out undefined override values so they don't shadow the resolved schema's fields.
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))
return createSchema({ ...ref.schema, ...definedOverrides })
}

@@ -5,57 +5,6 @@ export type { DedupeCanonical, DedupeLookups, DedupePlan } from './dedupe.ts'

export type { InferSchemaNode, ParserOptions } from './infer.ts'
export type {
ArraySchemaNode,
ArrowFunctionNode,
CodeNode,
ConstNode,
DateSchemaNode,
DatetimeSchemaNode,
EnumSchemaNode,
ExportNode,
FileNode,
FunctionNode,
FunctionParamKind,
FunctionParameterNode,
FunctionParametersNode,
FunctionParamNode,
HttpMethod,
HttpOperationNode,
ImportNode,
IndexedAccessTypeNode,
InputMeta,
InputNode,
IntersectionSchemaNode,
JSDocNode,
JsxNode,
Node,
NodeKind,
NumberSchemaNode,
ObjectBindingPatternNode,
ObjectSchemaNode,
OperationNode,
OutputNode,
ParameterLocation,
ParameterNode,
PrimitiveSchemaType,
PropertyNode,
RefSchemaNode,
ResponseNode,
ScalarSchemaType,
SchemaNode,
SchemaNodeByType,
SchemaType,
SourceNode,
StatusCode,
StringSchemaNode,
TextNode,
TimeSchemaNode,
TypeExpression,
TypeLiteralNode,
TypeNode,
UnionSchemaNode,
UrlSchemaNode,
} from './nodes/index.ts'
export type * from './nodes/index.ts'
export type { ParentOf, Visitor, VisitorContext } from './visitor.ts'
export type { Printer, PrinterFactoryOptions, PrinterPartial } from './printer.ts'
export type { OperationParamsResolver } from './utils/ast.ts'
export type { OperationParamsResolver } from './utils/operationParams.ts'
export type { UserFileNode } from './factory.ts'

@@ -1,341 +0,9 @@

import { isIdentifier, pascalCase, singleQuote } from '@internals/utils'
import { INDENT } from '../constants.ts'
import type { OperationNode, ParameterNode } from '../nodes/index.ts'
import type { OperationParamsResolver, ParamGroupType } from './ast.ts'
export { isValidVarName } from '@internals/utils'
export { buildJSDoc, buildList, buildObject, objectKey } from './codegen.ts'
export { extractStringsFromNodes } from './extractStringsFromNodes.ts'
export {
buildGroupParam,
buildTypeLiteral,
caseParams,
collectUsedSchemaNames,
containsCircularRef,
findCircularSchemas,
isStringType,
resolveParamType,
syncSchemaRef,
} from './ast.ts'
export type { BuildGroupArgs, ParamGroupType } from './ast.ts'
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
export function trimQuotes(text: string): string {
if (text.length >= 2) {
const first = text[0]
const last = text[text.length - 1]
if ((first === '"' && last === '"') || (first === "'" && last === "'") || (first === '`' && last === '`')) {
return text.slice(1, -1)
}
}
return text
}
/**
* Serializes a primitive value to a single-quoted string literal, stripping any surrounding quote
* characters first. Escaping comes from `JSON.stringify`, then the quote style switches to single
* quotes so generated code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
export function stringify(value: string | number | boolean | undefined): string {
if (value === undefined || value === null) return "''"
const json = JSON.stringify(trimQuotes(value.toString()))
const inner = json.slice(1, -1).replace(/\\"/g, '"').replace(/'/g, "\\'")
return `'${inner}'`
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
export function jsStringEscape(input: unknown): string {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case '"':
case "'":
case '\\':
return `\\${character}`
case '\n':
return '\\n'
case '\r':
return '\\r'
case '\u2028':
return '\\u2028'
case '\u2029':
return '\\u2029'
default:
return ''
}
})
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
export function toRegExpString(text: string, func: string | null = 'RegExp'): string {
const raw = trimQuotes(text)
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i)
const replacementTarget = match?.[1] ?? ''
const matchedFlags = match?.[2]
const cleaned = raw
.replace(/^\\?\//, '')
.replace(/\\?\/$/, '')
.replace(replacementTarget, '')
const { source, flags } = new RegExp(cleaned, matchedFlags)
if (func === null) return `/${source}/${flags}`
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
export function stringifyObject(value: Record<string, unknown>): string {
const items = Object.entries(value)
.map(([key, val]) => {
if (val !== null && typeof val === 'object') {
return `${key}: {\n ${stringifyObject(val as Record<string, unknown>)}\n }`
}
return `${key}: ${val}`
})
.filter(Boolean)
return items.join(',\n')
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
export function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {
const parts = Array.isArray(param) ? param : param.split('.')
if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null
return `${accessor}?.['${`${parts.join("']?.['")}']`}`
}
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
export function buildJSDoc(
comments: Array<string>,
options: {
/**
* String used to indent each comment line.
* @default ' * '
*/
indent?: string
/**
* String appended after the closing tag.
* @default '\n '
*/
suffix?: string
/**
* Returned as-is when `comments` is empty.
* @default ' '
*/
fallback?: string
} = {},
): string {
const { indent = ' * ', suffix = '\n ', fallback = ' ' } = options
if (comments.length === 0) return fallback
return `/**\n${comments.map((c) => `${indent}${c}`).join('\n')}\n */${suffix}`
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text: string): string {
if (!text) return ''
return text
.split('\n')
.map((line) => (line.trim() ? `${INDENT}${line}` : ''))
.join('\n')
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
export function objectKey(name: string): string {
return isIdentifier(name) ? name : singleQuote(name)
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
export function buildObject(entries: Array<string>): string {
if (entries.length === 0) return '{}'
const body = entries.map((entry) => `${indentLines(entry)},`).join('\n')
return `{\n${body}\n}`
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
export function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {
const [open, close] = brackets
if (items.length === 0) return `${open}${close}`
if (!items.some((item) => item.includes('\n'))) return `${open}${items.join(', ')}${close}`
const body = items.map((item) => `${indentLines(item)},`).join('\n')
return `${open}\n${body}\n${close}`
}
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
export function extractRefName(ref: string): string {
return ref.split('/').at(-1) ?? ref
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
export function childName(parentName: string | null | undefined, propName: string): string | null {
return parentName ? pascalCase([parentName, propName].join(' ')) : null
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
export function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {
return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
}
/**
* Returns the discriminator key whose mapping value matches `ref`, or `null` when there is no match.
*
* @example
* ```ts
* findDiscriminator({ dog: '#/components/schemas/Dog' }, '#/components/schemas/Dog') // 'dog'
* ```
*/
export function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {
if (!mapping || !ref) return null
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
export function resolveGroupType({
node,
params,
group,
resolver,
}: {
node: OperationNode
params: Array<ParameterNode>
group: 'query' | 'header'
resolver: OperationParamsResolver | undefined
}): ParamGroupType | null {
if (!resolver || !params.length) {
return null
}
const firstParam = params[0]!
const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName
const groupName = groupMethod.call(resolver, node, firstParam)
if (groupName === resolver.resolveParamName(node, firstParam)) {
return null
}
return { type: groupName, optional: params.every((p) => !p.required) }
}
export { childName, enumPropName, extractRefName, isStringType, resolveGroupType } from './refs.ts'
export { syncSchemaRef } from '../transformers.ts'
export { getNestedAccessor, jsStringEscape, stringify, stringifyObject, toRegExpString, trimQuotes } from './strings.ts'
export { collectUsedSchemaNames, containsCircularRef, findCircularSchemas } from './schemaGraph.ts'
export { buildGroupParam, buildTypeLiteral, caseParams, createOperationParams, resolveParamType } from './operationParams.ts'
export type { BuildGroupArgs, ParamGroupType } from './operationParams.ts'
import type { VisitorDepth } from './constants.ts'
import { visitorDepths, WALK_CONCURRENCY } from './constants.ts'
import { nodeRebuilders, VISITOR_KEY_BY_KIND, VISITOR_KEYS } from './registry.ts'
import type { NodeDef } from './node.ts'
import type {

@@ -8,2 +8,3 @@ ContentNode,

Node,
NodeKind,
OperationNode,

@@ -17,4 +18,29 @@ OutputNode,

} from './nodes/index.ts'
import { nodeDefs } from './registry.ts'
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<
Record<NodeKind, ReadonlyArray<string>>
>
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<
Record<NodeKind, NonNullable<NodeDef['visitorKey']>>
>
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(
nodeDefs.flatMap((def) => (def.rebuild ? [[def.kind, def.create as unknown as (node: Node) => Node] as const] : [])),
) as Partial<Record<NodeKind, (node: Node) => Node>>
/**
* Creates a small async concurrency limiter.

@@ -81,4 +107,3 @@ *

*
* This is used by visitor context so `ctx.parent` is correctly typed
* for each callback.
* Visitor context relies on this so `ctx.parent` is typed for each callback.
*

@@ -139,4 +164,3 @@ * @example

* Plugins typically expose `transformer` so users can supply a `Visitor` that
* rewrites operation IDs, drops descriptions, or otherwise tweaks the AST
* before printing.
* rewrites the AST before printing.
*

@@ -172,3 +196,3 @@ * @example Prefix every operationId

/**
* Utility type for values that can be returned directly or asynchronously.
* A visitor callback result that may be sync or async.
*/

@@ -237,3 +261,3 @@ type MaybePromise<T> = T | Promise<T>

/**
* Traversal depth (`'deep'` by default).
* Traversal depth.
* @default 'deep'

@@ -258,3 +282,3 @@ */

/**
* Traversal depth (`'deep'` by default).
* Traversal depth.
* @default 'deep'

@@ -280,3 +304,3 @@ */

/**
* Traversal depth (`'deep'` by default).
* Traversal depth.
* @default 'deep'

@@ -329,5 +353,5 @@ */

/**
* Invokes the visitor callback that matches `node.kind`, passing the traversal
* context. Returns the callback's result (a replacement node, a collected
* value, or `undefined` when no callback is registered for the kind).
* Runs the visitor callback that matches `node.kind` with the traversal
* context. The result is a replacement node, a collected value, or `undefined`
* when no callback is registered for the kind.
*

@@ -381,3 +405,3 @@ * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives

// run at once. Awaiting each child sequentially here would serialize the whole
// traversal and make `concurrency` inert, every visitor callback would run one
// traversal and make `concurrency` inert. Every visitor callback would run one
// at a time regardless of the limit.

@@ -391,7 +415,7 @@ const children = Array.from(getChildren(node, recurse))

/**
* Synchronous depth-first transform. Each visitor callback gets a chance to
* return a replacement node; `undefined` keeps the original.
* Synchronous depth-first transform. Each visitor callback can return a
* replacement node. Returning `undefined` keeps the original.
*
* The transform is immutable. The original tree is not mutated. A new tree
* is returned. Use `depth: 'shallow'` to skip recursion into children.
* The original tree is never mutated, a new tree is returned. Pass
* `depth: 'shallow'` to skip recursion into children.
*

@@ -503,4 +527,4 @@ * @example Prefix every operationId

/**
* Eager depth-first collection pass. Returns an array of every non-null value
* the visitor callbacks return.
* Eager depth-first collection pass. Gathers every non-null value the visitor
* callbacks return into an array.
*

@@ -507,0 +531,0 @@ * @example Collect every operationId

Sorry, the diff of this file is too big to display

import { n as __name } from "./chunk-CNktS9qV.js";
import { Ht as CodeNode } from "./ast-ClnJg9BN.js";
//#region src/utils/extractStringsFromNodes.d.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string;
//#endregion
export { extractStringsFromNodes as t };
//# sourceMappingURL=extractStringsFromNodes-Bn9cOos9.d.ts.map
import { t as __exportAll } from "./chunk-CNktS9qV.js";
import { B as createOperation, H as createRequestBody, I as createParameter, J as createSource, K as createExport, P as createResponse, R as createOutput, S as combineSources, T as createOperationParams, W as createInput, _t as createFunctionParameter, at as createBreak, b as combineExports, bt as createObjectBindingPattern, ct as createJsx, et as createContent, ht as createProperty, it as createArrowFunction, jt as createSchema, kt as extractStringsFromNodes, lt as createText, ot as createConst, q as createImport, st as createFunction, ut as createType, vt as createFunctionParameters, w as createDiscriminantNode, x as combineImports, xt as createTypeLiteral, yt as createIndexedAccessType } from "./utils-DN4XLVqz.js";
import { hash } from "node:crypto";
import path from "node:path";
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ __exportAll({
createArrowFunction: () => createArrowFunction,
createBreak: () => createBreak,
createConst: () => createConst,
createContent: () => createContent,
createDiscriminantNode: () => createDiscriminantNode,
createExport: () => createExport,
createFile: () => createFile,
createFunction: () => createFunction,
createFunctionParameter: () => createFunctionParameter,
createFunctionParameters: () => createFunctionParameters,
createImport: () => createImport,
createIndexedAccessType: () => createIndexedAccessType,
createInput: () => createInput,
createJsx: () => createJsx,
createObjectBindingPattern: () => createObjectBindingPattern,
createOperation: () => createOperation,
createOperationParams: () => createOperationParams,
createOutput: () => createOutput,
createParameter: () => createParameter,
createProperty: () => createProperty,
createRequestBody: () => createRequestBody,
createResponse: () => createResponse,
createSchema: () => createSchema,
createSource: () => createSource,
createText: () => createText,
createType: () => createType,
createTypeLiteral: () => createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = path.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: hash("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
export { factory_exports as n, update as r, createFile as t };
//# sourceMappingURL=factory-C5gHvtLU.js.map
{"version":3,"file":"factory-C5gHvtLU.js","names":[],"sources":["../../../internals/utils/src/fs.ts","../src/factory.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname, join, posix, resolve } from 'node:path'\nimport { camelCase } from './casing.ts'\nimport { runtime } from './runtime.ts'\n\n/**\n * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and\n * returns the absolute path of the nearest `package.json`, or `null` when none\n * is found before reaching the filesystem root.\n *\n * @example\n * ```ts\n * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'\n * ```\n */\nexport function findPackageJSON(cwd?: string): string | null {\n let dir = cwd ? resolve(cwd) : process.cwd()\n while (true) {\n const pkgPath = join(dir, 'package.json')\n if (existsSync(pkgPath)) return pkgPath\n const parent = dirname(dir)\n if (parent === dir) return null\n dir = parent\n }\n}\n\n/**\n * Converts all backslashes to forward slashes.\n * Extended-length Windows paths (`\\\\?\\...`) are left unchanged.\n */\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Resolves to `true` when the file or directory at `path` exists.\n * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.\n *\n * @example\n * ```ts\n * if (await exists('./kubb.config.ts')) {\n * const content = await read('./kubb.config.ts')\n * }\n * ```\n */\nexport async function exists(path: string): Promise<boolean> {\n if (runtime.isBun) {\n return Bun.file(path).exists()\n }\n return access(path).then(\n () => true,\n () => false,\n )\n}\n\n/**\n * Reads the file at `path` as a UTF-8 string.\n * Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.\n *\n * @example\n * ```ts\n * const source = await read('./src/Pet.ts')\n * ```\n */\nexport async function read(path: string): Promise<string> {\n if (runtime.isBun) {\n return Bun.file(path).text()\n }\n return readFile(path, { encoding: 'utf8' })\n}\n\ntype WriteOptions = {\n /**\n * When `true`, re-reads the file immediately after writing and throws if the\n * content does not match — useful for catching write failures on unreliable file systems.\n */\n sanity?: boolean\n}\n\n/**\n * Writes `data` to `path`, trimming leading/trailing whitespace before saving.\n * Skips the write when the trimmed content is empty or identical to what is already on disk.\n * Creates any missing parent directories automatically.\n * When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.\n *\n * @example\n * ```ts\n * await write('./src/Pet.ts', source) // writes and returns trimmed content\n * await write('./src/Pet.ts', source) // null — file unchanged\n * await write('./src/Pet.ts', ' ') // null — empty content skipped\n * ```\n */\nexport async function write(path: string, data: string, options: WriteOptions = {}): Promise<string | null> {\n const trimmed = data.trim()\n if (trimmed === '') return null\n\n const resolved = resolve(path)\n\n if (runtime.isBun) {\n const file = Bun.file(resolved)\n const oldContent = (await file.exists()) ? await file.text() : null\n if (oldContent === trimmed) return null\n await Bun.write(resolved, trimmed)\n return trimmed\n }\n\n try {\n const oldContent = await readFile(resolved, { encoding: 'utf-8' })\n if (oldContent === trimmed) return null\n } catch {\n /* file doesn't exist yet */\n }\n\n await mkdir(dirname(resolved), { recursive: true })\n await writeFile(resolved, trimmed, { encoding: 'utf-8' })\n\n if (options.sanity) {\n const savedData = await readFile(resolved, { encoding: 'utf-8' })\n if (savedData !== trimmed) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n return savedData\n }\n\n return trimmed\n}\n\n/**\n * Recursively removes `path`. Silently succeeds when `path` does not exist.\n *\n * @example\n * ```ts\n * await clean('./dist')\n * ```\n */\nexport async function clean(path: string): Promise<void> {\n return rm(path, { recursive: true, force: true })\n}\n\n/**\n * Converts a filesystem path to use POSIX (`/`) separators.\n *\n * Most of the codebase compares and composes paths as strings (prefix matching, joining for\n * import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated\n * paths, but on Windows it returns `\\`-separated paths, which breaks every such comparison.\n *\n * Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the\n * code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is\n * exercisable from POSIX CI.\n *\n * @example\n * toPosixPath('C:\\\\repo\\\\src\\\\pet.ts') // 'C:/repo/src/pet.ts'\n */\nexport function toPosixPath(filePath: string): string {\n return filePath.replaceAll('\\\\', '/')\n}\n\n/**\n * Strips the file extension from a path or file name.\n * Only removes the last `.ext` segment when the dot is not part of a directory name.\n *\n * @example\n * trimExtName('petStore.ts') // 'petStore'\n * trimExtName('/src/models/pet.ts') // '/src/models/pet'\n * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'\n * trimExtName('noExtension') // 'noExtension'\n */\nexport function trimExtName(text: string): string {\n const dotIndex = text.lastIndexOf('.')\n if (dotIndex > 0 && !text.includes('/', dotIndex)) {\n return text.slice(0, dotIndex)\n }\n return text\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { trimExtName } from '@internals/utils'\nimport type { FileNode, Node } from './nodes/index.ts'\nimport { combineExports, combineImports, combineSources } from './utils/ast.ts'\nimport { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createImport, createSource } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\nexport { createDiscriminantNode, createOperationParams } from './utils/ast.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const rawExtname = path.extname(input.baseName)\n // Handle dotfile basename like '.ts' where path.extname returns ''\n const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const source = (input.sources ?? [])\n .flatMap((item) => item.nodes ?? [])\n .map((node) => extractStringsFromNodes([node]))\n .filter(Boolean)\n .join('\\n\\n')\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []\n const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))\n const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))\n // Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's\n // definition and a cross-file import of it in the same file. The first pass catches imports that\n // resolve to this file's own path. The second drops imports of names the file already defines,\n // the case consolidation produces when the import path no longer matches `input.path`. Sources\n // stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.\n const resolvedImports = combinedImports\n .filter((imp) => imp.path !== input.path)\n .flatMap((imp) => {\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n return {\n kind: 'File',\n ...input,\n id: hash('sha256', input.path, 'hex'),\n name: trimExtName(input.baseName),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: input.meta ?? ({} as TMeta),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA8LA,SAAgB,YAAY,MAAsB;CAChD,MAAM,WAAW,KAAK,YAAY,GAAG;CACrC,IAAI,WAAW,KAAK,CAAC,KAAK,SAAS,KAAK,QAAQ,GAC9C,OAAO,KAAK,MAAM,GAAG,QAAQ;CAE/B,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC5JA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,WAA0C,OAA6C;CAGrG,MAAM,UAFa,KAAK,QAAQ,MAAM,QAEZ,MAAM,MAAM,SAAS,WAAW,GAAG,IAAI,MAAM,WAAW;CAClF,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,wBAAwB,MAAM,UAAU;CAG1D,MAAM,UAAU,MAAM,WAAW,CAAC,EAAA,CAC/B,SAAS,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,CACnC,KAAK,SAAS,wBAAwB,CAAC,IAAI,CAAC,CAAC,CAAC,CAC9C,OAAO,OAAO,CAAC,CACf,KAAK,MAAM;CACd,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,OAAO,IAAI,CAAC;CACjF,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,SAAS,iBAAiB,UAAU,KAAA,CAAS,IAAI,CAAC;CACvH,MAAM,aAAa,IAAI,KAAK,MAAM,WAAW,CAAC,EAAA,CAAG,KAAK,SAAS,KAAK,IAAI,CAAC,CAAC,QAAQ,SAAyB,QAAQ,IAAI,CAAC,CAAC;CACzH,MAAM,UAAU,SAAoE,OAAO,SAAS,WAAW,OAAQ,KAAK,QAAQ,KAAK;CAMzI,MAAM,kBAAkB,gBACrB,QAAQ,QAAQ,IAAI,SAAS,MAAM,IAAI,CAAC,CACxC,SAAS,QAAQ;EAChB,IAAI,CAAC,MAAM,QAAQ,IAAI,IAAI,GACzB,OAAO,OAAO,IAAI,SAAS,YAAY,WAAW,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG;EAE7E,MAAM,OAAO,IAAI,KAAK,QAAQ,SAAS,CAAC,WAAW,IAAI,OAAO,IAAI,CAAC,CAAC;EACpE,IAAI,CAAC,KAAK,QAAQ,OAAO,CAAC;EAC1B,OAAO,CAAC,KAAK,WAAW,IAAI,KAAK,SAAS,MAAM;GAAE,GAAG;GAAK,MAAM;EAAK,CAAC;CACxE,CAAC;CACH,MAAM,kBAAkB,MAAM,SAAS,SAAS,eAAe,MAAM,OAAO,IAAI,CAAC;CAEjF,OAAO;EACL,MAAM;EACN,GAAG;EACH,IAAI,KAAK,UAAU,MAAM,MAAM,KAAK;EACpC,MAAM,YAAY,MAAM,QAAQ;EAChC;EACA,SAAS;EACT,SAAS;EACT,SAAS;EACT,MAAM,MAAM,QAAS,CAAC;CACxB;AACF"}
const require_utils = require("./utils-C8bWAzhv.cjs");
let node_crypto = require("node:crypto");
let node_path = require("node:path");
node_path = require_utils.__toESM(node_path, 1);
//#region ../../internals/utils/src/fs.ts
/**
* Strips the file extension from a path or file name.
* Only removes the last `.ext` segment when the dot is not part of a directory name.
*
* @example
* trimExtName('petStore.ts') // 'petStore'
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
* trimExtName('noExtension') // 'noExtension'
*/
function trimExtName(text) {
const dotIndex = text.lastIndexOf(".");
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
return text;
}
//#endregion
//#region src/factory.ts
var factory_exports = /* @__PURE__ */ require_utils.__exportAll({
createArrowFunction: () => require_utils.createArrowFunction,
createBreak: () => require_utils.createBreak,
createConst: () => require_utils.createConst,
createContent: () => require_utils.createContent,
createDiscriminantNode: () => require_utils.createDiscriminantNode,
createExport: () => require_utils.createExport,
createFile: () => createFile,
createFunction: () => require_utils.createFunction,
createFunctionParameter: () => require_utils.createFunctionParameter,
createFunctionParameters: () => require_utils.createFunctionParameters,
createImport: () => require_utils.createImport,
createIndexedAccessType: () => require_utils.createIndexedAccessType,
createInput: () => require_utils.createInput,
createJsx: () => require_utils.createJsx,
createObjectBindingPattern: () => require_utils.createObjectBindingPattern,
createOperation: () => require_utils.createOperation,
createOperationParams: () => require_utils.createOperationParams,
createOutput: () => require_utils.createOutput,
createParameter: () => require_utils.createParameter,
createProperty: () => require_utils.createProperty,
createRequestBody: () => require_utils.createRequestBody,
createResponse: () => require_utils.createResponse,
createSchema: () => require_utils.createSchema,
createSource: () => require_utils.createSource,
createText: () => require_utils.createText,
createType: () => require_utils.createType,
createTypeLiteral: () => require_utils.createTypeLiteral,
update: () => update
});
/**
* Identity-preserving node update: returns `node` unchanged when every field in
* `changes` already equals (by reference) the current value, otherwise a new node
* with the changes applied.
*
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
* downstream passes can detect "nothing changed" by identity. Comparison is
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
*
* @example
* ```ts
* update(node, { name: node.name }) // -> same `node` reference
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
* ```
*/
function update(node, changes) {
for (const key in changes) if (changes[key] !== node[key]) return {
...node,
...changes
};
return node;
}
/**
* Creates a fully resolved `FileNode` from a file input descriptor.
*
* Computes:
* - `id` SHA256 hash of the file path
* - `name` `baseName` without extension
* - `extname` extension extracted from `baseName`
*
* Deduplicates:
* - `sources` via `combineSources`
* - `exports` via `combineExports`
* - `imports` via `combineImports` (also filters unused imports)
*
* @throws {Error} when `baseName` has no extension.
*
* @example
* ```ts
* const file = createFile({
* baseName: 'petStore.ts',
* path: 'src/models/petStore.ts',
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
* imports: [createImport({ name: ['z'], path: 'zod' })],
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
* })
* // file.id = SHA256 hash of 'src/models/petStore.ts'
* // file.name = 'petStore'
* // file.extname = '.ts'
* ```
*/
function createFile(input) {
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => require_utils.extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
const resolvedExports = input.exports?.length ? require_utils.combineExports(input.exports) : [];
const combinedImports = input.imports?.length ? require_utils.combineImports(input.imports, resolvedExports, source || void 0) : [];
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
if (!kept.length) return [];
return [kept.length === imp.name.length ? imp : {
...imp,
name: kept
}];
});
const resolvedSources = input.sources?.length ? require_utils.combineSources(input.sources) : [];
return {
kind: "File",
...input,
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
name: trimExtName(input.baseName),
extname,
imports: resolvedImports,
exports: resolvedExports,
sources: resolvedSources,
meta: input.meta ?? {}
};
}
//#endregion
Object.defineProperty(exports, "createFile", {
enumerable: true,
get: function() {
return createFile;
}
});
Object.defineProperty(exports, "factory_exports", {
enumerable: true,
get: function() {
return factory_exports;
}
});
Object.defineProperty(exports, "update", {
enumerable: true,
get: function() {
return update;
}
});
//# sourceMappingURL=factory-JN-Ylfl6.cjs.map
{"version":3,"file":"factory-JN-Ylfl6.cjs","names":["path","extractStringsFromNodes","combineExports","combineImports","combineSources"],"sources":["../../../internals/utils/src/fs.ts","../src/factory.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname, join, posix, resolve } from 'node:path'\nimport { camelCase } from './casing.ts'\nimport { runtime } from './runtime.ts'\n\n/**\n * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and\n * returns the absolute path of the nearest `package.json`, or `null` when none\n * is found before reaching the filesystem root.\n *\n * @example\n * ```ts\n * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'\n * ```\n */\nexport function findPackageJSON(cwd?: string): string | null {\n let dir = cwd ? resolve(cwd) : process.cwd()\n while (true) {\n const pkgPath = join(dir, 'package.json')\n if (existsSync(pkgPath)) return pkgPath\n const parent = dirname(dir)\n if (parent === dir) return null\n dir = parent\n }\n}\n\n/**\n * Converts all backslashes to forward slashes.\n * Extended-length Windows paths (`\\\\?\\...`) are left unchanged.\n */\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Resolves to `true` when the file or directory at `path` exists.\n * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.\n *\n * @example\n * ```ts\n * if (await exists('./kubb.config.ts')) {\n * const content = await read('./kubb.config.ts')\n * }\n * ```\n */\nexport async function exists(path: string): Promise<boolean> {\n if (runtime.isBun) {\n return Bun.file(path).exists()\n }\n return access(path).then(\n () => true,\n () => false,\n )\n}\n\n/**\n * Reads the file at `path` as a UTF-8 string.\n * Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.\n *\n * @example\n * ```ts\n * const source = await read('./src/Pet.ts')\n * ```\n */\nexport async function read(path: string): Promise<string> {\n if (runtime.isBun) {\n return Bun.file(path).text()\n }\n return readFile(path, { encoding: 'utf8' })\n}\n\ntype WriteOptions = {\n /**\n * When `true`, re-reads the file immediately after writing and throws if the\n * content does not match — useful for catching write failures on unreliable file systems.\n */\n sanity?: boolean\n}\n\n/**\n * Writes `data` to `path`, trimming leading/trailing whitespace before saving.\n * Skips the write when the trimmed content is empty or identical to what is already on disk.\n * Creates any missing parent directories automatically.\n * When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.\n *\n * @example\n * ```ts\n * await write('./src/Pet.ts', source) // writes and returns trimmed content\n * await write('./src/Pet.ts', source) // null — file unchanged\n * await write('./src/Pet.ts', ' ') // null — empty content skipped\n * ```\n */\nexport async function write(path: string, data: string, options: WriteOptions = {}): Promise<string | null> {\n const trimmed = data.trim()\n if (trimmed === '') return null\n\n const resolved = resolve(path)\n\n if (runtime.isBun) {\n const file = Bun.file(resolved)\n const oldContent = (await file.exists()) ? await file.text() : null\n if (oldContent === trimmed) return null\n await Bun.write(resolved, trimmed)\n return trimmed\n }\n\n try {\n const oldContent = await readFile(resolved, { encoding: 'utf-8' })\n if (oldContent === trimmed) return null\n } catch {\n /* file doesn't exist yet */\n }\n\n await mkdir(dirname(resolved), { recursive: true })\n await writeFile(resolved, trimmed, { encoding: 'utf-8' })\n\n if (options.sanity) {\n const savedData = await readFile(resolved, { encoding: 'utf-8' })\n if (savedData !== trimmed) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n return savedData\n }\n\n return trimmed\n}\n\n/**\n * Recursively removes `path`. Silently succeeds when `path` does not exist.\n *\n * @example\n * ```ts\n * await clean('./dist')\n * ```\n */\nexport async function clean(path: string): Promise<void> {\n return rm(path, { recursive: true, force: true })\n}\n\n/**\n * Converts a filesystem path to use POSIX (`/`) separators.\n *\n * Most of the codebase compares and composes paths as strings (prefix matching, joining for\n * import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated\n * paths, but on Windows it returns `\\`-separated paths, which breaks every such comparison.\n *\n * Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the\n * code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is\n * exercisable from POSIX CI.\n *\n * @example\n * toPosixPath('C:\\\\repo\\\\src\\\\pet.ts') // 'C:/repo/src/pet.ts'\n */\nexport function toPosixPath(filePath: string): string {\n return filePath.replaceAll('\\\\', '/')\n}\n\n/**\n * Strips the file extension from a path or file name.\n * Only removes the last `.ext` segment when the dot is not part of a directory name.\n *\n * @example\n * trimExtName('petStore.ts') // 'petStore'\n * trimExtName('/src/models/pet.ts') // '/src/models/pet'\n * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'\n * trimExtName('noExtension') // 'noExtension'\n */\nexport function trimExtName(text: string): string {\n const dotIndex = text.lastIndexOf('.')\n if (dotIndex > 0 && !text.includes('/', dotIndex)) {\n return text.slice(0, dotIndex)\n }\n return text\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { trimExtName } from '@internals/utils'\nimport type { FileNode, Node } from './nodes/index.ts'\nimport { combineExports, combineImports, combineSources } from './utils/ast.ts'\nimport { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createImport, createSource } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\nexport { createDiscriminantNode, createOperationParams } from './utils/ast.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const rawExtname = path.extname(input.baseName)\n // Handle dotfile basename like '.ts' where path.extname returns ''\n const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const source = (input.sources ?? [])\n .flatMap((item) => item.nodes ?? [])\n .map((node) => extractStringsFromNodes([node]))\n .filter(Boolean)\n .join('\\n\\n')\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []\n const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))\n const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))\n // Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's\n // definition and a cross-file import of it in the same file. The first pass catches imports that\n // resolve to this file's own path. The second drops imports of names the file already defines,\n // the case consolidation produces when the import path no longer matches `input.path`. Sources\n // stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.\n const resolvedImports = combinedImports\n .filter((imp) => imp.path !== input.path)\n .flatMap((imp) => {\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n return {\n kind: 'File',\n ...input,\n id: hash('sha256', input.path, 'hex'),\n name: trimExtName(input.baseName),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: input.meta ?? ({} as TMeta),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA8LA,SAAgB,YAAY,MAAsB;CAChD,MAAM,WAAW,KAAK,YAAY,GAAG;CACrC,IAAI,WAAW,KAAK,CAAC,KAAK,SAAS,KAAK,QAAQ,GAC9C,OAAO,KAAK,MAAM,GAAG,QAAQ;CAE/B,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AC5JA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,WAA0C,OAA6C;CAGrG,MAAM,UAFaA,UAAAA,QAAK,QAAQ,MAAM,QAEZ,MAAM,MAAM,SAAS,WAAW,GAAG,IAAI,MAAM,WAAW;CAClF,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,wBAAwB,MAAM,UAAU;CAG1D,MAAM,UAAU,MAAM,WAAW,CAAC,EAAA,CAC/B,SAAS,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,CACnC,KAAK,SAASC,cAAAA,wBAAwB,CAAC,IAAI,CAAC,CAAC,CAAC,CAC9C,OAAO,OAAO,CAAC,CACf,KAAK,MAAM;CACd,MAAM,kBAAkB,MAAM,SAAS,SAASC,cAAAA,eAAe,MAAM,OAAO,IAAI,CAAC;CACjF,MAAM,kBAAkB,MAAM,SAAS,SAASC,cAAAA,eAAe,MAAM,SAAS,iBAAiB,UAAU,KAAA,CAAS,IAAI,CAAC;CACvH,MAAM,aAAa,IAAI,KAAK,MAAM,WAAW,CAAC,EAAA,CAAG,KAAK,SAAS,KAAK,IAAI,CAAC,CAAC,QAAQ,SAAyB,QAAQ,IAAI,CAAC,CAAC;CACzH,MAAM,UAAU,SAAoE,OAAO,SAAS,WAAW,OAAQ,KAAK,QAAQ,KAAK;CAMzI,MAAM,kBAAkB,gBACrB,QAAQ,QAAQ,IAAI,SAAS,MAAM,IAAI,CAAC,CACxC,SAAS,QAAQ;EAChB,IAAI,CAAC,MAAM,QAAQ,IAAI,IAAI,GACzB,OAAO,OAAO,IAAI,SAAS,YAAY,WAAW,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG;EAE7E,MAAM,OAAO,IAAI,KAAK,QAAQ,SAAS,CAAC,WAAW,IAAI,OAAO,IAAI,CAAC,CAAC;EACpE,IAAI,CAAC,KAAK,QAAQ,OAAO,CAAC;EAC1B,OAAO,CAAC,KAAK,WAAW,IAAI,KAAK,SAAS,MAAM;GAAE,GAAG;GAAK,MAAM;EAAK,CAAC;CACxE,CAAC;CACH,MAAM,kBAAkB,MAAM,SAAS,SAASC,cAAAA,eAAe,MAAM,OAAO,IAAI,CAAC;CAEjF,OAAO;EACL,MAAM;EACN,GAAG;EACH,KAAA,GAAA,YAAA,KAAA,CAAS,UAAU,MAAM,MAAM,KAAK;EACpC,MAAM,YAAY,MAAM,QAAQ;EAChC;EACA,SAAS;EACT,SAAS;EACT,SAAS;EACT,MAAM,MAAM,QAAS,CAAC;CACxB;AACF"}
import { n as __name } from "./chunk-CNktS9qV.js";
import { Dt as SchemaNode, I as ParameterNode, It as PropertyNode, M as RequestBodyNode, O as ResponseNode, Ot as SchemaNodeByType, T as OperationNode, b as InputNode, g as OutputNode, h as Node, kt as SchemaType, mt as ContentNode } from "./ast-ClnJg9BN.js";
//#region src/constants.d.ts
/**
* Traversal depth for AST visitor utilities.
*
* - `'shallow'` visits only the immediate node, skipping children.
* - `'deep'` recursively visits all descendant nodes.
*/
type VisitorDepth = 'shallow' | 'deep';
/**
* Schema type discriminators used by all AST schema nodes.
*
* These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
*/
declare const schemaTypes: {
/**
* Text value.
*/
readonly string: "string";
/**
* Floating-point number (`float`, `double`).
*/
readonly number: "number";
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
readonly integer: "integer";
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
readonly bigint: "bigint";
/**
* Boolean value
*/
readonly boolean: "boolean";
/**
* Explicit null value.
*/
readonly null: "null";
/**
* Any value (no type restriction).
*/
readonly any: "any";
/**
* Unknown value (must be narrowed before usage).
*/
readonly unknown: "unknown";
/**
* No return value (`void`).
*/
readonly void: "void";
/**
* Object with named properties.
*/
readonly object: "object";
/**
* Sequential list of items.
*/
readonly array: "array";
/**
* Fixed-length list with position-specific items.
*/
readonly tuple: "tuple";
/**
* "One of" multiple schema members.
*/
readonly union: "union";
/**
* "All of" multiple schema members.
*/
readonly intersection: "intersection";
/**
* Enum schema.
*/
readonly enum: "enum";
/**
* Reference to another schema.
*/
readonly ref: "ref";
/**
* Calendar date (for example `2026-03-24`).
*/
readonly date: "date";
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
readonly datetime: "datetime";
/**
* Time-only value (for example `09:00:00`).
*/
readonly time: "time";
/**
* UUID value.
*/
readonly uuid: "uuid";
/**
* Email address value.
*/
readonly email: "email";
/**
* URL value.
*/
readonly url: "url";
/**
* IPv4 address value.
*/
readonly ipv4: "ipv4";
/**
* IPv6 address value.
*/
readonly ipv6: "ipv6";
/**
* Binary/blob value.
*/
readonly blob: "blob";
/**
* Impossible value (`never`).
*/
readonly never: "never";
};
/**
* HTTP method identifiers used by operation nodes.
*
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
*/
declare const httpMethods: {
readonly get: "GET";
readonly post: "POST";
readonly put: "PUT";
readonly patch: "PATCH";
readonly delete: "DELETE";
readonly head: "HEAD";
readonly options: "OPTIONS";
readonly trace: "TRACE";
};
//#endregion
//#region src/dedupe.d.ts
/**
* A canonical destination for a deduplicated shape: the shared schema name and
* the synthetic `$ref` path that points at it.
*/
type DedupeCanonical = {
/**
* Canonical schema name every duplicate occurrence refers to.
*/
name: string;
/**
* `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
*/
ref: string;
};
/**
* The result of {@link buildDedupePlan}: a lookup from structural signature to its
* canonical target, plus the freshly hoisted definitions that must be added to
* the schema list.
*/
type DedupePlan = {
/**
* Maps a structural signature to the canonical schema that represents it.
*/
canonicalBySignature: Map<string, DedupeCanonical>;
/**
* Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
* references to the duplicate can be repointed at the first schema with the same content.
*/
aliasNames: Map<string, DedupeCanonical>;
/**
* New top-level schema definitions created for inline shapes that had no existing
* named component. Nested duplicates inside each definition are already collapsed.
*/
hoisted: Array<SchemaNode>;
};
/**
* The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
*/
type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>;
/**
* Options that inject the naming and candidate policy into {@link buildDedupePlan}.
* The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
*/
type BuildDedupePlanOptions = {
/**
* Returns `true` when a node should be deduplicated. This is the only gate, so it must
* reject both ineligible kinds (return `false` for anything other than, say, enums and
* objects) and unsafe shapes (e.g. nodes that reference a circular schema).
*/
isCandidate: (node: SchemaNode) => boolean;
/**
* Produces the canonical name for an inline shape with no existing named component.
* Return `null` to leave the shape inline (for example when no contextual name exists).
*/
nameFor: (node: SchemaNode, signature: string) => string | null;
/**
* Builds the `$ref` path for a canonical name.
*/
refFor: (name: string) => string;
/**
* Minimum number of occurrences before a shape is deduplicated.
*
* @default 2
*/
minOccurrences?: number;
};
/**
* Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
* target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
* so nested duplicates inside a replaced shape are not visited again. A `ref` that points
* at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
* the same content.
*
* Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
* turned into a reference to itself. Nested duplicates are still collapsed.
*
* @example
* ```ts
* const next = applyDedupe(operationNode, plan)
* ```
*/
declare function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode;
declare function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode;
/**
* Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
*
* A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
* is a named top-level schema, the first one becomes the canonical (so other top-level
* duplicates and inline copies turn into references to it). Every other top-level name with
* the same content is recorded in `aliasNames`, so refs to it can be repointed at the
* canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
* per node with {@link applyDedupe}.
*
* @example
* ```ts
* const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
* isCandidate: (node) => node.type === 'enum' || node.type === 'object',
* nameFor: (node) => node.name ?? null,
* refFor: (name) => `#/components/schemas/${name}`,
* })
* ```
*/
declare function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan;
//#endregion
//#region src/dialect.d.ts
/**
* The spec-specific questions a schema parser answers while turning a source document into Kubb
* AST nodes. The rest of the pipeline is generic JSON Schema, so this is the one seam where
* OpenAPI, AsyncAPI, and plain JSON Schema differ.
*/
type SchemaDialect<TSchema = unknown, TRef = TSchema, TDiscriminated = TSchema, TDocument = unknown> = {
/**
* Identifies the dialect in logs and diagnostics.
*/
name: string;
/**
* Whether the schema is nullable.
*/
isNullable: (schema?: TSchema) => boolean;
/**
* Whether the value is a `$ref` pointer.
*/
isReference: (value?: unknown) => value is TRef;
/**
* Whether the schema carries a discriminator for polymorphism.
*/
isDiscriminator: (value?: unknown) => value is TDiscriminated;
/**
* Whether the schema is binary data, converted to a `blob` node.
*/
isBinary: (schema: TSchema) => boolean;
/**
* Resolves a local `$ref` against the document, or nullish when it cannot.
*/
resolveRef: <TResolved>(document: TDocument, ref: string) => TResolved | null | undefined;
};
/**
* Types a {@link SchemaDialect} for an adapter. Adds no runtime behavior and only pins the
* dialect's type for inference.
*
* @example
* ```ts
* export const oasDialect = defineSchemaDialect({
* name: 'oas',
* isNullable,
* isReference,
* isDiscriminator,
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
* resolveRef,
* })
* ```
*/
declare function defineSchemaDialect<TSchema, TRef, TDiscriminated, TDocument>(dialect: SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>): SchemaDialect<TSchema, TRef, TDiscriminated, TDocument>;
//#endregion
//#region src/printer.d.ts
/**
* Runtime context passed as `this` to printer handlers.
*
* `this.transform` dispatches to node-level handlers from `nodes`.
*
* @example
* ```ts
* const context: PrinterHandlerContext<string, {}> = {
* options: {},
* transform: () => 'value',
* }
* ```
*/
type PrinterHandlerContext<TOutput, TOptions extends object> = {
/**
* Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
* Use `this.transform` inside `nodes` handlers and inside the `print` override.
*/
transform: (node: SchemaNode) => TOutput | null;
/**
* Options for this printer instance.
*/
options: TOptions;
};
/**
* Handler for one schema node type.
*
* Use a regular function (not an arrow function) if you need `this`.
*
* @example
* ```ts
* const handler: PrinterHandler<string, {}, 'string'> = function () {
* return 'string'
* }
* ```
*/
type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
/**
* Partial map of per-node-type handler overrides for a printer.
*
* Each key is a `SchemaType` string (e.g. `'date'`, `'string'`).
* Supply only the handlers you want to replace. The printer's built-in
* defaults fill in the rest.
*
* @example
* ```ts
* pluginZod({
* printer: {
* nodes: {
* date(): string {
* return 'z.string().date()'
* },
* } satisfies PrinterPartial<string, PrinterZodOptions>,
* },
* })
* ```
*/
type PrinterPartial<TOutput, TOptions extends object> = Partial<{ [K in SchemaType]: PrinterHandler<TOutput, TOptions, K> }>;
/**
* Generic shape used by `definePrinter`.
*
* - `TName` unique string identifier (e.g. `'zod'`, `'ts'`)
* - `TOptions` options passed to and stored on the printer instance
* - `TOutput` the type emitted by node handlers
* - `TPrintOutput` type returned by public `print` (defaults to `TOutput`)
*
* @example
* ```ts
* type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
* ```
*/
type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
name: TName;
options: TOptions;
output: TOutput;
printOutput: TPrintOutput;
};
/**
* Printer instance returned by a printer factory.
*
* @example
* ```ts
* const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
* ```
*/
type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
/**
* Unique identifier supplied at creation time.
*/
name: T['name'];
/**
* Options for this printer instance.
*/
options: T['options'];
/**
* Node-level dispatcher, converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
* Always dispatches through the `nodes` map. Never calls the `print` override.
* Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
*/
transform: (node: SchemaNode) => T['output'] | null;
/**
* Public printer. If the builder provides a root-level `print`, this calls that
* higher-level function (which may produce full declarations).
* Otherwise, falls back to the node-level dispatcher.
*/
print: (node: SchemaNode) => T['printOutput'] | null;
};
/**
* Builder function passed to `definePrinter`.
*
* It receives resolved options and returns:
* - `name`
* - `options`
* - `nodes` handlers
* - optional top-level `print` override
*
* @example
* ```ts
* const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
* ```
*/
type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
name: T['name'];
/**
* Options to store on the printer.
*/
options: T['options'];
nodes: Partial<{ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K> }>;
/**
* Optional root-level print override. When provided, becomes the public `printer.print`.
* Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
* not the override itself, so recursion is safe.
*/
print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null;
};
/**
* Defines a schema printer: a function that takes a `SchemaNode` and emits
* code in your target language. Each plugin that produces code from schemas
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
* with this helper.
*
* The builder receives resolved options and returns:
*
* - `name` unique identifier for the printer.
* - `options` stored on the returned printer instance.
* - `nodes` map of `SchemaType` → handler. Handlers return the rendered
* output (a string, a TypeScript AST node, ...) for that schema type.
* - `print` (optional), top-level override exposed as `printer.print`.
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
*
* Without a `print` override, `printer.print` falls back to `printer.transform`
* (the node-level dispatcher).
*
* @example Tiny Zod printer
* ```ts
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
*
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
*
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
* name: 'zod',
* options: { strict: options.strict ?? true },
* nodes: {
* string: () => 'z.string()',
* object(node) {
* const props = node.properties
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
* .join(', ')
* return `z.object({ ${props} })`
* },
* },
* }))
* ```
*/
declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T>;
/**
* Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
*
* @example
* ```ts
* export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>(
* (node) => node.kind,
* )
* ```
*/
declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
name: T["name"];
options: T["options"];
nodes: Partial<{ [K in TKey]: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNodeByKey[K]) => T["output"] | null }>;
print?: (this: {
transform: (node: TNode) => T["output"] | null;
options: T["options"];
}, node: TNode) => T["printOutput"] | null;
}) => (options?: T["options"]) => {
name: T["name"];
options: T["options"];
transform: (node: TNode) => T["output"] | null;
print: (node: TNode) => T["printOutput"] | null;
};
//#endregion
//#region src/visitor.d.ts
/**
* Ordered mapping of `[NodeType, ParentType]` pairs.
*
* `ParentOf` uses this map to find parent types.
*/
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
/**
* Resolves the parent node type for a given AST node type.
*
* This is used by visitor context so `ctx.parent` is correctly typed
* for each callback.
*
* @example
* ```ts
* type InputParent = ParentOf<InputNode>
* // undefined
* ```
*
* @example
* ```ts
* type PropertyParent = ParentOf<PropertyNode>
* // SchemaNode
* ```
*
* @example
* ```ts
* type SchemaParent = ParentOf<SchemaNode>
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
* ```
*/
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
/**
* Traversal context passed as the second argument to every visitor callback.
* `parent` is typed from the current node type.
*
* @example
* ```ts
* const visitor: Visitor = {
* schema(node, { parent }) {
* // parent type is narrowed by node kind
* },
* }
* ```
*/
type VisitorContext<T extends Node = Node> = {
/**
* Parent node of the currently visited node.
* For `InputNode`, this is `undefined`.
*/
parent?: ParentOf<T>;
};
/**
* Synchronous visitor consumed by `transform`. Each optional callback runs
* for the matching node type. Return a new node to replace it, or `undefined`
* to leave it untouched.
*
* Plugins typically expose `transformer` so users can supply a `Visitor` that
* rewrites operation IDs, drops descriptions, or otherwise tweaks the AST
* before printing.
*
* @example Prefix every operationId
* ```ts
* const visitor: Visitor = {
* operation(node) {
* return { ...node, operationId: `api_${node.operationId}` }
* },
* }
* ```
*
* @example Strip schema descriptions
* ```ts
* const visitor: Visitor = {
* schema(node) {
* return { ...node, description: undefined }
* },
* }
* ```
*/
type Visitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
};
/**
* Utility type for values that can be returned directly or asynchronously.
*/
type MaybePromise<T> = T | Promise<T>;
/**
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
*
* @example
* ```ts
* const visitor: AsyncVisitor = {
* async operation(node) {
* await Promise.resolve(node.operationId)
* },
* }
* ```
*/
type AsyncVisitor = {
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
};
/**
* Visitor used by `collect`.
*
* @example
* ```ts
* const visitor: CollectVisitor<string> = {
* operation(node) {
* return node.operationId
* },
* }
* ```
*/
type CollectVisitor<T> = {
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
};
/**
* Options for `transform`.
*
* @example
* ```ts
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
* ```
*
* @example
* ```ts
* // Only transform the current node, not nested children
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
* ```
*/
type TransformOptions = Visitor & {
/**
* Traversal depth (`'deep'` by default).
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Options for `walk`.
*
* @example
* ```ts
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
* ```
*/
type WalkOptions = AsyncVisitor & {
/**
* Traversal depth (`'deep'` by default).
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Maximum number of sibling nodes visited concurrently.
* @default 30
*/
concurrency?: number;
};
/**
* Options for `collect`.
*
* @example
* ```ts
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
* ```
*/
type CollectOptions<T> = CollectVisitor<T> & {
/**
* Traversal depth (`'deep'` by default).
* @default 'deep'
*/
depth?: VisitorDepth;
/**
* Internal parent override used during recursion.
*/
parent?: Node;
};
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
declare function walk(node: Node, options: WalkOptions): Promise<void>;
/**
* Synchronous depth-first transform. Each visitor callback gets a chance to
* return a replacement node; `undefined` keeps the original.
*
* The transform is immutable. The original tree is not mutated. A new tree
* is returned. Use `depth: 'shallow'` to skip recursion into children.
*
* @example Prefix every operationId
* ```ts
* const next = transform(root, {
* operation(node) {
* return { ...node, operationId: `prefixed_${node.operationId}` }
* },
* })
* ```
*
* @example Replace only the root node
* ```ts
* const next = transform(root, {
* depth: 'shallow',
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
* })
* ```
*/
declare function transform(node: InputNode, options: TransformOptions): InputNode;
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
declare function transform(node: Node, options: TransformOptions): Node;
/**
* Eager depth-first collection pass. Returns an array of every non-null value
* the visitor callbacks return.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
//#endregion
export { applyDedupe as _, transform as a, schemaTypes as b, PrinterFactoryOptions as c, definePrinter as d, SchemaDialect as f, DedupePlan as g, DedupeLookups as h, collect as i, PrinterPartial as l, DedupeCanonical as m, Visitor as n, walk as o, defineSchemaDialect as p, VisitorContext as r, Printer as s, ParentOf as t, createPrinterFactory as u, buildDedupePlan as v, httpMethods as y };
//# sourceMappingURL=types-CB2oY8Dw.d.ts.map

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

import "./chunk-CNktS9qV.js";
//#region src/constants.ts
const visitorDepths = {
shallow: "shallow",
deep: "deep"
};
/**
* Schema type discriminators used by all AST schema nodes.
*
* These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
*/
const schemaTypes = {
/**
* Text value.
*/
string: "string",
/**
* Floating-point number (`float`, `double`).
*/
number: "number",
/**
* Whole number (`int32`). Use `bigint` for `int64`.
*/
integer: "integer",
/**
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
*/
bigint: "bigint",
/**
* Boolean value
*/
boolean: "boolean",
/**
* Explicit null value.
*/
null: "null",
/**
* Any value (no type restriction).
*/
any: "any",
/**
* Unknown value (must be narrowed before usage).
*/
unknown: "unknown",
/**
* No return value (`void`).
*/
void: "void",
/**
* Object with named properties.
*/
object: "object",
/**
* Sequential list of items.
*/
array: "array",
/**
* Fixed-length list with position-specific items.
*/
tuple: "tuple",
/**
* "One of" multiple schema members.
*/
union: "union",
/**
* "All of" multiple schema members.
*/
intersection: "intersection",
/**
* Enum schema.
*/
enum: "enum",
/**
* Reference to another schema.
*/
ref: "ref",
/**
* Calendar date (for example `2026-03-24`).
*/
date: "date",
/**
* Date-time value (for example `2026-03-24T09:00:00Z`).
*/
datetime: "datetime",
/**
* Time-only value (for example `09:00:00`).
*/
time: "time",
/**
* UUID value.
*/
uuid: "uuid",
/**
* Email address value.
*/
email: "email",
/**
* URL value.
*/
url: "url",
/**
* IPv4 address value.
*/
ipv4: "ipv4",
/**
* IPv6 address value.
*/
ipv6: "ipv6",
/**
* Binary/blob value.
*/
blob: "blob",
/**
* Impossible value (`never`).
*/
never: "never"
};
/**
* Scalar primitive schema types used for union simplification and type narrowing.
*
* Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.
*/
const SCALAR_PRIMITIVE_TYPES = new Set([
"string",
"number",
"integer",
"bigint",
"boolean"
]);
/**
* Type guard that returns `true` when `type` is a scalar primitive schema type.
*
* Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).
*/
function isScalarPrimitive(type) {
return SCALAR_PRIMITIVE_TYPES.has(type);
}
/**
* HTTP method identifiers used by operation nodes.
*
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
*/
const httpMethods = {
get: "GET",
post: "POST",
put: "PUT",
patch: "PATCH",
delete: "DELETE",
head: "HEAD",
options: "OPTIONS",
trace: "TRACE"
};
/**
* One indentation level, derived from {@link INDENT_SIZE}.
*/
const INDENT = Array.from({ length: 2 }, () => " ").join("");
//#endregion
//#region src/node.ts
/**
* Builds a type guard that matches nodes of the given `kind`.
*/
function isKind(kind) {
return (node) => node.kind === kind;
}
/**
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
* object properties combine "required" and "nullable" into a single AST.
*
* - Non-required + non-nullable → `optional: true`.
* - Non-required + nullable → `nullish: true`.
* - Required → both flags cleared.
*/
function syncOptionality(schema, required) {
const nullable = schema.nullable ?? false;
return {
...schema,
optional: !required && !nullable ? true : void 0,
nullish: !required && nullable ? true : void 0
};
}
/**
* Defines a node once and derives its `create` builder, `is` guard, and traversal
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
* `kind`, so node construction lives in one place without scattered `as` casts.
*
* Set `rebuild: true` when the `build` hook derives fields from children. After a
* transform rewrites those children, the registry reruns `create` so the derived
* fields stay correct.
*
* @example Simple node
* ```ts
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
* const createImport = importDef.create
* ```
*
* @example Node with a build hook that is rerun on transform
* ```ts
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
* kind: 'Property',
* build: (props) => ({ ...props, required: props.required ?? false }),
* children: ['schema'],
* visitorKey: 'property',
* rebuild: true,
* })
* ```
*/
function defineNode(config) {
const { kind, defaults, build, children, visitorKey, rebuild } = config;
function create(input) {
const base = build ? build(input) : input;
return {
...defaults,
...base,
kind
};
}
return {
kind,
create,
is: isKind(kind),
children,
visitorKey,
rebuild
};
}
//#endregion
//#region src/nodes/schema.ts
/**
* Maps schema `type` to its underlying `primitive`.
* Primitive types map to themselves. Special string formats map to `'string'`.
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
*/
const TYPE_TO_PRIMITIVE = {
string: "string",
number: "number",
integer: "integer",
bigint: "bigint",
boolean: "boolean",
null: "null",
any: "any",
unknown: "unknown",
void: "void",
never: "never",
object: "object",
array: "array",
date: "date",
uuid: "string",
email: "string",
url: "string",
datetime: "string",
time: "string"
};
/**
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
*/
const schemaDef = defineNode({
kind: "Schema",
build: (props) => {
if (props.type === "object") return {
properties: [],
primitive: "object",
...props
};
return {
primitive: TYPE_TO_PRIMITIVE[props.type],
...props
};
},
children: [
"properties",
"items",
"members",
"additionalProperties"
],
visitorKey: "schema"
});
function createSchema(props) {
return schemaDef.create(props);
}
//#endregion
//#region ../../internals/utils/src/casing.ts
/**
* Shared implementation for camelCase and PascalCase conversion.
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
* and capitalizes each word according to `pascal`.
*
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
*/
function toCamelOrPascal(text, pascal) {
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
if (word.length > 1 && word === word.toUpperCase()) return word;
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
}).join("").replace(/[^a-zA-Z0-9]/g, "");
}
/**
* Converts `text` to camelCase.
*
* @example Word boundaries
* `camelCase('hello-world') // 'helloWorld'`
*
* @example With a prefix
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
*/
function camelCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
}
/**
* Converts `text` to PascalCase.
*
* @example Word boundaries
* `pascalCase('hello-world') // 'HelloWorld'`
*
* @example With a suffix
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
*/
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
}
//#endregion
//#region ../../internals/utils/src/promise.ts
/**
* Wraps `factory` with a keyed cache backed by the provided store.
*
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
* collected) or a `Map` for primitive keys. For multi-argument functions,
* nest two `memoize` calls — the outer keyed by the first argument, the
* inner (created once per outer miss) keyed by the second.
*
* Because the cache is owned by the caller, it can be shared, inspected, or
* cleared independently of the memoized function.
*
* @example Single WeakMap key
* ```ts
* const cache = new WeakMap<SchemaNode, Set<string>>()
* const getRefs = memoize(cache, (node) => collectRefs(node))
* ```
*
* @example Single Map key (primitive)
* ```ts
* const cache = new Map<string, Resolver>()
* const getResolver = memoize(cache, (name) => buildResolver(name))
* ```
*
* @example Two-level (object + primitive)
* ```ts
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
* fn(params)('camelcase')
* ```
*/
function memoize(store, factory) {
return (key) => {
if (store.has(key)) return store.get(key);
const value = factory(key);
store.set(key, value);
return value;
};
}
//#endregion
//#region ../../internals/utils/src/reserved.ts
/**
* JavaScript and Java reserved words.
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
*/
const reservedWords = new Set([
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
"Array",
"Date",
"hasOwnProperty",
"Infinity",
"isFinite",
"isNaN",
"isPrototypeOf",
"length",
"Math",
"name",
"NaN",
"Number",
"Object",
"prototype",
"String",
"toString",
"undefined",
"valueOf"
]);
/**
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
*
* @example
* ```ts
* isValidVarName('status') // true
* isValidVarName('class') // false (reserved word)
* isValidVarName('42foo') // false (starts with digit)
* ```
*/
function isValidVarName(name) {
if (!name || reservedWords.has(name)) return false;
return isIdentifier(name);
}
/**
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
*
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
* deciding whether an object key needs quoting.
*
* @example
* ```ts
* isIdentifier('name') // true
* isIdentifier('x-total')// false
* ```
*/
function isIdentifier(name) {
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
}
//#endregion
//#region ../../internals/utils/src/string.ts
/**
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
* any backslash or single quote in the content.
*
* @example
* ```ts
* singleQuote('foo') // "'foo'"
* singleQuote("o'clock") // "'o\\'clock'"
* ```
*/
function singleQuote(value) {
if (value === void 0 || value === null) return "''";
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
}
//#endregion
//#region src/utils/extractStringsFromNodes.ts
/**
* Extracts all string content from a `CodeNode` tree recursively.
*
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
* and nested node content. Used to build the full source string for import filtering.
*/
function extractStringsFromNodes(nodes) {
if (!nodes?.length) return "";
return nodes.map((node) => {
if (typeof node === "string") return node;
if (node.kind === "Text") return node.value;
if (node.kind === "Break") return "";
if (node.kind === "Jsx") return node.value;
const parts = [];
if ("params" in node && node.params) parts.push(node.params);
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
if ("returnType" in node && node.returnType) parts.push(node.returnType);
if ("type" in node && typeof node.type === "string") parts.push(node.type);
const nested = extractStringsFromNodes(node.nodes);
if (nested) parts.push(nested);
return parts.join("\n");
}).filter(Boolean).join("\n");
}
//#endregion
//#region src/guards.ts
/**
* Narrows a `SchemaNode` to the variant that matches `type`.
*
* @example
* ```ts
* const schema = createSchema({ type: 'string' })
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
* ```
*/
function narrowSchema(node, type) {
return node?.type === type ? node : null;
}
/**
* Narrows an `OperationNode` to an `HttpOperationNode`, guaranteeing `method` and `path`.
*
* @example
* ```ts
* if (isHttpOperationNode(node)) {
* console.log(node.method, node.path)
* }
* ```
*/
function isHttpOperationNode(node) {
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
}
//#endregion
//#region src/nodes/function.ts
/**
* Definition for the {@link TypeLiteralNode}.
*/
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
/**
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
*
* @example
* ```ts
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
* // { petId: string }
* ```
*/
const createTypeLiteral = typeLiteralDef.create;
/**
* Definition for the {@link IndexedAccessTypeNode}.
*/
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
/**
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
*
* @example
* ```ts
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
* // DeletePetPathParams['petId']
* ```
*/
const createIndexedAccessType = indexedAccessTypeDef.create;
/**
* Definition for the {@link ObjectBindingPatternNode}.
*/
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
/**
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
*
* @example
* ```ts
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
* // { id, name }
* ```
*/
const createObjectBindingPattern = objectBindingPatternDef.create;
/**
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
* paired with a {@link TypeLiteralNode} type.
*/
const functionParameterDef = defineNode({
kind: "FunctionParameter",
build: (input) => {
if ("properties" in input) return {
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
type: createTypeLiteral({ members: input.properties.map((p) => ({
name: p.name,
type: p.type,
optional: p.optional ?? false
})) }),
optional: input.optional ?? false,
...input.default !== void 0 ? { default: input.default } : {}
};
return {
optional: false,
...input
};
}
});
/**
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
*
* @example Optional param
* ```ts
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
* // → params?: QueryParams
* ```
*
* @example Destructured group
* ```ts
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
* // → { id, name }: { id: string; name?: string } = {}
* ```
*/
const createFunctionParameter = functionParameterDef.create;
/**
* Definition for the {@link FunctionParametersNode}.
*/
const functionParametersDef = defineNode({
kind: "FunctionParameters",
defaults: { params: [] }
});
/**
* Creates a `FunctionParametersNode` from an ordered list of parameters.
*
* @example
* ```ts
* const empty = createFunctionParameters()
* // { kind: 'FunctionParameters', params: [] }
* ```
*/
function createFunctionParameters(props = {}) {
return functionParametersDef.create(props);
}
//#endregion
//#region src/nodes/property.ts
/**
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const propertyDef = defineNode({
kind: "Property",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "property",
rebuild: true
});
/**
* Creates a `PropertyNode`.
*
* @example
* ```ts
* const property = createProperty({
* name: 'status',
* required: true,
* schema: createSchema({ type: 'string', nullable: true }),
* })
* // required=true, no optional/nullish
* ```
*/
const createProperty = propertyDef.create;
//#endregion
//#region src/nodes/code.ts
/**
* Definition for the {@link ConstNode}.
*/
const constDef = defineNode({ kind: "Const" });
/**
* Creates a `ConstNode` representing a TypeScript `const` declaration.
*
* @example Exported constant with type and `as const`
* ```ts
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
* // export const pets: Pet[] = ... as const
* ```
*/
const createConst = constDef.create;
/**
* Definition for the {@link TypeNode}.
*/
const typeDef = defineNode({ kind: "Type" });
/**
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
*
* @example
* ```ts
* createType({ name: 'Pet', export: true })
* // export type Pet = ...
* ```
*/
const createType = typeDef.create;
/**
* Definition for the {@link FunctionNode}.
*/
const functionDef = defineNode({ kind: "Function" });
/**
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
*
* @example
* ```ts
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
* // export async function fetchPet(): Promise<Pet> { ... }
* ```
*/
const createFunction = functionDef.create;
/**
* Definition for the {@link ArrowFunctionNode}.
*/
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
/**
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
*
* @example
* ```ts
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
* // export const double = (n: number) => ...
* ```
*/
const createArrowFunction = arrowFunctionDef.create;
/**
* Definition for the {@link TextNode}.
*/
const textDef = defineNode({
kind: "Text",
build: (value) => ({ value })
});
/**
* Creates a {@link TextNode} representing a raw string fragment in the source output.
*
* @example
* ```ts
* createText('return fetch(id)')
* // { kind: 'Text', value: 'return fetch(id)' }
* ```
*/
const createText = textDef.create;
/**
* Definition for the {@link BreakNode}.
*/
const breakDef = defineNode({
kind: "Break",
build: () => ({})
});
/**
* Creates a {@link BreakNode} representing a line break in the source output.
*
* @example
* ```ts
* createBreak()
* // { kind: 'Break' }
* ```
*/
function createBreak() {
return breakDef.create();
}
/**
* Definition for the {@link JsxNode}.
*/
const jsxDef = defineNode({
kind: "Jsx",
build: (value) => ({ value })
});
/**
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
*
* @example
* ```ts
* createJsx('<>\n <a href={href}>Open</a>\n</>')
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
* ```
*/
const createJsx = jsxDef.create;
//#endregion
//#region src/nodes/content.ts
/**
* Definition for the {@link ContentNode}.
*/
const contentDef = defineNode({
kind: "Content",
children: ["schema"]
});
/**
* Creates a `ContentNode` for a single request-body or response content type.
*/
const createContent = contentDef.create;
//#endregion
//#region src/nodes/file.ts
/**
* Definition for the {@link ImportNode}.
*/
const importDef = defineNode({ kind: "Import" });
/**
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
*
* @example Named import
* ```ts
* createImport({ name: ['useState'], path: 'react' })
* // import { useState } from 'react'
* ```
*/
const createImport = importDef.create;
/**
* Definition for the {@link ExportNode}.
*/
const exportDef = defineNode({ kind: "Export" });
/**
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
*
* @example Named export
* ```ts
* createExport({ name: ['Pet'], path: './Pet' })
* // export { Pet } from './Pet'
* ```
*/
const createExport = exportDef.create;
/**
* Definition for the {@link SourceNode}.
*/
const sourceDef = defineNode({ kind: "Source" });
/**
* Creates a `SourceNode` representing a fragment of source code within a file.
*
* @example
* ```ts
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
* ```
*/
const createSource = sourceDef.create;
/**
* Definition for the {@link FileNode}. The fully resolved builder lives in
* `createFile`, so this definition only supplies the guard.
*/
const fileDef = defineNode({ kind: "File" });
//#endregion
//#region src/nodes/input.ts
/**
* Definition for the {@link InputNode}.
*/
const inputDef = defineNode({
kind: "Input",
defaults: {
schemas: [],
operations: [],
meta: {
circularNames: [],
enumNames: []
}
},
children: ["schemas", "operations"],
visitorKey: "input"
});
/**
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
*
* @example Eager
* ```ts
* const input = createInput()
* // { kind: 'Input', schemas: [], operations: [] }
* ```
*
* @example Streaming
* ```ts
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
* ```
*/
function createInput(options = {}) {
const { stream, ...overrides } = options;
if (stream) return {
kind: "Input",
...overrides
};
return inputDef.create(overrides);
}
//#endregion
//#region src/nodes/requestBody.ts
/**
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
*/
const requestBodyDef = defineNode({
kind: "RequestBody",
build: (props) => ({
...props,
content: props.content?.map(createContent)
}),
children: ["content"]
});
/**
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
*/
const createRequestBody = requestBodyDef.create;
//#endregion
//#region src/nodes/operation.ts
/**
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
* normalized into a `RequestBodyNode`.
*/
const operationDef = defineNode({
kind: "Operation",
build: (props) => {
const { requestBody, ...rest } = props;
const isHttp = rest.method !== void 0 && rest.path !== void 0;
return {
tags: [],
parameters: [],
responses: [],
...rest,
...isHttp ? { protocol: "http" } : {},
requestBody: requestBody ? createRequestBody(requestBody) : void 0
};
},
children: [
"parameters",
"requestBody",
"responses"
],
visitorKey: "operation"
});
function createOperation(props) {
return operationDef.create(props);
}
//#endregion
//#region src/nodes/output.ts
/**
* Definition for the {@link OutputNode}.
*/
const outputDef = defineNode({
kind: "Output",
defaults: { files: [] },
visitorKey: "output"
});
/**
* Creates an `OutputNode` with a stable default for `files`.
*
* @example
* ```ts
* const output = createOutput()
* // { kind: 'Output', files: [] }
* ```
*/
function createOutput(overrides = {}) {
return outputDef.create(overrides);
}
//#endregion
//#region src/nodes/parameter.ts
/**
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
* schema's `optional`/`nullish` flags are kept in sync with it.
*/
const parameterDef = defineNode({
kind: "Parameter",
build: (props) => {
const required = props.required ?? false;
return {
...props,
required,
schema: syncOptionality(props.schema, required)
};
},
children: ["schema"],
visitorKey: "parameter",
rebuild: true
});
/**
* Creates a `ParameterNode`.
*
* @example
* ```ts
* const param = createParameter({
* name: 'petId',
* in: 'path',
* required: true,
* schema: createSchema({ type: 'string' }),
* })
* ```
*/
const createParameter = parameterDef.create;
//#endregion
//#region src/nodes/response.ts
/**
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
*/
const responseDef = defineNode({
kind: "Response",
build: (props) => {
const { schema, mediaType, keysToOmit, content, ...rest } = props;
const entries = content ?? (schema ? [{
contentType: mediaType ?? "application/json",
schema,
keysToOmit: keysToOmit ?? null
}] : void 0);
return {
...rest,
content: entries?.map(createContent)
};
},
children: ["content"],
visitorKey: "response"
});
/**
* Creates a `ResponseNode`.
*
* @example
* ```ts
* const response = createResponse({
* statusCode: '200',
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
* })
* ```
*/
const createResponse = responseDef.create;
//#endregion
//#region src/registry.ts
/**
* Every node definition. Adding a node means adding its `defineNode` to one
* `nodes/*.ts` file and listing it here. The visitor tables below derive from it.
*/
const nodeDefs = [
inputDef,
outputDef,
operationDef,
requestBodyDef,
contentDef,
responseDef,
schemaDef,
propertyDef,
parameterDef,
functionParameterDef,
functionParametersDef,
typeLiteralDef,
indexedAccessTypeDef,
objectBindingPatternDef,
constDef,
typeDef,
functionDef,
arrowFunctionDef,
textDef,
breakDef,
jsxDef,
importDef,
exportDef,
sourceDef,
fileDef
];
/**
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
* Derived from each definition's `children`.
*/
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
/**
* Maps a node kind to the matching visitor callback name. Derived from each
* definition's `visitorKey`.
*/
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
/**
* Per-kind builders rerun after children are rebuilt. Derived from each
* definition's `rebuild` flag.
*/
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
//#endregion
//#region src/visitor.ts
/**
* Creates a small async concurrency limiter.
*
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
*
* @example
* ```ts
* const limit = createLimit(2)
* for (const task of [taskA, taskB, taskC]) {
* await limit(() => task())
* }
* // only 2 tasks run at the same time
* ```
*/
function createLimit(concurrency) {
let active = 0;
const queue = [];
function next() {
if (active < concurrency && queue.length > 0) {
active++;
queue.shift()();
}
}
return function limit(fn) {
return new Promise((resolve, reject) => {
queue.push(() => {
Promise.resolve(fn()).then(resolve, reject).finally(() => {
active--;
next();
});
});
next();
});
};
}
const visitorKeysByKind = VISITOR_KEYS;
/**
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
*/
function isNode(value) {
return typeof value === "object" && value !== null && "kind" in value;
}
/**
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
*
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
*
* @example
* ```ts
* const children = getChildren(operationNode, true)
* // returns parameters, the request body, and responses
* ```
*/
function* getChildren(node, recurse) {
if (node.kind === "Schema" && !recurse) return;
const keys = visitorKeysByKind[node.kind];
if (!keys) return;
const record = node;
for (const key of keys) {
const value = record[key];
if (Array.isArray(value)) {
for (const item of value) if (isNode(item)) yield item;
} else if (isNode(value)) yield value;
}
}
/**
* Invokes the visitor callback that matches `node.kind`, passing the traversal
* context. Returns the callback's result (a replacement node, a collected
* value, or `undefined` when no callback is registered for the kind).
*
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
* in one place. `TResult` is the caller's expected return: the same node type
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
*/
function applyVisitor(node, visitor, parent) {
const key = VISITOR_KEY_BY_KIND[node.kind];
if (!key) return void 0;
const fn = visitor[key];
return fn?.(node, { parent });
}
/**
* Async depth-first traversal for side effects. Visitor return values are
* ignored. Use `transform` when you want to rewrite nodes.
*
* Sibling nodes at each depth run concurrently up to `options.concurrency`
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
* work. Lower values reduce memory pressure.
*
* @example Log every operation
* ```ts
* await walk(root, {
* operation(node) {
* console.log(node.operationId)
* },
* })
* ```
*
* @example Only visit the root node
* ```ts
* await walk(root, { depth: 'shallow', input: () => {} })
* ```
*/
async function walk(node, options) {
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
}
async function _walk(node, visitor, recurse, limit, parent) {
await limit(() => applyVisitor(node, visitor, parent));
const children = Array.from(getChildren(node, recurse));
if (children.length === 0) return;
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
}
function transform(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
if (rebuilt === node) return node;
const rebuild = nodeRebuilders[rebuilt.kind];
return rebuild ? rebuild(rebuilt) : rebuilt;
}
/**
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
* `Schema` children are skipped in shallow mode.
*/
function transformChildren(node, options, recurse) {
if (node.kind === "Schema" && !recurse) return node;
const keys = visitorKeysByKind[node.kind];
if (!keys) return node;
const record = node;
const childOptions = {
...options,
parent: node
};
let updates;
for (const key of keys) {
if (!(key in record)) continue;
const value = record[key];
if (Array.isArray(value)) {
let changed = false;
const mapped = value.map((item) => {
if (!isNode(item)) return item;
const next = transform(item, childOptions);
if (next !== item) changed = true;
return next;
});
if (changed) (updates ??= {})[key] = mapped;
} else if (isNode(value)) {
const next = transform(value, childOptions);
if (next !== value) (updates ??= {})[key] = next;
}
}
return updates ? {
...node,
...updates
} : node;
}
/**
* Lazy depth-first collection pass. Yields every non-null value returned by
* the visitor callbacks. Use `collect` for the eager array form.
*
* @example Collect every operationId
* ```ts
* const ids: string[] = []
* for (const id of collectLazy<string>(root, {
* operation(node) {
* return node.operationId
* },
* })) {
* ids.push(id)
* }
* ```
*/
function* collectLazy(node, options) {
const { depth, parent, ...visitor } = options;
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
const v = applyVisitor(node, visitor, parent);
if (v != null) yield v;
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
...options,
parent: node
});
}
/**
* Eager depth-first collection pass. Returns an array of every non-null value
* the visitor callbacks return.
*
* @example Collect every operationId
* ```ts
* const ids = collect<string>(root, {
* operation(node) {
* return node.operationId
* },
* })
* ```
*/
function collect(node, options) {
return Array.from(collectLazy(node, options));
}
//#endregion
//#region src/utils/ast.ts
const plainStringTypes = new Set([
"string",
"uuid",
"email",
"url",
"datetime"
]);
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
* override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* // Ref with description override
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
function syncSchemaRef(node) {
const ref = narrowSchema(node, "ref");
if (!ref) return node;
if (!ref.schema) return node;
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
return createSchema({
...ref.schema,
...definedOverrides
});
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
function isStringType(node) {
if (plainStringTypes.has(node.type)) return true;
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
if (temporal) return temporal.representation !== "date";
return false;
}
/**
* Applies casing rules to parameter names and returns a new parameter array.
*
* Use this before passing parameters to schema builders so output property keys match
* the desired casing while preserving `OperationNode.parameters` for other consumers.
* The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
*/
const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => {
const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
return {
...param,
name: transformed
};
})));
function caseParams(params, casing) {
if (!casing) return params;
return caseParamsMemo(params)(casing);
}
/**
* Creates a single-property object schema used as a discriminator literal.
*
* @example
* ```ts
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
* ```
*/
function createDiscriminantNode({ propertyName, value }) {
return createSchema({
type: "object",
primitive: "object",
properties: [createProperty({
name: propertyName,
schema: createSchema({
type: "enum",
primitive: "string",
enumValues: [value]
}),
required: true
})]
});
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, falls back to the schema primitive (a plain type-name string).
* When the parameter belongs to a named group, emits an {@link IndexedAccessTypeNode}
* (`GroupParams['petId']`); otherwise the resolved individual name as a plain string.
*/
function resolveParamType({ node, param, resolver }) {
if (!resolver) return param.schema.primitive ?? "unknown";
const individualName = resolver.resolveParamName(node, param);
const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0;
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName
};
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0;
if (groupName && groupName !== individualName) return createIndexedAccessType({
objectType: groupName,
indexType: param.name
});
return individualName;
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
function createOperationParams(node, options) {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options;
const dataName = paramNames?.data ?? "data";
const paramsName = paramNames?.params ?? "params";
const headersName = paramNames?.headers ?? "headers";
const pathName = paramNames?.path ?? "pathParams";
const wrapType = (type) => typeWrapper ? typeWrapper(type) : type;
const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type;
const casedParams = caseParams(node.parameters, paramsCasing);
const pathParams = casedParams.filter((p) => p.in === "path");
const queryParams = casedParams.filter((p) => p.in === "query");
const headerParams = casedParams.filter((p) => p.in === "header");
const toProperty = (param) => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({
node,
param,
resolver
})),
optional: !param.required
});
const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0;
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0;
const bodyProperty = bodyType ? [{
name: dataName,
type: bodyType,
optional: !(node.requestBody?.required ?? false)
}] : [];
const trailingGroups = [{
name: paramsName,
node,
params: queryParams,
groupType: resolveGroupType({
node,
params: queryParams,
group: "query",
resolver
}),
resolver,
wrapType
}, {
name: headersName,
node,
params: headerParams,
groupType: resolveGroupType({
node,
params: headerParams,
group: "header",
resolver
}),
resolver,
wrapType
}];
const params = [];
if (paramsType === "object") {
const children = [
...pathParams.map(toProperty),
...bodyProperty,
...trailingGroups.flatMap(buildGroupProperty)
];
if (children.length) params.push(createFunctionParameter({
properties: children,
default: emptyObjectDefault(children)
}));
} else {
if (pathParamsType === "inlineSpread" && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
params.push(createFunctionParameter({
name: pathName,
type: spreadType ? wrapType(spreadType) : void 0,
rest: true
}));
} else if (pathParamsType === "inline") params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))));
else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty);
params.push(createFunctionParameter({
properties: pathChildren,
default: pathParamsDefault ?? emptyObjectDefault(pathChildren)
}));
}
params.push(...bodyProperty.map((p) => createFunctionParameter(p)));
params.push(...trailingGroups.flatMap(buildGroupParam));
}
params.push(...extraParams);
return createFunctionParameters({ params });
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) {
if (groupType) return [{
name,
type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type,
optional: groupType.optional
}];
if (params.length) return [{
name,
type: buildTypeLiteral({
node,
params,
resolver
}),
optional: params.every((p) => !p.required)
}];
return [];
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupParam(args) {
return buildGroupProperty(args).map((p) => createFunctionParameter(p));
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
function buildTypeLiteral({ node, params, resolver }) {
return createTypeLiteral({ members: params.map((p) => ({
name: p.name,
type: resolveParamType({
node,
param: p,
resolver
}),
optional: !p.required
})) });
}
function sourceKey(source) {
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
}
function pathTypeKey(path, isTypeOnly) {
return `${path}:${isTypeOnly ?? false}`;
}
function exportKey(path, name, isTypeOnly, asAlias) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
}
function importKey(path, name, isTypeOnly) {
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node) {
const isArray = Array.isArray(node.name) ? "1" : "0";
const typeOnly = node.isTypeOnly ? "0" : "1";
const hasName = node.name != null ? "1" : "0";
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
function combineSources(sources) {
const seen = /* @__PURE__ */ new Map();
for (const source of sources) {
const key = sourceKey(source);
if (!seen.has(key)) seen.set(key, source);
}
return [...seen.values()];
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays(existing, incoming) {
const merged = new Set(existing);
for (const name of incoming) merged.add(name);
return [...merged];
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
function combineExports(exports) {
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = exports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr;
if (Array.isArray(name)) {
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name: [...new Set(name)]
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*
* @note Use this when combining imports from multiple files to avoid duplicate declarations.
*/
function combineImports(imports, exports, source) {
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
const importNameMemo = /* @__PURE__ */ new Map();
const canonicalizeName = (n) => {
if (typeof n === "string") return n;
const key = `${n.propertyName}:${n.name ?? ""}`;
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
return importNameMemo.get(key);
};
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
for (const node of imports) {
if (!Array.isArray(node.name)) continue;
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
}
const result = [];
const namedByPath = /* @__PURE__ */ new Map();
const seen = /* @__PURE__ */ new Set();
const keyed = imports.map((node) => ({
node,
key: sortKey(node)
}));
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue;
const { path, isTypeOnly } = curr;
let { name } = curr;
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
if (!name.length) continue;
const key = pathTypeKey(path, isTypeOnly);
const existing = namedByPath.get(key);
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
else {
const newItem = {
...curr,
name
};
result.push(newItem);
namedByPath.set(key, newItem);
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
const key = importKey(path, name, isTypeOnly);
if (!seen.has(key)) {
result.push(curr);
seen.add(key);
}
}
}
return result;
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name can be resolved. Use this to get a schema's
* identifier for type definitions or error messages.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
function resolveRefName(node) {
if (!node || node.type !== "ref") return null;
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
return node.name ?? node.schema?.name ?? null;
}
/**
* Collects every named schema referenced (transitively) from a node via ref edges.
*
* Refs are followed by name only, the resolved `node.schema` is not traversed inline.
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
const refs = /* @__PURE__ */ new Set();
collect(node, { schema(child) {
if (child.type === "ref") {
const name = resolveRefName(child);
if (name) refs.add(name);
}
} });
return refs;
});
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
if (!node) return out;
for (const name of collectSchemaRefs(node)) out.add(name);
return out;
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when any of its parameters, request body content, or responses
* reference it, directly or indirectly through other named schemas.
* The walk is iterative and safe against reference cycles.
*
* Use this together with `include` filters to determine which schemas from `components/schemas`
* are reachable from the allowed operations, so that schemas used only by excluded operations
* are not generated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*
* @example Check whether a specific schema is needed
* ```ts
* const allowed = collectUsedSchemaNames(includedOps, schemas)
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
* ```
*/
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
function computeUsedSchemaNames(operations, schemas) {
const schemaMap = /* @__PURE__ */ new Map();
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
const result = /* @__PURE__ */ new Set();
function visitSchema(schema) {
const directRefs = collectReferencedSchemaNames(schema);
for (const name of directRefs) if (!result.has(name)) {
result.add(name);
const namedSchema = schemaMap.get(name);
if (namedSchema) visitSchema(namedSchema);
}
}
for (const op of operations) for (const schema of collectLazy(op, {
depth: "shallow",
schema: (node) => node
})) visitSchema(schema);
return result;
}
function collectUsedSchemaNames(operations, schemas) {
return collectUsedSchemaNamesMemo(operations)(schemas);
}
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
const graph = /* @__PURE__ */ new Map();
for (const schema of schemas) {
if (!schema.name) continue;
graph.set(schema.name, collectReferencedSchemaNames(schema));
}
const circular = /* @__PURE__ */ new Set();
for (const start of graph.keys()) {
const visited = /* @__PURE__ */ new Set();
const stack = [...graph.get(start) ?? []];
while (stack.length > 0) {
const node = stack.pop();
if (node === start) {
circular.add(start);
break;
}
if (visited.has(node)) continue;
visited.add(node);
const next = graph.get(node);
if (next) for (const r of next) stack.push(r);
}
}
return circular;
});
/**
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
*
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
* in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
* Refs are followed by name only, keeping the algorithm linear in the schema graph size.
*
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
*/
function findCircularSchemas(schemas) {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
return findCircularSchemasMemo(schemas);
}
/**
* Type guard returning `true` when a schema or anything nested within it contains a ref to a circular schema.
*
* Use `excludeName` to ignore refs to specific schemas (useful when self-references are handled separately).
* Commonly used with `findCircularSchemas()` to detect where lazy wrappers are needed in code generation.
*
* @note Returns `true` for the first matching circular ref found. Use for fast dependency checks.
*/
function containsCircularRef(node, { circularSchemas, excludeName }) {
if (!node || circularSchemas.size === 0) return false;
for (const _ of collectLazy(node, { schema(child) {
if (child.type !== "ref") return null;
const name = resolveRefName(child);
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
} })) return true;
return false;
}
//#endregion
//#region src/utils/index.ts
/**
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
* Returns the string unchanged when no balanced quote pair is found.
*
* @example
* ```ts
* trimQuotes('"hello"') // 'hello'
* trimQuotes('hello') // 'hello'
* ```
*/
function trimQuotes(text) {
if (text.length >= 2) {
const first = text[0];
const last = text[text.length - 1];
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
}
return text;
}
/**
* Serializes a primitive value to a single-quoted string literal, stripping any surrounding quote
* characters first. Escaping comes from `JSON.stringify`, then the quote style switches to single
* quotes so generated code matches the repo style without a formatter.
*
* @example
* ```ts
* stringify('hello') // "'hello'"
* stringify('"hello"') // "'hello'"
* ```
*/
function stringify(value) {
if (value === void 0 || value === null) return "''";
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
}
/**
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
* and the Unicode line terminators U+2028 and U+2029.
*
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
*
* @example
* ```ts
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
* ```
*/
function jsStringEscape(input) {
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
switch (character) {
case "\"":
case "'":
case "\\": return `\\${character}`;
case "\n": return "\\n";
case "\r": return "\\r";
case "\u2028": return "\\u2028";
case "\u2029": return "\\u2029";
default: return "";
}
});
}
/**
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
*
* @example
* ```ts
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
* toRegExpString('^(?im)foo', null) // '/^foo/im'
* ```
*/
function toRegExpString(text, func = "RegExp") {
const raw = trimQuotes(text);
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
const replacementTarget = match?.[1] ?? "";
const matchedFlags = match?.[2];
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
const { source, flags } = new RegExp(cleaned, matchedFlags);
if (func === null) return `/${source}/${flags}`;
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
}
/**
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
* objects recurse with fixed indentation, so the result drops straight into an object literal
* without re-parsing.
*
* @example
* ```ts
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
* // 'foo: bar,\nnested: {\n a: 1\n }'
* ```
*/
function stringifyObject(value) {
return Object.entries(value).map(([key, val]) => {
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
return `${key}: ${val}`;
}).filter(Boolean).join(",\n");
}
/**
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
* `accessor`. Returns `null` for an empty path.
*
* @example
* ```ts
* getNestedAccessor('pagination.next.id', 'lastPage')
* // "lastPage?.['pagination']?.['next']?.['id']"
* ```
*/
function getNestedAccessor(param, accessor) {
const parts = Array.isArray(param) ? param : param.split(".");
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
}
/**
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
* comments so callers always get a usable string.
*
* @example
* ```ts
* buildJSDoc(['@type string', '@example hello'])
* // '/**\n * @type string\n * @example hello\n *\/\n '
* ```
*/
function buildJSDoc(comments, options = {}) {
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
if (comments.length === 0) return fallback;
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
}
/**
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
*/
function indentLines(text) {
if (!text) return "";
return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n");
}
/**
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
*
* @example
* ```ts
* objectKey('name') // 'name'
* objectKey('x-total') // "'x-total'"
* ```
*/
function objectKey(name) {
return isIdentifier(name) ? name : singleQuote(name);
}
/**
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
* formatter's multi-line style.
*
* @example
* ```ts
* buildObject(['id: z.number()', 'name: z.string()'])
* // '{\n id: z.number(),\n name: z.string(),\n}'
* ```
*/
function buildObject(entries) {
if (entries.length === 0) return "{}";
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
}
/**
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
*
* @example
* ```ts
* buildList(['z.string()', 'z.number()'])
* // '[z.string(), z.number()]'
* ```
*/
function buildList(items, brackets = ["[", "]"]) {
const [open, close] = brackets;
if (items.length === 0) return `${open}${close}`;
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
}
/**
* Returns the last path segment of a reference string.
*
* @example
* ```ts
* extractRefName('#/components/schemas/Pet') // 'Pet'
* ```
*/
function extractRefName(ref) {
return ref.split("/").at(-1) ?? ref;
}
/**
* Builds a PascalCase child schema name by joining a parent name and property name.
* Returns `null` when there is no parent to nest under.
*
* @example
* ```ts
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
* childName(undefined, 'params') // null
* ```
*/
function childName(parentName, propName) {
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
}
/**
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
* empty parts.
*
* @example
* ```ts
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
* ```
*/
function enumPropName(parentName, propName, enumSuffix) {
return pascalCase([
parentName,
propName,
enumSuffix
].filter(Boolean).join(" "));
}
/**
* Returns the discriminator key whose mapping value matches `ref`, or `null` when there is no match.
*
* @example
* ```ts
* findDiscriminator({ dog: '#/components/schemas/Dog' }, '#/components/schemas/Dog') // 'dog'
* ```
*/
function findDiscriminator(mapping, ref) {
if (!mapping || !ref) return null;
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
}
/**
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
*
* Returns `null` when there is no resolver, no params, or the group name equals the
* individual param name (so there is no real group to emit).
*/
function resolveGroupType({ node, params, group, resolver }) {
if (!resolver || !params.length) return null;
const firstParam = params[0];
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
return {
type: groupName,
optional: params.every((p) => !p.required)
};
}
//#endregion
export { contentDef as $, collect as A, isValidVarName as At, createOperation as B, containsCircularRef as C, functionParametersDef as Ct, isStringType as D, isHttpOperationNode as Dt, findCircularSchemas as E, typeLiteralDef as Et, responseDef as F, httpMethods as Ft, inputDef as G, createRequestBody as H, createParameter as I, isScalarPrimitive as It, createSource as J, createExport as K, parameterDef as L, schemaTypes as Lt, transform as M, schemaDef as Mt, walk as N, defineNode as Nt, resolveParamType as O, narrowSchema as Ot, createResponse as P, syncOptionality as Pt, sourceDef as Q, createOutput as R, combineSources as S, functionParameterDef as St, createOperationParams as T, objectBindingPatternDef as Tt, requestBodyDef as U, operationDef as V, createInput as W, fileDef as X, exportDef as Y, importDef as Z, buildTypeLiteral as _, createFunctionParameter as _t, enumPropName as a, createBreak as at, combineExports as b, createObjectBindingPattern as bt, getNestedAccessor as c, createJsx as ct, resolveGroupType as d, functionDef as dt, createContent as et, stringify as f, jsxDef as ft, buildGroupParam as g, propertyDef as gt, trimQuotes as h, createProperty as ht, childName as i, createArrowFunction as it, collectLazy as j, createSchema as jt, syncSchemaRef as k, extractStringsFromNodes as kt, jsStringEscape as l, createText as lt, toRegExpString as m, typeDef as mt, buildList as n, breakDef as nt, extractRefName as o, createConst as ot, stringifyObject as p, textDef as pt, createImport as q, buildObject as r, constDef as rt, findDiscriminator as s, createFunction as st, buildJSDoc as t, arrowFunctionDef as tt, objectKey as u, createType as ut, caseParams as v, createFunctionParameters as vt, createDiscriminantNode as w, indexedAccessTypeDef as wt, combineImports as x, createTypeLiteral as xt, collectUsedSchemaNames as y, createIndexedAccessType as yt, outputDef as z };
//# sourceMappingURL=utils-DN4XLVqz.js.map

Sorry, the diff of this file is too big to display

import { camelCase, isValidVarName, memoize } from '@internals/utils'
import { narrowSchema } from '../guards.ts'
import { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createTypeLiteral } from '../nodes/function.ts'
import { createProperty } from '../nodes/property.ts'
import { createSchema } from '../nodes/schema.ts'
import type {
ExportNode,
FunctionParameterNode,
FunctionParametersNode,
ImportNode,
OperationNode,
ParameterNode,
SchemaNode,
SourceNode,
TypeExpression,
TypeLiteralNode,
} from '../nodes/index.ts'
import type { SchemaType } from '../nodes/schema.ts'
import { extractRefName, extractStringsFromNodes, resolveGroupType } from './index.ts'
import { collect, collectLazy } from '../visitor.ts'
const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
/**
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
*
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node
* override the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
*
* @example
* ```ts
* // Ref with description override
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
* ```
*/
export function syncSchemaRef(node: SchemaNode): SchemaNode {
const ref = narrowSchema(node, 'ref')
if (!ref) return node
if (!ref.schema) return node
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref
// Filter out undefined override values so they don't shadow the resolved schema's fields.
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))
return createSchema({ ...ref.schema, ...definedOverrides })
}
/**
* Type guard that returns `true` when a schema emits as a plain `string` type.
*
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
*/
export function isStringType(node: SchemaNode): boolean {
if (plainStringTypes.has(node.type)) {
return true
}
const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')
if (temporal) {
return temporal.representation !== 'date'
}
return false
}
/**
* Applies casing rules to parameter names and returns a new parameter array.
*
* Use this before passing parameters to schema builders so output property keys match
* the desired casing while preserving `OperationNode.parameters` for other consumers.
* The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
*/
const caseParamsMemo = memoize(new WeakMap<Array<ParameterNode>, (casing: string) => Array<ParameterNode>>(), (params) =>
memoize(new Map<string, Array<ParameterNode>>(), (casing: string) =>
params.map((param) => {
const transformed = casing === 'camelcase' || !isValidVarName(param.name) ? camelCase(param.name) : param.name
return { ...param, name: transformed }
}),
),
)
export function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
if (!casing) return params
return caseParamsMemo(params)(casing)
}
/**
* Creates a single-property object schema used as a discriminator literal.
*
* @example
* ```ts
* createDiscriminantNode({ propertyName: 'type', value: 'dog' })
* // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
* ```
*/
export function createDiscriminantNode({ propertyName, value }: { propertyName: string; value: string }): SchemaNode {
return createSchema({
type: 'object',
primitive: 'object',
properties: [
createProperty({
name: propertyName,
schema: createSchema({
type: 'enum',
primitive: 'string',
enumValues: [value],
}),
required: true,
}),
],
})
}
/**
* Named type for a group of parameters (query or header) emitted as a single typed parameter.
*/
export type ParamGroupType = {
/**
* Type expression for the group, a plain group-name reference.
*/
type: TypeExpression
/**
* Whether the parameter group is optional.
*/
optional: boolean
}
/**
* A single member of a destructured parameter group, fed to `createFunctionParameter({ properties })`.
*/
type GroupProperty = {
name: string
type: TypeExpression
optional?: boolean
}
/**
* Resolver interface for {@link createOperationParams}.
*
* `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
*/
export type OperationParamsResolver = {
/**
* Resolves the type name for an individual parameter.
*
* @example Individual path parameter name
* `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
*/
resolveParamName(node: OperationNode, param: ParameterNode): string
/**
* Resolves the request body type name.
*
* @example Request body type name
* `resolver.resolveDataName(node) // → 'CreatePetData'`
*/
resolveDataName(node: OperationNode): string
/**
* Resolves the grouped path parameters type name.
* When the return value equals `resolveParamName`, no indexed access is emitted.
*
* @example Grouped path params type name
* `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
*/
resolvePathParamsName(node: OperationNode, param: ParameterNode): string
/**
* Resolves the grouped query parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped query params type name
* `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
*/
resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
/**
* Resolves the grouped header parameters type name.
* When the return value equals `resolveParamName`, an inline struct type is emitted instead.
*
* @example Grouped header params type name
* `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
*/
resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
}
/**
* Options for {@link createOperationParams}.
*/
export type CreateOperationParamsOptions = {
/**
* How all operation parameters are grouped in the function signature.
* - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
* - `'inline'` emits each param category as a separate top-level parameter
*/
paramsType: 'object' | 'inline'
/**
* How path parameters are emitted when `paramsType` is `'inline'`.
* - `'object'` groups them as `{ petId, storeId }: PathParams`
* - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
* - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
*/
pathParamsType: 'object' | 'inline' | 'inlineSpread'
/**
* Converts parameter names to camelCase before output.
*/
paramsCasing?: 'camelcase'
/**
* Resolver for parameter and request body type names.
* Pass `ResolverTs` from `@kubb/plugin-ts` directly.
* When omitted, falls back to the schema primitive or `'unknown'`.
*/
resolver?: OperationParamsResolver
/**
* Default value for the path parameters binding when `pathParamsType` is `'object'`.
* Falls back to `'{}'` when all path params are optional.
*/
pathParamsDefault?: string
/**
* Extra parameters appended after the standard operation parameters.
*
* @example Plugin-specific trailing parameter
* ```ts
* extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
* ```
*/
extraParams?: Array<FunctionParameterNode>
/**
* Override the default parameter names used for body, query, header, and rest-path groups.
*
* Useful when targeting languages or frameworks with different naming conventions.
*
* @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
*/
paramNames?: {
/**
* Name for the request body parameter.
* @default 'data'
*/
data?: string
/**
* Name for the query parameters group parameter.
* @default 'params'
*/
params?: string
/**
* Name for the header parameters group parameter.
* @default 'headers'
*/
headers?: string
/**
* Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
* @default 'pathParams'
*/
path?: string
}
/**
* Applies a uniform transformation to every resolved type name before it is used
* in a parameter node. Use this for framework-level type wrappers.
*
* @example Vue Query, wrap every parameter type with `MaybeRefOrGetter`
* `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
*/
typeWrapper?: (type: string) => string
}
/**
* Resolves the {@link TypeExpression} for an individual parameter.
*
* Without a resolver, falls back to the schema primitive (a plain type-name string).
* When the parameter belongs to a named group, emits an {@link IndexedAccessTypeNode}
* (`GroupParams['petId']`); otherwise the resolved individual name as a plain string.
*/
export function resolveParamType({
node,
param,
resolver,
}: {
node: OperationNode
param: ParameterNode
resolver: OperationParamsResolver | undefined
}): TypeExpression {
if (!resolver) {
return param.schema.primitive ?? 'unknown'
}
const individualName = resolver.resolveParamName(node, param)
const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined
const groupResolvers = {
path: resolver.resolvePathParamsName,
query: resolver.resolveQueryParamsName,
header: resolver.resolveHeaderParamsName,
} as const
const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined
if (groupName && groupName !== individualName) {
return createIndexedAccessType({ objectType: groupName, indexType: param.name })
}
return individualName
}
/**
* Converts an `OperationNode` into function parameters for code generation.
*
* Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one
* destructured object parameter (`object`) and separate top-level parameters (`inline`), while
* `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type
* name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object.
*/
export function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {
const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options
const dataName = paramNames?.data ?? 'data'
const paramsName = paramNames?.params ?? 'params'
const headersName = paramNames?.headers ?? 'headers'
const pathName = paramNames?.path ?? 'pathParams'
const wrapType = (type: string): string => (typeWrapper ? typeWrapper(type) : type)
// Only plain type-name references are wrapped, they need casing applied.
// TypeLiteral and IndexedAccessType expressions are pre-resolved and pass through unchanged.
const wrapTypeExpression = (type: TypeExpression): TypeExpression => (typeof type === 'string' ? wrapType(type) : type)
const casedParams = caseParams(node.parameters, paramsCasing)
const pathParams = casedParams.filter((p) => p.in === 'path')
const queryParams = casedParams.filter((p) => p.in === 'query')
const headerParams = casedParams.filter((p) => p.in === 'header')
const toProperty = (param: ParameterNode): GroupProperty => ({
name: param.name,
type: wrapTypeExpression(resolveParamType({ node, param, resolver })),
optional: !param.required,
})
const emptyObjectDefault = (props: Array<GroupProperty>): string | undefined => (props.every((p) => p.optional) ? '{}' : undefined)
const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined
const bodyProperty: Array<GroupProperty> = bodyType ? [{ name: dataName, type: bodyType, optional: !(node.requestBody?.required ?? false) }] : []
const trailingGroups: Array<BuildGroupArgs> = [
{ name: paramsName, node, params: queryParams, groupType: resolveGroupType({ node, params: queryParams, group: 'query', resolver }), resolver, wrapType },
{
name: headersName,
node,
params: headerParams,
groupType: resolveGroupType({ node, params: headerParams, group: 'header', resolver }),
resolver,
wrapType,
},
]
const params: Array<FunctionParameterNode> = []
if (paramsType === 'object') {
const children = [...pathParams.map(toProperty), ...bodyProperty, ...trailingGroups.flatMap(buildGroupProperty)]
if (children.length) {
params.push(createFunctionParameter({ properties: children, default: emptyObjectDefault(children) }))
}
} else {
if (pathParamsType === 'inlineSpread' && pathParams.length) {
const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!)
params.push(createFunctionParameter({ name: pathName, type: spreadType ? wrapType(spreadType) : undefined, rest: true }))
} else if (pathParamsType === 'inline') {
params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p))))
} else if (pathParams.length) {
const pathChildren = pathParams.map(toProperty)
params.push(createFunctionParameter({ properties: pathChildren, default: pathParamsDefault ?? emptyObjectDefault(pathChildren) }))
}
params.push(...bodyProperty.map((p) => createFunctionParameter(p)))
params.push(...trailingGroups.flatMap(buildGroupParam))
}
params.push(...extraParams)
return createFunctionParameters({ params })
}
/**
* Shared arguments for building a query or header parameter group.
*/
export type BuildGroupArgs = {
name: string
node: OperationNode
params: Array<ParameterNode>
groupType: ParamGroupType | null
resolver: OperationParamsResolver | undefined
wrapType: (type: string) => string
}
/**
* Builds the property descriptor for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }: BuildGroupArgs): Array<GroupProperty> {
if (groupType) {
const type = typeof groupType.type === 'string' ? wrapType(groupType.type) : groupType.type
return [{ name, type, optional: groupType.optional }]
}
if (params.length) {
return [{ name, type: buildTypeLiteral({ node, params, resolver }), optional: params.every((p) => !p.required) }]
}
return []
}
/**
* Builds a single {@link FunctionParameterNode} for a query or header group.
* Returns an empty array when there are no params to emit.
*
* A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline
* {@link TypeLiteralNode} from the individual params.
*/
export function buildGroupParam(args: BuildGroupArgs): Array<FunctionParameterNode> {
return buildGroupProperty(args).map((p) => createFunctionParameter(p))
}
/**
* Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields.
*
* Used when query or header parameters have no dedicated group type name.
* Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
*/
export function buildTypeLiteral({
node,
params,
resolver,
}: {
node: OperationNode
params: Array<ParameterNode>
resolver: OperationParamsResolver | undefined
}): TypeLiteralNode {
return createTypeLiteral({
members: params.map((p) => ({
name: p.name,
type: resolveParamType({ node, param: p, resolver }),
optional: !p.required,
})),
})
}
function sourceKey(source: SourceNode): string {
const nameKey = source.name ?? extractStringsFromNodes(source.nodes)
return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`
}
function pathTypeKey(path: string, isTypeOnly: boolean | null | undefined): string {
return `${path}:${isTypeOnly ?? false}`
}
function exportKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined, asAlias: boolean | null | undefined): string {
return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`
}
function importKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined): string {
return `${path}:${name ?? ''}:${isTypeOnly ?? false}`
}
/**
* Computes a multi-level sort key for exports and imports:
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
*/
function sortKey(node: { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string }): string {
const isArray = Array.isArray(node.name) ? '1' : '0'
const typeOnly = node.isTypeOnly ? '0' : '1'
const hasName = node.name != null ? '1' : '0'
const name = Array.isArray(node.name) ? node.name.toSorted().join('\0') : (node.name ?? '')
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`
}
/**
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
*
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
*/
export function combineSources(sources: Array<SourceNode>): Array<SourceNode> {
const seen = new Map<string, SourceNode>()
for (const source of sources) {
const key = sourceKey(source)
if (!seen.has(key)) seen.set(key, source)
}
return [...seen.values()]
}
/**
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
*
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
*/
function mergeNameArrays<TName>(existing: Array<TName>, incoming: Array<TName>): Array<TName> {
const merged = new Set(existing)
for (const name of incoming) merged.add(name)
return [...merged]
}
/**
* Deduplicates and merges `ExportNode` objects by path and type.
*
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
*/
export function combineExports(exports: Array<ExportNode>): Array<ExportNode> {
const result: Array<ExportNode> = []
// Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging
const namedByPath = new Map<string, ExportNode>()
// Deduplicates non-array exports by their exact identity
const seen = new Set<string>()
// Precompute sort keys once, avoids recomputing per comparison.
const keyed = exports.map((node) => ({ node, key: sortKey(node) }))
keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
for (const { node: curr } of keyed) {
const { name, path, isTypeOnly, asAlias } = curr
if (Array.isArray(name)) {
if (!name.length) continue
const key = pathTypeKey(path, isTypeOnly)
const existing = namedByPath.get(key)
if (existing && Array.isArray(existing.name)) {
existing.name = mergeNameArrays(existing.name, name)
} else {
const newItem: ExportNode = { ...curr, name: [...new Set(name)] }
result.push(newItem)
namedByPath.set(key, newItem)
}
} else {
const key = exportKey(path, name, isTypeOnly, asAlias)
if (!seen.has(key)) {
result.push(curr)
seen.add(key)
}
}
}
return result
}
/**
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
*
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
*
* @note Use this when combining imports from multiple files to avoid duplicate declarations.
*/
export function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {
// Build a lookup of all exported names to retain imports that are re-exported
const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))
const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)
// Memoize object import names so the same logical (propertyName, name) pair always
// reuses the same object reference. Set-based deduplication then works correctly.
const importNameMemo = new Map<string, { propertyName: string; name?: string }>()
const canonicalizeName = (n: string | { propertyName: string; name?: string }): string | { propertyName: string; name?: string } => {
if (typeof n === 'string') return n
const key = `${n.propertyName}:${n.name ?? ''}`
if (!importNameMemo.has(key)) importNameMemo.set(key, n)
return importNameMemo.get(key)!
}
// Paths that keep at least one used named import. A default import from such a path is retained
// even when its binding can't be found in `source` e.g. a generated `client` default import
// alongside `import type { Client } from <same path>`, where merged grouped output omits the body.
const pathsWithUsedNamedImport = new Set<string>()
for (const node of imports) {
if (!Array.isArray(node.name)) continue
if (node.name.some((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))) {
pathsWithUsedNamedImport.add(node.path)
}
}
const result: Array<ImportNode> = []
// Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging
const namedByPath = new Map<string, ImportNode>()
// Deduplicates non-array imports by their exact identity
const seen = new Set<string>()
// Precompute sort keys once, avoids recomputing per comparison.
const keyed = imports.map((node) => ({ node, key: sortKey(node) }))
keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
for (const { node: curr } of keyed) {
if (curr.path === curr.root) continue
const { path, isTypeOnly } = curr
let { name } = curr
if (Array.isArray(name)) {
name = [...new Set(name.map(canonicalizeName))].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))
if (!name.length) continue
const key = pathTypeKey(path, isTypeOnly)
const existing = namedByPath.get(key)
if (existing && Array.isArray(existing.name)) {
existing.name = mergeNameArrays(existing.name, name)
} else {
const newItem: ImportNode = { ...curr, name }
result.push(newItem)
namedByPath.set(key, newItem)
}
} else {
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue
const key = importKey(path, name, isTypeOnly)
if (!seen.has(key)) {
result.push(curr)
seen.add(key)
}
}
}
return result
}
/**
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
*
* Returns `null` for non-ref nodes or when no name can be resolved. Use this to get a schema's
* identifier for type definitions or error messages.
*
* @example
* ```ts
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
* // => 'Pet'
* ```
*/
export function resolveRefName(node: SchemaNode | undefined): string | null {
if (!node || node.type !== 'ref') return null
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null
return node.name ?? node.schema?.name ?? null
}
/**
* Collects every named schema referenced (transitively) from a node via ref edges.
*
* Refs are followed by name only, the resolved `node.schema` is not traversed inline.
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
*
* @example Collect refs from a single schema
* ```ts
* const names = collectReferencedSchemaNames(petSchema)
* // → Set { 'Category', 'Tag' }
* ```
*
* @example Accumulate refs from multiple schemas into one set
* ```ts
* const out = new Set<string>()
* for (const schema of schemas) {
* collectReferencedSchemaNames(schema, out)
* }
* ```
*/
const collectSchemaRefs = memoize(new WeakMap<SchemaNode, ReadonlySet<string>>(), (node: SchemaNode): ReadonlySet<string> => {
const refs = new Set<string>()
collect<void>(node, {
schema(child) {
if (child.type === 'ref') {
const name = resolveRefName(child)
if (name) refs.add(name)
}
},
})
return refs
})
export function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {
if (!node) return out
for (const name of collectSchemaRefs(node)) out.add(name)
return out
}
/**
* Collects the names of all top-level schemas transitively used by a set of operations.
*
* An operation uses a schema when any of its parameters, request body content, or responses
* reference it, directly or indirectly through other named schemas.
* The walk is iterative and safe against reference cycles.
*
* Use this together with `include` filters to determine which schemas from `components/schemas`
* are reachable from the allowed operations, so that schemas used only by excluded operations
* are not generated.
*
* @example Only generate schemas referenced by included operations
* ```ts
* const includedOps = operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
* const allowed = collectUsedSchemaNames(includedOps, schemas)
*
* for (const schema of schemas) {
* if (schema.name && !allowed.has(schema.name)) continue
* // … generate schema
* }
* ```
*
* @example Check whether a specific schema is needed
* ```ts
* const allowed = collectUsedSchemaNames(includedOps, schemas)
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
* ```
*/
const collectUsedSchemaNamesMemo = memoize(new WeakMap<ReadonlyArray<OperationNode>, (schemas: ReadonlyArray<SchemaNode>) => Set<string>>(), (ops) =>
memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas) => computeUsedSchemaNames(ops, schemas)),
)
function computeUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {
const schemaMap = new Map<string, SchemaNode>()
for (const schema of schemas) {
if (schema.name) schemaMap.set(schema.name, schema)
}
const result = new Set<string>()
function visitSchema(schema: SchemaNode): void {
const directRefs = collectReferencedSchemaNames(schema)
for (const name of directRefs) {
if (!result.has(name)) {
result.add(name)
const namedSchema = schemaMap.get(name)
if (namedSchema) visitSchema(namedSchema)
}
}
}
for (const op of operations) {
for (const schema of collectLazy<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {
visitSchema(schema)
}
}
return result
}
export function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {
return collectUsedSchemaNamesMemo(operations)(schemas)
}
const EMPTY_CIRCULAR_SET = new Set<string>()
const findCircularSchemasMemo = memoize(new WeakMap<ReadonlyArray<SchemaNode>, Set<string>>(), (schemas: ReadonlyArray<SchemaNode>): Set<string> => {
const graph = new Map<string, Set<string>>()
for (const schema of schemas) {
if (!schema.name) continue
graph.set(schema.name, collectReferencedSchemaNames(schema))
}
const circular = new Set<string>()
for (const start of graph.keys()) {
const visited = new Set<string>()
const stack: Array<string> = [...(graph.get(start) ?? [])]
while (stack.length > 0) {
const node = stack.pop()!
if (node === start) {
circular.add(start)
break
}
if (visited.has(node)) continue
visited.add(node)
const next = graph.get(node)
if (next) for (const r of next) stack.push(r)
}
}
return circular
})
/**
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
*
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
* in deferred constructs (lazy getter, `z.lazy(() => …)`) to prevent infinite recursion when generated code runs.
* Refs are followed by name only, keeping the algorithm linear in the schema graph size.
*
* @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
*/
export function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {
if (schemas.length === 0) return EMPTY_CIRCULAR_SET
return findCircularSchemasMemo(schemas)
}
/**
* Type guard returning `true` when a schema or anything nested within it contains a ref to a circular schema.
*
* Use `excludeName` to ignore refs to specific schemas (useful when self-references are handled separately).
* Commonly used with `findCircularSchemas()` to detect where lazy wrappers are needed in code generation.
*
* @note Returns `true` for the first matching circular ref found. Use for fast dependency checks.
*/
export function containsCircularRef(
node: SchemaNode | undefined,
{ circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },
): boolean {
if (!node || circularSchemas.size === 0) return false
for (const _ of collectLazy<true>(node, {
schema(child) {
if (child.type !== 'ref') return null
const name = resolveRefName(child)
return name && name !== excludeName && circularSchemas.has(name) ? true : null
},
})) {
return true
}
return false
}

Sorry, the diff of this file is too big to display