@kubb/ast
Advanced tools
Sorry, the diff of this file is too big to display
| //#region \0rolldown/runtime.js | ||
| var __defProp = Object.defineProperty; | ||
| var __name = (target, value) => __defProp(target, "name", { | ||
| value, | ||
| configurable: true | ||
| }); | ||
| 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; | ||
| }; | ||
| //#endregion | ||
| export { __name as n, __exportAll as t }; |
| 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"} |
| 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; | ||
| 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.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"; | ||
| //#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 }; | ||
| } | ||
| /** | ||
| * 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 | ||
| * ``` | ||
| */ | ||
| declare function update<T extends Node>(node: T, changes: Partial<T>): T; | ||
| /** | ||
| * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed | ||
| * and `imports`/`exports`/`sources` are deduplicated. | ||
| */ | ||
| type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> & Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>; | ||
| /** | ||
| * 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' | ||
| * ``` | ||
| */ | ||
| declare function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta>; | ||
| //#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 }; | ||
| //# sourceMappingURL=factory.d.ts.map |
| 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 { 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 type { CodeNode } from '../nodes/code.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. | ||
| */ | ||
| export function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string { | ||
| if (!nodes?.length) return '' | ||
| return nodes | ||
| .map((node) => { | ||
| // Backward-compat: compiled plugins may still pass bare strings at runtime | ||
| if (typeof node === 'string') return node as string | ||
| if (node.kind === 'Text') return node.value | ||
| if (node.kind === 'Break') return '' | ||
| if (node.kind === 'Jsx') return node.value | ||
| const parts: Array<string> = [] | ||
| 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') | ||
| } |
+6
-3
@@ -1,3 +0,6 @@ | ||
| import { t as __name } from "./chunk-C0LytTxp.js"; | ||
| import { $ as createParameter, $t as InferSchemaNode, A as applyDedupe, At as contentDef, B as inputDef, Bt as ScalarSchemaType, C as createFile, Cn as DistributiveOmit, Ct as createExport, D as DedupeCanonical, Dn as NodeKind, Dt as fileDef, E as defineSchemaDialect, En as syncOptionality, Et as exportDef, F as outputDef, Ft as IntersectionSchemaNode, G as operationDef, Gt as TimeSchemaNode, H as HttpOperationNode, Ht as SchemaNodeByType, I as InputMeta, It as NumberSchemaNode, J as responseDef, Jt as createSchema, K as ResponseNode, Kt as UnionSchemaNode, L as InputNode, Lt as ObjectSchemaNode, M as Node, Mt as DateSchemaNode, N as OutputNode, Nt as DatetimeSchemaNode, O as DedupeLookups, On as httpMethods, Ot as importDef, P as createOutput, Pt as EnumSchemaNode, Q as ParameterNode, Qt as propertyDef, R as createInput, Rt as PrimitiveSchemaType, S as UserFileNode, Sn as typeDef, St as SourceNode, T as SchemaDialect, Tn as defineNode, Tt as createSource, U as OperationNode, Ut as SchemaType, V as HttpMethod, Vt as SchemaNode, W as createOperation, Wt as StringSchemaNode, X as requestBodyDef, Xt as PropertyNode, Y as StatusCode, Yt as schemaDef, Z as ParameterLocation, Zt as createProperty, _ as Printer, _n as createText, _t as objectBindingPatternDef, a as createDiscriminantNode, an as JSDocNode, at as IndexedAccessTypeNode, b as createPrinterFactory, bn as jsxDef, bt as FileNode, c as findCircularSchemas, cn as TypeNode, ct as TypeLiteralNode, d as ParentOf, dn as constDef, dt as createIndexedAccessType, en as ParserOptions, et as parameterDef, f as Visitor, fn as createArrowFunction, ft as createObjectBindingPattern, g as walk, gn as createJsx, gt as indexedAccessTypeDef, h as transform, hn as createFunction, ht as functionParametersDef, i as containsCircularRef, in as FunctionNode, it as FunctionParametersNode, j as buildDedupePlan, jt as ArraySchemaNode, k as DedupePlan, kn as schemaTypes, kt as sourceDef, l as isStringType, ln as arrowFunctionDef, lt as createFunctionParameter, m as collect, mn as createConst, mt as functionParameterDef, n as caseParams, nn as CodeNode, nt as FunctionParamNode, o as createOperationParams, on as JsxNode, ot as ObjectBindingPatternNode, p as VisitorContext, pn as createBreak, pt as createTypeLiteral, q as createResponse, qt as UrlSchemaNode, r as collectUsedSchemaNames, rn as ConstNode, rt as FunctionParameterNode, s as extractStringsFromNodes, sn as TextNode, st as TypeExpression, t as OperationParamsResolver, tn as ArrowFunctionNode, tt as FunctionNodeType, u as syncSchemaRef, un as breakDef, ut as createFunctionParameters, v as PrinterFactoryOptions, vn as createType, vt as typeLiteralDef, w as update, wn as NodeDef, wt as createImport, x as definePrinter, xn as textDef, xt as ImportNode, y as PrinterPartial, yn as functionDef, yt as ExportNode, z as createStreamInput, zt as RefSchemaNode } from "./types-C5aVnRE1.js"; | ||
| 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 { 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"; | ||
@@ -97,3 +100,3 @@ //#region src/guards.d.ts | ||
| //#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 FunctionNodeType, 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, caseParams, collect, collectUsedSchemaNames, constDef, containsCircularRef, contentDef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createIndexedAccessType, createInput, createJsx, createObjectBindingPattern, createOperation, createOperationParams, createOutput, createParameter, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createStreamInput, createText, createType, createTypeLiteral, defineNode, definePrinter, defineSchemaDialect, exportDef, extractStringsFromNodes, fileDef, findCircularSchemas, functionDef, functionParameterDef, functionParametersDef, httpMethods, importDef, indexedAccessTypeDef, inputDef, isHttpOperationNode, isStringType, jsxDef, mergeAdjacentObjectsLazy, narrowSchema, objectBindingPatternDef, operationDef, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, setDiscriminatorEnum, setEnumName, signatureOf, simplifyUnion, sourceDef, syncOptionality, syncSchemaRef, textDef, transform, typeDef, typeLiteralDef, update, walk }; | ||
| 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 }; | ||
| //# sourceMappingURL=index.d.ts.map |
+4
-2
@@ -1,2 +0,4 @@ | ||
| import { $t as InferSchemaNode, Bt as ScalarSchemaType, Cn as DistributiveOmit, D as DedupeCanonical, Dn as NodeKind, Ft as IntersectionSchemaNode, Gt as TimeSchemaNode, H as HttpOperationNode, Ht as SchemaNodeByType, I as InputMeta, It as NumberSchemaNode, K as ResponseNode, Kt as UnionSchemaNode, L as InputNode, Lt as ObjectSchemaNode, M as Node, Mt as DateSchemaNode, N as OutputNode, Nt as DatetimeSchemaNode, O as DedupeLookups, Pt as EnumSchemaNode, Q as ParameterNode, Rt as PrimitiveSchemaType, S as UserFileNode, St as SourceNode, T as SchemaDialect, U as OperationNode, Ut as SchemaType, V as HttpMethod, Vt as SchemaNode, Wt as StringSchemaNode, Xt as PropertyNode, Y as StatusCode, Z as ParameterLocation, _ as Printer, an as JSDocNode, at as IndexedAccessTypeNode, bt as FileNode, cn as TypeNode, ct as TypeLiteralNode, d as ParentOf, en as ParserOptions, f as Visitor, in as FunctionNode, it as FunctionParametersNode, jt as ArraySchemaNode, k as DedupePlan, nn as CodeNode, nt as FunctionParamNode, on as JsxNode, ot as ObjectBindingPatternNode, p as VisitorContext, qt as UrlSchemaNode, rn as ConstNode, rt as FunctionParameterNode, sn as TextNode, st as TypeExpression, t as OperationParamsResolver, tn as ArrowFunctionNode, tt as FunctionNodeType, v as PrinterFactoryOptions, xt as ImportNode, y as PrinterPartial, yt as ExportNode, zt as RefSchemaNode } from "./types-C5aVnRE1.js"; | ||
| export type { ArraySchemaNode, ArrowFunctionNode, CodeNode, ConstNode, DateSchemaNode, DatetimeSchemaNode, DedupeCanonical, DedupeLookups, DedupePlan, DistributiveOmit, EnumSchemaNode, ExportNode, FileNode, FunctionNode, FunctionNodeType, 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 { 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 { 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 }; |
+12
-1
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| const require_utils = require("./utils-cdQ6Pzyi.cjs"); | ||
| 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; | ||
| exports.childName = require_utils.childName; | ||
| exports.collectUsedSchemaNames = require_utils.collectUsedSchemaNames; | ||
| exports.containsCircularRef = require_utils.containsCircularRef; | ||
| 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; | ||
| exports.isStringType = require_utils.isStringType; | ||
| exports.isValidVarName = require_utils.isValidVarName; | ||
| exports.jsStringEscape = require_utils.jsStringEscape; | ||
| exports.objectKey = 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; |
+21
-2
@@ -1,2 +0,4 @@ | ||
| import { t as __name } from "./chunk-C0LytTxp.js"; | ||
| 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"; | ||
@@ -190,4 +192,21 @@ //#region ../../internals/utils/src/reserved.d.ts | ||
| declare function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | 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). | ||
| */ | ||
| declare function resolveGroupType({ | ||
| node, | ||
| params, | ||
| group, | ||
| resolver | ||
| }: { | ||
| node: OperationNode; | ||
| params: Array<ParameterNode>; | ||
| group: 'query' | 'header'; | ||
| resolver: OperationParamsResolver | undefined; | ||
| }): ParamGroupType | null; | ||
| //#endregion | ||
| export { buildJSDoc, buildList, buildObject, childName, enumPropName, extractRefName, findDiscriminator, getNestedAccessor, isValidVarName, jsStringEscape, objectKey, stringify, stringifyObject, toRegExpString, trimQuotes }; | ||
| 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 }; | ||
| //# sourceMappingURL=utils.d.ts.map |
+2
-2
@@ -1,2 +0,2 @@ | ||
| import { a as enumPropName, c as getNestedAccessor, d as stringify, f as stringifyObject, h as isValidVarName, i as childName, l as jsStringEscape, m as trimQuotes, n as buildList, o as extractRefName, p as toRegExpString, r as buildObject, s as findDiscriminator, t as buildJSDoc, u as objectKey } from "./utils-0p8ZO287.js"; | ||
| export { buildJSDoc, buildList, buildObject, childName, enumPropName, extractRefName, findDiscriminator, getNestedAccessor, isValidVarName, jsStringEscape, objectKey, stringify, stringifyObject, toRegExpString, trimQuotes }; | ||
| 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 }; |
+5
-1
| { | ||
| "name": "@kubb/ast", | ||
| "version": "5.0.0-beta.57", | ||
| "version": "5.0.0-beta.58", | ||
| "description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.", | ||
@@ -35,2 +35,6 @@ "keywords": [ | ||
| }, | ||
| "./factory": { | ||
| "import": "./dist/factory.js", | ||
| "require": "./dist/factory.cjs" | ||
| }, | ||
| "./types": { | ||
@@ -37,0 +41,0 @@ "import": "./dist/types.js", |
+11
-7
@@ -31,6 +31,8 @@ <div align="center"> | ||
| | Path | Contents | | ||
| | ----------------- | ------------------------------------------------------------------- | | ||
| | `@kubb/ast` | Runtime: factory functions, guards, visitor, ref helpers, constants | | ||
| | `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types | | ||
| | Path | Contents | | ||
| | ------------------- | ---------------------------------------------------------------------------------------- | | ||
| | `@kubb/ast` | Runtime: node definitions, guards, visitor, transformers, constants | | ||
| | `@kubb/ast/factory` | Node constructors (`createSchema`, `createFile`, and friends), the `ts.factory` analogue | | ||
| | `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types | | ||
| | `@kubb/ast/utils` | Spec-agnostic string and identifier helpers, ref helpers | | ||
@@ -60,6 +62,8 @@ ## Node tree | ||
| Constructors live on the `@kubb/ast/factory` subpath, mirroring `ts.factory.createX`. Through `@kubb/core` the same set is reachable as `ast.factory.createSchema(...)`. | ||
| ```ts | ||
| import { createRoot, createOperation, createSchema, createProperty } from '@kubb/ast' | ||
| import { createInput, createSchema, createProperty } from '@kubb/ast/factory' | ||
| const root = createRoot({ | ||
| const root = createInput({ | ||
| schemas: [ | ||
@@ -130,3 +134,3 @@ createSchema({ | ||
| ```ts | ||
| import { extractRefName } from '@kubb/ast' | ||
| import { extractRefName } from '@kubb/ast/utils' | ||
@@ -133,0 +137,0 @@ extractRefName('#/components/schemas/Pet') // 'Pet' |
+19
-1
@@ -5,4 +5,22 @@ import { hash } from 'node:crypto' | ||
| import type { FileNode, Node } from './nodes/index.ts' | ||
| import { combineExports, combineImports, combineSources, extractStringsFromNodes } from './utils/ast.ts' | ||
| import { combineExports, combineImports, combineSources } from './utils/ast.ts' | ||
| import { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts' | ||
| // Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes | ||
| // `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the | ||
| // barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module. | ||
| export { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts' | ||
| export { createContent } from './nodes/content.ts' | ||
| export { createExport, createImport, createSource } from './nodes/file.ts' | ||
| export { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts' | ||
| export { createInput } from './nodes/input.ts' | ||
| export { createOperation } from './nodes/operation.ts' | ||
| export { createOutput } from './nodes/output.ts' | ||
| export { createParameter } from './nodes/parameter.ts' | ||
| export { createProperty } from './nodes/property.ts' | ||
| export { createRequestBody } from './nodes/requestBody.ts' | ||
| export { createResponse } from './nodes/response.ts' | ||
| export { createSchema } from './nodes/schema.ts' | ||
| export { createDiscriminantNode, createOperationParams } from './utils/ast.ts' | ||
| /** | ||
@@ -9,0 +27,0 @@ * Identity-preserving node update: returns `node` unchanged when every field in |
+14
-48
| export { httpMethods, schemaTypes } from './constants.ts' | ||
| export { applyDedupe, buildDedupePlan } from './dedupe.ts' | ||
| export { defineSchemaDialect } from './dialect.ts' | ||
| export { createFile, update } from './factory.ts' | ||
| // Node constructors, also published as the `@kubb/ast/factory` subpath, mirroring `ts.factory.createX`. | ||
| export * as factory from './factory.ts' | ||
| export { isHttpOperationNode, narrowSchema } from './guards.ts' | ||
@@ -9,40 +10,14 @@ export { defineNode } from './node.ts' | ||
| export { syncOptionality } from './node.ts' | ||
| export { | ||
| arrowFunctionDef, | ||
| breakDef, | ||
| constDef, | ||
| createArrowFunction, | ||
| createBreak, | ||
| createConst, | ||
| createFunction, | ||
| createJsx, | ||
| createText, | ||
| createType, | ||
| functionDef, | ||
| jsxDef, | ||
| textDef, | ||
| typeDef, | ||
| } from './nodes/code.ts' | ||
| export { arrowFunctionDef, breakDef, constDef, functionDef, jsxDef, textDef, typeDef } from './nodes/code.ts' | ||
| export { contentDef } from './nodes/content.ts' | ||
| export { createExport, createImport, createSource, exportDef, fileDef, importDef, sourceDef } from './nodes/file.ts' | ||
| export { | ||
| createFunctionParameter, | ||
| createFunctionParameters, | ||
| createIndexedAccessType, | ||
| createObjectBindingPattern, | ||
| createTypeLiteral, | ||
| functionParameterDef, | ||
| functionParametersDef, | ||
| indexedAccessTypeDef, | ||
| objectBindingPatternDef, | ||
| typeLiteralDef, | ||
| } from './nodes/function.ts' | ||
| export { createInput, createStreamInput, inputDef } from './nodes/input.ts' | ||
| export { createOperation, operationDef } from './nodes/operation.ts' | ||
| export { createOutput, outputDef } from './nodes/output.ts' | ||
| export { createParameter, parameterDef } from './nodes/parameter.ts' | ||
| export { createProperty, propertyDef } from './nodes/property.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 { createResponse, responseDef } from './nodes/response.ts' | ||
| export { createSchema, schemaDef } from './nodes/schema.ts' | ||
| export { responseDef } from './nodes/response.ts' | ||
| export { schemaDef } from './nodes/schema.ts' | ||
| export { createPrinterFactory, definePrinter } from './printer.ts' | ||
@@ -52,13 +27,4 @@ export { signatureOf } from './signature.ts' | ||
| export type * from './types.ts' | ||
| export { | ||
| caseParams, | ||
| collectUsedSchemaNames, | ||
| containsCircularRef, | ||
| createDiscriminantNode, | ||
| createOperationParams, | ||
| extractStringsFromNodes, | ||
| findCircularSchemas, | ||
| isStringType, | ||
| syncSchemaRef, | ||
| } from './utils/ast.ts' | ||
| // The node/AST helpers in ./utils/ast.ts live on the `@kubb/ast/utils` subpath, not the root barrel. | ||
| export { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts' | ||
| export { collect, transform, walk } from './visitor.ts' |
+1
-1
@@ -26,3 +26,3 @@ import type { BaseNode, NodeKind } from './nodes/base.ts' | ||
| */ | ||
| export function isKind<T extends BaseNode>(kind: NodeKind) { | ||
| function isKind<T extends BaseNode>(kind: NodeKind) { | ||
| return (node: unknown): node is T => (node as BaseNode).kind === kind | ||
@@ -29,0 +29,0 @@ } |
+0
-10
@@ -50,11 +50,1 @@ /** | ||
| } | ||
| /** | ||
| * Minimal node type when only `kind` is needed. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const node: Node = { kind: 'Operation' } | ||
| * ``` | ||
| */ | ||
| export type Node = BaseNode |
@@ -177,5 +177,5 @@ import { defineNode } from '../node.ts' | ||
| /** | ||
| * Handler map keys, one per `FunctionParamNode` kind. | ||
| * Handler-map keys for the function-parameter printer, one per {@link FunctionParamNode} kind. | ||
| */ | ||
| export type FunctionNodeType = 'functionParameter' | 'functionParameters' | 'typeLiteral' | 'indexedAccessType' | 'objectBindingPattern' | ||
| export type FunctionParamKind = FunctionParamNode['kind'] | ||
@@ -182,0 +182,0 @@ /** |
@@ -19,3 +19,3 @@ import type { ArrowFunctionNode, ConstNode, FunctionNode, TypeNode } from './code.ts' | ||
| export type { | ||
| FunctionNodeType, | ||
| FunctionParamKind, | ||
| FunctionParameterNode, | ||
@@ -22,0 +22,0 @@ FunctionParametersNode, |
+14
-13
@@ -117,5 +117,7 @@ import type { Streamable } from '@internals/utils' | ||
| /** | ||
| * Creates an `InputNode` with stable defaults for `schemas` and `operations`. | ||
| * 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 | ||
| * @example Eager | ||
| * ```ts | ||
@@ -125,17 +127,16 @@ * const input = createInput() | ||
| * ``` | ||
| */ | ||
| export function createInput(overrides: Partial<Omit<InputNode, 'kind'>> = {}): InputNode { | ||
| return inputDef.create(overrides) | ||
| } | ||
| /** | ||
| * Creates a streaming `InputNode<true>` from pre-built `AsyncIterable` sources. | ||
| * | ||
| * @example | ||
| * @example Streaming | ||
| * ```ts | ||
| * const node = createStreamInput(schemasIterable, operationsIterable, { title: 'My API' }) | ||
| * const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } }) | ||
| * ``` | ||
| */ | ||
| export function createStreamInput(schemas: AsyncIterable<SchemaNode>, operations: AsyncIterable<OperationNode>, meta?: InputMeta): InputNode<true> { | ||
| return { kind: 'Input', schemas, operations, meta } | ||
| export function createInput<Stream extends boolean = false>(options: Partial<Omit<InputNode<Stream>, 'kind'>> & { stream?: Stream } = {}): InputNode<Stream> { | ||
| const { stream, ...overrides } = options | ||
| // Streaming inputs carry AsyncIterable sources, so skip the array/meta defaults that | ||
| // inputDef.create applies for the eager variant. | ||
| if (stream) { | ||
| return { kind: 'Input', ...overrides } as InputNode<Stream> | ||
| } | ||
| return inputDef.create(overrides as Partial<Omit<InputNode, 'kind'>>) as InputNode<Stream> | ||
| } |
+3
-3
@@ -195,7 +195,7 @@ import type { SchemaNode, SchemaNodeByType, SchemaType } from './nodes/index.ts' | ||
| * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`. | ||
| ** | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>( | ||
| * (node) => kindToHandlerKey[node.kind], | ||
| * export const defineFunctionPrinter = createPrinterFactory<FunctionParamNode, FunctionParamKind, Partial<Record<FunctionParamKind, FunctionParamNode>>>( | ||
| * (node) => node.kind, | ||
| * ) | ||
@@ -202,0 +202,0 @@ * ``` |
+1
-1
@@ -16,3 +16,3 @@ export type { DedupeCanonical, DedupeLookups, DedupePlan } from './dedupe.ts' | ||
| FunctionNode, | ||
| FunctionNodeType, | ||
| FunctionParamKind, | ||
| FunctionParameterNode, | ||
@@ -19,0 +19,0 @@ FunctionParametersNode, |
+3
-66
@@ -8,3 +8,2 @@ import { camelCase, isValidVarName, memoize } from '@internals/utils' | ||
| import type { | ||
| CodeNode, | ||
| ExportNode, | ||
@@ -22,3 +21,3 @@ FunctionParameterNode, | ||
| import type { SchemaType } from '../nodes/schema.ts' | ||
| import { extractRefName } from './index.ts' | ||
| import { extractRefName, extractStringsFromNodes, resolveGroupType } from './index.ts' | ||
| import { collect, collectLazy } from '../visitor.ts' | ||
@@ -125,3 +124,3 @@ | ||
| */ | ||
| type ParamGroupType = { | ||
| export type ParamGroupType = { | ||
| /** | ||
@@ -389,3 +388,3 @@ * Type expression for the group, a plain group-name reference. | ||
| */ | ||
| type BuildGroupArgs = { | ||
| export type BuildGroupArgs = { | ||
| name: string | ||
@@ -429,31 +428,2 @@ node: OperationNode | ||
| /** | ||
| * 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) } | ||
| } | ||
| /** | ||
| * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields. | ||
@@ -660,35 +630,2 @@ * | ||
| /** | ||
| * 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 internally to build the full source string for import filtering. | ||
| */ | ||
| export function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string { | ||
| if (!nodes?.length) return '' | ||
| return nodes | ||
| .map((node) => { | ||
| // Backward-compat: compiled plugins may still pass bare strings at runtime | ||
| if (typeof node === 'string') return node as string | ||
| if (node.kind === 'Text') return node.value | ||
| if (node.kind === 'Break') return '' | ||
| if (node.kind === 'Jsx') return node.value | ||
| const parts: Array<string> = [] | ||
| 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') | ||
| } | ||
| /** | ||
| * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`. | ||
@@ -695,0 +632,0 @@ * |
+44
-0
| 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 { extractStringsFromNodes } from './extractStringsFromNodes.ts' | ||
| export { | ||
| buildGroupParam, | ||
| buildTypeLiteral, | ||
| caseParams, | ||
| collectUsedSchemaNames, | ||
| containsCircularRef, | ||
| findCircularSchemas, | ||
| isStringType, | ||
| resolveParamType, | ||
| syncSchemaRef, | ||
| } from './ast.ts' | ||
| export type { BuildGroupArgs, ParamGroupType } from './ast.ts' | ||
@@ -298,1 +313,30 @@ /** | ||
| } | ||
| /** | ||
| * 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) } | ||
| } |
| //#region \0rolldown/runtime.js | ||
| var __defProp = Object.defineProperty; | ||
| var __name = (target, value) => __defProp(target, "name", { | ||
| value, | ||
| configurable: true | ||
| }); | ||
| //#endregion | ||
| export { __name as t }; |
Sorry, the diff of this file is too big to display
| import "./chunk-C0LytTxp.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 ../../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/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/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; | ||
| } | ||
| //#endregion | ||
| export { httpMethods as _, enumPropName as a, visitorDepths as b, getNestedAccessor as c, stringify as d, stringifyObject as f, camelCase as g, isValidVarName as h, childName as i, jsStringEscape as l, trimQuotes as m, buildList as n, extractRefName as o, toRegExpString as p, buildObject as r, findDiscriminator as s, buildJSDoc as t, objectKey as u, isScalarPrimitive as v, schemaTypes as y }; | ||
| //# sourceMappingURL=utils-0p8ZO287.js.map |
| {"version":3,"file":"utils-0p8ZO287.js","names":[],"sources":["../src/constants.ts","../../../internals/utils/src/casing.ts","../../../internals/utils/src/reserved.ts","../../../internals/utils/src/string.ts","../src/utils/index.ts"],"sourcesContent":["import type { HttpMethod } from './nodes/operation.ts'\nimport type { SchemaType } from './nodes/schema.ts'\n\n/**\n * Traversal depth for AST visitor utilities.\n *\n * - `'shallow'` visits only the immediate node, skipping children.\n * - `'deep'` recursively visits all descendant nodes.\n */\nexport type VisitorDepth = 'shallow' | 'deep'\n\nexport const visitorDepths = {\n shallow: 'shallow',\n deep: 'deep',\n} as const satisfies Record<VisitorDepth, VisitorDepth>\n\n/**\n * Schema type discriminators used by all AST schema nodes.\n *\n * These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).\n * Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),\n * and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.\n */\nexport const schemaTypes = {\n /**\n * Text value.\n */\n string: 'string',\n /**\n * Floating-point number (`float`, `double`).\n */\n number: 'number',\n /**\n * Whole number (`int32`). Use `bigint` for `int64`.\n */\n integer: 'integer',\n /**\n * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.\n */\n bigint: 'bigint',\n /**\n * Boolean value\n */\n boolean: 'boolean',\n /**\n * Explicit null value.\n */\n null: 'null',\n /**\n * Any value (no type restriction).\n */\n any: 'any',\n /**\n * Unknown value (must be narrowed before usage).\n */\n unknown: 'unknown',\n /**\n * No return value (`void`).\n */\n void: 'void',\n /**\n * Object with named properties.\n */\n object: 'object',\n /**\n * Sequential list of items.\n */\n array: 'array',\n /**\n * Fixed-length list with position-specific items.\n */\n tuple: 'tuple',\n /**\n * \"One of\" multiple schema members.\n */\n union: 'union',\n /**\n * \"All of\" multiple schema members.\n */\n intersection: 'intersection',\n /**\n * Enum schema.\n */\n enum: 'enum',\n /**\n * Reference to another schema.\n */\n ref: 'ref',\n /**\n * Calendar date (for example `2026-03-24`).\n */\n date: 'date',\n /**\n * Date-time value (for example `2026-03-24T09:00:00Z`).\n */\n datetime: 'datetime',\n /**\n * Time-only value (for example `09:00:00`).\n */\n time: 'time',\n /**\n * UUID value.\n */\n uuid: 'uuid',\n /**\n * Email address value.\n */\n email: 'email',\n /**\n * URL value.\n */\n url: 'url',\n /**\n * IPv4 address value.\n */\n ipv4: 'ipv4',\n /**\n * IPv6 address value.\n */\n ipv6: 'ipv6',\n /**\n * Binary/blob value.\n */\n blob: 'blob',\n /**\n * Impossible value (`never`).\n */\n never: 'never',\n} as const satisfies Record<SchemaType, SchemaType>\n\nexport type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n *\n * Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\n/**\n * Type guard that returns `true` when `type` is a scalar primitive schema type.\n *\n * Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).\n */\nexport function isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * HTTP method identifiers used by operation nodes.\n *\n * Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).\n */\nexport const httpMethods = {\n get: 'GET',\n post: 'POST',\n put: 'PUT',\n patch: 'PATCH',\n delete: 'DELETE',\n head: 'HEAD',\n options: 'OPTIONS',\n trace: 'TRACE',\n} as const satisfies Record<Lowercase<HttpMethod>, HttpMethod>\n\n/**\n * Default concurrency limit for `walk()` traversal utility.\n *\n * Set to 30 to balance I/O-bound resolver parallelism against event loop pressure and memory usage during large spec traversals.\n * Use `WALK_CONCURRENCY` when calling `walk()` or override for different hardware constraints.\n *\n * @example\n * ```ts\n * import { walk, WALK_CONCURRENCY } from '@kubb/ast'\n *\n * walk(root, { concurrency: WALK_CONCURRENCY, root: () => {} })\n * ```\n */\nexport const WALK_CONCURRENCY = 30\n\n/**\n * Number of spaces in one indentation level when assembling multi-line code as strings.\n * Set to 2, 3, … to change the indent width used by `buildObject`/`buildList`.\n */\nconst INDENT_SIZE = 2\n\n/**\n * One indentation level, derived from {@link INDENT_SIZE}.\n */\nexport const INDENT = Array.from({ length: INDENT_SIZE }, () => ' ').join('')\n","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","/**\n * JavaScript and Java reserved words.\n * @link https://github.com/jonschlinkert/reserved/blob/master/index.js\n */\nconst reservedWords = new Set([\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n 'Array',\n 'Date',\n 'hasOwnProperty',\n 'Infinity',\n 'isFinite',\n 'isNaN',\n 'isPrototypeOf',\n 'length',\n 'Math',\n 'name',\n 'NaN',\n 'Number',\n 'Object',\n 'prototype',\n 'String',\n 'toString',\n 'undefined',\n 'valueOf',\n] as const)\n\n/**\n * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.\n *\n * @example\n * ```ts\n * transformReservedWord('class') // '_class'\n * transformReservedWord('42foo') // '_42foo'\n * transformReservedWord('status') // 'status'\n * ```\n */\nexport function transformReservedWord(word: string): string {\n const firstChar = word.charCodeAt(0)\n if (word && (reservedWords.has(word as 'valueOf') || (firstChar >= 48 && firstChar <= 57))) {\n return `_${word}`\n }\n return word\n}\n\n/**\n * Returns `true` when `name` is a syntactically valid JavaScript variable name.\n *\n * @example\n * ```ts\n * isValidVarName('status') // true\n * isValidVarName('class') // false (reserved word)\n * isValidVarName('42foo') // false (starts with digit)\n * ```\n */\nexport function isValidVarName(name: string): boolean {\n if (!name || reservedWords.has(name as 'valueOf')) {\n return false\n }\n return isIdentifier(name)\n}\n\n/**\n * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.\n *\n * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys\n * even though they are not valid variable names, so use this (not {@link isValidVarName}) when\n * deciding whether an object key needs quoting.\n *\n * @example\n * ```ts\n * isIdentifier('name') // true\n * isIdentifier('x-total')// false\n * ```\n */\nexport function isIdentifier(name: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n","/**\n * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping\n * any backslash or single quote in the content.\n *\n * @example\n * ```ts\n * singleQuote('foo') // \"'foo'\"\n * singleQuote(\"o'clock\") // \"'o\\\\'clock'\"\n * ```\n */\nexport function singleQuote(value: string | number | boolean | undefined | null): string {\n if (value === undefined || value === null) return \"''\"\n const escaped = String(value).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")\n\n return `'${escaped}'`\n}\n","import { isIdentifier, pascalCase, singleQuote } from '@internals/utils'\nimport { INDENT } from '../constants.ts'\n\nexport { isValidVarName } from '@internals/utils'\n\n/**\n * Strips a single matching pair of `\"...\"`, `'...'`, or `` `...` `` from both ends of `text`.\n * Returns the string unchanged when no balanced quote pair is found.\n *\n * @example\n * ```ts\n * trimQuotes('\"hello\"') // 'hello'\n * trimQuotes('hello') // 'hello'\n * ```\n */\nexport function trimQuotes(text: string): string {\n if (text.length >= 2) {\n const first = text[0]\n const last = text[text.length - 1]\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\") || (first === '`' && last === '`')) {\n return text.slice(1, -1)\n }\n }\n return text\n}\n\n/**\n * Serializes a primitive value to a single-quoted string literal, stripping any surrounding quote\n * characters first. Escaping comes from `JSON.stringify`, then the quote style switches to single\n * quotes so generated code matches the repo style without a formatter.\n *\n * @example\n * ```ts\n * stringify('hello') // \"'hello'\"\n * stringify('\"hello\"') // \"'hello'\"\n * ```\n */\nexport function stringify(value: string | number | boolean | undefined): string {\n if (value === undefined || value === null) return \"''\"\n const json = JSON.stringify(trimQuotes(value.toString()))\n const inner = json.slice(1, -1).replace(/\\\\\"/g, '\"').replace(/'/g, \"\\\\'\")\n return `'${inner}'`\n}\n\n/**\n * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,\n * and the Unicode line terminators U+2028 and U+2029.\n *\n * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4\n *\n * @example\n * ```ts\n * jsStringEscape('say \"hi\"\\nbye') // 'say \\\\\"hi\\\\\"\\\\nbye'\n * ```\n */\nexport function jsStringEscape(input: unknown): string {\n return `${input}`.replace(/[\"'\\\\\\n\\r\\u2028\\u2029]/g, (character) => {\n switch (character) {\n case '\"':\n case \"'\":\n case '\\\\':\n return `\\\\${character}`\n case '\\n':\n return '\\\\n'\n case '\\r':\n return '\\\\r'\n case '\\u2028':\n return '\\\\u2028'\n case '\\u2029':\n return '\\\\u2029'\n default:\n return ''\n }\n })\n}\n\n/**\n * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.\n * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.\n * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.\n *\n * @example\n * ```ts\n * toRegExpString('^(?im)foo') // 'new RegExp(\"^foo\", \"im\")'\n * toRegExpString('^(?im)foo', null) // '/^foo/im'\n * ```\n */\nexport function toRegExpString(text: string, func: string | null = 'RegExp'): string {\n const raw = trimQuotes(text)\n\n const match = raw.match(/^\\^(\\(\\?([igmsuy]+)\\))/i)\n const replacementTarget = match?.[1] ?? ''\n const matchedFlags = match?.[2]\n const cleaned = raw\n .replace(/^\\\\?\\//, '')\n .replace(/\\\\?\\/$/, '')\n .replace(replacementTarget, '')\n\n const { source, flags } = new RegExp(cleaned, matchedFlags)\n\n if (func === null) return `/${source}/${flags}`\n\n return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`\n}\n\n/**\n * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested\n * objects recurse with fixed indentation, so the result drops straight into an object literal\n * without re-parsing.\n *\n * @example\n * ```ts\n * stringifyObject({ foo: 'bar', nested: { a: 1 } })\n * // 'foo: bar,\\nnested: {\\n a: 1\\n }'\n * ```\n */\nexport function stringifyObject(value: Record<string, unknown>): string {\n const items = Object.entries(value)\n .map(([key, val]) => {\n if (val !== null && typeof val === 'object') {\n return `${key}: {\\n ${stringifyObject(val as Record<string, unknown>)}\\n }`\n }\n return `${key}: ${val}`\n })\n .filter(Boolean)\n return items.join(',\\n')\n}\n\n/**\n * Renders a dotted path or string array as an optional-chaining accessor expression rooted at\n * `accessor`. Returns `null` for an empty path.\n *\n * @example\n * ```ts\n * getNestedAccessor('pagination.next.id', 'lastPage')\n * // \"lastPage?.['pagination']?.['next']?.['id']\"\n * ```\n */\nexport function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {\n const parts = Array.isArray(param) ? param : param.split('.')\n if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null\n return `${accessor}?.['${`${parts.join(\"']?.['\")}']`}`\n}\n\n/**\n * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no\n * comments so callers always get a usable string.\n *\n * @example\n * ```ts\n * buildJSDoc(['@type string', '@example hello'])\n * // '/**\\n * @type string\\n * @example hello\\n *\\/\\n '\n * ```\n */\nexport function buildJSDoc(\n comments: Array<string>,\n options: {\n /**\n * String used to indent each comment line.\n * @default ' * '\n */\n indent?: string\n /**\n * String appended after the closing tag.\n * @default '\\n '\n */\n suffix?: string\n /**\n * Returned as-is when `comments` is empty.\n * @default ' '\n */\n fallback?: string\n } = {},\n): string {\n const { indent = ' * ', suffix = '\\n ', fallback = ' ' } = options\n\n if (comments.length === 0) return fallback\n\n return `/**\\n${comments.map((c) => `${indent}${c}`).join('\\n')}\\n */${suffix}`\n}\n\n/**\n * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.\n */\nfunction indentLines(text: string): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${INDENT}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders an object key, quoting it with single quotes only when it is not a valid identifier.\n * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.\n *\n * @example\n * ```ts\n * objectKey('name') // 'name'\n * objectKey('x-total') // \"'x-total'\"\n * ```\n */\nexport function objectKey(name: string): string {\n return isIdentifier(name) ? name : singleQuote(name)\n}\n\n/**\n * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one\n * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,\n * so callers never re-parse the generated code. A trailing comma is added per entry to match the\n * formatter's multi-line style.\n *\n * @example\n * ```ts\n * buildObject(['id: z.number()', 'name: z.string()'])\n * // '{\\n id: z.number(),\\n name: z.string(),\\n}'\n * ```\n */\nexport function buildObject(entries: Array<string>): string {\n if (entries.length === 0) return '{}'\n const body = entries.map((entry) => `${indentLines(entry)},`).join('\\n')\n\n return `{\\n${body}\\n}`\n}\n\n/**\n * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on\n * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented\n * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,\n * `z.array([…])`, and similar member lists so objects inside them nest correctly.\n *\n * @example\n * ```ts\n * buildList(['z.string()', 'z.number()'])\n * // '[z.string(), z.number()]'\n * ```\n */\nexport function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {\n const [open, close] = brackets\n if (items.length === 0) return `${open}${close}`\n if (!items.some((item) => item.includes('\\n'))) return `${open}${items.join(', ')}${close}`\n const body = items.map((item) => `${indentLines(item)},`).join('\\n')\n\n return `${open}\\n${body}\\n${close}`\n}\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 * 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 * Returns the discriminator key whose mapping value matches `ref`, or `null` when there is no match.\n *\n * @example\n * ```ts\n * findDiscriminator({ dog: '#/components/schemas/Dog' }, '#/components/schemas/Dog') // 'dog'\n * ```\n */\nexport function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {\n if (!mapping || !ref) return null\n return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null\n}\n"],"mappings":";;AAWA,MAAa,gBAAgB;CAC3B,SAAS;CACT,MAAM;AACR;;;;;;;;AASA,MAAa,cAAc;;;;CAIzB,QAAQ;;;;CAIR,QAAQ;;;;CAIR,SAAS;;;;CAIT,QAAQ;;;;CAIR,SAAS;;;;CAIT,MAAM;;;;CAIN,KAAK;;;;CAIL,SAAS;;;;CAIT,MAAM;;;;CAIN,QAAQ;;;;CAIR,OAAO;;;;CAIP,OAAO;;;;CAIP,OAAO;;;;CAIP,cAAc;;;;CAId,MAAM;;;;CAIN,KAAK;;;;CAIL,MAAM;;;;CAIN,UAAU;;;;CAIV,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;;;;CAIP,KAAK;;;;CAIL,MAAM;;;;CAIN,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;AACT;;;;;;AASA,MAAM,yBAAyB,IAAI,IAAqB;CAAC;CAAU;CAAU;CAAW;CAAU;AAAS,CAAC;;;;;;AAO5G,SAAgB,kBAAkB,MAAuC;CACvE,OAAO,uBAAuB,IAAI,IAAuB;AAC3D;;;;;;AAOA,MAAa,cAAc;CACzB,KAAK;CACL,MAAM;CACN,KAAK;CACL,OAAO;CACP,QAAQ;CACR,MAAM;CACN,SAAS;CACT,OAAO;AACT;;;;AA0BA,MAAa,SAAS,MAAM,KAAK,EAAE,QAAQ,EAAY,SAAS,GAAG,CAAC,CAAC,KAAK,EAAE;;;;;;;;;;AC1K5E,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;;;;;;;;;;AAWA,SAAgB,UAAU,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC1F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,KAAK;AAC7D;;;;;;;;;;AAWA,SAAgB,WAAW,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC3F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,IAAI;AAC5D;;;;;;;ACvDA,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAU;;;;;;;;;;;AA8BV,SAAgB,eAAe,MAAuB;CACpD,IAAI,CAAC,QAAQ,cAAc,IAAI,IAAiB,GAC9C,OAAO;CAET,OAAO,aAAa,IAAI;AAC1B;;;;;;;;;;;;;;AAeA,SAAgB,aAAa,MAAuB;CAClD,OAAO,6BAA6B,KAAK,IAAI;AAC/C;;;;;;;;;;;;;AChIA,SAAgB,YAAY,OAA6D;CACvF,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM,OAAO;CAGlD,OAAO,IAFS,OAAO,KAAK,CAAC,CAAC,QAAQ,OAAO,MAAM,CAAC,CAAC,QAAQ,MAAM,KAElD,EAAE;AACrB;;;;;;;;;;;;;ACAA,SAAgB,WAAW,MAAsB;CAC/C,IAAI,KAAK,UAAU,GAAG;EACpB,MAAM,QAAQ,KAAK;EACnB,MAAM,OAAO,KAAK,KAAK,SAAS;EAChC,IAAK,UAAU,QAAO,SAAS,QAAS,UAAU,OAAO,SAAS,OAAS,UAAU,OAAO,SAAS,KACnG,OAAO,KAAK,MAAM,GAAG,EAAE;CAE3B;CACA,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,UAAU,OAAsD;CAC9E,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM,OAAO;CAGlD,OAAO,IAFM,KAAK,UAAU,WAAW,MAAM,SAAS,CAAC,CACtC,CAAC,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,QAAQ,QAAQ,IAAG,CAAC,CAAC,QAAQ,MAAM,KACpD,EAAE;AACnB;;;;;;;;;;;;AAaA,SAAgB,eAAe,OAAwB;CACrD,OAAO,GAAG,QAAQ,QAAQ,4BAA4B,cAAc;EAClE,QAAQ,WAAR;GACE,KAAK;GACL,KAAK;GACL,KAAK,MACH,OAAO,KAAK;GACd,KAAK,MACH,OAAO;GACT,KAAK,MACH,OAAO;GACT,KAAK,UACH,OAAO;GACT,KAAK,UACH,OAAO;GACT,SACE,OAAO;EACX;CACF,CAAC;AACH;;;;;;;;;;;;AAaA,SAAgB,eAAe,MAAc,OAAsB,UAAkB;CACnF,MAAM,MAAM,WAAW,IAAI;CAE3B,MAAM,QAAQ,IAAI,MAAM,yBAAyB;CACjD,MAAM,oBAAoB,QAAQ,MAAM;CACxC,MAAM,eAAe,QAAQ;CAC7B,MAAM,UAAU,IACb,QAAQ,UAAU,EAAE,CAAC,CACrB,QAAQ,UAAU,EAAE,CAAC,CACrB,QAAQ,mBAAmB,EAAE;CAEhC,MAAM,EAAE,QAAQ,UAAU,IAAI,OAAO,SAAS,YAAY;CAE1D,IAAI,SAAS,MAAM,OAAO,IAAI,OAAO,GAAG;CAExC,OAAO,OAAO,KAAK,GAAG,KAAK,UAAU,MAAM,IAAI,QAAQ,KAAK,KAAK,UAAU,KAAK,MAAM,GAAG;AAC3F;;;;;;;;;;;;AAaA,SAAgB,gBAAgB,OAAwC;CAStE,OARc,OAAO,QAAQ,KAAK,CAAC,CAChC,KAAK,CAAC,KAAK,SAAS;EACnB,IAAI,QAAQ,QAAQ,OAAO,QAAQ,UACjC,OAAO,GAAG,IAAI,eAAe,gBAAgB,GAA8B,EAAE;EAE/E,OAAO,GAAG,IAAI,IAAI;CACpB,CAAC,CAAC,CACD,OAAO,OACC,CAAC,CAAC,KAAK,KAAK;AACzB;;;;;;;;;;;AAYA,SAAgB,kBAAkB,OAA+B,UAAiC;CAChG,MAAM,QAAQ,MAAM,QAAQ,KAAK,IAAI,QAAQ,MAAM,MAAM,GAAG;CAC5D,IAAI,MAAM,WAAW,KAAM,MAAM,WAAW,KAAK,MAAM,OAAO,IAAK,OAAO;CAC1E,OAAO,GAAG,SAAS,MAAM,GAAG,MAAM,KAAK,QAAQ,EAAE;AACnD;;;;;;;;;;;AAYA,SAAgB,WACd,UACA,UAgBI,CAAC,GACG;CACR,MAAM,EAAE,SAAS,SAAS,SAAS,QAAQ,WAAW,SAAS;CAE/D,IAAI,SAAS,WAAW,GAAG,OAAO;CAElC,OAAO,QAAQ,SAAS,KAAK,MAAM,GAAG,SAAS,GAAG,CAAC,CAAC,KAAK,IAAI,EAAE,SAAS;AAC1E;;;;AAKA,SAAS,YAAY,MAAsB;CACzC,IAAI,CAAC,MAAM,OAAO;CAClB,OAAO,KACJ,MAAM,IAAI,CAAC,CACX,KAAK,SAAU,KAAK,KAAK,IAAI,GAAG,SAAS,SAAS,EAAG,CAAC,CACtD,KAAK,IAAI;AACd;;;;;;;;;;;AAYA,SAAgB,UAAU,MAAsB;CAC9C,OAAO,aAAa,IAAI,IAAI,OAAO,YAAY,IAAI;AACrD;;;;;;;;;;;;;AAcA,SAAgB,YAAY,SAAgC;CAC1D,IAAI,QAAQ,WAAW,GAAG,OAAO;CAGjC,OAAO,MAFM,QAAQ,KAAK,UAAU,GAAG,YAAY,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,IAEnD,EAAE;AACpB;;;;;;;;;;;;;AAcA,SAAgB,UAAU,OAAsB,WAA0C,CAAC,KAAK,GAAG,GAAW;CAC5G,MAAM,CAAC,MAAM,SAAS;CACtB,IAAI,MAAM,WAAW,GAAG,OAAO,GAAG,OAAO;CACzC,IAAI,CAAC,MAAM,MAAM,SAAS,KAAK,SAAS,IAAI,CAAC,GAAG,OAAO,GAAG,OAAO,MAAM,KAAK,IAAI,IAAI;CAGpF,OAAO,GAAG,KAAK,IAFF,MAAM,KAAK,SAAS,GAAG,YAAY,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,IAEzC,EAAE,IAAI;AAC9B;;;;;;;;;AAUA,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;AAClC;;;;;;;;;;;AAYA,SAAgB,UAAU,YAAuC,UAAiC;CAChG,OAAO,aAAa,WAAW,CAAC,YAAY,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI;AACrE;;;;;;;;;;AAWA,SAAgB,aAAa,YAAuC,UAAkB,YAA4B;CAChH,OAAO,WAAW;EAAC;EAAY;EAAU;CAAU,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;AAChF;;;;;;;;;AAUA,SAAgB,kBAAkB,SAA6C,KAAwC;CACrH,IAAI,CAAC,WAAW,CAAC,KAAK,OAAO;CAC7B,OAAO,OAAO,QAAQ,OAAO,CAAC,CAAC,MAAM,GAAG,WAAW,UAAU,GAAG,CAAC,GAAG,MAAM;AAC5E"} |
| //#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 __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 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 ../../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/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/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; | ||
| } | ||
| //#endregion | ||
| Object.defineProperty(exports, "__name", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __name; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "__toESM", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __toESM; | ||
| } | ||
| }); | ||
| 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, "camelCase", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return camelCase; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "childName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return childName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "enumPropName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return enumPropName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "extractRefName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return extractRefName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "findDiscriminator", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return findDiscriminator; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "getNestedAccessor", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return getNestedAccessor; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "httpMethods", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return httpMethods; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isScalarPrimitive", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isScalarPrimitive; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isValidVarName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isValidVarName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "jsStringEscape", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return jsStringEscape; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "objectKey", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return objectKey; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "schemaTypes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return schemaTypes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringify", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return stringify; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringifyObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return stringifyObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "toRegExpString", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return toRegExpString; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "trimQuotes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return trimQuotes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "visitorDepths", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return visitorDepths; | ||
| } | ||
| }); | ||
| //# sourceMappingURL=utils-cdQ6Pzyi.cjs.map |
| {"version":3,"file":"utils-cdQ6Pzyi.cjs","names":[],"sources":["../src/constants.ts","../../../internals/utils/src/casing.ts","../../../internals/utils/src/reserved.ts","../../../internals/utils/src/string.ts","../src/utils/index.ts"],"sourcesContent":["import type { HttpMethod } from './nodes/operation.ts'\nimport type { SchemaType } from './nodes/schema.ts'\n\n/**\n * Traversal depth for AST visitor utilities.\n *\n * - `'shallow'` visits only the immediate node, skipping children.\n * - `'deep'` recursively visits all descendant nodes.\n */\nexport type VisitorDepth = 'shallow' | 'deep'\n\nexport const visitorDepths = {\n shallow: 'shallow',\n deep: 'deep',\n} as const satisfies Record<VisitorDepth, VisitorDepth>\n\n/**\n * Schema type discriminators used by all AST schema nodes.\n *\n * These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).\n * Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),\n * and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.\n */\nexport const schemaTypes = {\n /**\n * Text value.\n */\n string: 'string',\n /**\n * Floating-point number (`float`, `double`).\n */\n number: 'number',\n /**\n * Whole number (`int32`). Use `bigint` for `int64`.\n */\n integer: 'integer',\n /**\n * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.\n */\n bigint: 'bigint',\n /**\n * Boolean value\n */\n boolean: 'boolean',\n /**\n * Explicit null value.\n */\n null: 'null',\n /**\n * Any value (no type restriction).\n */\n any: 'any',\n /**\n * Unknown value (must be narrowed before usage).\n */\n unknown: 'unknown',\n /**\n * No return value (`void`).\n */\n void: 'void',\n /**\n * Object with named properties.\n */\n object: 'object',\n /**\n * Sequential list of items.\n */\n array: 'array',\n /**\n * Fixed-length list with position-specific items.\n */\n tuple: 'tuple',\n /**\n * \"One of\" multiple schema members.\n */\n union: 'union',\n /**\n * \"All of\" multiple schema members.\n */\n intersection: 'intersection',\n /**\n * Enum schema.\n */\n enum: 'enum',\n /**\n * Reference to another schema.\n */\n ref: 'ref',\n /**\n * Calendar date (for example `2026-03-24`).\n */\n date: 'date',\n /**\n * Date-time value (for example `2026-03-24T09:00:00Z`).\n */\n datetime: 'datetime',\n /**\n * Time-only value (for example `09:00:00`).\n */\n time: 'time',\n /**\n * UUID value.\n */\n uuid: 'uuid',\n /**\n * Email address value.\n */\n email: 'email',\n /**\n * URL value.\n */\n url: 'url',\n /**\n * IPv4 address value.\n */\n ipv4: 'ipv4',\n /**\n * IPv6 address value.\n */\n ipv6: 'ipv6',\n /**\n * Binary/blob value.\n */\n blob: 'blob',\n /**\n * Impossible value (`never`).\n */\n never: 'never',\n} as const satisfies Record<SchemaType, SchemaType>\n\nexport type ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n *\n * Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\n/**\n * Type guard that returns `true` when `type` is a scalar primitive schema type.\n *\n * Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).\n */\nexport function isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * HTTP method identifiers used by operation nodes.\n *\n * Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).\n */\nexport const httpMethods = {\n get: 'GET',\n post: 'POST',\n put: 'PUT',\n patch: 'PATCH',\n delete: 'DELETE',\n head: 'HEAD',\n options: 'OPTIONS',\n trace: 'TRACE',\n} as const satisfies Record<Lowercase<HttpMethod>, HttpMethod>\n\n/**\n * Default concurrency limit for `walk()` traversal utility.\n *\n * Set to 30 to balance I/O-bound resolver parallelism against event loop pressure and memory usage during large spec traversals.\n * Use `WALK_CONCURRENCY` when calling `walk()` or override for different hardware constraints.\n *\n * @example\n * ```ts\n * import { walk, WALK_CONCURRENCY } from '@kubb/ast'\n *\n * walk(root, { concurrency: WALK_CONCURRENCY, root: () => {} })\n * ```\n */\nexport const WALK_CONCURRENCY = 30\n\n/**\n * Number of spaces in one indentation level when assembling multi-line code as strings.\n * Set to 2, 3, … to change the indent width used by `buildObject`/`buildList`.\n */\nconst INDENT_SIZE = 2\n\n/**\n * One indentation level, derived from {@link INDENT_SIZE}.\n */\nexport const INDENT = Array.from({ length: INDENT_SIZE }, () => ' ').join('')\n","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","/**\n * JavaScript and Java reserved words.\n * @link https://github.com/jonschlinkert/reserved/blob/master/index.js\n */\nconst reservedWords = new Set([\n 'abstract',\n 'arguments',\n 'boolean',\n 'break',\n 'byte',\n 'case',\n 'catch',\n 'char',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'double',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'final',\n 'finally',\n 'float',\n 'for',\n 'function',\n 'goto',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'int',\n 'interface',\n 'let',\n 'long',\n 'native',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'short',\n 'static',\n 'super',\n 'switch',\n 'synchronized',\n 'this',\n 'throw',\n 'throws',\n 'transient',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'volatile',\n 'while',\n 'with',\n 'yield',\n 'Array',\n 'Date',\n 'hasOwnProperty',\n 'Infinity',\n 'isFinite',\n 'isNaN',\n 'isPrototypeOf',\n 'length',\n 'Math',\n 'name',\n 'NaN',\n 'Number',\n 'Object',\n 'prototype',\n 'String',\n 'toString',\n 'undefined',\n 'valueOf',\n] as const)\n\n/**\n * Prefixes `word` with `_` when it is a reserved JavaScript/Java identifier or starts with a digit.\n *\n * @example\n * ```ts\n * transformReservedWord('class') // '_class'\n * transformReservedWord('42foo') // '_42foo'\n * transformReservedWord('status') // 'status'\n * ```\n */\nexport function transformReservedWord(word: string): string {\n const firstChar = word.charCodeAt(0)\n if (word && (reservedWords.has(word as 'valueOf') || (firstChar >= 48 && firstChar <= 57))) {\n return `_${word}`\n }\n return word\n}\n\n/**\n * Returns `true` when `name` is a syntactically valid JavaScript variable name.\n *\n * @example\n * ```ts\n * isValidVarName('status') // true\n * isValidVarName('class') // false (reserved word)\n * isValidVarName('42foo') // false (starts with digit)\n * ```\n */\nexport function isValidVarName(name: string): boolean {\n if (!name || reservedWords.has(name as 'valueOf')) {\n return false\n }\n return isIdentifier(name)\n}\n\n/**\n * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.\n *\n * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys\n * even though they are not valid variable names, so use this (not {@link isValidVarName}) when\n * deciding whether an object key needs quoting.\n *\n * @example\n * ```ts\n * isIdentifier('name') // true\n * isIdentifier('x-total')// false\n * ```\n */\nexport function isIdentifier(name: string): boolean {\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name)\n}\n","/**\n * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping\n * any backslash or single quote in the content.\n *\n * @example\n * ```ts\n * singleQuote('foo') // \"'foo'\"\n * singleQuote(\"o'clock\") // \"'o\\\\'clock'\"\n * ```\n */\nexport function singleQuote(value: string | number | boolean | undefined | null): string {\n if (value === undefined || value === null) return \"''\"\n const escaped = String(value).replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")\n\n return `'${escaped}'`\n}\n","import { isIdentifier, pascalCase, singleQuote } from '@internals/utils'\nimport { INDENT } from '../constants.ts'\n\nexport { isValidVarName } from '@internals/utils'\n\n/**\n * Strips a single matching pair of `\"...\"`, `'...'`, or `` `...` `` from both ends of `text`.\n * Returns the string unchanged when no balanced quote pair is found.\n *\n * @example\n * ```ts\n * trimQuotes('\"hello\"') // 'hello'\n * trimQuotes('hello') // 'hello'\n * ```\n */\nexport function trimQuotes(text: string): string {\n if (text.length >= 2) {\n const first = text[0]\n const last = text[text.length - 1]\n if ((first === '\"' && last === '\"') || (first === \"'\" && last === \"'\") || (first === '`' && last === '`')) {\n return text.slice(1, -1)\n }\n }\n return text\n}\n\n/**\n * Serializes a primitive value to a single-quoted string literal, stripping any surrounding quote\n * characters first. Escaping comes from `JSON.stringify`, then the quote style switches to single\n * quotes so generated code matches the repo style without a formatter.\n *\n * @example\n * ```ts\n * stringify('hello') // \"'hello'\"\n * stringify('\"hello\"') // \"'hello'\"\n * ```\n */\nexport function stringify(value: string | number | boolean | undefined): string {\n if (value === undefined || value === null) return \"''\"\n const json = JSON.stringify(trimQuotes(value.toString()))\n const inner = json.slice(1, -1).replace(/\\\\\"/g, '\"').replace(/'/g, \"\\\\'\")\n return `'${inner}'`\n}\n\n/**\n * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,\n * and the Unicode line terminators U+2028 and U+2029.\n *\n * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4\n *\n * @example\n * ```ts\n * jsStringEscape('say \"hi\"\\nbye') // 'say \\\\\"hi\\\\\"\\\\nbye'\n * ```\n */\nexport function jsStringEscape(input: unknown): string {\n return `${input}`.replace(/[\"'\\\\\\n\\r\\u2028\\u2029]/g, (character) => {\n switch (character) {\n case '\"':\n case \"'\":\n case '\\\\':\n return `\\\\${character}`\n case '\\n':\n return '\\\\n'\n case '\\r':\n return '\\\\r'\n case '\\u2028':\n return '\\\\u2028'\n case '\\u2029':\n return '\\\\u2029'\n default:\n return ''\n }\n })\n}\n\n/**\n * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.\n * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.\n * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.\n *\n * @example\n * ```ts\n * toRegExpString('^(?im)foo') // 'new RegExp(\"^foo\", \"im\")'\n * toRegExpString('^(?im)foo', null) // '/^foo/im'\n * ```\n */\nexport function toRegExpString(text: string, func: string | null = 'RegExp'): string {\n const raw = trimQuotes(text)\n\n const match = raw.match(/^\\^(\\(\\?([igmsuy]+)\\))/i)\n const replacementTarget = match?.[1] ?? ''\n const matchedFlags = match?.[2]\n const cleaned = raw\n .replace(/^\\\\?\\//, '')\n .replace(/\\\\?\\/$/, '')\n .replace(replacementTarget, '')\n\n const { source, flags } = new RegExp(cleaned, matchedFlags)\n\n if (func === null) return `/${source}/${flags}`\n\n return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ''})`\n}\n\n/**\n * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested\n * objects recurse with fixed indentation, so the result drops straight into an object literal\n * without re-parsing.\n *\n * @example\n * ```ts\n * stringifyObject({ foo: 'bar', nested: { a: 1 } })\n * // 'foo: bar,\\nnested: {\\n a: 1\\n }'\n * ```\n */\nexport function stringifyObject(value: Record<string, unknown>): string {\n const items = Object.entries(value)\n .map(([key, val]) => {\n if (val !== null && typeof val === 'object') {\n return `${key}: {\\n ${stringifyObject(val as Record<string, unknown>)}\\n }`\n }\n return `${key}: ${val}`\n })\n .filter(Boolean)\n return items.join(',\\n')\n}\n\n/**\n * Renders a dotted path or string array as an optional-chaining accessor expression rooted at\n * `accessor`. Returns `null` for an empty path.\n *\n * @example\n * ```ts\n * getNestedAccessor('pagination.next.id', 'lastPage')\n * // \"lastPage?.['pagination']?.['next']?.['id']\"\n * ```\n */\nexport function getNestedAccessor(param: string | Array<string>, accessor: string): string | null {\n const parts = Array.isArray(param) ? param : param.split('.')\n if (parts.length === 0 || (parts.length === 1 && parts[0] === '')) return null\n return `${accessor}?.['${`${parts.join(\"']?.['\")}']`}`\n}\n\n/**\n * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no\n * comments so callers always get a usable string.\n *\n * @example\n * ```ts\n * buildJSDoc(['@type string', '@example hello'])\n * // '/**\\n * @type string\\n * @example hello\\n *\\/\\n '\n * ```\n */\nexport function buildJSDoc(\n comments: Array<string>,\n options: {\n /**\n * String used to indent each comment line.\n * @default ' * '\n */\n indent?: string\n /**\n * String appended after the closing tag.\n * @default '\\n '\n */\n suffix?: string\n /**\n * Returned as-is when `comments` is empty.\n * @default ' '\n */\n fallback?: string\n } = {},\n): string {\n const { indent = ' * ', suffix = '\\n ', fallback = ' ' } = options\n\n if (comments.length === 0) return fallback\n\n return `/**\\n${comments.map((c) => `${indent}${c}`).join('\\n')}\\n */${suffix}`\n}\n\n/**\n * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.\n */\nfunction indentLines(text: string): string {\n if (!text) return ''\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${INDENT}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders an object key, quoting it with single quotes only when it is not a valid identifier.\n * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.\n *\n * @example\n * ```ts\n * objectKey('name') // 'name'\n * objectKey('x-total') // \"'x-total'\"\n * ```\n */\nexport function objectKey(name: string): string {\n return isIdentifier(name) ? name : singleQuote(name)\n}\n\n/**\n * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one\n * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,\n * so callers never re-parse the generated code. A trailing comma is added per entry to match the\n * formatter's multi-line style.\n *\n * @example\n * ```ts\n * buildObject(['id: z.number()', 'name: z.string()'])\n * // '{\\n id: z.number(),\\n name: z.string(),\\n}'\n * ```\n */\nexport function buildObject(entries: Array<string>): string {\n if (entries.length === 0) return '{}'\n const body = entries.map((entry) => `${indentLines(entry)},`).join('\\n')\n\n return `{\\n${body}\\n}`\n}\n\n/**\n * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on\n * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented\n * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,\n * `z.array([…])`, and similar member lists so objects inside them nest correctly.\n *\n * @example\n * ```ts\n * buildList(['z.string()', 'z.number()'])\n * // '[z.string(), z.number()]'\n * ```\n */\nexport function buildList(items: Array<string>, brackets: [open: string, close: string] = ['[', ']']): string {\n const [open, close] = brackets\n if (items.length === 0) return `${open}${close}`\n if (!items.some((item) => item.includes('\\n'))) return `${open}${items.join(', ')}${close}`\n const body = items.map((item) => `${indentLines(item)},`).join('\\n')\n\n return `${open}\\n${body}\\n${close}`\n}\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 * 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 * Returns the discriminator key whose mapping value matches `ref`, or `null` when there is no match.\n *\n * @example\n * ```ts\n * findDiscriminator({ dog: '#/components/schemas/Dog' }, '#/components/schemas/Dog') // 'dog'\n * ```\n */\nexport function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {\n if (!mapping || !ref) return null\n return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAa,gBAAgB;CAC3B,SAAS;CACT,MAAM;AACR;;;;;;;;AASA,MAAa,cAAc;;;;CAIzB,QAAQ;;;;CAIR,QAAQ;;;;CAIR,SAAS;;;;CAIT,QAAQ;;;;CAIR,SAAS;;;;CAIT,MAAM;;;;CAIN,KAAK;;;;CAIL,SAAS;;;;CAIT,MAAM;;;;CAIN,QAAQ;;;;CAIR,OAAO;;;;CAIP,OAAO;;;;CAIP,OAAO;;;;CAIP,cAAc;;;;CAId,MAAM;;;;CAIN,KAAK;;;;CAIL,MAAM;;;;CAIN,UAAU;;;;CAIV,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;;;;CAIP,KAAK;;;;CAIL,MAAM;;;;CAIN,MAAM;;;;CAIN,MAAM;;;;CAIN,OAAO;AACT;;;;;;AASA,MAAM,yBAAyB,IAAI,IAAqB;CAAC;CAAU;CAAU;CAAW;CAAU;AAAS,CAAC;;;;;;AAO5G,SAAgB,kBAAkB,MAAuC;CACvE,OAAO,uBAAuB,IAAI,IAAuB;AAC3D;;;;;;AAOA,MAAa,cAAc;CACzB,KAAK;CACL,MAAM;CACN,KAAK;CACL,OAAO;CACP,QAAQ;CACR,MAAM;CACN,SAAS;CACT,OAAO;AACT;;;;AA0BA,MAAa,SAAS,MAAM,KAAK,EAAE,QAAQ,EAAY,SAAS,GAAG,CAAC,CAAC,KAAK,EAAE;;;;;;;;;;AC1K5E,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;;;;;;;;;;AAWA,SAAgB,UAAU,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC1F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,KAAK;AAC7D;;;;;;;;;;AAWA,SAAgB,WAAW,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC3F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,IAAI;AAC5D;;;;;;;ACvDA,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAU;;;;;;;;;;;AA8BV,SAAgB,eAAe,MAAuB;CACpD,IAAI,CAAC,QAAQ,cAAc,IAAI,IAAiB,GAC9C,OAAO;CAET,OAAO,aAAa,IAAI;AAC1B;;;;;;;;;;;;;;AAeA,SAAgB,aAAa,MAAuB;CAClD,OAAO,6BAA6B,KAAK,IAAI;AAC/C;;;;;;;;;;;;;AChIA,SAAgB,YAAY,OAA6D;CACvF,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM,OAAO;CAGlD,OAAO,IAFS,OAAO,KAAK,CAAC,CAAC,QAAQ,OAAO,MAAM,CAAC,CAAC,QAAQ,MAAM,KAElD,EAAE;AACrB;;;;;;;;;;;;;ACAA,SAAgB,WAAW,MAAsB;CAC/C,IAAI,KAAK,UAAU,GAAG;EACpB,MAAM,QAAQ,KAAK;EACnB,MAAM,OAAO,KAAK,KAAK,SAAS;EAChC,IAAK,UAAU,QAAO,SAAS,QAAS,UAAU,OAAO,SAAS,OAAS,UAAU,OAAO,SAAS,KACnG,OAAO,KAAK,MAAM,GAAG,EAAE;CAE3B;CACA,OAAO;AACT;;;;;;;;;;;;AAaA,SAAgB,UAAU,OAAsD;CAC9E,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM,OAAO;CAGlD,OAAO,IAFM,KAAK,UAAU,WAAW,MAAM,SAAS,CAAC,CACtC,CAAC,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC,QAAQ,QAAQ,IAAG,CAAC,CAAC,QAAQ,MAAM,KACpD,EAAE;AACnB;;;;;;;;;;;;AAaA,SAAgB,eAAe,OAAwB;CACrD,OAAO,GAAG,QAAQ,QAAQ,4BAA4B,cAAc;EAClE,QAAQ,WAAR;GACE,KAAK;GACL,KAAK;GACL,KAAK,MACH,OAAO,KAAK;GACd,KAAK,MACH,OAAO;GACT,KAAK,MACH,OAAO;GACT,KAAK,UACH,OAAO;GACT,KAAK,UACH,OAAO;GACT,SACE,OAAO;EACX;CACF,CAAC;AACH;;;;;;;;;;;;AAaA,SAAgB,eAAe,MAAc,OAAsB,UAAkB;CACnF,MAAM,MAAM,WAAW,IAAI;CAE3B,MAAM,QAAQ,IAAI,MAAM,yBAAyB;CACjD,MAAM,oBAAoB,QAAQ,MAAM;CACxC,MAAM,eAAe,QAAQ;CAC7B,MAAM,UAAU,IACb,QAAQ,UAAU,EAAE,CAAC,CACrB,QAAQ,UAAU,EAAE,CAAC,CACrB,QAAQ,mBAAmB,EAAE;CAEhC,MAAM,EAAE,QAAQ,UAAU,IAAI,OAAO,SAAS,YAAY;CAE1D,IAAI,SAAS,MAAM,OAAO,IAAI,OAAO,GAAG;CAExC,OAAO,OAAO,KAAK,GAAG,KAAK,UAAU,MAAM,IAAI,QAAQ,KAAK,KAAK,UAAU,KAAK,MAAM,GAAG;AAC3F;;;;;;;;;;;;AAaA,SAAgB,gBAAgB,OAAwC;CAStE,OARc,OAAO,QAAQ,KAAK,CAAC,CAChC,KAAK,CAAC,KAAK,SAAS;EACnB,IAAI,QAAQ,QAAQ,OAAO,QAAQ,UACjC,OAAO,GAAG,IAAI,eAAe,gBAAgB,GAA8B,EAAE;EAE/E,OAAO,GAAG,IAAI,IAAI;CACpB,CAAC,CAAC,CACD,OAAO,OACC,CAAC,CAAC,KAAK,KAAK;AACzB;;;;;;;;;;;AAYA,SAAgB,kBAAkB,OAA+B,UAAiC;CAChG,MAAM,QAAQ,MAAM,QAAQ,KAAK,IAAI,QAAQ,MAAM,MAAM,GAAG;CAC5D,IAAI,MAAM,WAAW,KAAM,MAAM,WAAW,KAAK,MAAM,OAAO,IAAK,OAAO;CAC1E,OAAO,GAAG,SAAS,MAAM,GAAG,MAAM,KAAK,QAAQ,EAAE;AACnD;;;;;;;;;;;AAYA,SAAgB,WACd,UACA,UAgBI,CAAC,GACG;CACR,MAAM,EAAE,SAAS,SAAS,SAAS,QAAQ,WAAW,SAAS;CAE/D,IAAI,SAAS,WAAW,GAAG,OAAO;CAElC,OAAO,QAAQ,SAAS,KAAK,MAAM,GAAG,SAAS,GAAG,CAAC,CAAC,KAAK,IAAI,EAAE,SAAS;AAC1E;;;;AAKA,SAAS,YAAY,MAAsB;CACzC,IAAI,CAAC,MAAM,OAAO;CAClB,OAAO,KACJ,MAAM,IAAI,CAAC,CACX,KAAK,SAAU,KAAK,KAAK,IAAI,GAAG,SAAS,SAAS,EAAG,CAAC,CACtD,KAAK,IAAI;AACd;;;;;;;;;;;AAYA,SAAgB,UAAU,MAAsB;CAC9C,OAAO,aAAa,IAAI,IAAI,OAAO,YAAY,IAAI;AACrD;;;;;;;;;;;;;AAcA,SAAgB,YAAY,SAAgC;CAC1D,IAAI,QAAQ,WAAW,GAAG,OAAO;CAGjC,OAAO,MAFM,QAAQ,KAAK,UAAU,GAAG,YAAY,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,IAEnD,EAAE;AACpB;;;;;;;;;;;;;AAcA,SAAgB,UAAU,OAAsB,WAA0C,CAAC,KAAK,GAAG,GAAW;CAC5G,MAAM,CAAC,MAAM,SAAS;CACtB,IAAI,MAAM,WAAW,GAAG,OAAO,GAAG,OAAO;CACzC,IAAI,CAAC,MAAM,MAAM,SAAS,KAAK,SAAS,IAAI,CAAC,GAAG,OAAO,GAAG,OAAO,MAAM,KAAK,IAAI,IAAI;CAGpF,OAAO,GAAG,KAAK,IAFF,MAAM,KAAK,SAAS,GAAG,YAAY,IAAI,EAAE,EAAE,CAAC,CAAC,KAAK,IAEzC,EAAE,IAAI;AAC9B;;;;;;;;;AAUA,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;AAClC;;;;;;;;;;;AAYA,SAAgB,UAAU,YAAuC,UAAiC;CAChG,OAAO,aAAa,WAAW,CAAC,YAAY,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI;AACrE;;;;;;;;;;AAWA,SAAgB,aAAa,YAAuC,UAAkB,YAA4B;CAChH,OAAO,WAAW;EAAC;EAAY;EAAU;CAAU,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;AAChF;;;;;;;;;AAUA,SAAgB,kBAAkB,SAA6C,KAAwC;CACrH,IAAI,CAAC,WAAW,CAAC,KAAK,OAAO;CAC7B,OAAO,OAAO,QAAQ,OAAO,CAAC,CAAC,MAAM,GAAG,WAAW,UAAU,GAAG,CAAC,GAAG,MAAM;AAC5E"} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
975497
3.27%62
19.23%16019
4.3%166
2.47%1
Infinity%