@kubb/ast
Advanced tools
| const require_response = require("./response-DS5S3IG4.cjs"); | ||
| let node_crypto = require("node:crypto"); | ||
| let node_path = require("node:path"); | ||
| node_path = require_response.__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/utils/fileMerge.ts | ||
| function sourceKey(source) { | ||
| return `${source.name ?? require_response.extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`; | ||
| } | ||
| function pathTypeKey(path, isTypeOnly) { | ||
| return `${path}:${isTypeOnly ?? false}`; | ||
| } | ||
| function exportKey(path, name, isTypeOnly, asAlias) { | ||
| return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`; | ||
| } | ||
| function importKey(path, name, isTypeOnly) { | ||
| return `${path}:${name ?? ""}:${isTypeOnly ?? false}`; | ||
| } | ||
| /** | ||
| * Computes a multi-level sort key for exports and imports: | ||
| * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named. | ||
| */ | ||
| function sortKey(node) { | ||
| const isArray = Array.isArray(node.name) ? "1" : "0"; | ||
| const typeOnly = node.isTypeOnly ? "0" : "1"; | ||
| const hasName = node.name != null ? "1" : "0"; | ||
| const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? ""; | ||
| return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`; | ||
| } | ||
| /** | ||
| * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`. | ||
| * | ||
| * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order. | ||
| */ | ||
| function combineSources(sources) { | ||
| const seen = /* @__PURE__ */ new Map(); | ||
| for (const source of sources) { | ||
| const key = sourceKey(source); | ||
| if (!seen.has(key)) seen.set(key, source); | ||
| } | ||
| return [...seen.values()]; | ||
| } | ||
| /** | ||
| * Merges `incoming` names into `existing`, preserving order and dropping duplicates. | ||
| * | ||
| * Shared by `combineExports` and `combineImports` for the same-path name-merge case. | ||
| */ | ||
| function mergeNameArrays(existing, incoming) { | ||
| const merged = new Set(existing); | ||
| for (const name of incoming) merged.add(name); | ||
| return [...merged]; | ||
| } | ||
| /** | ||
| * Deduplicates and merges `ExportNode` objects by path and type. | ||
| * | ||
| * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export. | ||
| * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array. | ||
| */ | ||
| function combineExports(exports) { | ||
| const result = []; | ||
| const namedByPath = /* @__PURE__ */ new Map(); | ||
| const seen = /* @__PURE__ */ new Set(); | ||
| const keyed = exports.map((node) => ({ | ||
| node, | ||
| key: sortKey(node) | ||
| })); | ||
| keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0); | ||
| for (const { node: curr } of keyed) { | ||
| const { name, path, isTypeOnly, asAlias } = curr; | ||
| if (Array.isArray(name)) { | ||
| if (!name.length) continue; | ||
| const key = pathTypeKey(path, isTypeOnly); | ||
| const existing = namedByPath.get(key); | ||
| if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name); | ||
| else { | ||
| const newItem = { | ||
| ...curr, | ||
| name: [...new Set(name)] | ||
| }; | ||
| result.push(newItem); | ||
| namedByPath.set(key, newItem); | ||
| } | ||
| } else { | ||
| const key = exportKey(path, name, isTypeOnly, asAlias); | ||
| if (!seen.has(key)) { | ||
| result.push(curr); | ||
| seen.add(key); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| /** | ||
| * Deduplicates and merges `ImportNode` objects, filtering out unused imports. | ||
| * | ||
| * Retains imports that are referenced in `source` or re-exported. Imports with the same path and | ||
| * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array. | ||
| */ | ||
| function combineImports(imports, exports, source) { | ||
| const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])); | ||
| const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName); | ||
| const importNameMemo = /* @__PURE__ */ new Map(); | ||
| const canonicalizeName = (n) => { | ||
| if (typeof n === "string") return n; | ||
| const key = `${n.propertyName}:${n.name ?? ""}`; | ||
| if (!importNameMemo.has(key)) importNameMemo.set(key, n); | ||
| return importNameMemo.get(key); | ||
| }; | ||
| const pathsWithUsedNamedImport = /* @__PURE__ */ new Set(); | ||
| for (const node of imports) { | ||
| if (!Array.isArray(node.name)) continue; | ||
| if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path); | ||
| } | ||
| const result = []; | ||
| const namedByPath = /* @__PURE__ */ new Map(); | ||
| const seen = /* @__PURE__ */ new Set(); | ||
| const keyed = imports.map((node) => ({ | ||
| node, | ||
| key: sortKey(node) | ||
| })); | ||
| keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0); | ||
| for (const { node: curr } of keyed) { | ||
| if (curr.path === curr.root) continue; | ||
| const { path, isTypeOnly } = curr; | ||
| let { name } = curr; | ||
| if (Array.isArray(name)) { | ||
| name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName)); | ||
| if (!name.length) continue; | ||
| const key = pathTypeKey(path, isTypeOnly); | ||
| const existing = namedByPath.get(key); | ||
| if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name); | ||
| else { | ||
| const newItem = { | ||
| ...curr, | ||
| name | ||
| }; | ||
| result.push(newItem); | ||
| namedByPath.set(key, newItem); | ||
| } | ||
| } else { | ||
| if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue; | ||
| const key = importKey(path, name, isTypeOnly); | ||
| if (!seen.has(key)) { | ||
| result.push(curr); | ||
| seen.add(key); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| //#endregion | ||
| //#region src/factory.ts | ||
| var factory_exports = /* @__PURE__ */ require_response.__exportAll({ | ||
| createArrowFunction: () => require_response.createArrowFunction, | ||
| createBreak: () => require_response.createBreak, | ||
| createConst: () => require_response.createConst, | ||
| createContent: () => require_response.createContent, | ||
| createExport: () => require_response.createExport, | ||
| createFile: () => createFile, | ||
| createFunction: () => require_response.createFunction, | ||
| createFunctionParameter: () => require_response.createFunctionParameter, | ||
| createFunctionParameters: () => require_response.createFunctionParameters, | ||
| createImport: () => require_response.createImport, | ||
| createIndexedAccessType: () => require_response.createIndexedAccessType, | ||
| createInput: () => require_response.createInput, | ||
| createJsx: () => require_response.createJsx, | ||
| createObjectBindingPattern: () => require_response.createObjectBindingPattern, | ||
| createOperation: () => require_response.createOperation, | ||
| createOutput: () => require_response.createOutput, | ||
| createParameter: () => require_response.createParameter, | ||
| createProperty: () => require_response.createProperty, | ||
| createRequestBody: () => require_response.createRequestBody, | ||
| createResponse: () => require_response.createResponse, | ||
| createSchema: () => require_response.createSchema, | ||
| createSource: () => require_response.createSource, | ||
| createText: () => require_response.createText, | ||
| createType: () => require_response.createType, | ||
| createTypeLiteral: () => require_response.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_response.extractStringsFromNodes([node])).filter(Boolean).join("\n\n"); | ||
| const resolvedExports = input.exports?.length ? combineExports(input.exports) : []; | ||
| const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : []; | ||
| const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name))); | ||
| const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName; | ||
| const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => { | ||
| if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp]; | ||
| const kept = imp.name.filter((item) => !localNames.has(nameOf(item))); | ||
| if (!kept.length) return []; | ||
| return [kept.length === imp.name.length ? imp : { | ||
| ...imp, | ||
| name: kept | ||
| }]; | ||
| }); | ||
| const resolvedSources = input.sources?.length ? combineSources(input.sources) : []; | ||
| return { | ||
| kind: "File", | ||
| ...input, | ||
| id: (0, node_crypto.hash)("sha256", input.path, "hex"), | ||
| name: trimExtName(input.baseName), | ||
| extname, | ||
| imports: resolvedImports, | ||
| exports: resolvedExports, | ||
| sources: resolvedSources, | ||
| meta: input.meta ?? {} | ||
| }; | ||
| } | ||
| //#endregion | ||
| Object.defineProperty(exports, "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-Cl8Z7mcc.cjs.map |
| {"version":3,"file":"factory-Cl8Z7mcc.cjs","names":["extractStringsFromNodes","path","extractStringsFromNodes"],"sources":["../../../internals/utils/src/fs.ts","../src/utils/fileMerge.ts","../src/factory.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises'\nimport { dirname, join, posix, resolve } from 'node:path'\nimport { camelCase } from './casing.ts'\nimport { runtime } from './runtime.ts'\n\n/**\n * Walks up the directory tree from `cwd` (defaults to `process.cwd()`) and\n * returns the absolute path of the nearest `package.json`, or `null` when none\n * is found before reaching the filesystem root.\n *\n * @example\n * ```ts\n * const pkgPath = findPackageJSON('/home/user/project/src') // '/home/user/project/package.json'\n * ```\n */\nexport function findPackageJSON(cwd?: string): string | null {\n let dir = cwd ? resolve(cwd) : process.cwd()\n while (true) {\n const pkgPath = join(dir, 'package.json')\n if (existsSync(pkgPath)) return pkgPath\n const parent = dirname(dir)\n if (parent === dir) return null\n dir = parent\n }\n}\n\n/**\n * Converts all backslashes to forward slashes.\n * Extended-length Windows paths (`\\\\?\\...`) are left unchanged.\n */\nfunction toSlash(p: string): string {\n if (p.startsWith('\\\\\\\\?\\\\')) return p\n\n return p.replaceAll('\\\\', '/')\n}\n\n/**\n * Returns the relative path from `rootDir` to `filePath`, always using forward slashes\n * and prefixed with `./` when not already traversing upward.\n *\n * @example\n * ```ts\n * getRelativePath('/src/components', '/src/components/Button.tsx') // './Button.tsx'\n * getRelativePath('/src/components', '/src/utils/helpers.ts') // '../utils/helpers.ts'\n * ```\n */\nexport function getRelativePath(rootDir?: string | null, filePath?: string | null): string {\n if (!rootDir || !filePath) {\n throw new Error(`Root and file should be filled in when retrieving the relativePath, ${rootDir || ''} ${filePath || ''}`)\n }\n\n const relativePath = posix.relative(toSlash(rootDir), toSlash(filePath))\n\n return relativePath.startsWith('../') ? relativePath : `./${relativePath}`\n}\n\n/**\n * Resolves to `true` when the file or directory at `path` exists.\n * Uses `Bun.file().exists()` when running under Bun, `fs.access` otherwise.\n *\n * @example\n * ```ts\n * if (await exists('./kubb.config.ts')) {\n * const content = await read('./kubb.config.ts')\n * }\n * ```\n */\nexport async function exists(path: string): Promise<boolean> {\n if (runtime.isBun) {\n return Bun.file(path).exists()\n }\n return access(path).then(\n () => true,\n () => false,\n )\n}\n\n/**\n * Reads the file at `path` as a UTF-8 string.\n * Uses `Bun.file().text()` when running under Bun, `fs.readFile` otherwise.\n *\n * @example\n * ```ts\n * const source = await read('./src/Pet.ts')\n * ```\n */\nexport async function read(path: string): Promise<string> {\n if (runtime.isBun) {\n return Bun.file(path).text()\n }\n return readFile(path, { encoding: 'utf8' })\n}\n\ntype WriteOptions = {\n /**\n * When `true`, re-reads the file immediately after writing and throws if the\n * content does not match — useful for catching write failures on unreliable file systems.\n */\n sanity?: boolean\n}\n\n/**\n * Writes `data` to `path`, trimming leading/trailing whitespace before saving.\n * Skips the write when the trimmed content is empty or identical to what is already on disk.\n * Creates any missing parent directories automatically.\n * When `sanity` is `true`, re-reads the file after writing and throws if the content does not match.\n *\n * @example\n * ```ts\n * await write('./src/Pet.ts', source) // writes and returns trimmed content\n * await write('./src/Pet.ts', source) // null — file unchanged\n * await write('./src/Pet.ts', ' ') // null — empty content skipped\n * ```\n */\nexport async function write(path: string, data: string, options: WriteOptions = {}): Promise<string | null> {\n const trimmed = data.trim()\n if (trimmed === '') return null\n\n const resolved = resolve(path)\n\n if (runtime.isBun) {\n const file = Bun.file(resolved)\n const oldContent = (await file.exists()) ? await file.text() : null\n if (oldContent === trimmed) return null\n await Bun.write(resolved, trimmed)\n return trimmed\n }\n\n try {\n const oldContent = await readFile(resolved, { encoding: 'utf-8' })\n if (oldContent === trimmed) return null\n } catch {\n /* file doesn't exist yet */\n }\n\n await mkdir(dirname(resolved), { recursive: true })\n await writeFile(resolved, trimmed, { encoding: 'utf-8' })\n\n if (options.sanity) {\n const savedData = await readFile(resolved, { encoding: 'utf-8' })\n if (savedData !== trimmed) {\n throw new Error(`Sanity check failed for ${path}\\n\\nData[${data.length}]:\\n${data}\\n\\nSaved[${savedData.length}]:\\n${savedData}\\n`)\n }\n return savedData\n }\n\n return trimmed\n}\n\n/**\n * Recursively removes `path`. Silently succeeds when `path` does not exist.\n *\n * @example\n * ```ts\n * await clean('./dist')\n * ```\n */\nexport async function clean(path: string): Promise<void> {\n return rm(path, { recursive: true, force: true })\n}\n\n/**\n * Converts a filesystem path to use POSIX (`/`) separators.\n *\n * Most of the codebase compares and composes paths as strings (prefix matching, joining for\n * import specifiers, splitting on `/`). On POSIX `path.resolve` already returns `/`-separated\n * paths, but on Windows it returns `\\`-separated paths, which breaks every such comparison.\n *\n * Routing every path that crosses a module boundary through `toPosixPath` keeps the rest of the\n * code platform-agnostic. The conversion runs unconditionally so Windows-specific behavior is\n * exercisable from POSIX CI.\n *\n * @example\n * toPosixPath('C:\\\\repo\\\\src\\\\pet.ts') // 'C:/repo/src/pet.ts'\n */\nexport function toPosixPath(filePath: string): string {\n return filePath.replaceAll('\\\\', '/')\n}\n\n/**\n * Strips the file extension from a path or file name.\n * Only removes the last `.ext` segment when the dot is not part of a directory name.\n *\n * @example\n * trimExtName('petStore.ts') // 'petStore'\n * trimExtName('/src/models/pet.ts') // '/src/models/pet'\n * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'\n * trimExtName('noExtension') // 'noExtension'\n */\nexport function trimExtName(text: string): string {\n const dotIndex = text.lastIndexOf('.')\n if (dotIndex > 0 && !text.includes('/', dotIndex)) {\n return text.slice(0, dotIndex)\n }\n return text\n}\n\n/**\n * Builds a nested file path from a dotted name. Splits on dots that precede a letter\n * (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases\n * every earlier segment, applies `caseLast` to the final segment, and joins with `/`.\n *\n * Empty segments are dropped before joining. They arise when the name starts with a dot\n * followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to\n * an empty string). Without this a leading `/` would form, which `path.resolve` reads as an\n * absolute path, letting generated files escape the configured output directory.\n *\n * @example Nested path from a dotted name\n * `toFilePath('pet.petId') // 'pet/petId'`\n *\n * @example PascalCase the final segment\n * `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`\n *\n * @example Suffix applied to the final segment only\n * `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`\n */\nexport function toFilePath(name: string, caseLast: (part: string) => string = camelCase): string {\n const parts = name.split(/\\.(?=[a-zA-Z])/)\n return parts\n .map((part, i) => (i === parts.length - 1 ? caseLast(part) : camelCase(part)))\n .filter(Boolean)\n .join('/')\n}\n","/**\n * File-member merging. `combineImports`, `combineExports`, and `combineSources` deduplicate and sort\n * the import, export, and source entries of one file, and drop imports nothing references. This works\n * on a file's members, not on schema content.\n *\n * For collapsing duplicate schema shapes by structural signature, see `dedupe.ts`.\n */\nimport type { ExportNode, ImportNode, SourceNode } from '../nodes/index.ts'\nimport { extractStringsFromNodes } from './extractStringsFromNodes.ts'\n\nfunction sourceKey(source: SourceNode): string {\n const nameKey = source.name ?? extractStringsFromNodes(source.nodes)\n return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`\n}\n\nfunction pathTypeKey(path: string, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${isTypeOnly ?? false}`\n}\n\nfunction exportKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined, asAlias: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`\n}\n\nfunction importKey(path: string, name: string | null | undefined, isTypeOnly: boolean | null | undefined): string {\n return `${path}:${name ?? ''}:${isTypeOnly ?? false}`\n}\n\n/**\n * Computes a multi-level sort key for exports and imports:\n * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.\n */\nfunction sortKey(node: { name?: string | Array<unknown> | null; isTypeOnly?: boolean | null; path: string }): string {\n const isArray = Array.isArray(node.name) ? '1' : '0'\n const typeOnly = node.isTypeOnly ? '0' : '1'\n const hasName = node.name != null ? '1' : '0'\n const name = Array.isArray(node.name) ? node.name.toSorted().join('\\0') : (node.name ?? '')\n return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`\n}\n\n/**\n * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.\n *\n * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.\n */\nexport function combineSources(sources: Array<SourceNode>): Array<SourceNode> {\n const seen = new Map<string, SourceNode>()\n for (const source of sources) {\n const key = sourceKey(source)\n if (!seen.has(key)) seen.set(key, source)\n }\n return [...seen.values()]\n}\n\n/**\n * Merges `incoming` names into `existing`, preserving order and dropping duplicates.\n *\n * Shared by `combineExports` and `combineImports` for the same-path name-merge case.\n */\nfunction mergeNameArrays<TName>(existing: Array<TName>, incoming: Array<TName>): Array<TName> {\n const merged = new Set(existing)\n for (const name of incoming) merged.add(name)\n return [...merged]\n}\n\n/**\n * Deduplicates and merges `ExportNode` objects by path and type.\n *\n * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.\n * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.\n */\nexport function combineExports(exports: Array<ExportNode>): Array<ExportNode> {\n const result: Array<ExportNode> = []\n // Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ExportNode>()\n // Deduplicates non-array exports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = exports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n const { name, path, isTypeOnly, asAlias } = curr\n\n if (Array.isArray(name)) {\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ExportNode = { ...curr, name: [...new Set(name)] }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n const key = exportKey(path, name, isTypeOnly, asAlias)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n\n/**\n * Deduplicates and merges `ImportNode` objects, filtering out unused imports.\n *\n * Retains imports that are referenced in `source` or re-exported. Imports with the same path and\n * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.\n */\nexport function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {\n // Build a lookup of all exported names to retain imports that are re-exported\n const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))\n const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)\n\n // Memoize object import names so the same logical (propertyName, name) pair always\n // reuses the same object reference. Set-based deduplication then works correctly.\n const importNameMemo = new Map<string, { propertyName: string; name?: string }>()\n const canonicalizeName = (n: string | { propertyName: string; name?: string }): string | { propertyName: string; name?: string } => {\n if (typeof n === 'string') return n\n const key = `${n.propertyName}:${n.name ?? ''}`\n if (!importNameMemo.has(key)) importNameMemo.set(key, n)\n return importNameMemo.get(key)!\n }\n\n // Paths that keep at least one used named import. A default import from such a path is retained\n // even when its binding can't be found in `source` e.g. a generated `client` default import\n // alongside `import type { Client } from <same path>`, where merged grouped output omits the body.\n const pathsWithUsedNamedImport = new Set<string>()\n for (const node of imports) {\n if (!Array.isArray(node.name)) continue\n if (node.name.some((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))) {\n pathsWithUsedNamedImport.add(node.path)\n }\n }\n\n const result: Array<ImportNode> = []\n // Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging\n const namedByPath = new Map<string, ImportNode>()\n // Deduplicates non-array imports by their exact identity\n const seen = new Set<string>()\n\n // Precompute sort keys once, avoids recomputing per comparison.\n const keyed = imports.map((node) => ({ node, key: sortKey(node) }))\n keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))\n\n for (const { node: curr } of keyed) {\n if (curr.path === curr.root) continue\n\n const { path, isTypeOnly } = curr\n let { name } = curr\n\n if (Array.isArray(name)) {\n name = [...new Set(name.map(canonicalizeName))].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))\n if (!name.length) continue\n\n const key = pathTypeKey(path, isTypeOnly)\n const existing = namedByPath.get(key)\n\n if (existing && Array.isArray(existing.name)) {\n existing.name = mergeNameArrays(existing.name, name)\n } else {\n const newItem: ImportNode = { ...curr, name }\n result.push(newItem)\n namedByPath.set(key, newItem)\n }\n } else {\n if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue\n\n const key = importKey(path, name, isTypeOnly)\n if (!seen.has(key)) {\n result.push(curr)\n seen.add(key)\n }\n }\n }\n\n return result\n}\n","import { hash } from 'node:crypto'\nimport path from 'node:path'\nimport { trimExtName } from '@internals/utils'\nimport type { FileNode, Node } from './nodes/index.ts'\nimport { extractStringsFromNodes } from './utils/extractStringsFromNodes.ts'\nimport { combineExports, combineImports, combineSources } from './utils/fileMerge.ts'\n\n// Node constructors, grouped under the `factory` namespace the way the TypeScript compiler exposes\n// `ts.factory.createX`. Aggregating them here lets `export * as factory from './factory.ts'` in the\n// barrel surface every `createX` alongside the `createFile`/`update` helpers from a single module.\nexport { createArrowFunction, createBreak, createConst, createFunction, createJsx, createText, createType } from './nodes/code.ts'\nexport { createContent } from './nodes/content.ts'\nexport { createExport, createImport, createSource } from './nodes/file.ts'\nexport { createFunctionParameter, createFunctionParameters, createIndexedAccessType, createObjectBindingPattern, createTypeLiteral } from './nodes/function.ts'\nexport { createInput } from './nodes/input.ts'\nexport { createOperation } from './nodes/operation.ts'\nexport { createOutput } from './nodes/output.ts'\nexport { createParameter } from './nodes/parameter.ts'\nexport { createProperty } from './nodes/property.ts'\nexport { createRequestBody } from './nodes/requestBody.ts'\nexport { createResponse } from './nodes/response.ts'\nexport { createSchema } from './nodes/schema.ts'\n\n/**\n * Identity-preserving node update: returns `node` unchanged when every field in\n * `changes` already equals (by reference) the current value, otherwise a new node\n * with the changes applied.\n *\n * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the\n * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and\n * downstream passes can detect \"nothing changed\" by identity. Comparison is\n * shallow: a structurally-equal but newly-allocated array/object counts as a change.\n *\n * @example\n * ```ts\n * update(node, { name: node.name }) // -> same `node` reference\n * update(node, { name: 'renamed' }) // -> new node, `name` replaced\n * ```\n */\nexport function update<T extends Node>(node: T, changes: Partial<T>): T {\n for (const key in changes) {\n if (changes[key] !== node[key as keyof T]) {\n return { ...node, ...changes }\n }\n }\n\n return node\n}\n\n/**\n * Input descriptor for {@link createFile}, before `id`, `name`, and `extname` are computed\n * and `imports`/`exports`/`sources` are deduplicated.\n */\nexport type UserFileNode<TMeta extends object = object> = Omit<FileNode<TMeta>, 'kind' | 'id' | 'name' | 'extname' | 'imports' | 'exports' | 'sources'> &\n Pick<Partial<FileNode<TMeta>>, 'imports' | 'exports' | 'sources'>\n\n/**\n * Creates a fully resolved `FileNode` from a file input descriptor.\n *\n * Computes:\n * - `id` SHA256 hash of the file path\n * - `name` `baseName` without extension\n * - `extname` extension extracted from `baseName`\n *\n * Deduplicates:\n * - `sources` via `combineSources`\n * - `exports` via `combineExports`\n * - `imports` via `combineImports` (also filters unused imports)\n *\n * @throws {Error} when `baseName` has no extension.\n *\n * @example\n * ```ts\n * const file = createFile({\n * baseName: 'petStore.ts',\n * path: 'src/models/petStore.ts',\n * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],\n * imports: [createImport({ name: ['z'], path: 'zod' })],\n * exports: [createExport({ name: ['Pet'], path: './petStore' })],\n * })\n * // file.id = SHA256 hash of 'src/models/petStore.ts'\n * // file.name = 'petStore'\n * // file.extname = '.ts'\n * ```\n */\nexport function createFile<TMeta extends object = object>(input: UserFileNode<TMeta>): FileNode<TMeta> {\n const rawExtname = path.extname(input.baseName)\n // Handle dotfile basename like '.ts' where path.extname returns ''\n const extname = (rawExtname || (input.baseName.startsWith('.') ? input.baseName : '')) as `.${string}`\n if (!extname) {\n throw new Error(`No extname found for ${input.baseName}`)\n }\n\n const source = (input.sources ?? [])\n .flatMap((item) => item.nodes ?? [])\n .map((node) => extractStringsFromNodes([node]))\n .filter(Boolean)\n .join('\\n\\n')\n const resolvedExports = input.exports?.length ? combineExports(input.exports) : []\n const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || undefined) : []\n const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name): name is string => Boolean(name)))\n const nameOf = (item: string | { propertyName: string; name?: string }): string => (typeof item === 'string' ? item : (item.name ?? item.propertyName))\n // Drop self-imports. Consolidating output (`mode: 'file'`) can place a symbol's\n // definition and a cross-file import of it in the same file. The first pass catches imports that\n // resolve to this file's own path. The second drops imports of names the file already defines,\n // the case consolidation produces when the import path no longer matches `input.path`. Sources\n // stay intact, so the local definition remains. Bare specifiers like `'zod'` never match a path.\n const resolvedImports = combinedImports\n .filter((imp) => imp.path !== input.path)\n .flatMap((imp) => {\n if (!Array.isArray(imp.name)) {\n return typeof imp.name === 'string' && localNames.has(imp.name) ? [] : [imp]\n }\n const kept = imp.name.filter((item) => !localNames.has(nameOf(item)))\n if (!kept.length) return []\n return [kept.length === imp.name.length ? imp : { ...imp, name: kept }]\n })\n const resolvedSources = input.sources?.length ? combineSources(input.sources) : []\n\n return {\n kind: 'File',\n ...input,\n id: hash('sha256', input.path, 'hex'),\n name: trimExtName(input.baseName),\n extname,\n imports: resolvedImports,\n exports: resolvedExports,\n sources: resolvedSources,\n meta: input.meta ?? ({} as TMeta),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA8LA,SAAgB,YAAY,MAAsB;CAChD,MAAM,WAAW,KAAK,YAAY,GAAG;CACrC,IAAI,WAAW,KAAK,CAAC,KAAK,SAAS,KAAK,QAAQ,GAC9C,OAAO,KAAK,MAAM,GAAG,QAAQ;CAE/B,OAAO;AACT;;;AC1LA,SAAS,UAAU,QAA4B;CAE7C,OAAO,GADS,OAAO,QAAQA,iBAAAA,wBAAwB,OAAO,KAAK,EACjD,GAAG,OAAO,gBAAgB,MAAM,GAAG,OAAO,cAAc;AAC5E;AAEA,SAAS,YAAY,MAAc,YAAgD;CACjF,OAAO,GAAG,KAAK,GAAG,cAAc;AAClC;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAwC,SAA6C;CACrJ,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc,MAAM,GAAG,WAAW;AACpE;AAEA,SAAS,UAAU,MAAc,MAAiC,YAAgD;CAChH,OAAO,GAAG,KAAK,GAAG,QAAQ,GAAG,GAAG,cAAc;AAChD;;;;;AAMA,SAAS,QAAQ,MAAoG;CACnH,MAAM,UAAU,MAAM,QAAQ,KAAK,IAAI,IAAI,MAAM;CACjD,MAAM,WAAW,KAAK,aAAa,MAAM;CACzC,MAAM,UAAU,KAAK,QAAQ,OAAO,MAAM;CAC1C,MAAM,OAAO,MAAM,QAAQ,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,KAAK,IAAI,IAAK,KAAK,QAAQ;CACxF,OAAO,GAAG,QAAQ,GAAG,SAAS,GAAG,KAAK,KAAK,GAAG,QAAQ,GAAG;AAC3D;;;;;;AAOA,SAAgB,eAAe,SAA+C;CAC5E,MAAM,uBAAO,IAAI,IAAwB;CACzC,KAAK,MAAM,UAAU,SAAS;EAC5B,MAAM,MAAM,UAAU,MAAM;EAC5B,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG,KAAK,IAAI,KAAK,MAAM;CAC1C;CACA,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC;AAC1B;;;;;;AAOA,SAAS,gBAAuB,UAAwB,UAAsC;CAC5F,MAAM,SAAS,IAAI,IAAI,QAAQ;CAC/B,KAAK,MAAM,QAAQ,UAAU,OAAO,IAAI,IAAI;CAC5C,OAAO,CAAC,GAAG,MAAM;AACnB;;;;;;;AAQA,SAAgB,eAAe,SAA+C;CAC5E,MAAM,SAA4B,CAAC;CAEnC,MAAM,8BAAc,IAAI,IAAwB;CAEhD,MAAM,uBAAO,IAAI,IAAY;CAG7B,MAAM,QAAQ,QAAQ,KAAK,UAAU;EAAE;EAAM,KAAK,QAAQ,IAAI;CAAE,EAAE;CAClE,MAAM,MAAM,GAAG,MAAO,EAAE,MAAM,EAAE,MAAM,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAE;CAEjE,KAAK,MAAM,EAAE,MAAM,UAAU,OAAO;EAClC,MAAM,EAAE,MAAM,MAAM,YAAY,YAAY;EAE5C,IAAI,MAAM,QAAQ,IAAI,GAAG;GACvB,IAAI,CAAC,KAAK,QAAQ;GAElB,MAAM,MAAM,YAAY,MAAM,UAAU;GACxC,MAAM,WAAW,YAAY,IAAI,GAAG;GAEpC,IAAI,YAAY,MAAM,QAAQ,SAAS,IAAI,GACzC,SAAS,OAAO,gBAAgB,SAAS,MAAM,IAAI;QAC9C;IACL,MAAM,UAAsB;KAAE,GAAG;KAAM,MAAM,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC;IAAE;IAChE,OAAO,KAAK,OAAO;IACnB,YAAY,IAAI,KAAK,OAAO;GAC9B;EACF,OAAO;GACL,MAAM,MAAM,UAAU,MAAM,MAAM,YAAY,OAAO;GACrD,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG;IAClB,OAAO,KAAK,IAAI;IAChB,KAAK,IAAI,GAAG;GACd;EACF;CACF;CAEA,OAAO;AACT;;;;;;;AAQA,SAAgB,eAAe,SAA4B,SAA4B,QAAoC;CAEzH,MAAM,gBAAgB,IAAI,IAAI,QAAQ,SAAS,MAAO,MAAM,QAAQ,EAAE,IAAI,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,IAAI,IAAI,CAAC,CAAE,CAAC;CAC/G,MAAM,UAAU,eAAgC,CAAC,UAAU,OAAO,SAAS,UAAU,KAAK,cAAc,IAAI,UAAU;CAItH,MAAM,iCAAiB,IAAI,IAAqD;CAChF,MAAM,oBAAoB,MAA0G;EAClI,IAAI,OAAO,MAAM,UAAU,OAAO;EAClC,MAAM,MAAM,GAAG,EAAE,aAAa,GAAG,EAAE,QAAQ;EAC3C,IAAI,CAAC,eAAe,IAAI,GAAG,GAAG,eAAe,IAAI,KAAK,CAAC;EACvD,OAAO,eAAe,IAAI,GAAG;CAC/B;CAKA,MAAM,2CAA2B,IAAI,IAAY;CACjD,KAAK,MAAM,QAAQ,SAAS;EAC1B,IAAI,CAAC,MAAM,QAAQ,KAAK,IAAI,GAAG;EAC/B,IAAI,KAAK,KAAK,MAAM,SAAU,OAAO,SAAS,WAAW,OAAO,IAAI,IAAI,OAAO,KAAK,QAAQ,KAAK,YAAY,CAAE,GAC7G,yBAAyB,IAAI,KAAK,IAAI;CAE1C;CAEA,MAAM,SAA4B,CAAC;CAEnC,MAAM,8BAAc,IAAI,IAAwB;CAEhD,MAAM,uBAAO,IAAI,IAAY;CAG7B,MAAM,QAAQ,QAAQ,KAAK,UAAU;EAAE;EAAM,KAAK,QAAQ,IAAI;CAAE,EAAE;CAClE,MAAM,MAAM,GAAG,MAAO,EAAE,MAAM,EAAE,MAAM,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAE;CAEjE,KAAK,MAAM,EAAE,MAAM,UAAU,OAAO;EAClC,IAAI,KAAK,SAAS,KAAK,MAAM;EAE7B,MAAM,EAAE,MAAM,eAAe;EAC7B,IAAI,EAAE,SAAS;EAEf,IAAI,MAAM,QAAQ,IAAI,GAAG;GACvB,OAAO,CAAC,GAAG,IAAI,IAAI,KAAK,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC,QAAQ,SAAU,OAAO,SAAS,WAAW,OAAO,IAAI,IAAI,OAAO,KAAK,QAAQ,KAAK,YAAY,CAAE;GACnJ,IAAI,CAAC,KAAK,QAAQ;GAElB,MAAM,MAAM,YAAY,MAAM,UAAU;GACxC,MAAM,WAAW,YAAY,IAAI,GAAG;GAEpC,IAAI,YAAY,MAAM,QAAQ,SAAS,IAAI,GACzC,SAAS,OAAO,gBAAgB,SAAS,MAAM,IAAI;QAC9C;IACL,MAAM,UAAsB;KAAE,GAAG;KAAM;IAAK;IAC5C,OAAO,KAAK,OAAO;IACnB,YAAY,IAAI,KAAK,OAAO;GAC9B;EACF,OAAO;GACL,IAAI,QAAQ,CAAC,OAAO,IAAI,KAAK,CAAC,yBAAyB,IAAI,IAAI,GAAG;GAElE,MAAM,MAAM,UAAU,MAAM,MAAM,UAAU;GAC5C,IAAI,CAAC,KAAK,IAAI,GAAG,GAAG;IAClB,OAAO,KAAK,IAAI;IAChB,KAAK,IAAI,GAAG;GACd;EACF;CACF;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AChJA,SAAgB,OAAuB,MAAS,SAAwB;CACtE,KAAK,MAAM,OAAO,SAChB,IAAI,QAAQ,SAAS,KAAK,MACxB,OAAO;EAAE,GAAG;EAAM,GAAG;CAAQ;CAIjC,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,WAA0C,OAA6C;CAGrG,MAAM,UAFaC,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,iBAAAA,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,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"} |
| //#region \0rolldown/runtime.js | ||
| var __create = Object.create; | ||
| var __defProp = Object.defineProperty; | ||
| var __name = (target, value) => __defProp(target, "name", { | ||
| value, | ||
| configurable: true | ||
| }); | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __getProtoOf = Object.getPrototypeOf; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __exportAll = (all, no_symbols) => { | ||
| let target = {}; | ||
| for (var name in all) __defProp(target, name, { | ||
| get: all[name], | ||
| enumerable: true | ||
| }); | ||
| if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" }); | ||
| return target; | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { | ||
| key = keys[i]; | ||
| if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { | ||
| get: ((k) => from[k]).bind(null, key), | ||
| enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable | ||
| }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { | ||
| value: mod, | ||
| enumerable: true | ||
| }) : target, mod)); | ||
| //#endregion | ||
| //#region src/node.ts | ||
| /** | ||
| * Builds a type guard that matches nodes of the given `kind`. | ||
| */ | ||
| function isKind(kind) { | ||
| return (node) => node.kind === kind; | ||
| } | ||
| /** | ||
| * Updates a schema's `optional` and `nullish` flags from a parent's `required` | ||
| * value and the schema's own `nullable`. Mirrors how OpenAPI parameters and | ||
| * object properties combine "required" and "nullable" into a single AST. | ||
| * | ||
| * - Non-required + non-nullable → `optional: true`. | ||
| * - Non-required + nullable → `nullish: true`. | ||
| * - Required → both flags cleared. | ||
| */ | ||
| function syncOptionality(schema, required) { | ||
| const nullable = schema.nullable ?? false; | ||
| return { | ||
| ...schema, | ||
| optional: !required && !nullable ? true : void 0, | ||
| nullish: !required && nullable ? true : void 0 | ||
| }; | ||
| } | ||
| /** | ||
| * Defines a node once and derives its `create` builder, `is` guard, and traversal | ||
| * metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the | ||
| * `kind`, so node construction lives in one place without scattered `as` casts. | ||
| * | ||
| * Set `rebuild: true` when the `build` hook derives fields from children. After a | ||
| * transform rewrites those children, the registry reruns `create` so the derived | ||
| * fields stay correct. | ||
| * | ||
| * @example Simple node | ||
| * ```ts | ||
| * const importDef = defineNode<ImportNode>({ kind: 'Import' }) | ||
| * const createImport = importDef.create | ||
| * ``` | ||
| * | ||
| * @example Node with a build hook that is rerun on transform | ||
| * ```ts | ||
| * const propertyDef = defineNode<PropertyNode, UserPropertyNode>({ | ||
| * kind: 'Property', | ||
| * build: (props) => ({ ...props, required: props.required ?? false }), | ||
| * children: ['schema'], | ||
| * visitorKey: 'property', | ||
| * rebuild: true, | ||
| * }) | ||
| * ``` | ||
| */ | ||
| function defineNode(config) { | ||
| const { kind, defaults, build, children, visitorKey, rebuild } = config; | ||
| function create(input) { | ||
| const base = build ? build(input) : input; | ||
| return { | ||
| ...defaults, | ||
| ...base, | ||
| kind | ||
| }; | ||
| } | ||
| return { | ||
| kind, | ||
| create, | ||
| is: isKind(kind), | ||
| children, | ||
| visitorKey, | ||
| rebuild | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/schema.ts | ||
| /** | ||
| * Maps schema `type` to its underlying `primitive`. | ||
| * Primitive types map to themselves. Special string formats map to `'string'`. | ||
| * Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset. | ||
| */ | ||
| const TYPE_TO_PRIMITIVE = { | ||
| string: "string", | ||
| number: "number", | ||
| integer: "integer", | ||
| bigint: "bigint", | ||
| boolean: "boolean", | ||
| null: "null", | ||
| any: "any", | ||
| unknown: "unknown", | ||
| void: "void", | ||
| never: "never", | ||
| object: "object", | ||
| array: "array", | ||
| date: "date", | ||
| uuid: "string", | ||
| email: "string", | ||
| url: "string", | ||
| datetime: "string", | ||
| time: "string" | ||
| }; | ||
| /** | ||
| * Definition for the {@link SchemaNode}. Object schemas default `properties` to an | ||
| * empty array, and `primitive` is inferred from `type` when not explicitly provided. | ||
| */ | ||
| const schemaDef = defineNode({ | ||
| kind: "Schema", | ||
| build: (props) => { | ||
| if (props.type === "object") return { | ||
| properties: [], | ||
| primitive: "object", | ||
| ...props | ||
| }; | ||
| return { | ||
| primitive: TYPE_TO_PRIMITIVE[props.type], | ||
| ...props | ||
| }; | ||
| }, | ||
| children: [ | ||
| "properties", | ||
| "items", | ||
| "members", | ||
| "additionalProperties" | ||
| ], | ||
| visitorKey: "schema" | ||
| }); | ||
| function createSchema(props) { | ||
| return schemaDef.create(props); | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/casing.ts | ||
| /** | ||
| * Shared implementation for camelCase and PascalCase conversion. | ||
| * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons) | ||
| * and capitalizes each word according to `pascal`. | ||
| * | ||
| * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are. | ||
| */ | ||
| function toCamelOrPascal(text, pascal) { | ||
| return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => { | ||
| if (word.length > 1 && word === word.toUpperCase()) return word; | ||
| return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1); | ||
| }).join("").replace(/[^a-zA-Z0-9]/g, ""); | ||
| } | ||
| /** | ||
| * Converts `text` to camelCase. | ||
| * | ||
| * @example Word boundaries | ||
| * `camelCase('hello-world') // 'helloWorld'` | ||
| * | ||
| * @example With a prefix | ||
| * `camelCase('tag', { prefix: 'create' }) // 'createTag'` | ||
| */ | ||
| function camelCase(text, { prefix = "", suffix = "" } = {}) { | ||
| return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false); | ||
| } | ||
| /** | ||
| * Converts `text` to PascalCase. | ||
| * | ||
| * @example Word boundaries | ||
| * `pascalCase('hello-world') // 'HelloWorld'` | ||
| * | ||
| * @example With a suffix | ||
| * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'` | ||
| */ | ||
| function pascalCase(text, { prefix = "", suffix = "" } = {}) { | ||
| return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/extractStringsFromNodes.ts | ||
| /** | ||
| * Extracts all string content from a `CodeNode` tree recursively. | ||
| * | ||
| * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`), | ||
| * and nested node content. Used to build the full source string for import filtering. | ||
| */ | ||
| function extractStringsFromNodes(nodes) { | ||
| if (!nodes?.length) return ""; | ||
| return nodes.map((node) => { | ||
| if (typeof node === "string") return node; | ||
| if (node.kind === "Text") return node.value; | ||
| if (node.kind === "Break") return ""; | ||
| if (node.kind === "Jsx") return node.value; | ||
| const parts = []; | ||
| if ("params" in node && node.params) parts.push(node.params); | ||
| if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics); | ||
| if ("returnType" in node && node.returnType) parts.push(node.returnType); | ||
| if ("type" in node && typeof node.type === "string") parts.push(node.type); | ||
| const nested = extractStringsFromNodes(node.nodes); | ||
| if (nested) parts.push(nested); | ||
| return parts.join("\n"); | ||
| }).filter(Boolean).join("\n"); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/property.ts | ||
| /** | ||
| * Definition for the {@link PropertyNode}. `required` defaults to `false` and the | ||
| * schema's `optional`/`nullish` flags are kept in sync with it. | ||
| */ | ||
| const propertyDef = defineNode({ | ||
| kind: "Property", | ||
| build: (props) => { | ||
| const required = props.required ?? false; | ||
| return { | ||
| ...props, | ||
| required, | ||
| schema: syncOptionality(props.schema, required) | ||
| }; | ||
| }, | ||
| children: ["schema"], | ||
| visitorKey: "property", | ||
| rebuild: true | ||
| }); | ||
| /** | ||
| * Creates a `PropertyNode`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const property = createProperty({ | ||
| * name: 'status', | ||
| * required: true, | ||
| * schema: createSchema({ type: 'string', nullable: true }), | ||
| * }) | ||
| * // required=true, no optional/nullish | ||
| * ``` | ||
| */ | ||
| const createProperty = propertyDef.create; | ||
| //#endregion | ||
| //#region src/nodes/code.ts | ||
| /** | ||
| * Definition for the {@link ConstNode}. | ||
| */ | ||
| const constDef = defineNode({ kind: "Const" }); | ||
| /** | ||
| * Creates a `ConstNode` representing a TypeScript `const` declaration. | ||
| * | ||
| * @example Exported constant with type and `as const` | ||
| * ```ts | ||
| * createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true }) | ||
| * // export const pets: Pet[] = ... as const | ||
| * ``` | ||
| */ | ||
| const createConst = constDef.create; | ||
| /** | ||
| * Definition for the {@link TypeNode}. | ||
| */ | ||
| const typeDef = defineNode({ kind: "Type" }); | ||
| /** | ||
| * Creates a `TypeNode` representing a TypeScript `type` alias declaration. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createType({ name: 'Pet', export: true }) | ||
| * // export type Pet = ... | ||
| * ``` | ||
| */ | ||
| const createType = typeDef.create; | ||
| /** | ||
| * Definition for the {@link FunctionNode}. | ||
| */ | ||
| const functionDef = defineNode({ kind: "Function" }); | ||
| /** | ||
| * Creates a `FunctionNode` representing a TypeScript `function` declaration. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' }) | ||
| * // export async function fetchPet(): Promise<Pet> { ... } | ||
| * ``` | ||
| */ | ||
| const createFunction = functionDef.create; | ||
| /** | ||
| * Definition for the {@link ArrowFunctionNode}. | ||
| */ | ||
| const arrowFunctionDef = defineNode({ kind: "ArrowFunction" }); | ||
| /** | ||
| * Creates an `ArrowFunctionNode` representing a TypeScript arrow function. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true }) | ||
| * // export const double = (n: number) => ... | ||
| * ``` | ||
| */ | ||
| const createArrowFunction = arrowFunctionDef.create; | ||
| /** | ||
| * Definition for the {@link TextNode}. | ||
| */ | ||
| const textDef = defineNode({ | ||
| kind: "Text", | ||
| build: (value) => ({ value }) | ||
| }); | ||
| /** | ||
| * Creates a {@link TextNode} representing a raw string fragment in the source output. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createText('return fetch(id)') | ||
| * // { kind: 'Text', value: 'return fetch(id)' } | ||
| * ``` | ||
| */ | ||
| const createText = textDef.create; | ||
| /** | ||
| * Definition for the {@link BreakNode}. | ||
| */ | ||
| const breakDef = defineNode({ | ||
| kind: "Break", | ||
| build: () => ({}) | ||
| }); | ||
| /** | ||
| * Creates a {@link BreakNode} representing a line break in the source output. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createBreak() | ||
| * // { kind: 'Break' } | ||
| * ``` | ||
| */ | ||
| function createBreak() { | ||
| return breakDef.create(); | ||
| } | ||
| /** | ||
| * Definition for the {@link JsxNode}. | ||
| */ | ||
| const jsxDef = defineNode({ | ||
| kind: "Jsx", | ||
| build: (value) => ({ value }) | ||
| }); | ||
| /** | ||
| * Creates a {@link JsxNode} representing a raw JSX fragment in the source output. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createJsx('<>\n <a href={href}>Open</a>\n</>') | ||
| * // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' } | ||
| * ``` | ||
| */ | ||
| const createJsx = jsxDef.create; | ||
| //#endregion | ||
| //#region src/nodes/content.ts | ||
| /** | ||
| * Definition for the {@link ContentNode}. | ||
| */ | ||
| const contentDef = defineNode({ | ||
| kind: "Content", | ||
| children: ["schema"] | ||
| }); | ||
| /** | ||
| * Creates a `ContentNode` for a single request-body or response content type. | ||
| */ | ||
| const createContent = contentDef.create; | ||
| //#endregion | ||
| //#region src/nodes/file.ts | ||
| /** | ||
| * Definition for the {@link ImportNode}. | ||
| */ | ||
| const importDef = defineNode({ kind: "Import" }); | ||
| /** | ||
| * Creates an `ImportNode` representing a language-agnostic import/dependency declaration. | ||
| * | ||
| * @example Named import | ||
| * ```ts | ||
| * createImport({ name: ['useState'], path: 'react' }) | ||
| * // import { useState } from 'react' | ||
| * ``` | ||
| */ | ||
| const createImport = importDef.create; | ||
| /** | ||
| * Definition for the {@link ExportNode}. | ||
| */ | ||
| const exportDef = defineNode({ kind: "Export" }); | ||
| /** | ||
| * Creates an `ExportNode` representing a language-agnostic export/public API declaration. | ||
| * | ||
| * @example Named export | ||
| * ```ts | ||
| * createExport({ name: ['Pet'], path: './Pet' }) | ||
| * // export { Pet } from './Pet' | ||
| * ``` | ||
| */ | ||
| const createExport = exportDef.create; | ||
| /** | ||
| * Definition for the {@link SourceNode}. | ||
| */ | ||
| const sourceDef = defineNode({ kind: "Source" }); | ||
| /** | ||
| * Creates a `SourceNode` representing a fragment of source code within a file. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true }) | ||
| * ``` | ||
| */ | ||
| const createSource = sourceDef.create; | ||
| /** | ||
| * Definition for the {@link FileNode}. The fully resolved builder lives in | ||
| * `createFile`, so this definition only supplies the guard. | ||
| */ | ||
| const fileDef = defineNode({ kind: "File" }); | ||
| //#endregion | ||
| //#region src/nodes/function.ts | ||
| /** | ||
| * Definition for the {@link TypeLiteralNode}. | ||
| */ | ||
| const typeLiteralDef = defineNode({ kind: "TypeLiteral" }); | ||
| /** | ||
| * Creates a {@link TypeLiteralNode} representing an inline anonymous object type. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] }) | ||
| * // { petId: string } | ||
| * ``` | ||
| */ | ||
| const createTypeLiteral = typeLiteralDef.create; | ||
| /** | ||
| * Definition for the {@link IndexedAccessTypeNode}. | ||
| */ | ||
| const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" }); | ||
| /** | ||
| * Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' }) | ||
| * // DeletePetPathParams['petId'] | ||
| * ``` | ||
| */ | ||
| const createIndexedAccessType = indexedAccessTypeDef.create; | ||
| /** | ||
| * Definition for the {@link ObjectBindingPatternNode}. | ||
| */ | ||
| const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" }); | ||
| /** | ||
| * Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] }) | ||
| * // { id, name } | ||
| * ``` | ||
| */ | ||
| const createObjectBindingPattern = objectBindingPatternDef.create; | ||
| /** | ||
| * Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`. | ||
| * Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name | ||
| * paired with a {@link TypeLiteralNode} type. | ||
| */ | ||
| const functionParameterDef = defineNode({ | ||
| kind: "FunctionParameter", | ||
| build: (input) => { | ||
| if ("properties" in input) return { | ||
| name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }), | ||
| type: createTypeLiteral({ members: input.properties.map((p) => ({ | ||
| name: p.name, | ||
| type: p.type, | ||
| optional: p.optional ?? false | ||
| })) }), | ||
| optional: input.optional ?? false, | ||
| ...input.default !== void 0 ? { default: input.default } : {} | ||
| }; | ||
| return { | ||
| optional: false, | ||
| ...input | ||
| }; | ||
| } | ||
| }); | ||
| /** | ||
| * Creates a `FunctionParameterNode`. `optional` defaults to `false`. | ||
| * | ||
| * @example Optional param | ||
| * ```ts | ||
| * createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true }) | ||
| * // → params?: QueryParams | ||
| * ``` | ||
| * | ||
| * @example Destructured group | ||
| * ```ts | ||
| * createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' }) | ||
| * // → { id, name }: { id: string; name?: string } = {} | ||
| * ``` | ||
| */ | ||
| const createFunctionParameter = functionParameterDef.create; | ||
| /** | ||
| * Definition for the {@link FunctionParametersNode}. | ||
| */ | ||
| const functionParametersDef = defineNode({ | ||
| kind: "FunctionParameters", | ||
| defaults: { params: [] } | ||
| }); | ||
| /** | ||
| * Creates a `FunctionParametersNode` from an ordered list of parameters. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const empty = createFunctionParameters() | ||
| * // { kind: 'FunctionParameters', params: [] } | ||
| * ``` | ||
| */ | ||
| function createFunctionParameters(props = {}) { | ||
| return functionParametersDef.create(props); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/input.ts | ||
| /** | ||
| * Definition for the {@link InputNode}. | ||
| */ | ||
| const inputDef = defineNode({ | ||
| kind: "Input", | ||
| defaults: { | ||
| schemas: [], | ||
| operations: [], | ||
| meta: { | ||
| circularNames: [], | ||
| enumNames: [] | ||
| } | ||
| }, | ||
| children: ["schemas", "operations"], | ||
| visitorKey: "input" | ||
| }); | ||
| /** | ||
| * Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and | ||
| * `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the | ||
| * eager variant with array `schemas`/`operations` and the defaulted `meta`. | ||
| * | ||
| * @example Eager | ||
| * ```ts | ||
| * const input = createInput() | ||
| * // { kind: 'Input', schemas: [], operations: [] } | ||
| * ``` | ||
| * | ||
| * @example Streaming | ||
| * ```ts | ||
| * const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } }) | ||
| * ``` | ||
| */ | ||
| function createInput(options = {}) { | ||
| const { stream, ...overrides } = options; | ||
| if (stream) return { | ||
| kind: "Input", | ||
| ...overrides | ||
| }; | ||
| return inputDef.create(overrides); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/requestBody.ts | ||
| /** | ||
| * Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`. | ||
| */ | ||
| const requestBodyDef = defineNode({ | ||
| kind: "RequestBody", | ||
| build: (props) => ({ | ||
| ...props, | ||
| content: props.content?.map(createContent) | ||
| }), | ||
| children: ["content"] | ||
| }); | ||
| /** | ||
| * Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`. | ||
| */ | ||
| const createRequestBody = requestBodyDef.create; | ||
| //#endregion | ||
| //#region src/nodes/operation.ts | ||
| /** | ||
| * Definition for the {@link OperationNode}. HTTP operations (those carrying both | ||
| * `method` and `path`) are tagged with `protocol: 'http'`, and the request body is | ||
| * normalized into a `RequestBodyNode`. | ||
| */ | ||
| const operationDef = defineNode({ | ||
| kind: "Operation", | ||
| build: (props) => { | ||
| const { requestBody, ...rest } = props; | ||
| const isHttp = rest.method !== void 0 && rest.path !== void 0; | ||
| return { | ||
| tags: [], | ||
| parameters: [], | ||
| responses: [], | ||
| ...rest, | ||
| ...isHttp ? { protocol: "http" } : {}, | ||
| requestBody: requestBody ? createRequestBody(requestBody) : void 0 | ||
| }; | ||
| }, | ||
| children: [ | ||
| "parameters", | ||
| "requestBody", | ||
| "responses" | ||
| ], | ||
| visitorKey: "operation" | ||
| }); | ||
| function createOperation(props) { | ||
| return operationDef.create(props); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/output.ts | ||
| /** | ||
| * Definition for the {@link OutputNode}. | ||
| */ | ||
| const outputDef = defineNode({ | ||
| kind: "Output", | ||
| defaults: { files: [] }, | ||
| visitorKey: "output" | ||
| }); | ||
| /** | ||
| * Creates an `OutputNode` with a stable default for `files`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const output = createOutput() | ||
| * // { kind: 'Output', files: [] } | ||
| * ``` | ||
| */ | ||
| function createOutput(overrides = {}) { | ||
| return outputDef.create(overrides); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/parameter.ts | ||
| /** | ||
| * Definition for the {@link ParameterNode}. `required` defaults to `false` and the | ||
| * schema's `optional`/`nullish` flags are kept in sync with it. | ||
| */ | ||
| const parameterDef = defineNode({ | ||
| kind: "Parameter", | ||
| build: (props) => { | ||
| const required = props.required ?? false; | ||
| return { | ||
| ...props, | ||
| required, | ||
| schema: syncOptionality(props.schema, required) | ||
| }; | ||
| }, | ||
| children: ["schema"], | ||
| visitorKey: "parameter", | ||
| rebuild: true | ||
| }); | ||
| /** | ||
| * Creates a `ParameterNode`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const param = createParameter({ | ||
| * name: 'petId', | ||
| * in: 'path', | ||
| * required: true, | ||
| * schema: createSchema({ type: 'string' }), | ||
| * }) | ||
| * ``` | ||
| */ | ||
| const createParameter = parameterDef.create; | ||
| //#endregion | ||
| //#region src/nodes/response.ts | ||
| /** | ||
| * Definition for the {@link ResponseNode}. A single legacy `schema` (with optional | ||
| * `mediaType`/`keysToOmit`) is normalized into one `content` entry. | ||
| */ | ||
| const responseDef = defineNode({ | ||
| kind: "Response", | ||
| build: (props) => { | ||
| const { schema, mediaType, keysToOmit, content, ...rest } = props; | ||
| const entries = content ?? (schema ? [{ | ||
| contentType: mediaType ?? "application/json", | ||
| schema, | ||
| keysToOmit: keysToOmit ?? null | ||
| }] : void 0); | ||
| return { | ||
| ...rest, | ||
| content: entries?.map(createContent) | ||
| }; | ||
| }, | ||
| children: ["content"], | ||
| visitorKey: "response" | ||
| }); | ||
| /** | ||
| * Creates a `ResponseNode`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const response = createResponse({ | ||
| * statusCode: '200', | ||
| * content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }], | ||
| * }) | ||
| * ``` | ||
| */ | ||
| const createResponse = responseDef.create; | ||
| //#endregion | ||
| Object.defineProperty(exports, "__exportAll", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __exportAll; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "__name", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __name; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "__toESM", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __toESM; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "arrowFunctionDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return arrowFunctionDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "breakDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return breakDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "camelCase", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return camelCase; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "constDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return constDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "contentDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return contentDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createArrowFunction", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createArrowFunction; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createBreak", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createBreak; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createConst", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createConst; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createContent", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createContent; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createExport", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createExport; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFunction", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFunction; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFunctionParameter", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFunctionParameter; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFunctionParameters", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFunctionParameters; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createImport", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createImport; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createIndexedAccessType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createIndexedAccessType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createInput", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createInput; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createJsx", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createJsx; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createObjectBindingPattern", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createObjectBindingPattern; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOperation", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createOperation; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOutput", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createOutput; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createParameter", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createParameter; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createProperty", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createProperty; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createRequestBody", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createRequestBody; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createResponse", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createResponse; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createSchema", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createSchema; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createSource", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createSource; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createText", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createText; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createTypeLiteral", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createTypeLiteral; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "defineNode", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return defineNode; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "exportDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return exportDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "extractStringsFromNodes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return extractStringsFromNodes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "fileDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return fileDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "functionDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return functionDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "functionParameterDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return functionParameterDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "functionParametersDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return functionParametersDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "importDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return importDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "indexedAccessTypeDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return indexedAccessTypeDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "inputDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return inputDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "jsxDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return jsxDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "objectBindingPatternDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return objectBindingPatternDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "operationDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return operationDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "outputDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return outputDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "parameterDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return parameterDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "pascalCase", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return pascalCase; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "propertyDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return propertyDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "requestBodyDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return requestBodyDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "responseDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return responseDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "schemaDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return schemaDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "sourceDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return sourceDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "syncOptionality", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return syncOptionality; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "textDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return textDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "typeDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return typeDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "typeLiteralDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return typeLiteralDef; | ||
| } | ||
| }); | ||
| //# sourceMappingURL=response-DS5S3IG4.cjs.map |
Sorry, the diff of this file is too big to display
| const require_response = require("./response-DS5S3IG4.cjs"); | ||
| //#region src/constants.ts | ||
| const visitorDepths = { | ||
| shallow: "shallow", | ||
| deep: "deep" | ||
| }; | ||
| /** | ||
| * Schema type discriminators used by all AST schema nodes. | ||
| * | ||
| * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`). | ||
| * Call `isScalarPrimitive()` to check for the scalar types. | ||
| */ | ||
| const schemaTypes = { | ||
| /** | ||
| * Text value. | ||
| */ | ||
| string: "string", | ||
| /** | ||
| * Floating-point number (`float`, `double`). | ||
| */ | ||
| number: "number", | ||
| /** | ||
| * Whole number (`int32`). Use `bigint` for `int64`. | ||
| */ | ||
| integer: "integer", | ||
| /** | ||
| * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`. | ||
| */ | ||
| bigint: "bigint", | ||
| /** | ||
| * Boolean value. | ||
| */ | ||
| boolean: "boolean", | ||
| /** | ||
| * Explicit null value. | ||
| */ | ||
| null: "null", | ||
| /** | ||
| * Any value (no type restriction). | ||
| */ | ||
| any: "any", | ||
| /** | ||
| * Unknown value (must be narrowed before usage). | ||
| */ | ||
| unknown: "unknown", | ||
| /** | ||
| * No return value (`void`). | ||
| */ | ||
| void: "void", | ||
| /** | ||
| * Object with named properties. | ||
| */ | ||
| object: "object", | ||
| /** | ||
| * Sequential list of items. | ||
| */ | ||
| array: "array", | ||
| /** | ||
| * Fixed-length list with position-specific items. | ||
| */ | ||
| tuple: "tuple", | ||
| /** | ||
| * "One of" multiple schema members. | ||
| */ | ||
| union: "union", | ||
| /** | ||
| * "All of" multiple schema members. | ||
| */ | ||
| intersection: "intersection", | ||
| /** | ||
| * Enum schema. | ||
| */ | ||
| enum: "enum", | ||
| /** | ||
| * Reference to another schema. | ||
| */ | ||
| ref: "ref", | ||
| /** | ||
| * Calendar date (for example `2026-03-24`). | ||
| */ | ||
| date: "date", | ||
| /** | ||
| * Date-time value (for example `2026-03-24T09:00:00Z`). | ||
| */ | ||
| datetime: "datetime", | ||
| /** | ||
| * Time-only value (for example `09:00:00`). | ||
| */ | ||
| time: "time", | ||
| /** | ||
| * UUID value. | ||
| */ | ||
| uuid: "uuid", | ||
| /** | ||
| * Email address value. | ||
| */ | ||
| email: "email", | ||
| /** | ||
| * URL value. | ||
| */ | ||
| url: "url", | ||
| /** | ||
| * IPv4 address value. | ||
| */ | ||
| ipv4: "ipv4", | ||
| /** | ||
| * IPv6 address value. | ||
| */ | ||
| ipv6: "ipv6", | ||
| /** | ||
| * Binary/blob value. | ||
| */ | ||
| blob: "blob", | ||
| /** | ||
| * Impossible value (`never`). | ||
| */ | ||
| never: "never" | ||
| }; | ||
| /** | ||
| * Scalar primitive schema types used for union simplification and type narrowing. | ||
| */ | ||
| const SCALAR_PRIMITIVE_TYPES = new Set([ | ||
| "string", | ||
| "number", | ||
| "integer", | ||
| "bigint", | ||
| "boolean" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `type` is a scalar primitive that can be assigned without wrapping | ||
| * (for example `string | number | boolean`). | ||
| */ | ||
| function isScalarPrimitive(type) { | ||
| return SCALAR_PRIMITIVE_TYPES.has(type); | ||
| } | ||
| /** | ||
| * HTTP method identifiers used by operation nodes. | ||
| */ | ||
| const httpMethods = { | ||
| get: "GET", | ||
| post: "POST", | ||
| put: "PUT", | ||
| patch: "PATCH", | ||
| delete: "DELETE", | ||
| head: "HEAD", | ||
| options: "OPTIONS", | ||
| trace: "TRACE" | ||
| }; | ||
| /** | ||
| * One indentation level, derived from {@link INDENT_SIZE}. | ||
| */ | ||
| const INDENT = Array.from({ length: 2 }, () => " ").join(""); | ||
| //#endregion | ||
| //#region ../../internals/utils/src/promise.ts | ||
| /** | ||
| * Wraps `factory` with a keyed cache backed by the provided store. | ||
| * | ||
| * Pass a `WeakMap` for object keys (results are GC-eligible when the key is | ||
| * collected) or a `Map` for primitive keys. For multi-argument functions, | ||
| * nest two `memoize` calls — the outer keyed by the first argument, the | ||
| * inner (created once per outer miss) keyed by the second. | ||
| * | ||
| * Because the cache is owned by the caller, it can be shared, inspected, or | ||
| * cleared independently of the memoized function. | ||
| * | ||
| * @example Single WeakMap key | ||
| * ```ts | ||
| * const cache = new WeakMap<SchemaNode, Set<string>>() | ||
| * const getRefs = memoize(cache, (node) => collectRefs(node)) | ||
| * ``` | ||
| * | ||
| * @example Single Map key (primitive) | ||
| * ```ts | ||
| * const cache = new Map<string, Resolver>() | ||
| * const getResolver = memoize(cache, (name) => buildResolver(name)) | ||
| * ``` | ||
| * | ||
| * @example Two-level (object + primitive) | ||
| * ```ts | ||
| * const outer = new WeakMap<Params[], Map<string, Params[]>>() | ||
| * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key))) | ||
| * fn(params)('camelcase') | ||
| * ``` | ||
| */ | ||
| function memoize(store, factory) { | ||
| return (key) => { | ||
| if (store.has(key)) return store.get(key); | ||
| const value = factory(key); | ||
| store.set(key, value); | ||
| return value; | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/reserved.ts | ||
| /** | ||
| * JavaScript and Java reserved words. | ||
| * @link https://github.com/jonschlinkert/reserved/blob/master/index.js | ||
| */ | ||
| const reservedWords = new Set([ | ||
| "abstract", | ||
| "arguments", | ||
| "boolean", | ||
| "break", | ||
| "byte", | ||
| "case", | ||
| "catch", | ||
| "char", | ||
| "class", | ||
| "const", | ||
| "continue", | ||
| "debugger", | ||
| "default", | ||
| "delete", | ||
| "do", | ||
| "double", | ||
| "else", | ||
| "enum", | ||
| "eval", | ||
| "export", | ||
| "extends", | ||
| "false", | ||
| "final", | ||
| "finally", | ||
| "float", | ||
| "for", | ||
| "function", | ||
| "goto", | ||
| "if", | ||
| "implements", | ||
| "import", | ||
| "in", | ||
| "instanceof", | ||
| "int", | ||
| "interface", | ||
| "let", | ||
| "long", | ||
| "native", | ||
| "new", | ||
| "null", | ||
| "package", | ||
| "private", | ||
| "protected", | ||
| "public", | ||
| "return", | ||
| "short", | ||
| "static", | ||
| "super", | ||
| "switch", | ||
| "synchronized", | ||
| "this", | ||
| "throw", | ||
| "throws", | ||
| "transient", | ||
| "true", | ||
| "try", | ||
| "typeof", | ||
| "var", | ||
| "void", | ||
| "volatile", | ||
| "while", | ||
| "with", | ||
| "yield", | ||
| "Array", | ||
| "Date", | ||
| "hasOwnProperty", | ||
| "Infinity", | ||
| "isFinite", | ||
| "isNaN", | ||
| "isPrototypeOf", | ||
| "length", | ||
| "Math", | ||
| "name", | ||
| "NaN", | ||
| "Number", | ||
| "Object", | ||
| "prototype", | ||
| "String", | ||
| "toString", | ||
| "undefined", | ||
| "valueOf" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `name` is a syntactically valid JavaScript variable name. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * isValidVarName('status') // true | ||
| * isValidVarName('class') // false (reserved word) | ||
| * isValidVarName('42foo') // false (starts with digit) | ||
| * ``` | ||
| */ | ||
| function isValidVarName(name) { | ||
| if (!name || reservedWords.has(name)) return false; | ||
| return isIdentifier(name); | ||
| } | ||
| /** | ||
| * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words. | ||
| * | ||
| * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys | ||
| * even though they are not valid variable names, so use this (not {@link isValidVarName}) when | ||
| * deciding whether an object key needs quoting. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * isIdentifier('name') // true | ||
| * isIdentifier('x-total')// false | ||
| * ``` | ||
| */ | ||
| function isIdentifier(name) { | ||
| return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name); | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/string.ts | ||
| /** | ||
| * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping | ||
| * any backslash or single quote in the content. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * singleQuote('foo') // "'foo'" | ||
| * singleQuote("o'clock") // "'o\\'clock'" | ||
| * ``` | ||
| */ | ||
| function singleQuote(value) { | ||
| if (value === void 0 || value === null) return "''"; | ||
| return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/codegen.ts | ||
| /** | ||
| * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no | ||
| * comments so callers always get a usable string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildJSDoc(['@type string', '@example hello']) | ||
| * // '/**\n * @type string\n * @example hello\n *\/\n ' | ||
| * ``` | ||
| */ | ||
| function buildJSDoc(comments, options = {}) { | ||
| const { indent = " * ", suffix = "\n ", fallback = " " } = options; | ||
| if (comments.length === 0) return fallback; | ||
| return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`; | ||
| } | ||
| /** | ||
| * Indents every non-empty line of `text` by one indent level, leaving blank lines empty. | ||
| */ | ||
| function indentLines(text) { | ||
| if (!text) return ""; | ||
| return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n"); | ||
| } | ||
| /** | ||
| * Renders an object key, quoting it with single quotes only when it is not a valid identifier. | ||
| * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * objectKey('name') // 'name' | ||
| * objectKey('x-total') // "'x-total'" | ||
| * ``` | ||
| */ | ||
| function objectKey(name) { | ||
| return isIdentifier(name) ? name : singleQuote(name); | ||
| } | ||
| /** | ||
| * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one | ||
| * level and closing the brace at column zero. Nested objects built the same way indent cumulatively, | ||
| * so callers never re-parse the generated code. A trailing comma is added per entry to match the | ||
| * formatter's multi-line style. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildObject(['id: z.number()', 'name: z.string()']) | ||
| * // '{\n id: z.number(),\n name: z.string(),\n}' | ||
| * ``` | ||
| */ | ||
| function buildObject(entries) { | ||
| if (entries.length === 0) return "{}"; | ||
| return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`; | ||
| } | ||
| /** | ||
| * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on | ||
| * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented | ||
| * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`, | ||
| * `z.array([…])`, and similar member lists so objects inside them nest correctly. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildList(['z.string()', 'z.number()']) | ||
| * // '[z.string(), z.number()]' | ||
| * ``` | ||
| */ | ||
| function buildList(items, brackets = ["[", "]"]) { | ||
| const [open, close] = brackets; | ||
| if (items.length === 0) return `${open}${close}`; | ||
| if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`; | ||
| return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`; | ||
| } | ||
| //#endregion | ||
| //#region src/guards.ts | ||
| /** | ||
| * Narrows a `SchemaNode` to the variant that matches `type`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ type: 'string' }) | ||
| * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null | ||
| * ``` | ||
| */ | ||
| function narrowSchema(node, type) { | ||
| return node?.type === type ? node : null; | ||
| } | ||
| /** | ||
| * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * if (isHttpOperationNode(node)) { | ||
| * console.log(node.method, node.path) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function isHttpOperationNode(node) { | ||
| return node.protocol === "http" || node.method !== void 0 && node.path !== void 0; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/refs.ts | ||
| const plainStringTypes = new Set([ | ||
| "string", | ||
| "uuid", | ||
| "email", | ||
| "url", | ||
| "datetime" | ||
| ]); | ||
| /** | ||
| * Returns the last path segment of a reference string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * extractRefName('#/components/schemas/Pet') // 'Pet' | ||
| * ``` | ||
| */ | ||
| function extractRefName(ref) { | ||
| return ref.split("/").at(-1) ?? ref; | ||
| } | ||
| /** | ||
| * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`. | ||
| * | ||
| * Returns `null` for non-ref nodes or when no name resolves. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) | ||
| * // => 'Pet' | ||
| * ``` | ||
| */ | ||
| function resolveRefName(node) { | ||
| if (!node || node.type !== "ref") return null; | ||
| if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null; | ||
| return node.name ?? node.schema?.name ?? null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase child schema name by joining a parent name and property name. | ||
| * Returns `null` when there is no parent to nest under. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * childName('Order', 'shipping_address') // 'OrderShippingAddress' | ||
| * childName(undefined, 'params') // null | ||
| * ``` | ||
| */ | ||
| function childName(parentName, propName) { | ||
| return parentName ? require_response.pascalCase([parentName, propName].join(" ")) : null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any | ||
| * empty parts. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum' | ||
| * ``` | ||
| */ | ||
| function enumPropName(parentName, propName, enumSuffix) { | ||
| return require_response.pascalCase([ | ||
| parentName, | ||
| propName, | ||
| enumSuffix | ||
| ].filter(Boolean).join(" ")); | ||
| } | ||
| /** | ||
| * Type guard that returns `true` when a schema emits as a plain `string` type. | ||
| * | ||
| * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time` | ||
| * types, returns `true` only when `representation` is `'string'` rather than `'date'`. | ||
| */ | ||
| function isStringType(node) { | ||
| if (plainStringTypes.has(node.type)) return true; | ||
| const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time"); | ||
| if (temporal) return temporal.representation !== "date"; | ||
| return false; | ||
| } | ||
| /** | ||
| * Derives a {@link ParamGroupType} for a query or header group from the resolver. | ||
| * | ||
| * Returns `null` when there is no resolver, no params, or the group name equals the | ||
| * individual param name (so there is no real group to emit). | ||
| */ | ||
| function resolveGroupType({ node, params, group, resolver }) { | ||
| if (!resolver || !params.length) return null; | ||
| const firstParam = params[0]; | ||
| const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam); | ||
| if (groupName === resolver.resolveParamName(node, firstParam)) return null; | ||
| return { | ||
| type: groupName, | ||
| optional: params.every((p) => !p.required) | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/transformers.ts | ||
| /** | ||
| * Replaces a discriminator property's schema with a string enum of allowed values. | ||
| * | ||
| * If `node` is not an object schema, or if the property does not exist, the input | ||
| * node is returned as-is. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ | ||
| * type: 'object', | ||
| * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })], | ||
| * }) | ||
| * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] }) | ||
| * ``` | ||
| */ | ||
| function setDiscriminatorEnum({ node, propertyName, values, enumName }) { | ||
| const objectNode = narrowSchema(node, "object"); | ||
| if (!objectNode?.properties?.length) return node; | ||
| if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node; | ||
| return require_response.createSchema({ | ||
| ...objectNode, | ||
| properties: objectNode.properties.map((prop) => { | ||
| if (prop.name !== propertyName) return prop; | ||
| return require_response.createProperty({ | ||
| ...prop, | ||
| schema: require_response.createSchema({ | ||
| type: "enum", | ||
| primitive: "string", | ||
| enumValues: values, | ||
| name: enumName, | ||
| readOnly: prop.schema.readOnly, | ||
| writeOnly: prop.schema.writeOnly | ||
| }) | ||
| }); | ||
| }) | ||
| }); | ||
| } | ||
| /** | ||
| * Merges adjacent anonymous object members into a single anonymous object member. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const merged = mergeAdjacentObjects([ | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }), | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }), | ||
| * ]) | ||
| * ``` | ||
| */ | ||
| function* mergeAdjacentObjectsLazy(members) { | ||
| let acc; | ||
| for (const member of members) { | ||
| const objectMember = narrowSchema(member, "object"); | ||
| if (objectMember && !objectMember.name && acc !== void 0) { | ||
| const accObject = narrowSchema(acc, "object"); | ||
| if (accObject && !accObject.name) { | ||
| acc = require_response.createSchema({ | ||
| ...accObject, | ||
| properties: [...accObject.properties ?? [], ...objectMember.properties ?? []] | ||
| }); | ||
| continue; | ||
| } | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| acc = member; | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| } | ||
| /** | ||
| * Removes enum members that are covered by broader scalar primitives in the same union. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const simplified = simplifyUnion([ | ||
| * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }), | ||
| * createSchema({ type: 'string' }), | ||
| * ]) | ||
| * // keeps only string member | ||
| * ``` | ||
| */ | ||
| function simplifyUnion(members) { | ||
| const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type)); | ||
| if (!scalarPrimitives.size) return members; | ||
| return members.filter((member) => { | ||
| const enumNode = narrowSchema(member, "enum"); | ||
| if (!enumNode) return true; | ||
| const primitive = enumNode.primitive; | ||
| if (!primitive) return true; | ||
| if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true; | ||
| if (scalarPrimitives.has(primitive)) return false; | ||
| if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false; | ||
| return true; | ||
| }); | ||
| } | ||
| function setEnumName(propNode, parentName, propName, enumSuffix) { | ||
| const enumNode = narrowSchema(propNode, "enum"); | ||
| if (enumNode?.primitive === "boolean") return { | ||
| ...propNode, | ||
| name: null | ||
| }; | ||
| if (enumNode) return { | ||
| ...propNode, | ||
| name: enumPropName(parentName, propName, enumSuffix) | ||
| }; | ||
| return propNode; | ||
| } | ||
| /** | ||
| * Merges a ref node with its resolved schema, giving usage-site fields precedence. | ||
| * | ||
| * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override | ||
| * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ref with description override | ||
| * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' }) | ||
| * const merged = syncSchemaRef(ref) // merges with resolved Pet schema | ||
| * ``` | ||
| */ | ||
| function syncSchemaRef(node) { | ||
| const ref = narrowSchema(node, "ref"); | ||
| if (!ref) return node; | ||
| if (!ref.schema) return node; | ||
| const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref; | ||
| const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0)); | ||
| return require_response.createSchema({ | ||
| ...ref.schema, | ||
| ...definedOverrides | ||
| }); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/strings.ts | ||
| /** | ||
| * Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`. | ||
| * Returns the string unchanged when no balanced quote pair is found. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * trimQuotes('"hello"') // 'hello' | ||
| * trimQuotes('hello') // 'hello' | ||
| * ``` | ||
| */ | ||
| function trimQuotes(text) { | ||
| if (text.length >= 2) { | ||
| const first = text[0]; | ||
| const last = text[text.length - 1]; | ||
| if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1); | ||
| } | ||
| return text; | ||
| } | ||
| /** | ||
| * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first. | ||
| * | ||
| * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated | ||
| * code matches the repo style without a formatter. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * stringify('hello') // "'hello'" | ||
| * stringify('"hello"') // "'hello'" | ||
| * ``` | ||
| */ | ||
| function stringify(value) { | ||
| if (value === void 0 || value === null) return "''"; | ||
| return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`; | ||
| } | ||
| /** | ||
| * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes, | ||
| * and the Unicode line terminators U+2028 and U+2029. | ||
| * | ||
| * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4 | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye' | ||
| * ``` | ||
| */ | ||
| function jsStringEscape(input) { | ||
| return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => { | ||
| switch (character) { | ||
| case "\"": | ||
| case "'": | ||
| case "\\": return `\\${character}`; | ||
| case "\n": return "\\n"; | ||
| case "\r": return "\\r"; | ||
| case "\u2028": return "\\u2028"; | ||
| case "\u2029": return "\\u2029"; | ||
| default: return ""; | ||
| } | ||
| }); | ||
| } | ||
| /** | ||
| * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string. | ||
| * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression. | ||
| * Pass `null` as the second argument to emit a `/pattern/flags` literal instead. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")' | ||
| * toRegExpString('^(?im)foo', null) // '/^foo/im' | ||
| * ``` | ||
| */ | ||
| function toRegExpString(text, func = "RegExp") { | ||
| const raw = trimQuotes(text); | ||
| const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i); | ||
| const replacementTarget = match?.[1] ?? ""; | ||
| const matchedFlags = match?.[2]; | ||
| const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, ""); | ||
| const { source, flags } = new RegExp(cleaned, matchedFlags); | ||
| if (func === null) return `/${source}/${flags}`; | ||
| return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`; | ||
| } | ||
| /** | ||
| * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested | ||
| * objects recurse with fixed indentation, so the result drops straight into an object literal | ||
| * without re-parsing. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * stringifyObject({ foo: 'bar', nested: { a: 1 } }) | ||
| * // 'foo: bar,\nnested: {\n a: 1\n }' | ||
| * ``` | ||
| */ | ||
| function stringifyObject(value) { | ||
| return Object.entries(value).map(([key, val]) => { | ||
| if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`; | ||
| return `${key}: ${val}`; | ||
| }).filter(Boolean).join(",\n"); | ||
| } | ||
| /** | ||
| * Renders a dotted path or string array as an optional-chaining accessor expression rooted at | ||
| * `accessor`. Returns `null` for an empty path. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * getNestedAccessor('pagination.next.id', 'lastPage') | ||
| * // "lastPage?.['pagination']?.['next']?.['id']" | ||
| * ``` | ||
| */ | ||
| function getNestedAccessor(param, accessor) { | ||
| const parts = Array.isArray(param) ? param : param.split("."); | ||
| if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null; | ||
| return `${accessor}?.['${`${parts.join("']?.['")}']`}`; | ||
| } | ||
| //#endregion | ||
| //#region src/registry.ts | ||
| /** | ||
| * Every node definition. Adding a node means adding its `defineNode` to one | ||
| * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it. | ||
| */ | ||
| const nodeDefs = [ | ||
| require_response.inputDef, | ||
| require_response.outputDef, | ||
| require_response.operationDef, | ||
| require_response.requestBodyDef, | ||
| require_response.contentDef, | ||
| require_response.responseDef, | ||
| require_response.schemaDef, | ||
| require_response.propertyDef, | ||
| require_response.parameterDef, | ||
| require_response.functionParameterDef, | ||
| require_response.functionParametersDef, | ||
| require_response.typeLiteralDef, | ||
| require_response.indexedAccessTypeDef, | ||
| require_response.objectBindingPatternDef, | ||
| require_response.constDef, | ||
| require_response.typeDef, | ||
| require_response.functionDef, | ||
| require_response.arrowFunctionDef, | ||
| require_response.textDef, | ||
| require_response.breakDef, | ||
| require_response.jsxDef, | ||
| require_response.importDef, | ||
| require_response.exportDef, | ||
| require_response.sourceDef, | ||
| require_response.fileDef | ||
| ]; | ||
| //#endregion | ||
| //#region src/visitor.ts | ||
| /** | ||
| * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`). | ||
| * Derived from each definition's `children`. | ||
| */ | ||
| const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : [])); | ||
| /** | ||
| * Maps a node kind to the matching visitor callback name. Derived from each | ||
| * definition's `visitorKey`. | ||
| */ | ||
| const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : [])); | ||
| /** | ||
| * Per-kind builders rerun after children are rebuilt. Derived from each | ||
| * definition's `rebuild` flag. | ||
| */ | ||
| const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : [])); | ||
| /** | ||
| * Creates a small async concurrency limiter. | ||
| * | ||
| * At most `concurrency` tasks are in flight at once. Extra tasks are queued. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const limit = createLimit(2) | ||
| * for (const task of [taskA, taskB, taskC]) { | ||
| * await limit(() => task()) | ||
| * } | ||
| * // only 2 tasks run at the same time | ||
| * ``` | ||
| */ | ||
| function createLimit(concurrency) { | ||
| let active = 0; | ||
| const queue = []; | ||
| function next() { | ||
| if (active < concurrency && queue.length > 0) { | ||
| active++; | ||
| queue.shift()(); | ||
| } | ||
| } | ||
| return function limit(fn) { | ||
| return new Promise((resolve, reject) => { | ||
| queue.push(() => { | ||
| Promise.resolve(fn()).then(resolve, reject).finally(() => { | ||
| active--; | ||
| next(); | ||
| }); | ||
| }); | ||
| next(); | ||
| }); | ||
| }; | ||
| } | ||
| const visitorKeysByKind = VISITOR_KEYS; | ||
| /** | ||
| * Returns `true` when `value` is an AST node (an object carrying a `kind`). | ||
| */ | ||
| function isNode(value) { | ||
| return typeof value === "object" && value !== null && "kind" in value; | ||
| } | ||
| /** | ||
| * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}. | ||
| * | ||
| * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const children = getChildren(operationNode, true) | ||
| * // returns parameters, the request body, and responses | ||
| * ``` | ||
| */ | ||
| function* getChildren(node, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return; | ||
| const record = node; | ||
| for (const key of keys) { | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) if (isNode(item)) yield item; | ||
| } else if (isNode(value)) yield value; | ||
| } | ||
| } | ||
| /** | ||
| * Runs the visitor callback that matches `node.kind` with the traversal | ||
| * context. The result is a replacement node, a collected value, or `undefined` | ||
| * when no callback is registered for the kind. | ||
| * | ||
| * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives | ||
| * in one place. `TResult` is the caller's expected return: the same node type | ||
| * for `transform`, the collected value type for `collectLazy`, ignored for `walk`. | ||
| */ | ||
| function applyVisitor(node, visitor, parent) { | ||
| const key = VISITOR_KEY_BY_KIND[node.kind]; | ||
| if (!key) return void 0; | ||
| const fn = visitor[key]; | ||
| return fn?.(node, { parent }); | ||
| } | ||
| /** | ||
| * Async depth-first traversal for side effects. Visitor return values are | ||
| * ignored. Use `transform` when you want to rewrite nodes. | ||
| * | ||
| * Sibling nodes at each depth run concurrently up to `options.concurrency` | ||
| * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor | ||
| * work. Lower values reduce memory pressure. | ||
| * | ||
| * @example Log every operation | ||
| * ```ts | ||
| * await walk(root, { | ||
| * operation(node) { | ||
| * console.log(node.operationId) | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @example Only visit the root node | ||
| * ```ts | ||
| * await walk(root, { depth: 'shallow', input: () => {} }) | ||
| * ``` | ||
| */ | ||
| async function walk(node, options) { | ||
| return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0); | ||
| } | ||
| async function _walk(node, visitor, recurse, limit, parent) { | ||
| await limit(() => applyVisitor(node, visitor, parent)); | ||
| const children = Array.from(getChildren(node, recurse)); | ||
| if (children.length === 0) return; | ||
| await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node))); | ||
| } | ||
| function transform(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse); | ||
| if (rebuilt === node) return node; | ||
| const rebuild = nodeRebuilders[rebuilt.kind]; | ||
| return rebuild ? rebuild(rebuilt) : rebuilt; | ||
| } | ||
| /** | ||
| * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming | ||
| * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact. | ||
| * `Schema` children are skipped in shallow mode. | ||
| */ | ||
| function transformChildren(node, options, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return node; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return node; | ||
| const record = node; | ||
| const childOptions = { | ||
| ...options, | ||
| parent: node | ||
| }; | ||
| let updates; | ||
| for (const key of keys) { | ||
| if (!(key in record)) continue; | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| let changed = false; | ||
| const mapped = value.map((item) => { | ||
| if (!isNode(item)) return item; | ||
| const next = transform(item, childOptions); | ||
| if (next !== item) changed = true; | ||
| return next; | ||
| }); | ||
| if (changed) (updates ??= {})[key] = mapped; | ||
| } else if (isNode(value)) { | ||
| const next = transform(value, childOptions); | ||
| if (next !== value) (updates ??= {})[key] = next; | ||
| } | ||
| } | ||
| return updates ? { | ||
| ...node, | ||
| ...updates | ||
| } : node; | ||
| } | ||
| /** | ||
| * Lazy depth-first collection pass. Yields every non-null value returned by | ||
| * the visitor callbacks. Use `collect` for the eager array form. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids: string[] = [] | ||
| * for (const id of collectLazy<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * })) { | ||
| * ids.push(id) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function* collectLazy(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const v = applyVisitor(node, visitor, parent); | ||
| if (v != null) yield v; | ||
| for (const child of getChildren(node, recurse)) yield* collectLazy(child, { | ||
| ...options, | ||
| parent: node | ||
| }); | ||
| } | ||
| /** | ||
| * Eager depth-first collection pass. Gathers every non-null value the visitor | ||
| * callbacks return into an array. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids = collect<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| */ | ||
| function collect(node, options) { | ||
| return Array.from(collectLazy(node, options)); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/schemaGraph.ts | ||
| /** | ||
| * Collects every named schema referenced transitively from a node through its ref edges. | ||
| * | ||
| * Refs are followed by name only, so the resolved `node.schema` is never traversed inline. | ||
| * | ||
| * @example Collect refs from a single schema | ||
| * ```ts | ||
| * const names = collectReferencedSchemaNames(petSchema) | ||
| * // → Set { 'Category', 'Tag' } | ||
| * ``` | ||
| * | ||
| * @example Accumulate refs from multiple schemas into one set | ||
| * ```ts | ||
| * const out = new Set<string>() | ||
| * for (const schema of schemas) { | ||
| * collectReferencedSchemaNames(schema, out) | ||
| * } | ||
| * ``` | ||
| */ | ||
| const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => { | ||
| const refs = /* @__PURE__ */ new Set(); | ||
| collect(node, { schema(child) { | ||
| if (child.type === "ref") { | ||
| const name = resolveRefName(child); | ||
| if (name) refs.add(name); | ||
| } | ||
| } }); | ||
| return refs; | ||
| }); | ||
| function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) { | ||
| if (!node) return out; | ||
| for (const name of collectSchemaRefs(node)) out.add(name); | ||
| return out; | ||
| } | ||
| /** | ||
| * Collects the names of all top-level schemas transitively used by a set of operations. | ||
| * | ||
| * An operation uses a schema when its parameters, request body, or responses reference it, directly | ||
| * or through other named schemas. The walk is iterative, so reference cycles are safe. | ||
| * | ||
| * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated. | ||
| * | ||
| * @example Only generate schemas referenced by included operations | ||
| * ```ts | ||
| * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null) | ||
| * const allowed = collectUsedSchemaNames(includedOps, schemas) | ||
| * | ||
| * for (const schema of schemas) { | ||
| * if (schema.name && !allowed.has(schema.name)) continue | ||
| * // … generate schema | ||
| * } | ||
| * ``` | ||
| */ | ||
| const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas))); | ||
| function computeUsedSchemaNames(operations, schemas) { | ||
| const schemaMap = /* @__PURE__ */ new Map(); | ||
| for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema); | ||
| const result = /* @__PURE__ */ new Set(); | ||
| function visitSchema(schema) { | ||
| const directRefs = collectReferencedSchemaNames(schema); | ||
| for (const name of directRefs) if (!result.has(name)) { | ||
| result.add(name); | ||
| const namedSchema = schemaMap.get(name); | ||
| if (namedSchema) visitSchema(namedSchema); | ||
| } | ||
| } | ||
| for (const op of operations) for (const schema of collectLazy(op, { | ||
| depth: "shallow", | ||
| schema: (node) => node | ||
| })) visitSchema(schema); | ||
| return result; | ||
| } | ||
| function collectUsedSchemaNames(operations, schemas) { | ||
| return collectUsedSchemaNamesMemo(operations)(schemas); | ||
| } | ||
| const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set(); | ||
| const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => { | ||
| const graph = /* @__PURE__ */ new Map(); | ||
| for (const schema of schemas) { | ||
| if (!schema.name) continue; | ||
| graph.set(schema.name, collectReferencedSchemaNames(schema)); | ||
| } | ||
| const circular = /* @__PURE__ */ new Set(); | ||
| for (const start of graph.keys()) { | ||
| const visited = /* @__PURE__ */ new Set(); | ||
| const stack = [...graph.get(start) ?? []]; | ||
| while (stack.length > 0) { | ||
| const node = stack.pop(); | ||
| if (node === start) { | ||
| circular.add(start); | ||
| break; | ||
| } | ||
| if (visited.has(node)) continue; | ||
| visited.add(node); | ||
| const next = graph.get(node); | ||
| if (next) for (const r of next) stack.push(r); | ||
| } | ||
| } | ||
| return circular; | ||
| }); | ||
| /** | ||
| * Finds every schema that takes part in a circular dependency chain, including direct self-loops. | ||
| * | ||
| * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so | ||
| * the generated code does not recurse forever. Refs are followed by name only, so the walk stays | ||
| * linear in the size of the schema graph. | ||
| * | ||
| * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`. | ||
| */ | ||
| function findCircularSchemas(schemas) { | ||
| if (schemas.length === 0) return EMPTY_CIRCULAR_SET; | ||
| return findCircularSchemasMemo(schemas); | ||
| } | ||
| /** | ||
| * Returns `true` when a schema, or anything nested inside it, references a circular schema. | ||
| * | ||
| * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled | ||
| * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go. | ||
| * | ||
| * @note Stops at the first matching circular ref. | ||
| */ | ||
| function containsCircularRef(node, { circularSchemas, excludeName }) { | ||
| if (!node || circularSchemas.size === 0) return false; | ||
| for (const _ of collectLazy(node, { schema(child) { | ||
| if (child.type !== "ref") return null; | ||
| const name = resolveRefName(child); | ||
| return name && name !== excludeName && circularSchemas.has(name) ? true : null; | ||
| } })) return true; | ||
| return false; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/schemaTraversal.ts | ||
| /** | ||
| * Maps each property of an object schema to its transformed output. Pairs every result with the | ||
| * original property so the printer keeps full control over modifiers, getters, and key syntax. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const entries = mapSchemaProperties(node, (schema) => this.transform(schema)) | ||
| * // entries: [{ name: 'id', property, output: 'z.number()' }, ...] | ||
| * ``` | ||
| */ | ||
| function mapSchemaProperties(node, transform) { | ||
| return node.properties.map((property) => ({ | ||
| name: property.name, | ||
| property, | ||
| output: transform(property.schema) | ||
| })); | ||
| } | ||
| /** | ||
| * Maps each member of a union or intersection schema to its transformed output, pairing every | ||
| * result with the original member. | ||
| */ | ||
| function mapSchemaMembers(node, transform) { | ||
| return (node.members ?? []).map((schema) => ({ | ||
| schema, | ||
| output: transform(schema) | ||
| })); | ||
| } | ||
| /** | ||
| * Maps each item of an array or tuple schema to its transformed output, pairing every result with | ||
| * the original item. | ||
| */ | ||
| function mapSchemaItems(node, transform) { | ||
| return (node.items ?? []).map((schema) => ({ | ||
| schema, | ||
| output: transform(schema) | ||
| })); | ||
| } | ||
| /** | ||
| * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key | ||
| * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation | ||
| * of a recursive schema until first access. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' }) | ||
| * // "get parent() { return z.lazy(() => Pet) }" | ||
| * ``` | ||
| */ | ||
| function lazyGetter({ name, body }) { | ||
| return `get ${objectKey(name)}() { return ${body} }`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/operationParams.ts | ||
| /** | ||
| * Applies casing rules to parameter names and returns a new array without mutating the input. | ||
| * | ||
| * Run it before handing parameters to schema builders so output property keys get the right casing | ||
| * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the | ||
| * original array is returned unchanged. | ||
| */ | ||
| const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => { | ||
| const transformed = casing === "camelcase" || !isValidVarName(param.name) ? require_response.camelCase(param.name) : param.name; | ||
| return { | ||
| ...param, | ||
| name: transformed | ||
| }; | ||
| }))); | ||
| function caseParams(params, casing) { | ||
| if (!casing) return params; | ||
| return caseParamsMemo(params)(casing); | ||
| } | ||
| /** | ||
| * Resolves the {@link TypeExpression} for an individual parameter. | ||
| * | ||
| * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the | ||
| * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like | ||
| * `GroupParams['petId']`, otherwise the resolved individual name. | ||
| */ | ||
| function resolveParamType({ node, param, resolver }) { | ||
| if (!resolver) return param.schema.primitive ?? "unknown"; | ||
| const individualName = resolver.resolveParamName(node, param); | ||
| const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0; | ||
| const groupResolvers = { | ||
| path: resolver.resolvePathParamsName, | ||
| query: resolver.resolveQueryParamsName, | ||
| header: resolver.resolveHeaderParamsName | ||
| }; | ||
| const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0; | ||
| if (groupName && groupName !== individualName) return require_response.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(require_response.createFunctionParameter({ | ||
| properties: children, | ||
| default: emptyObjectDefault(children) | ||
| })); | ||
| } else { | ||
| if (pathParamsType === "inlineSpread" && pathParams.length) { | ||
| const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]); | ||
| params.push(require_response.createFunctionParameter({ | ||
| name: pathName, | ||
| type: spreadType ? wrapType(spreadType) : void 0, | ||
| rest: true | ||
| })); | ||
| } else if (pathParamsType === "inline") params.push(...pathParams.map((p) => require_response.createFunctionParameter(toProperty(p)))); | ||
| else if (pathParams.length) { | ||
| const pathChildren = pathParams.map(toProperty); | ||
| params.push(require_response.createFunctionParameter({ | ||
| properties: pathChildren, | ||
| default: pathParamsDefault ?? emptyObjectDefault(pathChildren) | ||
| })); | ||
| } | ||
| params.push(...bodyProperty.map((p) => require_response.createFunctionParameter(p))); | ||
| params.push(...trailingGroups.flatMap(buildGroupParam)); | ||
| } | ||
| params.push(...extraParams); | ||
| return require_response.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) => require_response.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 require_response.createTypeLiteral({ members: params.map((p) => ({ | ||
| name: p.name, | ||
| type: resolveParamType({ | ||
| node, | ||
| param: p, | ||
| resolver | ||
| }), | ||
| optional: !p.required | ||
| })) }); | ||
| } | ||
| //#endregion | ||
| Object.defineProperty(exports, "buildGroupParam", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildGroupParam; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildJSDoc", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildJSDoc; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildList", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildList; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildTypeLiteral", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildTypeLiteral; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "caseParams", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return caseParams; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "childName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return childName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "collect", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return collect; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "collectLazy", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return collectLazy; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "collectUsedSchemaNames", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return collectUsedSchemaNames; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "containsCircularRef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return containsCircularRef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOperationParams", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createOperationParams; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "enumPropName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return enumPropName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "extractRefName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return extractRefName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "findCircularSchemas", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return findCircularSchemas; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "getNestedAccessor", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return getNestedAccessor; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "httpMethods", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return httpMethods; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isHttpOperationNode", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isHttpOperationNode; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isStringType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isStringType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isValidVarName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isValidVarName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "jsStringEscape", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return jsStringEscape; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "lazyGetter", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return lazyGetter; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "mapSchemaItems", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return mapSchemaItems; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "mapSchemaMembers", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return mapSchemaMembers; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "mapSchemaProperties", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return mapSchemaProperties; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "mergeAdjacentObjectsLazy", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return mergeAdjacentObjectsLazy; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "narrowSchema", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return narrowSchema; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "nodeDefs", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return nodeDefs; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "objectKey", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return objectKey; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "resolveGroupType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return resolveGroupType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "resolveParamType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return resolveParamType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "resolveRefName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return resolveRefName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "schemaTypes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return schemaTypes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "setDiscriminatorEnum", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return setDiscriminatorEnum; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "setEnumName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return setEnumName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "simplifyUnion", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return simplifyUnion; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringify", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return stringify; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringifyObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return stringifyObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "syncSchemaRef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return syncSchemaRef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "toRegExpString", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return toRegExpString; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "transform", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return transform; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "trimQuotes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return trimQuotes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "walk", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return walk; | ||
| } | ||
| }); | ||
| //# sourceMappingURL=utils-D83JA6Xx.cjs.map |
Sorry, the diff of this file is too big to display
| import "./chunk-CNktS9qV.js"; | ||
| import { A as contentDef, D as fileDef, E as exportDef, G as typeDef, H as functionDef, K as createProperty, M as arrowFunctionDef, N as breakDef, O as importDef, P as constDef, Q as schemaDef, S as typeLiteralDef, U as jsxDef, W as textDef, X as pascalCase, Y as camelCase, Z as createSchema, _ as createTypeLiteral, b as indexedAccessTypeDef, c as operationDef, f as inputDef, h as createIndexedAccessType, i as parameterDef, k as sourceDef, m as createFunctionParameters, n as responseDef, o as outputDef, p as createFunctionParameter, q as propertyDef, u as requestBodyDef, v as functionParameterDef, x as objectBindingPatternDef, y as functionParametersDef } from "./response-DKxTr522.js"; | ||
| //#region src/constants.ts | ||
| const visitorDepths = { | ||
| shallow: "shallow", | ||
| deep: "deep" | ||
| }; | ||
| /** | ||
| * Schema type discriminators used by all AST schema nodes. | ||
| * | ||
| * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`). | ||
| * Call `isScalarPrimitive()` to check for the scalar types. | ||
| */ | ||
| const schemaTypes = { | ||
| /** | ||
| * Text value. | ||
| */ | ||
| string: "string", | ||
| /** | ||
| * Floating-point number (`float`, `double`). | ||
| */ | ||
| number: "number", | ||
| /** | ||
| * Whole number (`int32`). Use `bigint` for `int64`. | ||
| */ | ||
| integer: "integer", | ||
| /** | ||
| * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`. | ||
| */ | ||
| bigint: "bigint", | ||
| /** | ||
| * Boolean value. | ||
| */ | ||
| boolean: "boolean", | ||
| /** | ||
| * Explicit null value. | ||
| */ | ||
| null: "null", | ||
| /** | ||
| * Any value (no type restriction). | ||
| */ | ||
| any: "any", | ||
| /** | ||
| * Unknown value (must be narrowed before usage). | ||
| */ | ||
| unknown: "unknown", | ||
| /** | ||
| * No return value (`void`). | ||
| */ | ||
| void: "void", | ||
| /** | ||
| * Object with named properties. | ||
| */ | ||
| object: "object", | ||
| /** | ||
| * Sequential list of items. | ||
| */ | ||
| array: "array", | ||
| /** | ||
| * Fixed-length list with position-specific items. | ||
| */ | ||
| tuple: "tuple", | ||
| /** | ||
| * "One of" multiple schema members. | ||
| */ | ||
| union: "union", | ||
| /** | ||
| * "All of" multiple schema members. | ||
| */ | ||
| intersection: "intersection", | ||
| /** | ||
| * Enum schema. | ||
| */ | ||
| enum: "enum", | ||
| /** | ||
| * Reference to another schema. | ||
| */ | ||
| ref: "ref", | ||
| /** | ||
| * Calendar date (for example `2026-03-24`). | ||
| */ | ||
| date: "date", | ||
| /** | ||
| * Date-time value (for example `2026-03-24T09:00:00Z`). | ||
| */ | ||
| datetime: "datetime", | ||
| /** | ||
| * Time-only value (for example `09:00:00`). | ||
| */ | ||
| time: "time", | ||
| /** | ||
| * UUID value. | ||
| */ | ||
| uuid: "uuid", | ||
| /** | ||
| * Email address value. | ||
| */ | ||
| email: "email", | ||
| /** | ||
| * URL value. | ||
| */ | ||
| url: "url", | ||
| /** | ||
| * IPv4 address value. | ||
| */ | ||
| ipv4: "ipv4", | ||
| /** | ||
| * IPv6 address value. | ||
| */ | ||
| ipv6: "ipv6", | ||
| /** | ||
| * Binary/blob value. | ||
| */ | ||
| blob: "blob", | ||
| /** | ||
| * Impossible value (`never`). | ||
| */ | ||
| never: "never" | ||
| }; | ||
| /** | ||
| * Scalar primitive schema types used for union simplification and type narrowing. | ||
| */ | ||
| const SCALAR_PRIMITIVE_TYPES = new Set([ | ||
| "string", | ||
| "number", | ||
| "integer", | ||
| "bigint", | ||
| "boolean" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `type` is a scalar primitive that can be assigned without wrapping | ||
| * (for example `string | number | boolean`). | ||
| */ | ||
| function isScalarPrimitive(type) { | ||
| return SCALAR_PRIMITIVE_TYPES.has(type); | ||
| } | ||
| /** | ||
| * HTTP method identifiers used by operation nodes. | ||
| */ | ||
| const httpMethods = { | ||
| get: "GET", | ||
| post: "POST", | ||
| put: "PUT", | ||
| patch: "PATCH", | ||
| delete: "DELETE", | ||
| head: "HEAD", | ||
| options: "OPTIONS", | ||
| trace: "TRACE" | ||
| }; | ||
| /** | ||
| * One indentation level, derived from {@link INDENT_SIZE}. | ||
| */ | ||
| const INDENT = Array.from({ length: 2 }, () => " ").join(""); | ||
| //#endregion | ||
| //#region ../../internals/utils/src/promise.ts | ||
| /** | ||
| * Wraps `factory` with a keyed cache backed by the provided store. | ||
| * | ||
| * Pass a `WeakMap` for object keys (results are GC-eligible when the key is | ||
| * collected) or a `Map` for primitive keys. For multi-argument functions, | ||
| * nest two `memoize` calls — the outer keyed by the first argument, the | ||
| * inner (created once per outer miss) keyed by the second. | ||
| * | ||
| * Because the cache is owned by the caller, it can be shared, inspected, or | ||
| * cleared independently of the memoized function. | ||
| * | ||
| * @example Single WeakMap key | ||
| * ```ts | ||
| * const cache = new WeakMap<SchemaNode, Set<string>>() | ||
| * const getRefs = memoize(cache, (node) => collectRefs(node)) | ||
| * ``` | ||
| * | ||
| * @example Single Map key (primitive) | ||
| * ```ts | ||
| * const cache = new Map<string, Resolver>() | ||
| * const getResolver = memoize(cache, (name) => buildResolver(name)) | ||
| * ``` | ||
| * | ||
| * @example Two-level (object + primitive) | ||
| * ```ts | ||
| * const outer = new WeakMap<Params[], Map<string, Params[]>>() | ||
| * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key))) | ||
| * fn(params)('camelcase') | ||
| * ``` | ||
| */ | ||
| function memoize(store, factory) { | ||
| return (key) => { | ||
| if (store.has(key)) return store.get(key); | ||
| const value = factory(key); | ||
| store.set(key, value); | ||
| return value; | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/reserved.ts | ||
| /** | ||
| * JavaScript and Java reserved words. | ||
| * @link https://github.com/jonschlinkert/reserved/blob/master/index.js | ||
| */ | ||
| const reservedWords = new Set([ | ||
| "abstract", | ||
| "arguments", | ||
| "boolean", | ||
| "break", | ||
| "byte", | ||
| "case", | ||
| "catch", | ||
| "char", | ||
| "class", | ||
| "const", | ||
| "continue", | ||
| "debugger", | ||
| "default", | ||
| "delete", | ||
| "do", | ||
| "double", | ||
| "else", | ||
| "enum", | ||
| "eval", | ||
| "export", | ||
| "extends", | ||
| "false", | ||
| "final", | ||
| "finally", | ||
| "float", | ||
| "for", | ||
| "function", | ||
| "goto", | ||
| "if", | ||
| "implements", | ||
| "import", | ||
| "in", | ||
| "instanceof", | ||
| "int", | ||
| "interface", | ||
| "let", | ||
| "long", | ||
| "native", | ||
| "new", | ||
| "null", | ||
| "package", | ||
| "private", | ||
| "protected", | ||
| "public", | ||
| "return", | ||
| "short", | ||
| "static", | ||
| "super", | ||
| "switch", | ||
| "synchronized", | ||
| "this", | ||
| "throw", | ||
| "throws", | ||
| "transient", | ||
| "true", | ||
| "try", | ||
| "typeof", | ||
| "var", | ||
| "void", | ||
| "volatile", | ||
| "while", | ||
| "with", | ||
| "yield", | ||
| "Array", | ||
| "Date", | ||
| "hasOwnProperty", | ||
| "Infinity", | ||
| "isFinite", | ||
| "isNaN", | ||
| "isPrototypeOf", | ||
| "length", | ||
| "Math", | ||
| "name", | ||
| "NaN", | ||
| "Number", | ||
| "Object", | ||
| "prototype", | ||
| "String", | ||
| "toString", | ||
| "undefined", | ||
| "valueOf" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `name` is a syntactically valid JavaScript variable name. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * isValidVarName('status') // true | ||
| * isValidVarName('class') // false (reserved word) | ||
| * isValidVarName('42foo') // false (starts with digit) | ||
| * ``` | ||
| */ | ||
| function isValidVarName(name) { | ||
| if (!name || reservedWords.has(name)) return false; | ||
| return isIdentifier(name); | ||
| } | ||
| /** | ||
| * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words. | ||
| * | ||
| * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys | ||
| * even though they are not valid variable names, so use this (not {@link isValidVarName}) when | ||
| * deciding whether an object key needs quoting. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * isIdentifier('name') // true | ||
| * isIdentifier('x-total')// false | ||
| * ``` | ||
| */ | ||
| function isIdentifier(name) { | ||
| return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name); | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/string.ts | ||
| /** | ||
| * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping | ||
| * any backslash or single quote in the content. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * singleQuote('foo') // "'foo'" | ||
| * singleQuote("o'clock") // "'o\\'clock'" | ||
| * ``` | ||
| */ | ||
| function singleQuote(value) { | ||
| if (value === void 0 || value === null) return "''"; | ||
| return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/codegen.ts | ||
| /** | ||
| * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no | ||
| * comments so callers always get a usable string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildJSDoc(['@type string', '@example hello']) | ||
| * // '/**\n * @type string\n * @example hello\n *\/\n ' | ||
| * ``` | ||
| */ | ||
| function buildJSDoc(comments, options = {}) { | ||
| const { indent = " * ", suffix = "\n ", fallback = " " } = options; | ||
| if (comments.length === 0) return fallback; | ||
| return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`; | ||
| } | ||
| /** | ||
| * Indents every non-empty line of `text` by one indent level, leaving blank lines empty. | ||
| */ | ||
| function indentLines(text) { | ||
| if (!text) return ""; | ||
| return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n"); | ||
| } | ||
| /** | ||
| * Renders an object key, quoting it with single quotes only when it is not a valid identifier. | ||
| * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * objectKey('name') // 'name' | ||
| * objectKey('x-total') // "'x-total'" | ||
| * ``` | ||
| */ | ||
| function objectKey(name) { | ||
| return isIdentifier(name) ? name : singleQuote(name); | ||
| } | ||
| /** | ||
| * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one | ||
| * level and closing the brace at column zero. Nested objects built the same way indent cumulatively, | ||
| * so callers never re-parse the generated code. A trailing comma is added per entry to match the | ||
| * formatter's multi-line style. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildObject(['id: z.number()', 'name: z.string()']) | ||
| * // '{\n id: z.number(),\n name: z.string(),\n}' | ||
| * ``` | ||
| */ | ||
| function buildObject(entries) { | ||
| if (entries.length === 0) return "{}"; | ||
| return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`; | ||
| } | ||
| /** | ||
| * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on | ||
| * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented | ||
| * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`, | ||
| * `z.array([…])`, and similar member lists so objects inside them nest correctly. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildList(['z.string()', 'z.number()']) | ||
| * // '[z.string(), z.number()]' | ||
| * ``` | ||
| */ | ||
| function buildList(items, brackets = ["[", "]"]) { | ||
| const [open, close] = brackets; | ||
| if (items.length === 0) return `${open}${close}`; | ||
| if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`; | ||
| return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`; | ||
| } | ||
| //#endregion | ||
| //#region src/guards.ts | ||
| /** | ||
| * Narrows a `SchemaNode` to the variant that matches `type`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ type: 'string' }) | ||
| * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null | ||
| * ``` | ||
| */ | ||
| function narrowSchema(node, type) { | ||
| return node?.type === type ? node : null; | ||
| } | ||
| /** | ||
| * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * if (isHttpOperationNode(node)) { | ||
| * console.log(node.method, node.path) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function isHttpOperationNode(node) { | ||
| return node.protocol === "http" || node.method !== void 0 && node.path !== void 0; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/refs.ts | ||
| const plainStringTypes = new Set([ | ||
| "string", | ||
| "uuid", | ||
| "email", | ||
| "url", | ||
| "datetime" | ||
| ]); | ||
| /** | ||
| * Returns the last path segment of a reference string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * extractRefName('#/components/schemas/Pet') // 'Pet' | ||
| * ``` | ||
| */ | ||
| function extractRefName(ref) { | ||
| return ref.split("/").at(-1) ?? ref; | ||
| } | ||
| /** | ||
| * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`. | ||
| * | ||
| * Returns `null` for non-ref nodes or when no name resolves. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) | ||
| * // => 'Pet' | ||
| * ``` | ||
| */ | ||
| function resolveRefName(node) { | ||
| if (!node || node.type !== "ref") return null; | ||
| if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null; | ||
| return node.name ?? node.schema?.name ?? null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase child schema name by joining a parent name and property name. | ||
| * Returns `null` when there is no parent to nest under. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * childName('Order', 'shipping_address') // 'OrderShippingAddress' | ||
| * childName(undefined, 'params') // null | ||
| * ``` | ||
| */ | ||
| function childName(parentName, propName) { | ||
| return parentName ? pascalCase([parentName, propName].join(" ")) : null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any | ||
| * empty parts. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum' | ||
| * ``` | ||
| */ | ||
| function enumPropName(parentName, propName, enumSuffix) { | ||
| return pascalCase([ | ||
| parentName, | ||
| propName, | ||
| enumSuffix | ||
| ].filter(Boolean).join(" ")); | ||
| } | ||
| /** | ||
| * Type guard that returns `true` when a schema emits as a plain `string` type. | ||
| * | ||
| * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time` | ||
| * types, returns `true` only when `representation` is `'string'` rather than `'date'`. | ||
| */ | ||
| function isStringType(node) { | ||
| if (plainStringTypes.has(node.type)) return true; | ||
| const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time"); | ||
| if (temporal) return temporal.representation !== "date"; | ||
| return false; | ||
| } | ||
| /** | ||
| * Derives a {@link ParamGroupType} for a query or header group from the resolver. | ||
| * | ||
| * Returns `null` when there is no resolver, no params, or the group name equals the | ||
| * individual param name (so there is no real group to emit). | ||
| */ | ||
| function resolveGroupType({ node, params, group, resolver }) { | ||
| if (!resolver || !params.length) return null; | ||
| const firstParam = params[0]; | ||
| const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam); | ||
| if (groupName === resolver.resolveParamName(node, firstParam)) return null; | ||
| return { | ||
| type: groupName, | ||
| optional: params.every((p) => !p.required) | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/transformers.ts | ||
| /** | ||
| * Replaces a discriminator property's schema with a string enum of allowed values. | ||
| * | ||
| * If `node` is not an object schema, or if the property does not exist, the input | ||
| * node is returned as-is. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ | ||
| * type: 'object', | ||
| * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })], | ||
| * }) | ||
| * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] }) | ||
| * ``` | ||
| */ | ||
| function setDiscriminatorEnum({ node, propertyName, values, enumName }) { | ||
| const objectNode = narrowSchema(node, "object"); | ||
| if (!objectNode?.properties?.length) return node; | ||
| if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node; | ||
| return createSchema({ | ||
| ...objectNode, | ||
| properties: objectNode.properties.map((prop) => { | ||
| if (prop.name !== propertyName) return prop; | ||
| return createProperty({ | ||
| ...prop, | ||
| schema: createSchema({ | ||
| type: "enum", | ||
| primitive: "string", | ||
| enumValues: values, | ||
| name: enumName, | ||
| readOnly: prop.schema.readOnly, | ||
| writeOnly: prop.schema.writeOnly | ||
| }) | ||
| }); | ||
| }) | ||
| }); | ||
| } | ||
| /** | ||
| * Merges adjacent anonymous object members into a single anonymous object member. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const merged = mergeAdjacentObjects([ | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }), | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }), | ||
| * ]) | ||
| * ``` | ||
| */ | ||
| function* mergeAdjacentObjectsLazy(members) { | ||
| let acc; | ||
| for (const member of members) { | ||
| const objectMember = narrowSchema(member, "object"); | ||
| if (objectMember && !objectMember.name && acc !== void 0) { | ||
| const accObject = narrowSchema(acc, "object"); | ||
| if (accObject && !accObject.name) { | ||
| acc = createSchema({ | ||
| ...accObject, | ||
| properties: [...accObject.properties ?? [], ...objectMember.properties ?? []] | ||
| }); | ||
| continue; | ||
| } | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| acc = member; | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| } | ||
| /** | ||
| * Removes enum members that are covered by broader scalar primitives in the same union. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const simplified = simplifyUnion([ | ||
| * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }), | ||
| * createSchema({ type: 'string' }), | ||
| * ]) | ||
| * // keeps only string member | ||
| * ``` | ||
| */ | ||
| function simplifyUnion(members) { | ||
| const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type)); | ||
| if (!scalarPrimitives.size) return members; | ||
| return members.filter((member) => { | ||
| const enumNode = narrowSchema(member, "enum"); | ||
| if (!enumNode) return true; | ||
| const primitive = enumNode.primitive; | ||
| if (!primitive) return true; | ||
| if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true; | ||
| if (scalarPrimitives.has(primitive)) return false; | ||
| if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false; | ||
| return true; | ||
| }); | ||
| } | ||
| function setEnumName(propNode, parentName, propName, enumSuffix) { | ||
| const enumNode = narrowSchema(propNode, "enum"); | ||
| if (enumNode?.primitive === "boolean") return { | ||
| ...propNode, | ||
| name: null | ||
| }; | ||
| if (enumNode) return { | ||
| ...propNode, | ||
| name: enumPropName(parentName, propName, enumSuffix) | ||
| }; | ||
| return propNode; | ||
| } | ||
| /** | ||
| * Merges a ref node with its resolved schema, giving usage-site fields precedence. | ||
| * | ||
| * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override | ||
| * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ref with description override | ||
| * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' }) | ||
| * const merged = syncSchemaRef(ref) // merges with resolved Pet schema | ||
| * ``` | ||
| */ | ||
| function syncSchemaRef(node) { | ||
| const ref = narrowSchema(node, "ref"); | ||
| if (!ref) return node; | ||
| if (!ref.schema) return node; | ||
| const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref; | ||
| const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0)); | ||
| return createSchema({ | ||
| ...ref.schema, | ||
| ...definedOverrides | ||
| }); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/strings.ts | ||
| /** | ||
| * Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`. | ||
| * Returns the string unchanged when no balanced quote pair is found. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * trimQuotes('"hello"') // 'hello' | ||
| * trimQuotes('hello') // 'hello' | ||
| * ``` | ||
| */ | ||
| function trimQuotes(text) { | ||
| if (text.length >= 2) { | ||
| const first = text[0]; | ||
| const last = text[text.length - 1]; | ||
| if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1); | ||
| } | ||
| return text; | ||
| } | ||
| /** | ||
| * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first. | ||
| * | ||
| * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated | ||
| * code matches the repo style without a formatter. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * stringify('hello') // "'hello'" | ||
| * stringify('"hello"') // "'hello'" | ||
| * ``` | ||
| */ | ||
| function stringify(value) { | ||
| if (value === void 0 || value === null) return "''"; | ||
| return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`; | ||
| } | ||
| /** | ||
| * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes, | ||
| * and the Unicode line terminators U+2028 and U+2029. | ||
| * | ||
| * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4 | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye' | ||
| * ``` | ||
| */ | ||
| function jsStringEscape(input) { | ||
| return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => { | ||
| switch (character) { | ||
| case "\"": | ||
| case "'": | ||
| case "\\": return `\\${character}`; | ||
| case "\n": return "\\n"; | ||
| case "\r": return "\\r"; | ||
| case "\u2028": return "\\u2028"; | ||
| case "\u2029": return "\\u2029"; | ||
| default: return ""; | ||
| } | ||
| }); | ||
| } | ||
| /** | ||
| * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string. | ||
| * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression. | ||
| * Pass `null` as the second argument to emit a `/pattern/flags` literal instead. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")' | ||
| * toRegExpString('^(?im)foo', null) // '/^foo/im' | ||
| * ``` | ||
| */ | ||
| function toRegExpString(text, func = "RegExp") { | ||
| const raw = trimQuotes(text); | ||
| const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i); | ||
| const replacementTarget = match?.[1] ?? ""; | ||
| const matchedFlags = match?.[2]; | ||
| const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, ""); | ||
| const { source, flags } = new RegExp(cleaned, matchedFlags); | ||
| if (func === null) return `/${source}/${flags}`; | ||
| return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`; | ||
| } | ||
| /** | ||
| * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested | ||
| * objects recurse with fixed indentation, so the result drops straight into an object literal | ||
| * without re-parsing. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * stringifyObject({ foo: 'bar', nested: { a: 1 } }) | ||
| * // 'foo: bar,\nnested: {\n a: 1\n }' | ||
| * ``` | ||
| */ | ||
| function stringifyObject(value) { | ||
| return Object.entries(value).map(([key, val]) => { | ||
| if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`; | ||
| return `${key}: ${val}`; | ||
| }).filter(Boolean).join(",\n"); | ||
| } | ||
| /** | ||
| * Renders a dotted path or string array as an optional-chaining accessor expression rooted at | ||
| * `accessor`. Returns `null` for an empty path. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * getNestedAccessor('pagination.next.id', 'lastPage') | ||
| * // "lastPage?.['pagination']?.['next']?.['id']" | ||
| * ``` | ||
| */ | ||
| function getNestedAccessor(param, accessor) { | ||
| const parts = Array.isArray(param) ? param : param.split("."); | ||
| if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null; | ||
| return `${accessor}?.['${`${parts.join("']?.['")}']`}`; | ||
| } | ||
| //#endregion | ||
| //#region src/registry.ts | ||
| /** | ||
| * Every node definition. Adding a node means adding its `defineNode` to one | ||
| * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it. | ||
| */ | ||
| const nodeDefs = [ | ||
| inputDef, | ||
| outputDef, | ||
| operationDef, | ||
| requestBodyDef, | ||
| contentDef, | ||
| responseDef, | ||
| schemaDef, | ||
| propertyDef, | ||
| parameterDef, | ||
| functionParameterDef, | ||
| functionParametersDef, | ||
| typeLiteralDef, | ||
| indexedAccessTypeDef, | ||
| objectBindingPatternDef, | ||
| constDef, | ||
| typeDef, | ||
| functionDef, | ||
| arrowFunctionDef, | ||
| textDef, | ||
| breakDef, | ||
| jsxDef, | ||
| importDef, | ||
| exportDef, | ||
| sourceDef, | ||
| fileDef | ||
| ]; | ||
| //#endregion | ||
| //#region src/visitor.ts | ||
| /** | ||
| * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`). | ||
| * Derived from each definition's `children`. | ||
| */ | ||
| const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : [])); | ||
| /** | ||
| * Maps a node kind to the matching visitor callback name. Derived from each | ||
| * definition's `visitorKey`. | ||
| */ | ||
| const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : [])); | ||
| /** | ||
| * Per-kind builders rerun after children are rebuilt. Derived from each | ||
| * definition's `rebuild` flag. | ||
| */ | ||
| const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : [])); | ||
| /** | ||
| * Creates a small async concurrency limiter. | ||
| * | ||
| * At most `concurrency` tasks are in flight at once. Extra tasks are queued. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const limit = createLimit(2) | ||
| * for (const task of [taskA, taskB, taskC]) { | ||
| * await limit(() => task()) | ||
| * } | ||
| * // only 2 tasks run at the same time | ||
| * ``` | ||
| */ | ||
| function createLimit(concurrency) { | ||
| let active = 0; | ||
| const queue = []; | ||
| function next() { | ||
| if (active < concurrency && queue.length > 0) { | ||
| active++; | ||
| queue.shift()(); | ||
| } | ||
| } | ||
| return function limit(fn) { | ||
| return new Promise((resolve, reject) => { | ||
| queue.push(() => { | ||
| Promise.resolve(fn()).then(resolve, reject).finally(() => { | ||
| active--; | ||
| next(); | ||
| }); | ||
| }); | ||
| next(); | ||
| }); | ||
| }; | ||
| } | ||
| const visitorKeysByKind = VISITOR_KEYS; | ||
| /** | ||
| * Returns `true` when `value` is an AST node (an object carrying a `kind`). | ||
| */ | ||
| function isNode(value) { | ||
| return typeof value === "object" && value !== null && "kind" in value; | ||
| } | ||
| /** | ||
| * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}. | ||
| * | ||
| * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const children = getChildren(operationNode, true) | ||
| * // returns parameters, the request body, and responses | ||
| * ``` | ||
| */ | ||
| function* getChildren(node, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return; | ||
| const record = node; | ||
| for (const key of keys) { | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) if (isNode(item)) yield item; | ||
| } else if (isNode(value)) yield value; | ||
| } | ||
| } | ||
| /** | ||
| * Runs the visitor callback that matches `node.kind` with the traversal | ||
| * context. The result is a replacement node, a collected value, or `undefined` | ||
| * when no callback is registered for the kind. | ||
| * | ||
| * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives | ||
| * in one place. `TResult` is the caller's expected return: the same node type | ||
| * for `transform`, the collected value type for `collectLazy`, ignored for `walk`. | ||
| */ | ||
| function applyVisitor(node, visitor, parent) { | ||
| const key = VISITOR_KEY_BY_KIND[node.kind]; | ||
| if (!key) return void 0; | ||
| const fn = visitor[key]; | ||
| return fn?.(node, { parent }); | ||
| } | ||
| /** | ||
| * Async depth-first traversal for side effects. Visitor return values are | ||
| * ignored. Use `transform` when you want to rewrite nodes. | ||
| * | ||
| * Sibling nodes at each depth run concurrently up to `options.concurrency` | ||
| * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor | ||
| * work. Lower values reduce memory pressure. | ||
| * | ||
| * @example Log every operation | ||
| * ```ts | ||
| * await walk(root, { | ||
| * operation(node) { | ||
| * console.log(node.operationId) | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @example Only visit the root node | ||
| * ```ts | ||
| * await walk(root, { depth: 'shallow', input: () => {} }) | ||
| * ``` | ||
| */ | ||
| async function walk(node, options) { | ||
| return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0); | ||
| } | ||
| async function _walk(node, visitor, recurse, limit, parent) { | ||
| await limit(() => applyVisitor(node, visitor, parent)); | ||
| const children = Array.from(getChildren(node, recurse)); | ||
| if (children.length === 0) return; | ||
| await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node))); | ||
| } | ||
| function transform(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse); | ||
| if (rebuilt === node) return node; | ||
| const rebuild = nodeRebuilders[rebuilt.kind]; | ||
| return rebuild ? rebuild(rebuilt) : rebuilt; | ||
| } | ||
| /** | ||
| * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming | ||
| * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact. | ||
| * `Schema` children are skipped in shallow mode. | ||
| */ | ||
| function transformChildren(node, options, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return node; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return node; | ||
| const record = node; | ||
| const childOptions = { | ||
| ...options, | ||
| parent: node | ||
| }; | ||
| let updates; | ||
| for (const key of keys) { | ||
| if (!(key in record)) continue; | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| let changed = false; | ||
| const mapped = value.map((item) => { | ||
| if (!isNode(item)) return item; | ||
| const next = transform(item, childOptions); | ||
| if (next !== item) changed = true; | ||
| return next; | ||
| }); | ||
| if (changed) (updates ??= {})[key] = mapped; | ||
| } else if (isNode(value)) { | ||
| const next = transform(value, childOptions); | ||
| if (next !== value) (updates ??= {})[key] = next; | ||
| } | ||
| } | ||
| return updates ? { | ||
| ...node, | ||
| ...updates | ||
| } : node; | ||
| } | ||
| /** | ||
| * Lazy depth-first collection pass. Yields every non-null value returned by | ||
| * the visitor callbacks. Use `collect` for the eager array form. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids: string[] = [] | ||
| * for (const id of collectLazy<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * })) { | ||
| * ids.push(id) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function* collectLazy(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const v = applyVisitor(node, visitor, parent); | ||
| if (v != null) yield v; | ||
| for (const child of getChildren(node, recurse)) yield* collectLazy(child, { | ||
| ...options, | ||
| parent: node | ||
| }); | ||
| } | ||
| /** | ||
| * Eager depth-first collection pass. Gathers every non-null value the visitor | ||
| * callbacks return into an array. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids = collect<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| */ | ||
| function collect(node, options) { | ||
| return Array.from(collectLazy(node, options)); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/schemaGraph.ts | ||
| /** | ||
| * Collects every named schema referenced transitively from a node through its ref edges. | ||
| * | ||
| * Refs are followed by name only, so the resolved `node.schema` is never traversed inline. | ||
| * | ||
| * @example Collect refs from a single schema | ||
| * ```ts | ||
| * const names = collectReferencedSchemaNames(petSchema) | ||
| * // → Set { 'Category', 'Tag' } | ||
| * ``` | ||
| * | ||
| * @example Accumulate refs from multiple schemas into one set | ||
| * ```ts | ||
| * const out = new Set<string>() | ||
| * for (const schema of schemas) { | ||
| * collectReferencedSchemaNames(schema, out) | ||
| * } | ||
| * ``` | ||
| */ | ||
| const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => { | ||
| const refs = /* @__PURE__ */ new Set(); | ||
| collect(node, { schema(child) { | ||
| if (child.type === "ref") { | ||
| const name = resolveRefName(child); | ||
| if (name) refs.add(name); | ||
| } | ||
| } }); | ||
| return refs; | ||
| }); | ||
| function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) { | ||
| if (!node) return out; | ||
| for (const name of collectSchemaRefs(node)) out.add(name); | ||
| return out; | ||
| } | ||
| /** | ||
| * Collects the names of all top-level schemas transitively used by a set of operations. | ||
| * | ||
| * An operation uses a schema when its parameters, request body, or responses reference it, directly | ||
| * or through other named schemas. The walk is iterative, so reference cycles are safe. | ||
| * | ||
| * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated. | ||
| * | ||
| * @example Only generate schemas referenced by included operations | ||
| * ```ts | ||
| * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null) | ||
| * const allowed = collectUsedSchemaNames(includedOps, schemas) | ||
| * | ||
| * for (const schema of schemas) { | ||
| * if (schema.name && !allowed.has(schema.name)) continue | ||
| * // … generate schema | ||
| * } | ||
| * ``` | ||
| */ | ||
| const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas))); | ||
| function computeUsedSchemaNames(operations, schemas) { | ||
| const schemaMap = /* @__PURE__ */ new Map(); | ||
| for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema); | ||
| const result = /* @__PURE__ */ new Set(); | ||
| function visitSchema(schema) { | ||
| const directRefs = collectReferencedSchemaNames(schema); | ||
| for (const name of directRefs) if (!result.has(name)) { | ||
| result.add(name); | ||
| const namedSchema = schemaMap.get(name); | ||
| if (namedSchema) visitSchema(namedSchema); | ||
| } | ||
| } | ||
| for (const op of operations) for (const schema of collectLazy(op, { | ||
| depth: "shallow", | ||
| schema: (node) => node | ||
| })) visitSchema(schema); | ||
| return result; | ||
| } | ||
| function collectUsedSchemaNames(operations, schemas) { | ||
| return collectUsedSchemaNamesMemo(operations)(schemas); | ||
| } | ||
| const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set(); | ||
| const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => { | ||
| const graph = /* @__PURE__ */ new Map(); | ||
| for (const schema of schemas) { | ||
| if (!schema.name) continue; | ||
| graph.set(schema.name, collectReferencedSchemaNames(schema)); | ||
| } | ||
| const circular = /* @__PURE__ */ new Set(); | ||
| for (const start of graph.keys()) { | ||
| const visited = /* @__PURE__ */ new Set(); | ||
| const stack = [...graph.get(start) ?? []]; | ||
| while (stack.length > 0) { | ||
| const node = stack.pop(); | ||
| if (node === start) { | ||
| circular.add(start); | ||
| break; | ||
| } | ||
| if (visited.has(node)) continue; | ||
| visited.add(node); | ||
| const next = graph.get(node); | ||
| if (next) for (const r of next) stack.push(r); | ||
| } | ||
| } | ||
| return circular; | ||
| }); | ||
| /** | ||
| * Finds every schema that takes part in a circular dependency chain, including direct self-loops. | ||
| * | ||
| * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so | ||
| * the generated code does not recurse forever. Refs are followed by name only, so the walk stays | ||
| * linear in the size of the schema graph. | ||
| * | ||
| * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`. | ||
| */ | ||
| function findCircularSchemas(schemas) { | ||
| if (schemas.length === 0) return EMPTY_CIRCULAR_SET; | ||
| return findCircularSchemasMemo(schemas); | ||
| } | ||
| /** | ||
| * Returns `true` when a schema, or anything nested inside it, references a circular schema. | ||
| * | ||
| * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled | ||
| * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go. | ||
| * | ||
| * @note Stops at the first matching circular ref. | ||
| */ | ||
| function containsCircularRef(node, { circularSchemas, excludeName }) { | ||
| if (!node || circularSchemas.size === 0) return false; | ||
| for (const _ of collectLazy(node, { schema(child) { | ||
| if (child.type !== "ref") return null; | ||
| const name = resolveRefName(child); | ||
| return name && name !== excludeName && circularSchemas.has(name) ? true : null; | ||
| } })) return true; | ||
| return false; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/schemaTraversal.ts | ||
| /** | ||
| * Maps each property of an object schema to its transformed output. Pairs every result with the | ||
| * original property so the printer keeps full control over modifiers, getters, and key syntax. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const entries = mapSchemaProperties(node, (schema) => this.transform(schema)) | ||
| * // entries: [{ name: 'id', property, output: 'z.number()' }, ...] | ||
| * ``` | ||
| */ | ||
| function mapSchemaProperties(node, transform) { | ||
| return node.properties.map((property) => ({ | ||
| name: property.name, | ||
| property, | ||
| output: transform(property.schema) | ||
| })); | ||
| } | ||
| /** | ||
| * Maps each member of a union or intersection schema to its transformed output, pairing every | ||
| * result with the original member. | ||
| */ | ||
| function mapSchemaMembers(node, transform) { | ||
| return (node.members ?? []).map((schema) => ({ | ||
| schema, | ||
| output: transform(schema) | ||
| })); | ||
| } | ||
| /** | ||
| * Maps each item of an array or tuple schema to its transformed output, pairing every result with | ||
| * the original item. | ||
| */ | ||
| function mapSchemaItems(node, transform) { | ||
| return (node.items ?? []).map((schema) => ({ | ||
| schema, | ||
| output: transform(schema) | ||
| })); | ||
| } | ||
| /** | ||
| * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key | ||
| * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation | ||
| * of a recursive schema until first access. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' }) | ||
| * // "get parent() { return z.lazy(() => Pet) }" | ||
| * ``` | ||
| */ | ||
| function lazyGetter({ name, body }) { | ||
| return `get ${objectKey(name)}() { return ${body} }`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/operationParams.ts | ||
| /** | ||
| * Applies casing rules to parameter names and returns a new array without mutating the input. | ||
| * | ||
| * Run it before handing parameters to schema builders so output property keys get the right casing | ||
| * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the | ||
| * original array is returned unchanged. | ||
| */ | ||
| const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => { | ||
| const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name; | ||
| return { | ||
| ...param, | ||
| name: transformed | ||
| }; | ||
| }))); | ||
| function caseParams(params, casing) { | ||
| if (!casing) return params; | ||
| return caseParamsMemo(params)(casing); | ||
| } | ||
| /** | ||
| * Resolves the {@link TypeExpression} for an individual parameter. | ||
| * | ||
| * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the | ||
| * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like | ||
| * `GroupParams['petId']`, otherwise the resolved individual name. | ||
| */ | ||
| function resolveParamType({ node, param, resolver }) { | ||
| if (!resolver) return param.schema.primitive ?? "unknown"; | ||
| const individualName = resolver.resolveParamName(node, param); | ||
| const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0; | ||
| const groupResolvers = { | ||
| path: resolver.resolvePathParamsName, | ||
| query: resolver.resolveQueryParamsName, | ||
| header: resolver.resolveHeaderParamsName | ||
| }; | ||
| const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0; | ||
| if (groupName && groupName !== individualName) return createIndexedAccessType({ | ||
| objectType: groupName, | ||
| indexType: param.name | ||
| }); | ||
| return individualName; | ||
| } | ||
| /** | ||
| * Converts an `OperationNode` into function parameters for code generation. | ||
| * | ||
| * Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one | ||
| * destructured object parameter (`object`) and separate top-level parameters (`inline`), while | ||
| * `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type | ||
| * name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object. | ||
| */ | ||
| function createOperationParams(node, options) { | ||
| const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options; | ||
| const dataName = paramNames?.data ?? "data"; | ||
| const paramsName = paramNames?.params ?? "params"; | ||
| const headersName = paramNames?.headers ?? "headers"; | ||
| const pathName = paramNames?.path ?? "pathParams"; | ||
| const wrapType = (type) => typeWrapper ? typeWrapper(type) : type; | ||
| const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type; | ||
| const casedParams = caseParams(node.parameters, paramsCasing); | ||
| const pathParams = casedParams.filter((p) => p.in === "path"); | ||
| const queryParams = casedParams.filter((p) => p.in === "query"); | ||
| const headerParams = casedParams.filter((p) => p.in === "header"); | ||
| const toProperty = (param) => ({ | ||
| name: param.name, | ||
| type: wrapTypeExpression(resolveParamType({ | ||
| node, | ||
| param, | ||
| resolver | ||
| })), | ||
| optional: !param.required | ||
| }); | ||
| const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0; | ||
| const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0; | ||
| const bodyProperty = bodyType ? [{ | ||
| name: dataName, | ||
| type: bodyType, | ||
| optional: !(node.requestBody?.required ?? false) | ||
| }] : []; | ||
| const trailingGroups = [{ | ||
| name: paramsName, | ||
| node, | ||
| params: queryParams, | ||
| groupType: resolveGroupType({ | ||
| node, | ||
| params: queryParams, | ||
| group: "query", | ||
| resolver | ||
| }), | ||
| resolver, | ||
| wrapType | ||
| }, { | ||
| name: headersName, | ||
| node, | ||
| params: headerParams, | ||
| groupType: resolveGroupType({ | ||
| node, | ||
| params: headerParams, | ||
| group: "header", | ||
| resolver | ||
| }), | ||
| resolver, | ||
| wrapType | ||
| }]; | ||
| const params = []; | ||
| if (paramsType === "object") { | ||
| const children = [ | ||
| ...pathParams.map(toProperty), | ||
| ...bodyProperty, | ||
| ...trailingGroups.flatMap(buildGroupProperty) | ||
| ]; | ||
| if (children.length) params.push(createFunctionParameter({ | ||
| properties: children, | ||
| default: emptyObjectDefault(children) | ||
| })); | ||
| } else { | ||
| if (pathParamsType === "inlineSpread" && pathParams.length) { | ||
| const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]); | ||
| params.push(createFunctionParameter({ | ||
| name: pathName, | ||
| type: spreadType ? wrapType(spreadType) : void 0, | ||
| rest: true | ||
| })); | ||
| } else if (pathParamsType === "inline") params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p)))); | ||
| else if (pathParams.length) { | ||
| const pathChildren = pathParams.map(toProperty); | ||
| params.push(createFunctionParameter({ | ||
| properties: pathChildren, | ||
| default: pathParamsDefault ?? emptyObjectDefault(pathChildren) | ||
| })); | ||
| } | ||
| params.push(...bodyProperty.map((p) => createFunctionParameter(p))); | ||
| params.push(...trailingGroups.flatMap(buildGroupParam)); | ||
| } | ||
| params.push(...extraParams); | ||
| return createFunctionParameters({ params }); | ||
| } | ||
| /** | ||
| * Builds the property descriptor for a query or header group. | ||
| * Returns an empty array when there are no params to emit. | ||
| * | ||
| * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline | ||
| * {@link TypeLiteralNode} from the individual params. | ||
| */ | ||
| function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) { | ||
| if (groupType) return [{ | ||
| name, | ||
| type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type, | ||
| optional: groupType.optional | ||
| }]; | ||
| if (params.length) return [{ | ||
| name, | ||
| type: buildTypeLiteral({ | ||
| node, | ||
| params, | ||
| resolver | ||
| }), | ||
| optional: params.every((p) => !p.required) | ||
| }]; | ||
| return []; | ||
| } | ||
| /** | ||
| * Builds a single {@link FunctionParameterNode} for a query or header group. | ||
| * Returns an empty array when there are no params to emit. | ||
| * | ||
| * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline | ||
| * {@link TypeLiteralNode} from the individual params. | ||
| */ | ||
| function buildGroupParam(args) { | ||
| return buildGroupProperty(args).map((p) => createFunctionParameter(p)); | ||
| } | ||
| /** | ||
| * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields. | ||
| * | ||
| * Used when query or header parameters have no dedicated group type name. | ||
| * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`). | ||
| */ | ||
| function buildTypeLiteral({ node, params, resolver }) { | ||
| return createTypeLiteral({ members: params.map((p) => ({ | ||
| name: p.name, | ||
| type: resolveParamType({ | ||
| node, | ||
| param: p, | ||
| resolver | ||
| }), | ||
| optional: !p.required | ||
| })) }); | ||
| } | ||
| //#endregion | ||
| export { enumPropName as A, objectKey as B, trimQuotes as C, simplifyUnion as D, setEnumName as E, isHttpOperationNode as F, httpMethods as H, narrowSchema as I, buildJSDoc as L, isStringType as M, resolveGroupType as N, syncSchemaRef as O, resolveRefName as P, buildList as R, toRegExpString as S, setDiscriminatorEnum as T, schemaTypes as U, isValidVarName as V, nodeDefs as _, resolveParamType as a, stringify as b, mapSchemaMembers as c, containsCircularRef as d, findCircularSchemas as f, walk as g, transform as h, createOperationParams as i, extractRefName as j, childName as k, mapSchemaProperties as l, collectLazy as m, buildTypeLiteral as n, lazyGetter as o, collect as p, caseParams as r, mapSchemaItems as s, buildGroupParam as t, collectUsedSchemaNames as u, getNestedAccessor as v, mergeAdjacentObjectsLazy as w, stringifyObject as x, jsStringEscape as y, buildObject as z }; | ||
| //# sourceMappingURL=utils-Dj_KoXMv.js.map |
Sorry, the diff of this file is too big to display
| import type { ArraySchemaNode, IntersectionSchemaNode, ObjectSchemaNode, PropertyNode, SchemaNode, UnionSchemaNode } from '../nodes/index.ts' | ||
| import { objectKey } from './codegen.ts' | ||
| /** | ||
| * Converts a child schema to printer output. Plugins instantiate it with their own output type: | ||
| * `string` for the zod and faker printers, `ts.TypeNode` for the TypeScript printer. A printer's | ||
| * `this.transform` fits directly, so its `null` for an empty result carries through to `output`. | ||
| */ | ||
| export type SchemaTransform<TOutput> = (schema: SchemaNode) => TOutput | ||
| /** | ||
| * A union or intersection member, or an array or tuple item, paired with its transformed output. | ||
| */ | ||
| export type MappedSchema<TOutput> = { | ||
| /** | ||
| * The original child schema, kept so the printer can read its metadata for leaf formatting. | ||
| */ | ||
| schema: SchemaNode | ||
| /** | ||
| * The child schema after being run through the transform. | ||
| */ | ||
| output: TOutput | ||
| } | ||
| /** | ||
| * An object property paired with its transformed output. | ||
| */ | ||
| export type MappedProperty<TOutput> = { | ||
| /** | ||
| * The property name as written on the schema, before any identifier quoting. | ||
| */ | ||
| name: string | ||
| /** | ||
| * The original property node, kept so the printer can read `required`, `schema`, and metadata. | ||
| */ | ||
| property: PropertyNode | ||
| /** | ||
| * The property schema after being run through the transform. | ||
| */ | ||
| output: TOutput | ||
| } | ||
| /** | ||
| * Maps each property of an object schema to its transformed output. Pairs every result with the | ||
| * original property so the printer keeps full control over modifiers, getters, and key syntax. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const entries = mapSchemaProperties(node, (schema) => this.transform(schema)) | ||
| * // entries: [{ name: 'id', property, output: 'z.number()' }, ...] | ||
| * ``` | ||
| */ | ||
| export function mapSchemaProperties<TOutput>(node: ObjectSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedProperty<TOutput>> { | ||
| return node.properties.map((property) => ({ name: property.name, property, output: transform(property.schema) })) | ||
| } | ||
| /** | ||
| * Maps each member of a union or intersection schema to its transformed output, pairing every | ||
| * result with the original member. | ||
| */ | ||
| export function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> { | ||
| return (node.members ?? []).map((schema) => ({ schema, output: transform(schema) })) | ||
| } | ||
| /** | ||
| * Maps each item of an array or tuple schema to its transformed output, pairing every result with | ||
| * the original item. | ||
| */ | ||
| export function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>> { | ||
| return (node.items ?? []).map((schema) => ({ schema, output: transform(schema) })) | ||
| } | ||
| /** | ||
| * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key | ||
| * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation | ||
| * of a recursive schema until first access. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' }) | ||
| * // "get parent() { return z.lazy(() => Pet) }" | ||
| * ``` | ||
| */ | ||
| export function lazyGetter({ name, body }: { name: string; body: string }): string { | ||
| return `get ${objectKey(name)}() { return ${body} }` | ||
| } |
+26
-25
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| const require_factory = require("./factory-BmcGBdeg.cjs"); | ||
| exports.createArrowFunction = require_factory.createArrowFunction; | ||
| exports.createBreak = require_factory.createBreak; | ||
| exports.createConst = require_factory.createConst; | ||
| exports.createContent = require_factory.createContent; | ||
| exports.createExport = require_factory.createExport; | ||
| const require_response = require("./response-DS5S3IG4.cjs"); | ||
| const require_factory = require("./factory-Cl8Z7mcc.cjs"); | ||
| exports.createArrowFunction = require_response.createArrowFunction; | ||
| exports.createBreak = require_response.createBreak; | ||
| exports.createConst = require_response.createConst; | ||
| exports.createContent = require_response.createContent; | ||
| exports.createExport = require_response.createExport; | ||
| exports.createFile = require_factory.createFile; | ||
| exports.createFunction = require_factory.createFunction; | ||
| exports.createFunctionParameter = require_factory.createFunctionParameter; | ||
| exports.createFunctionParameters = require_factory.createFunctionParameters; | ||
| exports.createImport = require_factory.createImport; | ||
| exports.createIndexedAccessType = require_factory.createIndexedAccessType; | ||
| exports.createInput = require_factory.createInput; | ||
| exports.createJsx = require_factory.createJsx; | ||
| exports.createObjectBindingPattern = require_factory.createObjectBindingPattern; | ||
| exports.createOperation = require_factory.createOperation; | ||
| exports.createOutput = require_factory.createOutput; | ||
| exports.createParameter = require_factory.createParameter; | ||
| exports.createProperty = require_factory.createProperty; | ||
| exports.createRequestBody = require_factory.createRequestBody; | ||
| exports.createResponse = require_factory.createResponse; | ||
| exports.createSchema = require_factory.createSchema; | ||
| exports.createSource = require_factory.createSource; | ||
| exports.createText = require_factory.createText; | ||
| exports.createType = require_factory.createType; | ||
| exports.createTypeLiteral = require_factory.createTypeLiteral; | ||
| exports.createFunction = require_response.createFunction; | ||
| exports.createFunctionParameter = require_response.createFunctionParameter; | ||
| exports.createFunctionParameters = require_response.createFunctionParameters; | ||
| exports.createImport = require_response.createImport; | ||
| exports.createIndexedAccessType = require_response.createIndexedAccessType; | ||
| exports.createInput = require_response.createInput; | ||
| exports.createJsx = require_response.createJsx; | ||
| exports.createObjectBindingPattern = require_response.createObjectBindingPattern; | ||
| exports.createOperation = require_response.createOperation; | ||
| exports.createOutput = require_response.createOutput; | ||
| exports.createParameter = require_response.createParameter; | ||
| exports.createProperty = require_response.createProperty; | ||
| exports.createRequestBody = require_response.createRequestBody; | ||
| exports.createResponse = require_response.createResponse; | ||
| exports.createSchema = require_response.createSchema; | ||
| exports.createSource = require_response.createSource; | ||
| exports.createText = require_response.createText; | ||
| exports.createType = require_response.createType; | ||
| exports.createTypeLiteral = require_response.createTypeLiteral; | ||
| exports.update = require_factory.update; |
+44
-548
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| require("./casing-BE2R1RXg.cjs"); | ||
| const require_factory = require("./factory-BmcGBdeg.cjs"); | ||
| const require_utils = require("./utils-BCtRXfhI.cjs"); | ||
| const require_response = require("./response-DS5S3IG4.cjs"); | ||
| const require_utils = require("./utils-D83JA6Xx.cjs"); | ||
| const require_factory = require("./factory-Cl8Z7mcc.cjs"); | ||
| let node_crypto = require("node:crypto"); | ||
| //#region src/constants.ts | ||
| const visitorDepths = { | ||
| shallow: "shallow", | ||
| deep: "deep" | ||
| }; | ||
| /** | ||
| * Schema type discriminators used by all AST schema nodes. | ||
| * | ||
| * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`). | ||
| * Call `isScalarPrimitive()` to check for the scalar types. | ||
| */ | ||
| const schemaTypes = { | ||
| /** | ||
| * Text value. | ||
| */ | ||
| string: "string", | ||
| /** | ||
| * Floating-point number (`float`, `double`). | ||
| */ | ||
| number: "number", | ||
| /** | ||
| * Whole number (`int32`). Use `bigint` for `int64`. | ||
| */ | ||
| integer: "integer", | ||
| /** | ||
| * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`. | ||
| */ | ||
| bigint: "bigint", | ||
| /** | ||
| * Boolean value. | ||
| */ | ||
| boolean: "boolean", | ||
| /** | ||
| * Explicit null value. | ||
| */ | ||
| null: "null", | ||
| /** | ||
| * Any value (no type restriction). | ||
| */ | ||
| any: "any", | ||
| /** | ||
| * Unknown value (must be narrowed before usage). | ||
| */ | ||
| unknown: "unknown", | ||
| /** | ||
| * No return value (`void`). | ||
| */ | ||
| void: "void", | ||
| /** | ||
| * Object with named properties. | ||
| */ | ||
| object: "object", | ||
| /** | ||
| * Sequential list of items. | ||
| */ | ||
| array: "array", | ||
| /** | ||
| * Fixed-length list with position-specific items. | ||
| */ | ||
| tuple: "tuple", | ||
| /** | ||
| * "One of" multiple schema members. | ||
| */ | ||
| union: "union", | ||
| /** | ||
| * "All of" multiple schema members. | ||
| */ | ||
| intersection: "intersection", | ||
| /** | ||
| * Enum schema. | ||
| */ | ||
| enum: "enum", | ||
| /** | ||
| * Reference to another schema. | ||
| */ | ||
| ref: "ref", | ||
| /** | ||
| * Calendar date (for example `2026-03-24`). | ||
| */ | ||
| date: "date", | ||
| /** | ||
| * Date-time value (for example `2026-03-24T09:00:00Z`). | ||
| */ | ||
| datetime: "datetime", | ||
| /** | ||
| * Time-only value (for example `09:00:00`). | ||
| */ | ||
| time: "time", | ||
| /** | ||
| * UUID value. | ||
| */ | ||
| uuid: "uuid", | ||
| /** | ||
| * Email address value. | ||
| */ | ||
| email: "email", | ||
| /** | ||
| * URL value. | ||
| */ | ||
| url: "url", | ||
| /** | ||
| * IPv4 address value. | ||
| */ | ||
| ipv4: "ipv4", | ||
| /** | ||
| * IPv6 address value. | ||
| */ | ||
| ipv6: "ipv6", | ||
| /** | ||
| * Binary/blob value. | ||
| */ | ||
| blob: "blob", | ||
| /** | ||
| * Impossible value (`never`). | ||
| */ | ||
| never: "never" | ||
| }; | ||
| /** | ||
| * Scalar primitive schema types used for union simplification and type narrowing. | ||
| */ | ||
| const SCALAR_PRIMITIVE_TYPES = new Set([ | ||
| "string", | ||
| "number", | ||
| "integer", | ||
| "bigint", | ||
| "boolean" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `type` is a scalar primitive that can be assigned without wrapping | ||
| * (for example `string | number | boolean`). | ||
| */ | ||
| function isScalarPrimitive(type) { | ||
| return SCALAR_PRIMITIVE_TYPES.has(type); | ||
| } | ||
| /** | ||
| * HTTP method identifiers used by operation nodes. | ||
| */ | ||
| const httpMethods = { | ||
| get: "GET", | ||
| post: "POST", | ||
| put: "PUT", | ||
| patch: "PATCH", | ||
| delete: "DELETE", | ||
| head: "HEAD", | ||
| options: "OPTIONS", | ||
| trace: "TRACE" | ||
| }; | ||
| Array.from({ length: 2 }, () => " ").join(""); | ||
| //#endregion | ||
| //#region src/signature.ts | ||
@@ -407,250 +258,2 @@ /** | ||
| //#endregion | ||
| //#region src/registry.ts | ||
| /** | ||
| * Every node definition. Adding a node means adding its `defineNode` to one | ||
| * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it. | ||
| */ | ||
| const nodeDefs = [ | ||
| require_factory.inputDef, | ||
| require_factory.outputDef, | ||
| require_factory.operationDef, | ||
| require_factory.requestBodyDef, | ||
| require_factory.contentDef, | ||
| require_factory.responseDef, | ||
| require_factory.schemaDef, | ||
| require_factory.propertyDef, | ||
| require_factory.parameterDef, | ||
| require_factory.functionParameterDef, | ||
| require_factory.functionParametersDef, | ||
| require_factory.typeLiteralDef, | ||
| require_factory.indexedAccessTypeDef, | ||
| require_factory.objectBindingPatternDef, | ||
| require_factory.constDef, | ||
| require_factory.typeDef, | ||
| require_factory.functionDef, | ||
| require_factory.arrowFunctionDef, | ||
| require_factory.textDef, | ||
| require_factory.breakDef, | ||
| require_factory.jsxDef, | ||
| require_factory.importDef, | ||
| require_factory.exportDef, | ||
| require_factory.sourceDef, | ||
| require_factory.fileDef | ||
| ]; | ||
| //#endregion | ||
| //#region src/visitor.ts | ||
| /** | ||
| * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`). | ||
| * Derived from each definition's `children`. | ||
| */ | ||
| const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : [])); | ||
| /** | ||
| * Maps a node kind to the matching visitor callback name. Derived from each | ||
| * definition's `visitorKey`. | ||
| */ | ||
| const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : [])); | ||
| /** | ||
| * Per-kind builders rerun after children are rebuilt. Derived from each | ||
| * definition's `rebuild` flag. | ||
| */ | ||
| const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : [])); | ||
| /** | ||
| * Creates a small async concurrency limiter. | ||
| * | ||
| * At most `concurrency` tasks are in flight at once. Extra tasks are queued. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const limit = createLimit(2) | ||
| * for (const task of [taskA, taskB, taskC]) { | ||
| * await limit(() => task()) | ||
| * } | ||
| * // only 2 tasks run at the same time | ||
| * ``` | ||
| */ | ||
| function createLimit(concurrency) { | ||
| let active = 0; | ||
| const queue = []; | ||
| function next() { | ||
| if (active < concurrency && queue.length > 0) { | ||
| active++; | ||
| queue.shift()(); | ||
| } | ||
| } | ||
| return function limit(fn) { | ||
| return new Promise((resolve, reject) => { | ||
| queue.push(() => { | ||
| Promise.resolve(fn()).then(resolve, reject).finally(() => { | ||
| active--; | ||
| next(); | ||
| }); | ||
| }); | ||
| next(); | ||
| }); | ||
| }; | ||
| } | ||
| const visitorKeysByKind = VISITOR_KEYS; | ||
| /** | ||
| * Returns `true` when `value` is an AST node (an object carrying a `kind`). | ||
| */ | ||
| function isNode(value) { | ||
| return typeof value === "object" && value !== null && "kind" in value; | ||
| } | ||
| /** | ||
| * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}. | ||
| * | ||
| * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const children = getChildren(operationNode, true) | ||
| * // returns parameters, the request body, and responses | ||
| * ``` | ||
| */ | ||
| function* getChildren(node, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return; | ||
| const record = node; | ||
| for (const key of keys) { | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) if (isNode(item)) yield item; | ||
| } else if (isNode(value)) yield value; | ||
| } | ||
| } | ||
| /** | ||
| * Runs the visitor callback that matches `node.kind` with the traversal | ||
| * context. The result is a replacement node, a collected value, or `undefined` | ||
| * when no callback is registered for the kind. | ||
| * | ||
| * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives | ||
| * in one place. `TResult` is the caller's expected return: the same node type | ||
| * for `transform`, the collected value type for `collectLazy`, ignored for `walk`. | ||
| */ | ||
| function applyVisitor(node, visitor, parent) { | ||
| const key = VISITOR_KEY_BY_KIND[node.kind]; | ||
| if (!key) return void 0; | ||
| const fn = visitor[key]; | ||
| return fn?.(node, { parent }); | ||
| } | ||
| /** | ||
| * Async depth-first traversal for side effects. Visitor return values are | ||
| * ignored. Use `transform` when you want to rewrite nodes. | ||
| * | ||
| * Sibling nodes at each depth run concurrently up to `options.concurrency` | ||
| * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor | ||
| * work. Lower values reduce memory pressure. | ||
| * | ||
| * @example Log every operation | ||
| * ```ts | ||
| * await walk(root, { | ||
| * operation(node) { | ||
| * console.log(node.operationId) | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @example Only visit the root node | ||
| * ```ts | ||
| * await walk(root, { depth: 'shallow', input: () => {} }) | ||
| * ``` | ||
| */ | ||
| async function walk(node, options) { | ||
| return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0); | ||
| } | ||
| async function _walk(node, visitor, recurse, limit, parent) { | ||
| await limit(() => applyVisitor(node, visitor, parent)); | ||
| const children = Array.from(getChildren(node, recurse)); | ||
| if (children.length === 0) return; | ||
| await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node))); | ||
| } | ||
| function transform(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse); | ||
| if (rebuilt === node) return node; | ||
| const rebuild = nodeRebuilders[rebuilt.kind]; | ||
| return rebuild ? rebuild(rebuilt) : rebuilt; | ||
| } | ||
| /** | ||
| * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming | ||
| * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact. | ||
| * `Schema` children are skipped in shallow mode. | ||
| */ | ||
| function transformChildren(node, options, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return node; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return node; | ||
| const record = node; | ||
| const childOptions = { | ||
| ...options, | ||
| parent: node | ||
| }; | ||
| let updates; | ||
| for (const key of keys) { | ||
| if (!(key in record)) continue; | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| let changed = false; | ||
| const mapped = value.map((item) => { | ||
| if (!isNode(item)) return item; | ||
| const next = transform(item, childOptions); | ||
| if (next !== item) changed = true; | ||
| return next; | ||
| }); | ||
| if (changed) (updates ??= {})[key] = mapped; | ||
| } else if (isNode(value)) { | ||
| const next = transform(value, childOptions); | ||
| if (next !== value) (updates ??= {})[key] = next; | ||
| } | ||
| } | ||
| return updates ? { | ||
| ...node, | ||
| ...updates | ||
| } : node; | ||
| } | ||
| /** | ||
| * Lazy depth-first collection pass. Yields every non-null value returned by | ||
| * the visitor callbacks. Use `collect` for the eager array form. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids: string[] = [] | ||
| * for (const id of collectLazy<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * })) { | ||
| * ids.push(id) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function* collectLazy(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const v = applyVisitor(node, visitor, parent); | ||
| if (v != null) yield v; | ||
| for (const child of getChildren(node, recurse)) yield* collectLazy(child, { | ||
| ...options, | ||
| parent: node | ||
| }); | ||
| } | ||
| /** | ||
| * Eager depth-first collection pass. Gathers every non-null value the visitor | ||
| * callbacks return into an array. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids = collect<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| */ | ||
| function collect(node, options) { | ||
| return Array.from(collectLazy(node, options)); | ||
| } | ||
| //#endregion | ||
| //#region src/dedupe.ts | ||
@@ -662,3 +265,3 @@ /** | ||
| function createRefNode(node, canonical) { | ||
| return require_factory.createSchema({ | ||
| return require_response.createSchema({ | ||
| type: "ref", | ||
@@ -681,3 +284,3 @@ name: canonical.name, | ||
| const root = node; | ||
| return transform(node, { schema(schemaNode) { | ||
| return require_utils.transform(node, { schema(schemaNode) { | ||
| if (schemaNode.type === "ref") { | ||
@@ -750,3 +353,3 @@ const target = schemaNode.ref ? require_utils.extractRefName(schemaNode.ref) : schemaNode.name; | ||
| if (root.kind === "Schema") topLevelNodes.add(root); | ||
| for (const schemaNode of collectLazy(root, { schema: (node) => node })) record(schemaNode); | ||
| for (const schemaNode of require_utils.collectLazy(root, { schema: (node) => node })) record(schemaNode); | ||
| } | ||
@@ -887,122 +490,15 @@ const canonicalBySignature = /* @__PURE__ */ new Map(); | ||
| //#endregion | ||
| //#region src/transformers.ts | ||
| /** | ||
| * Replaces a discriminator property's schema with a string enum of allowed values. | ||
| * | ||
| * If `node` is not an object schema, or if the property does not exist, the input | ||
| * node is returned as-is. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ | ||
| * type: 'object', | ||
| * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })], | ||
| * }) | ||
| * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] }) | ||
| * ``` | ||
| */ | ||
| function setDiscriminatorEnum({ node, propertyName, values, enumName }) { | ||
| const objectNode = require_utils.narrowSchema(node, "object"); | ||
| if (!objectNode?.properties?.length) return node; | ||
| if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node; | ||
| return require_factory.createSchema({ | ||
| ...objectNode, | ||
| properties: objectNode.properties.map((prop) => { | ||
| if (prop.name !== propertyName) return prop; | ||
| return require_factory.createProperty({ | ||
| ...prop, | ||
| schema: require_factory.createSchema({ | ||
| type: "enum", | ||
| primitive: "string", | ||
| enumValues: values, | ||
| name: enumName, | ||
| readOnly: prop.schema.readOnly, | ||
| writeOnly: prop.schema.writeOnly | ||
| }) | ||
| }); | ||
| }) | ||
| }); | ||
| } | ||
| /** | ||
| * Merges adjacent anonymous object members into a single anonymous object member. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const merged = mergeAdjacentObjects([ | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }), | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }), | ||
| * ]) | ||
| * ``` | ||
| */ | ||
| function* mergeAdjacentObjectsLazy(members) { | ||
| let acc; | ||
| for (const member of members) { | ||
| const objectMember = require_utils.narrowSchema(member, "object"); | ||
| if (objectMember && !objectMember.name && acc !== void 0) { | ||
| const accObject = require_utils.narrowSchema(acc, "object"); | ||
| if (accObject && !accObject.name) { | ||
| acc = require_factory.createSchema({ | ||
| ...accObject, | ||
| properties: [...accObject.properties ?? [], ...objectMember.properties ?? []] | ||
| }); | ||
| continue; | ||
| } | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| acc = member; | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| } | ||
| /** | ||
| * Removes enum members that are covered by broader scalar primitives in the same union. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const simplified = simplifyUnion([ | ||
| * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }), | ||
| * createSchema({ type: 'string' }), | ||
| * ]) | ||
| * // keeps only string member | ||
| * ``` | ||
| */ | ||
| function simplifyUnion(members) { | ||
| const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type)); | ||
| if (!scalarPrimitives.size) return members; | ||
| return members.filter((member) => { | ||
| const enumNode = require_utils.narrowSchema(member, "enum"); | ||
| if (!enumNode) return true; | ||
| const primitive = enumNode.primitive; | ||
| if (!primitive) return true; | ||
| if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true; | ||
| if (scalarPrimitives.has(primitive)) return false; | ||
| if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false; | ||
| return true; | ||
| }); | ||
| } | ||
| function setEnumName(propNode, parentName, propName, enumSuffix) { | ||
| const enumNode = require_utils.narrowSchema(propNode, "enum"); | ||
| if (enumNode?.primitive === "boolean") return { | ||
| ...propNode, | ||
| name: null | ||
| }; | ||
| if (enumNode) return { | ||
| ...propNode, | ||
| name: require_utils.enumPropName(parentName, propName, enumSuffix) | ||
| }; | ||
| return propNode; | ||
| } | ||
| //#endregion | ||
| exports.applyDedupe = applyDedupe; | ||
| exports.arrowFunctionDef = require_factory.arrowFunctionDef; | ||
| exports.breakDef = require_factory.breakDef; | ||
| exports.arrowFunctionDef = require_response.arrowFunctionDef; | ||
| exports.breakDef = require_response.breakDef; | ||
| exports.buildDedupePlan = buildDedupePlan; | ||
| exports.collect = collect; | ||
| exports.constDef = require_factory.constDef; | ||
| exports.contentDef = require_factory.contentDef; | ||
| exports.collect = require_utils.collect; | ||
| exports.constDef = require_response.constDef; | ||
| exports.contentDef = require_response.contentDef; | ||
| exports.createPrinterFactory = createPrinterFactory; | ||
| exports.defineNode = require_factory.defineNode; | ||
| exports.defineNode = require_response.defineNode; | ||
| exports.definePrinter = definePrinter; | ||
| exports.defineSchemaDialect = defineSchemaDialect; | ||
| exports.exportDef = require_factory.exportDef; | ||
| exports.extractStringsFromNodes = require_factory.extractStringsFromNodes; | ||
| exports.exportDef = require_response.exportDef; | ||
| exports.extractStringsFromNodes = require_response.extractStringsFromNodes; | ||
| Object.defineProperty(exports, "factory", { | ||
@@ -1014,36 +510,36 @@ enumerable: true, | ||
| }); | ||
| exports.fileDef = require_factory.fileDef; | ||
| exports.functionDef = require_factory.functionDef; | ||
| exports.functionParameterDef = require_factory.functionParameterDef; | ||
| exports.functionParametersDef = require_factory.functionParametersDef; | ||
| exports.httpMethods = httpMethods; | ||
| exports.importDef = require_factory.importDef; | ||
| exports.indexedAccessTypeDef = require_factory.indexedAccessTypeDef; | ||
| exports.inputDef = require_factory.inputDef; | ||
| exports.fileDef = require_response.fileDef; | ||
| exports.functionDef = require_response.functionDef; | ||
| exports.functionParameterDef = require_response.functionParameterDef; | ||
| exports.functionParametersDef = require_response.functionParametersDef; | ||
| exports.httpMethods = require_utils.httpMethods; | ||
| exports.importDef = require_response.importDef; | ||
| exports.indexedAccessTypeDef = require_response.indexedAccessTypeDef; | ||
| exports.inputDef = require_response.inputDef; | ||
| exports.isHttpOperationNode = require_utils.isHttpOperationNode; | ||
| exports.jsxDef = require_factory.jsxDef; | ||
| exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy; | ||
| exports.jsxDef = require_response.jsxDef; | ||
| exports.mergeAdjacentObjectsLazy = require_utils.mergeAdjacentObjectsLazy; | ||
| exports.narrowSchema = require_utils.narrowSchema; | ||
| exports.nodeDefs = nodeDefs; | ||
| exports.objectBindingPatternDef = require_factory.objectBindingPatternDef; | ||
| exports.operationDef = require_factory.operationDef; | ||
| exports.outputDef = require_factory.outputDef; | ||
| exports.parameterDef = require_factory.parameterDef; | ||
| exports.propertyDef = require_factory.propertyDef; | ||
| exports.requestBodyDef = require_factory.requestBodyDef; | ||
| exports.responseDef = require_factory.responseDef; | ||
| exports.schemaDef = require_factory.schemaDef; | ||
| exports.schemaTypes = schemaTypes; | ||
| exports.setDiscriminatorEnum = setDiscriminatorEnum; | ||
| exports.setEnumName = setEnumName; | ||
| exports.nodeDefs = require_utils.nodeDefs; | ||
| exports.objectBindingPatternDef = require_response.objectBindingPatternDef; | ||
| exports.operationDef = require_response.operationDef; | ||
| exports.outputDef = require_response.outputDef; | ||
| exports.parameterDef = require_response.parameterDef; | ||
| exports.propertyDef = require_response.propertyDef; | ||
| exports.requestBodyDef = require_response.requestBodyDef; | ||
| exports.responseDef = require_response.responseDef; | ||
| exports.schemaDef = require_response.schemaDef; | ||
| exports.schemaTypes = require_utils.schemaTypes; | ||
| exports.setDiscriminatorEnum = require_utils.setDiscriminatorEnum; | ||
| exports.setEnumName = require_utils.setEnumName; | ||
| exports.signatureOf = signatureOf; | ||
| exports.simplifyUnion = simplifyUnion; | ||
| exports.sourceDef = require_factory.sourceDef; | ||
| exports.syncOptionality = require_factory.syncOptionality; | ||
| exports.textDef = require_factory.textDef; | ||
| exports.transform = transform; | ||
| exports.typeDef = require_factory.typeDef; | ||
| exports.typeLiteralDef = require_factory.typeLiteralDef; | ||
| exports.walk = walk; | ||
| exports.simplifyUnion = require_utils.simplifyUnion; | ||
| exports.sourceDef = require_response.sourceDef; | ||
| exports.syncOptionality = require_response.syncOptionality; | ||
| exports.textDef = require_response.textDef; | ||
| exports.transform = require_utils.transform; | ||
| exports.typeDef = require_response.typeDef; | ||
| exports.typeLiteralDef = require_response.typeLiteralDef; | ||
| exports.walk = require_utils.walk; | ||
| //# sourceMappingURL=index.cjs.map |
+9
-9
@@ -36,3 +36,3 @@ import { n as __name } from "./chunk-CNktS9qV.js"; | ||
| */ | ||
| declare const nodeDefs: (NodeDef<ConstNode, Omit<ConstNode, "kind">> | NodeDef<TypeNode, Omit<TypeNode, "kind">> | NodeDef<FunctionNode, Omit<FunctionNode, "kind">> | NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">> | NodeDef<TextNode, string> | NodeDef<BreakNode, void> | NodeDef<JsxNode, string> | NodeDef<SchemaNode, (Omit<ObjectSchemaNode, "kind" | "properties" | "primitive"> & { | ||
| declare const nodeDefs: (NodeDef<SchemaNode, (Omit<ObjectSchemaNode, "kind" | "properties" | "primitive"> & { | ||
| properties?: Array<PropertyNode>; | ||
@@ -93,3 +93,3 @@ primitive?: "object"; | ||
| type: "ipv6"; | ||
| }) | ScalarSchemaNode, "kind">> | NodeDef<TypeLiteralNode, Pick<TypeLiteralNode, "members">> | NodeDef<IndexedAccessTypeNode, Omit<IndexedAccessTypeNode, "kind">> | NodeDef<ObjectBindingPatternNode, Pick<ObjectBindingPatternNode, "elements">> | NodeDef<FunctionParameterNode, { | ||
| }) | ScalarSchemaNode, "kind">> | NodeDef<ConstNode, Omit<ConstNode, "kind">> | NodeDef<TypeNode, Omit<TypeNode, "kind">> | NodeDef<FunctionNode, Omit<FunctionNode, "kind">> | NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">> | NodeDef<TextNode, string> | NodeDef<BreakNode, void> | NodeDef<JsxNode, string> | NodeDef<ContentNode, UserContent> | NodeDef<ImportNode, Omit<ImportNode, "kind">> | NodeDef<ExportNode, Omit<ExportNode, "kind">> | NodeDef<SourceNode, Omit<SourceNode, "kind">> | NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">> | NodeDef<TypeLiteralNode, Pick<TypeLiteralNode, "members">> | NodeDef<IndexedAccessTypeNode, Omit<IndexedAccessTypeNode, "kind">> | NodeDef<ObjectBindingPatternNode, Pick<ObjectBindingPatternNode, "elements">> | NodeDef<FunctionParameterNode, { | ||
| name: string; | ||
@@ -108,8 +108,3 @@ type?: TypeExpression; | ||
| default?: string; | ||
| }> | NodeDef<FunctionParametersNode, Partial<Omit<FunctionParametersNode, "kind">>> | NodeDef<ContentNode, UserContent> | NodeDef<ResponseNode, Pick<ResponseNode, "statusCode"> & Partial<Omit<ResponseNode, "kind" | "statusCode" | "content">> & { | ||
| content?: Array<UserContent>; | ||
| schema?: SchemaNode; | ||
| mediaType?: string | null; | ||
| keysToOmit?: Array<string> | null; | ||
| }> | NodeDef<InputNode<false>, Partial<Omit<InputNode<false>, "kind">>> | NodeDef<ImportNode, Omit<ImportNode, "kind">> | NodeDef<ExportNode, Omit<ExportNode, "kind">> | NodeDef<SourceNode, Omit<SourceNode, "kind">> | NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">> | NodeDef<ParameterNode, Pick<ParameterNode, "name" | "schema" | "in"> & Partial<Omit<ParameterNode, "kind" | "name" | "schema" | "in">>> | NodeDef<PropertyNode, UserPropertyNode> | NodeDef<RequestBodyNode, UserRequestBody> | NodeDef<OperationNode, { | ||
| }> | NodeDef<FunctionParametersNode, Partial<Omit<FunctionParametersNode, "kind">>> | NodeDef<InputNode<false>, Partial<Omit<InputNode<false>, "kind">>> | NodeDef<RequestBodyNode, UserRequestBody> | NodeDef<OperationNode, { | ||
| [key: string]: unknown; | ||
@@ -120,3 +115,8 @@ operationId: string; | ||
| requestBody?: UserRequestBody; | ||
| }> | NodeDef<OutputNode, Partial<Omit<OutputNode, "kind">>>)[]; | ||
| }> | NodeDef<OutputNode, Partial<Omit<OutputNode, "kind">>> | NodeDef<ParameterNode, Pick<ParameterNode, "name" | "schema" | "in"> & Partial<Omit<ParameterNode, "kind" | "name" | "schema" | "in">>> | NodeDef<PropertyNode, UserPropertyNode> | NodeDef<ResponseNode, Pick<ResponseNode, "statusCode"> & Partial<Omit<ResponseNode, "kind" | "content" | "statusCode">> & { | ||
| content?: Array<UserContent>; | ||
| schema?: SchemaNode; | ||
| mediaType?: string | null; | ||
| keysToOmit?: Array<string> | null; | ||
| }>)[]; | ||
| //#endregion | ||
@@ -123,0 +123,0 @@ //#region src/signature.d.ts |
+1
-1
| import "./chunk-CNktS9qV.js"; | ||
| import { A as isHttpOperationNode, C as simplifyUnion, D as extractRefName, L as httpMethods, R as schemaTypes, S as setEnumName, b as mergeAdjacentObjectsLazy, d as transform, f as walk, j as narrowSchema, l as collect, p as nodeDefs, u as collectLazy, x as setDiscriminatorEnum } from "./utils-SdZU0F3H.js"; | ||
| import { D as simplifyUnion, E as setEnumName, F as isHttpOperationNode, H as httpMethods, I as narrowSchema, T as setDiscriminatorEnum, U as schemaTypes, _ as nodeDefs, g as walk, h as transform, j as extractRefName, m as collectLazy, p as collect, w as mergeAdjacentObjectsLazy } from "./utils-Dj_KoXMv.js"; | ||
| import { $ as defineNode, A as contentDef, D as fileDef, E as exportDef, G as typeDef, H as functionDef, J as extractStringsFromNodes, M as arrowFunctionDef, N as breakDef, O as importDef, P as constDef, Q as schemaDef, S as typeLiteralDef, U as jsxDef, W as textDef, Z as createSchema, b as indexedAccessTypeDef, c as operationDef, et as syncOptionality, f as inputDef, i as parameterDef, k as sourceDef, n as responseDef, o as outputDef, q as propertyDef, u as requestBodyDef, v as functionParameterDef, x as objectBindingPatternDef, y as functionParametersDef } from "./response-DKxTr522.js"; | ||
@@ -4,0 +4,0 @@ import { n as factory_exports } from "./factory-Du7nEP4B.js"; |
+28
-127
| Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); | ||
| const require_utils = require("./utils-BCtRXfhI.cjs"); | ||
| Object.defineProperty(exports, "buildGroupParam", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.buildGroupParam; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildJSDoc", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.buildJSDoc; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildList", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.buildList; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.buildObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildTypeLiteral", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.buildTypeLiteral; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "caseParams", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.caseParams; | ||
| } | ||
| }); | ||
| const require_response = require("./response-DS5S3IG4.cjs"); | ||
| const require_utils = require("./utils-D83JA6Xx.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; | ||
| Object.defineProperty(exports, "collectUsedSchemaNames", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.collectUsedSchemaNames; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "containsCircularRef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.containsCircularRef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOperationParams", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.createOperationParams; | ||
| } | ||
| }); | ||
| exports.collectUsedSchemaNames = require_utils.collectUsedSchemaNames; | ||
| exports.containsCircularRef = require_utils.containsCircularRef; | ||
| exports.createOperationParams = require_utils.createOperationParams; | ||
| exports.enumPropName = require_utils.enumPropName; | ||
| exports.extractRefName = require_utils.extractRefName; | ||
| Object.defineProperty(exports, "extractStringsFromNodes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.extractStringsFromNodes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "findCircularSchemas", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.findCircularSchemas; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "getNestedAccessor", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.getNestedAccessor; | ||
| } | ||
| }); | ||
| exports.extractStringsFromNodes = require_response.extractStringsFromNodes; | ||
| exports.findCircularSchemas = require_utils.findCircularSchemas; | ||
| exports.getNestedAccessor = require_utils.getNestedAccessor; | ||
| exports.isStringType = require_utils.isStringType; | ||
| Object.defineProperty(exports, "isValidVarName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.isValidVarName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "jsStringEscape", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.jsStringEscape; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "objectKey", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.objectKey; | ||
| } | ||
| }); | ||
| exports.isValidVarName = require_utils.isValidVarName; | ||
| exports.jsStringEscape = require_utils.jsStringEscape; | ||
| exports.lazyGetter = require_utils.lazyGetter; | ||
| exports.mapSchemaItems = require_utils.mapSchemaItems; | ||
| exports.mapSchemaMembers = require_utils.mapSchemaMembers; | ||
| exports.mapSchemaProperties = require_utils.mapSchemaProperties; | ||
| exports.objectKey = require_utils.objectKey; | ||
| exports.resolveGroupType = require_utils.resolveGroupType; | ||
| Object.defineProperty(exports, "resolveParamType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.resolveParamType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringify", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.stringify; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringifyObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.stringifyObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "syncSchemaRef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.syncSchemaRef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "toRegExpString", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.toRegExpString; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "trimQuotes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return require_utils.trimQuotes; | ||
| } | ||
| }); | ||
| exports.resolveParamType = require_utils.resolveParamType; | ||
| exports.resolveRefName = require_utils.resolveRefName; | ||
| 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; |
+91
-2
| import { n as __name } from "./chunk-CNktS9qV.js"; | ||
| import { _t as SchemaNode, f as OperationNode, w as ParameterNode } from "./index-BzjwdK2M.js"; | ||
| import { Et as PropertyNode, St as UnionSchemaNode, _t as SchemaNode, f as OperationNode, ft as ObjectSchemaNode, ot as ArraySchemaNode, ut as IntersectionSchemaNode, w as ParameterNode } from "./index-BzjwdK2M.js"; | ||
| import { a as buildTypeLiteral, c as resolveParamType, i as buildGroupParam, n as OperationParamsResolver, o as caseParams, r as ParamGroupType, s as createOperationParams, t as BuildGroupArgs } from "./operationParams-BZ07xDm0.js"; | ||
@@ -96,2 +96,14 @@ import { o as syncSchemaRef, t as extractStringsFromNodes } from "./extractStringsFromNodes-WMYJ8nQL.js"; | ||
| /** | ||
| * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`. | ||
| * | ||
| * Returns `null` for non-ref nodes or when no name resolves. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) | ||
| * // => 'Pet' | ||
| * ``` | ||
| */ | ||
| declare function resolveRefName(node: SchemaNode | undefined): string | null; | ||
| /** | ||
| * Builds a PascalCase child schema name by joining a parent name and property name. | ||
@@ -243,3 +255,80 @@ * Returns `null` when there is no parent to nest under. | ||
| //#endregion | ||
| export { type BuildGroupArgs, type ParamGroupType, buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, objectKey, resolveGroupType, resolveParamType, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes }; | ||
| //#region src/utils/schemaTraversal.d.ts | ||
| /** | ||
| * Converts a child schema to printer output. Plugins instantiate it with their own output type: | ||
| * `string` for the zod and faker printers, `ts.TypeNode` for the TypeScript printer. A printer's | ||
| * `this.transform` fits directly, so its `null` for an empty result carries through to `output`. | ||
| */ | ||
| type SchemaTransform<TOutput> = (schema: SchemaNode) => TOutput; | ||
| /** | ||
| * A union or intersection member, or an array or tuple item, paired with its transformed output. | ||
| */ | ||
| type MappedSchema<TOutput> = { | ||
| /** | ||
| * The original child schema, kept so the printer can read its metadata for leaf formatting. | ||
| */ | ||
| schema: SchemaNode; | ||
| /** | ||
| * The child schema after being run through the transform. | ||
| */ | ||
| output: TOutput; | ||
| }; | ||
| /** | ||
| * An object property paired with its transformed output. | ||
| */ | ||
| type MappedProperty<TOutput> = { | ||
| /** | ||
| * The property name as written on the schema, before any identifier quoting. | ||
| */ | ||
| name: string; | ||
| /** | ||
| * The original property node, kept so the printer can read `required`, `schema`, and metadata. | ||
| */ | ||
| property: PropertyNode; | ||
| /** | ||
| * The property schema after being run through the transform. | ||
| */ | ||
| output: TOutput; | ||
| }; | ||
| /** | ||
| * Maps each property of an object schema to its transformed output. Pairs every result with the | ||
| * original property so the printer keeps full control over modifiers, getters, and key syntax. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const entries = mapSchemaProperties(node, (schema) => this.transform(schema)) | ||
| * // entries: [{ name: 'id', property, output: 'z.number()' }, ...] | ||
| * ``` | ||
| */ | ||
| declare function mapSchemaProperties<TOutput>(node: ObjectSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedProperty<TOutput>>; | ||
| /** | ||
| * Maps each member of a union or intersection schema to its transformed output, pairing every | ||
| * result with the original member. | ||
| */ | ||
| declare function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionSchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>>; | ||
| /** | ||
| * Maps each item of an array or tuple schema to its transformed output, pairing every result with | ||
| * the original item. | ||
| */ | ||
| declare function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>>; | ||
| /** | ||
| * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key | ||
| * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation | ||
| * of a recursive schema until first access. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' }) | ||
| * // "get parent() { return z.lazy(() => Pet) }" | ||
| * ``` | ||
| */ | ||
| declare function lazyGetter({ | ||
| name, | ||
| body | ||
| }: { | ||
| name: string; | ||
| body: string; | ||
| }): string; | ||
| //#endregion | ||
| export { type BuildGroupArgs, type MappedProperty, type MappedSchema, type ParamGroupType, type SchemaTransform, buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, lazyGetter, mapSchemaItems, mapSchemaMembers, mapSchemaProperties, objectKey, resolveGroupType, resolveParamType, resolveRefName, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes }; | ||
| //# sourceMappingURL=utils.d.ts.map |
+2
-2
@@ -1,3 +0,3 @@ | ||
| import { D as extractRefName, E as enumPropName, F as objectKey, I as isValidVarName, M as buildJSDoc, N as buildList, O as isStringType, P as buildObject, T as childName, _ as stringifyObject, a as resolveParamType, c as findCircularSchemas, g as stringify, h as jsStringEscape, i as createOperationParams, k as resolveGroupType, m as getNestedAccessor, n as buildTypeLiteral, o as collectUsedSchemaNames, r as caseParams, s as containsCircularRef, t as buildGroupParam, v as toRegExpString, w as syncSchemaRef, y as trimQuotes } from "./utils-SdZU0F3H.js"; | ||
| import { A as enumPropName, B as objectKey, C as trimQuotes, L as buildJSDoc, M as isStringType, N as resolveGroupType, O as syncSchemaRef, P as resolveRefName, R as buildList, S as toRegExpString, V as isValidVarName, a as resolveParamType, b as stringify, c as mapSchemaMembers, d as containsCircularRef, f as findCircularSchemas, i as createOperationParams, j as extractRefName, k as childName, l as mapSchemaProperties, n as buildTypeLiteral, o as lazyGetter, r as caseParams, s as mapSchemaItems, t as buildGroupParam, u as collectUsedSchemaNames, v as getNestedAccessor, x as stringifyObject, y as jsStringEscape, z as buildObject } from "./utils-Dj_KoXMv.js"; | ||
| import { J as extractStringsFromNodes } from "./response-DKxTr522.js"; | ||
| export { buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, objectKey, resolveGroupType, resolveParamType, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes }; | ||
| export { buildGroupParam, buildJSDoc, buildList, buildObject, buildTypeLiteral, caseParams, childName, collectUsedSchemaNames, containsCircularRef, createOperationParams, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, getNestedAccessor, isStringType, isValidVarName, jsStringEscape, lazyGetter, mapSchemaItems, mapSchemaMembers, mapSchemaProperties, objectKey, resolveGroupType, resolveParamType, resolveRefName, stringify, stringifyObject, syncSchemaRef, toRegExpString, trimQuotes }; |
+1
-2
| { | ||
| "name": "@kubb/ast", | ||
| "version": "5.0.0-beta.59", | ||
| "version": "5.0.0-beta.60", | ||
| "description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.", | ||
@@ -26,3 +26,2 @@ "keywords": [ | ||
| "type": "module", | ||
| "sideEffects": false, | ||
| "main": "./dist/index.cjs", | ||
@@ -29,0 +28,0 @@ "module": "./dist/index.js", |
| export { isValidVarName } from '@internals/utils' | ||
| export { buildJSDoc, buildList, buildObject, objectKey } from './codegen.ts' | ||
| export { extractStringsFromNodes } from './extractStringsFromNodes.ts' | ||
| export { childName, enumPropName, extractRefName, isStringType, resolveGroupType } from './refs.ts' | ||
| export { childName, enumPropName, extractRefName, isStringType, resolveGroupType, resolveRefName } from './refs.ts' | ||
| export { syncSchemaRef } from '../transformers.ts' | ||
| export { getNestedAccessor, jsStringEscape, stringify, stringifyObject, toRegExpString, trimQuotes } from './strings.ts' | ||
| export { collectUsedSchemaNames, containsCircularRef, findCircularSchemas } from './schemaGraph.ts' | ||
| export { lazyGetter, mapSchemaItems, mapSchemaMembers, mapSchemaProperties } from './schemaTraversal.ts' | ||
| export type { MappedProperty, MappedSchema, SchemaTransform } from './schemaTraversal.ts' | ||
| export { buildGroupParam, buildTypeLiteral, caseParams, createOperationParams, resolveParamType } from './operationParams.ts' | ||
| export type { BuildGroupArgs, ParamGroupType } from './operationParams.ts' |
| //#region \0rolldown/runtime.js | ||
| var __create = Object.create; | ||
| var __defProp = Object.defineProperty; | ||
| var __name = (target, value) => __defProp(target, "name", { | ||
| value, | ||
| configurable: true | ||
| }); | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __getProtoOf = Object.getPrototypeOf; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __exportAll = (all, no_symbols) => { | ||
| let target = {}; | ||
| for (var name in all) __defProp(target, name, { | ||
| get: all[name], | ||
| enumerable: true | ||
| }); | ||
| if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" }); | ||
| return target; | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { | ||
| key = keys[i]; | ||
| if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { | ||
| get: ((k) => from[k]).bind(null, key), | ||
| enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable | ||
| }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { | ||
| value: mod, | ||
| enumerable: true | ||
| }) : target, mod)); | ||
| //#endregion | ||
| //#region ../../internals/utils/src/casing.ts | ||
| /** | ||
| * Shared implementation for camelCase and PascalCase conversion. | ||
| * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons) | ||
| * and capitalizes each word according to `pascal`. | ||
| * | ||
| * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are. | ||
| */ | ||
| function toCamelOrPascal(text, pascal) { | ||
| return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => { | ||
| if (word.length > 1 && word === word.toUpperCase()) return word; | ||
| return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1); | ||
| }).join("").replace(/[^a-zA-Z0-9]/g, ""); | ||
| } | ||
| /** | ||
| * Converts `text` to PascalCase. | ||
| * | ||
| * @example Word boundaries | ||
| * `pascalCase('hello-world') // 'HelloWorld'` | ||
| * | ||
| * @example With a suffix | ||
| * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'` | ||
| */ | ||
| function pascalCase(text, { prefix = "", suffix = "" } = {}) { | ||
| return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true); | ||
| } | ||
| //#endregion | ||
| Object.defineProperty(exports, "__exportAll", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __exportAll; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "__name", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __name; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "__toESM", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return __toESM; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "pascalCase", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return pascalCase; | ||
| } | ||
| }); | ||
| //# sourceMappingURL=casing-BE2R1RXg.cjs.map |
| {"version":3,"file":"casing-BE2R1RXg.cjs","names":[],"sources":["../../../internals/utils/src/casing.ts"],"sourcesContent":["type Options = {\n /**\n * Text prepended before casing is applied.\n */\n prefix?: string\n /**\n * Text appended before casing is applied.\n */\n suffix?: string\n}\n\n/**\n * Shared implementation for camelCase and PascalCase conversion.\n * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)\n * and capitalizes each word according to `pascal`.\n *\n * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.\n */\nfunction toCamelOrPascal(text: string, pascal: boolean): string {\n return text\n .trim()\n .replace(/([a-z\\d])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')\n .replace(/(\\d)([a-z])/g, '$1 $2')\n .split(/[\\s\\-_./\\\\:]+/)\n .filter(Boolean)\n .map((word, i) => {\n if (word.length > 1 && word === word.toUpperCase()) return word\n const head = i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()\n return head + word.slice(1)\n })\n .join('')\n .replace(/[^a-zA-Z0-9]/g, '')\n}\n\n/**\n * Converts `text` to camelCase.\n *\n * @example Word boundaries\n * `camelCase('hello-world') // 'helloWorld'`\n *\n * @example With a prefix\n * `camelCase('tag', { prefix: 'create' }) // 'createTag'`\n */\nexport function camelCase(text: string, { prefix = '', suffix = '' }: Options = {}): string {\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false)\n}\n\n/**\n * Converts `text` to PascalCase.\n *\n * @example Word boundaries\n * `pascalCase('hello-world') // 'HelloWorld'`\n *\n * @example With a suffix\n * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`\n */\nexport function pascalCase(text: string, { prefix = '', suffix = '' }: Options = {}): string {\n return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA,SAAS,gBAAgB,MAAc,QAAyB;CAC9D,OAAO,KACJ,KAAK,CAAC,CACN,QAAQ,qBAAqB,OAAO,CAAC,CACrC,QAAQ,yBAAyB,OAAO,CAAC,CACzC,QAAQ,gBAAgB,OAAO,CAAC,CAChC,MAAM,eAAe,CAAC,CACtB,OAAO,OAAO,CAAC,CACf,KAAK,MAAM,MAAM;EAChB,IAAI,KAAK,SAAS,KAAK,SAAS,KAAK,YAAY,GAAG,OAAO;EAE3D,QADa,MAAM,KAAK,CAAC,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,KAC9E,KAAK,MAAM,CAAC;CAC5B,CAAC,CAAC,CACD,KAAK,EAAE,CAAC,CACR,QAAQ,iBAAiB,EAAE;AAChC;;;;;;;;;;AAwBA,SAAgB,WAAW,MAAc,EAAE,SAAS,IAAI,SAAS,OAAgB,CAAC,GAAW;CAC3F,OAAO,gBAAgB,GAAG,OAAO,GAAG,KAAK,GAAG,UAAU,IAAI;AAC5D"} |
| const require_casing = require("./casing-BE2R1RXg.cjs"); | ||
| let node_crypto = require("node:crypto"); | ||
| let node_path = require("node:path"); | ||
| node_path = require_casing.__toESM(node_path, 1); | ||
| //#region src/node.ts | ||
| /** | ||
| * Builds a type guard that matches nodes of the given `kind`. | ||
| */ | ||
| function isKind(kind) { | ||
| return (node) => node.kind === kind; | ||
| } | ||
| /** | ||
| * Updates a schema's `optional` and `nullish` flags from a parent's `required` | ||
| * value and the schema's own `nullable`. Mirrors how OpenAPI parameters and | ||
| * object properties combine "required" and "nullable" into a single AST. | ||
| * | ||
| * - Non-required + non-nullable → `optional: true`. | ||
| * - Non-required + nullable → `nullish: true`. | ||
| * - Required → both flags cleared. | ||
| */ | ||
| function syncOptionality(schema, required) { | ||
| const nullable = schema.nullable ?? false; | ||
| return { | ||
| ...schema, | ||
| optional: !required && !nullable ? true : void 0, | ||
| nullish: !required && nullable ? true : void 0 | ||
| }; | ||
| } | ||
| /** | ||
| * Defines a node once and derives its `create` builder, `is` guard, and traversal | ||
| * metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the | ||
| * `kind`, so node construction lives in one place without scattered `as` casts. | ||
| * | ||
| * Set `rebuild: true` when the `build` hook derives fields from children. After a | ||
| * transform rewrites those children, the registry reruns `create` so the derived | ||
| * fields stay correct. | ||
| * | ||
| * @example Simple node | ||
| * ```ts | ||
| * const importDef = defineNode<ImportNode>({ kind: 'Import' }) | ||
| * const createImport = importDef.create | ||
| * ``` | ||
| * | ||
| * @example Node with a build hook that is rerun on transform | ||
| * ```ts | ||
| * const propertyDef = defineNode<PropertyNode, UserPropertyNode>({ | ||
| * kind: 'Property', | ||
| * build: (props) => ({ ...props, required: props.required ?? false }), | ||
| * children: ['schema'], | ||
| * visitorKey: 'property', | ||
| * rebuild: true, | ||
| * }) | ||
| * ``` | ||
| */ | ||
| function defineNode(config) { | ||
| const { kind, defaults, build, children, visitorKey, rebuild } = config; | ||
| function create(input) { | ||
| const base = build ? build(input) : input; | ||
| return { | ||
| ...defaults, | ||
| ...base, | ||
| kind | ||
| }; | ||
| } | ||
| return { | ||
| kind, | ||
| create, | ||
| is: isKind(kind), | ||
| children, | ||
| visitorKey, | ||
| rebuild | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/schema.ts | ||
| /** | ||
| * Maps schema `type` to its underlying `primitive`. | ||
| * Primitive types map to themselves. Special string formats map to `'string'`. | ||
| * Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset. | ||
| */ | ||
| const TYPE_TO_PRIMITIVE = { | ||
| string: "string", | ||
| number: "number", | ||
| integer: "integer", | ||
| bigint: "bigint", | ||
| boolean: "boolean", | ||
| null: "null", | ||
| any: "any", | ||
| unknown: "unknown", | ||
| void: "void", | ||
| never: "never", | ||
| object: "object", | ||
| array: "array", | ||
| date: "date", | ||
| uuid: "string", | ||
| email: "string", | ||
| url: "string", | ||
| datetime: "string", | ||
| time: "string" | ||
| }; | ||
| /** | ||
| * Definition for the {@link SchemaNode}. Object schemas default `properties` to an | ||
| * empty array, and `primitive` is inferred from `type` when not explicitly provided. | ||
| */ | ||
| const schemaDef = defineNode({ | ||
| kind: "Schema", | ||
| build: (props) => { | ||
| if (props.type === "object") return { | ||
| properties: [], | ||
| primitive: "object", | ||
| ...props | ||
| }; | ||
| return { | ||
| primitive: TYPE_TO_PRIMITIVE[props.type], | ||
| ...props | ||
| }; | ||
| }, | ||
| children: [ | ||
| "properties", | ||
| "items", | ||
| "members", | ||
| "additionalProperties" | ||
| ], | ||
| visitorKey: "schema" | ||
| }); | ||
| function createSchema(props) { | ||
| return schemaDef.create(props); | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/fs.ts | ||
| /** | ||
| * Strips the file extension from a path or file name. | ||
| * Only removes the last `.ext` segment when the dot is not part of a directory name. | ||
| * | ||
| * @example | ||
| * trimExtName('petStore.ts') // 'petStore' | ||
| * trimExtName('/src/models/pet.ts') // '/src/models/pet' | ||
| * trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet' | ||
| * trimExtName('noExtension') // 'noExtension' | ||
| */ | ||
| function trimExtName(text) { | ||
| const dotIndex = text.lastIndexOf("."); | ||
| if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex); | ||
| return text; | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/code.ts | ||
| /** | ||
| * Definition for the {@link ConstNode}. | ||
| */ | ||
| const constDef = defineNode({ kind: "Const" }); | ||
| /** | ||
| * Creates a `ConstNode` representing a TypeScript `const` declaration. | ||
| * | ||
| * @example Exported constant with type and `as const` | ||
| * ```ts | ||
| * createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true }) | ||
| * // export const pets: Pet[] = ... as const | ||
| * ``` | ||
| */ | ||
| const createConst = constDef.create; | ||
| /** | ||
| * Definition for the {@link TypeNode}. | ||
| */ | ||
| const typeDef = defineNode({ kind: "Type" }); | ||
| /** | ||
| * Creates a `TypeNode` representing a TypeScript `type` alias declaration. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createType({ name: 'Pet', export: true }) | ||
| * // export type Pet = ... | ||
| * ``` | ||
| */ | ||
| const createType = typeDef.create; | ||
| /** | ||
| * Definition for the {@link FunctionNode}. | ||
| */ | ||
| const functionDef = defineNode({ kind: "Function" }); | ||
| /** | ||
| * Creates a `FunctionNode` representing a TypeScript `function` declaration. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' }) | ||
| * // export async function fetchPet(): Promise<Pet> { ... } | ||
| * ``` | ||
| */ | ||
| const createFunction = functionDef.create; | ||
| /** | ||
| * Definition for the {@link ArrowFunctionNode}. | ||
| */ | ||
| const arrowFunctionDef = defineNode({ kind: "ArrowFunction" }); | ||
| /** | ||
| * Creates an `ArrowFunctionNode` representing a TypeScript arrow function. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true }) | ||
| * // export const double = (n: number) => ... | ||
| * ``` | ||
| */ | ||
| const createArrowFunction = arrowFunctionDef.create; | ||
| /** | ||
| * Definition for the {@link TextNode}. | ||
| */ | ||
| const textDef = defineNode({ | ||
| kind: "Text", | ||
| build: (value) => ({ value }) | ||
| }); | ||
| /** | ||
| * Creates a {@link TextNode} representing a raw string fragment in the source output. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createText('return fetch(id)') | ||
| * // { kind: 'Text', value: 'return fetch(id)' } | ||
| * ``` | ||
| */ | ||
| const createText = textDef.create; | ||
| /** | ||
| * Definition for the {@link BreakNode}. | ||
| */ | ||
| const breakDef = defineNode({ | ||
| kind: "Break", | ||
| build: () => ({}) | ||
| }); | ||
| /** | ||
| * Creates a {@link BreakNode} representing a line break in the source output. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createBreak() | ||
| * // { kind: 'Break' } | ||
| * ``` | ||
| */ | ||
| function createBreak() { | ||
| return breakDef.create(); | ||
| } | ||
| /** | ||
| * Definition for the {@link JsxNode}. | ||
| */ | ||
| const jsxDef = defineNode({ | ||
| kind: "Jsx", | ||
| build: (value) => ({ value }) | ||
| }); | ||
| /** | ||
| * Creates a {@link JsxNode} representing a raw JSX fragment in the source output. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createJsx('<>\n <a href={href}>Open</a>\n</>') | ||
| * // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' } | ||
| * ``` | ||
| */ | ||
| const createJsx = jsxDef.create; | ||
| //#endregion | ||
| //#region src/nodes/content.ts | ||
| /** | ||
| * Definition for the {@link ContentNode}. | ||
| */ | ||
| const contentDef = defineNode({ | ||
| kind: "Content", | ||
| children: ["schema"] | ||
| }); | ||
| /** | ||
| * Creates a `ContentNode` for a single request-body or response content type. | ||
| */ | ||
| const createContent = contentDef.create; | ||
| //#endregion | ||
| //#region src/nodes/file.ts | ||
| /** | ||
| * Definition for the {@link ImportNode}. | ||
| */ | ||
| const importDef = defineNode({ kind: "Import" }); | ||
| /** | ||
| * Creates an `ImportNode` representing a language-agnostic import/dependency declaration. | ||
| * | ||
| * @example Named import | ||
| * ```ts | ||
| * createImport({ name: ['useState'], path: 'react' }) | ||
| * // import { useState } from 'react' | ||
| * ``` | ||
| */ | ||
| const createImport = importDef.create; | ||
| /** | ||
| * Definition for the {@link ExportNode}. | ||
| */ | ||
| const exportDef = defineNode({ kind: "Export" }); | ||
| /** | ||
| * Creates an `ExportNode` representing a language-agnostic export/public API declaration. | ||
| * | ||
| * @example Named export | ||
| * ```ts | ||
| * createExport({ name: ['Pet'], path: './Pet' }) | ||
| * // export { Pet } from './Pet' | ||
| * ``` | ||
| */ | ||
| const createExport = exportDef.create; | ||
| /** | ||
| * Definition for the {@link SourceNode}. | ||
| */ | ||
| const sourceDef = defineNode({ kind: "Source" }); | ||
| /** | ||
| * Creates a `SourceNode` representing a fragment of source code within a file. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true }) | ||
| * ``` | ||
| */ | ||
| const createSource = sourceDef.create; | ||
| /** | ||
| * Definition for the {@link FileNode}. The fully resolved builder lives in | ||
| * `createFile`, so this definition only supplies the guard. | ||
| */ | ||
| const fileDef = defineNode({ kind: "File" }); | ||
| //#endregion | ||
| //#region src/nodes/function.ts | ||
| /** | ||
| * Definition for the {@link TypeLiteralNode}. | ||
| */ | ||
| const typeLiteralDef = defineNode({ kind: "TypeLiteral" }); | ||
| /** | ||
| * Creates a {@link TypeLiteralNode} representing an inline anonymous object type. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] }) | ||
| * // { petId: string } | ||
| * ``` | ||
| */ | ||
| const createTypeLiteral = typeLiteralDef.create; | ||
| /** | ||
| * Definition for the {@link IndexedAccessTypeNode}. | ||
| */ | ||
| const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" }); | ||
| /** | ||
| * Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' }) | ||
| * // DeletePetPathParams['petId'] | ||
| * ``` | ||
| */ | ||
| const createIndexedAccessType = indexedAccessTypeDef.create; | ||
| /** | ||
| * Definition for the {@link ObjectBindingPatternNode}. | ||
| */ | ||
| const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" }); | ||
| /** | ||
| * Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] }) | ||
| * // { id, name } | ||
| * ``` | ||
| */ | ||
| const createObjectBindingPattern = objectBindingPatternDef.create; | ||
| /** | ||
| * Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`. | ||
| * Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name | ||
| * paired with a {@link TypeLiteralNode} type. | ||
| */ | ||
| const functionParameterDef = defineNode({ | ||
| kind: "FunctionParameter", | ||
| build: (input) => { | ||
| if ("properties" in input) return { | ||
| name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }), | ||
| type: createTypeLiteral({ members: input.properties.map((p) => ({ | ||
| name: p.name, | ||
| type: p.type, | ||
| optional: p.optional ?? false | ||
| })) }), | ||
| optional: input.optional ?? false, | ||
| ...input.default !== void 0 ? { default: input.default } : {} | ||
| }; | ||
| return { | ||
| optional: false, | ||
| ...input | ||
| }; | ||
| } | ||
| }); | ||
| /** | ||
| * Creates a `FunctionParameterNode`. `optional` defaults to `false`. | ||
| * | ||
| * @example Optional param | ||
| * ```ts | ||
| * createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true }) | ||
| * // → params?: QueryParams | ||
| * ``` | ||
| * | ||
| * @example Destructured group | ||
| * ```ts | ||
| * createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' }) | ||
| * // → { id, name }: { id: string; name?: string } = {} | ||
| * ``` | ||
| */ | ||
| const createFunctionParameter = functionParameterDef.create; | ||
| /** | ||
| * Definition for the {@link FunctionParametersNode}. | ||
| */ | ||
| const functionParametersDef = defineNode({ | ||
| kind: "FunctionParameters", | ||
| defaults: { params: [] } | ||
| }); | ||
| /** | ||
| * Creates a `FunctionParametersNode` from an ordered list of parameters. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const empty = createFunctionParameters() | ||
| * // { kind: 'FunctionParameters', params: [] } | ||
| * ``` | ||
| */ | ||
| function createFunctionParameters(props = {}) { | ||
| return functionParametersDef.create(props); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/input.ts | ||
| /** | ||
| * Definition for the {@link InputNode}. | ||
| */ | ||
| const inputDef = defineNode({ | ||
| kind: "Input", | ||
| defaults: { | ||
| schemas: [], | ||
| operations: [], | ||
| meta: { | ||
| circularNames: [], | ||
| enumNames: [] | ||
| } | ||
| }, | ||
| children: ["schemas", "operations"], | ||
| visitorKey: "input" | ||
| }); | ||
| /** | ||
| * Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and | ||
| * `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the | ||
| * eager variant with array `schemas`/`operations` and the defaulted `meta`. | ||
| * | ||
| * @example Eager | ||
| * ```ts | ||
| * const input = createInput() | ||
| * // { kind: 'Input', schemas: [], operations: [] } | ||
| * ``` | ||
| * | ||
| * @example Streaming | ||
| * ```ts | ||
| * const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } }) | ||
| * ``` | ||
| */ | ||
| function createInput(options = {}) { | ||
| const { stream, ...overrides } = options; | ||
| if (stream) return { | ||
| kind: "Input", | ||
| ...overrides | ||
| }; | ||
| return inputDef.create(overrides); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/requestBody.ts | ||
| /** | ||
| * Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`. | ||
| */ | ||
| const requestBodyDef = defineNode({ | ||
| kind: "RequestBody", | ||
| build: (props) => ({ | ||
| ...props, | ||
| content: props.content?.map(createContent) | ||
| }), | ||
| children: ["content"] | ||
| }); | ||
| /** | ||
| * Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`. | ||
| */ | ||
| const createRequestBody = requestBodyDef.create; | ||
| //#endregion | ||
| //#region src/nodes/operation.ts | ||
| /** | ||
| * Definition for the {@link OperationNode}. HTTP operations (those carrying both | ||
| * `method` and `path`) are tagged with `protocol: 'http'`, and the request body is | ||
| * normalized into a `RequestBodyNode`. | ||
| */ | ||
| const operationDef = defineNode({ | ||
| kind: "Operation", | ||
| build: (props) => { | ||
| const { requestBody, ...rest } = props; | ||
| const isHttp = rest.method !== void 0 && rest.path !== void 0; | ||
| return { | ||
| tags: [], | ||
| parameters: [], | ||
| responses: [], | ||
| ...rest, | ||
| ...isHttp ? { protocol: "http" } : {}, | ||
| requestBody: requestBody ? createRequestBody(requestBody) : void 0 | ||
| }; | ||
| }, | ||
| children: [ | ||
| "parameters", | ||
| "requestBody", | ||
| "responses" | ||
| ], | ||
| visitorKey: "operation" | ||
| }); | ||
| function createOperation(props) { | ||
| return operationDef.create(props); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/output.ts | ||
| /** | ||
| * Definition for the {@link OutputNode}. | ||
| */ | ||
| const outputDef = defineNode({ | ||
| kind: "Output", | ||
| defaults: { files: [] }, | ||
| visitorKey: "output" | ||
| }); | ||
| /** | ||
| * Creates an `OutputNode` with a stable default for `files`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const output = createOutput() | ||
| * // { kind: 'Output', files: [] } | ||
| * ``` | ||
| */ | ||
| function createOutput(overrides = {}) { | ||
| return outputDef.create(overrides); | ||
| } | ||
| //#endregion | ||
| //#region src/nodes/parameter.ts | ||
| /** | ||
| * Definition for the {@link ParameterNode}. `required` defaults to `false` and the | ||
| * schema's `optional`/`nullish` flags are kept in sync with it. | ||
| */ | ||
| const parameterDef = defineNode({ | ||
| kind: "Parameter", | ||
| build: (props) => { | ||
| const required = props.required ?? false; | ||
| return { | ||
| ...props, | ||
| required, | ||
| schema: syncOptionality(props.schema, required) | ||
| }; | ||
| }, | ||
| children: ["schema"], | ||
| visitorKey: "parameter", | ||
| rebuild: true | ||
| }); | ||
| /** | ||
| * Creates a `ParameterNode`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const param = createParameter({ | ||
| * name: 'petId', | ||
| * in: 'path', | ||
| * required: true, | ||
| * schema: createSchema({ type: 'string' }), | ||
| * }) | ||
| * ``` | ||
| */ | ||
| const createParameter = parameterDef.create; | ||
| //#endregion | ||
| //#region src/nodes/property.ts | ||
| /** | ||
| * Definition for the {@link PropertyNode}. `required` defaults to `false` and the | ||
| * schema's `optional`/`nullish` flags are kept in sync with it. | ||
| */ | ||
| const propertyDef = defineNode({ | ||
| kind: "Property", | ||
| build: (props) => { | ||
| const required = props.required ?? false; | ||
| return { | ||
| ...props, | ||
| required, | ||
| schema: syncOptionality(props.schema, required) | ||
| }; | ||
| }, | ||
| children: ["schema"], | ||
| visitorKey: "property", | ||
| rebuild: true | ||
| }); | ||
| /** | ||
| * Creates a `PropertyNode`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const property = createProperty({ | ||
| * name: 'status', | ||
| * required: true, | ||
| * schema: createSchema({ type: 'string', nullable: true }), | ||
| * }) | ||
| * // required=true, no optional/nullish | ||
| * ``` | ||
| */ | ||
| const createProperty = propertyDef.create; | ||
| //#endregion | ||
| //#region src/nodes/response.ts | ||
| /** | ||
| * Definition for the {@link ResponseNode}. A single legacy `schema` (with optional | ||
| * `mediaType`/`keysToOmit`) is normalized into one `content` entry. | ||
| */ | ||
| const responseDef = defineNode({ | ||
| kind: "Response", | ||
| build: (props) => { | ||
| const { schema, mediaType, keysToOmit, content, ...rest } = props; | ||
| const entries = content ?? (schema ? [{ | ||
| contentType: mediaType ?? "application/json", | ||
| schema, | ||
| keysToOmit: keysToOmit ?? null | ||
| }] : void 0); | ||
| return { | ||
| ...rest, | ||
| content: entries?.map(createContent) | ||
| }; | ||
| }, | ||
| children: ["content"], | ||
| visitorKey: "response" | ||
| }); | ||
| /** | ||
| * Creates a `ResponseNode`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const response = createResponse({ | ||
| * statusCode: '200', | ||
| * content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }], | ||
| * }) | ||
| * ``` | ||
| */ | ||
| const createResponse = responseDef.create; | ||
| //#endregion | ||
| //#region src/utils/extractStringsFromNodes.ts | ||
| /** | ||
| * Extracts all string content from a `CodeNode` tree recursively. | ||
| * | ||
| * Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`), | ||
| * and nested node content. Used to build the full source string for import filtering. | ||
| */ | ||
| function extractStringsFromNodes(nodes) { | ||
| if (!nodes?.length) return ""; | ||
| return nodes.map((node) => { | ||
| if (typeof node === "string") return node; | ||
| if (node.kind === "Text") return node.value; | ||
| if (node.kind === "Break") return ""; | ||
| if (node.kind === "Jsx") return node.value; | ||
| const parts = []; | ||
| if ("params" in node && node.params) parts.push(node.params); | ||
| if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics); | ||
| if ("returnType" in node && node.returnType) parts.push(node.returnType); | ||
| if ("type" in node && typeof node.type === "string") parts.push(node.type); | ||
| const nested = extractStringsFromNodes(node.nodes); | ||
| if (nested) parts.push(nested); | ||
| return parts.join("\n"); | ||
| }).filter(Boolean).join("\n"); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/fileMerge.ts | ||
| function sourceKey(source) { | ||
| return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`; | ||
| } | ||
| function pathTypeKey(path, isTypeOnly) { | ||
| return `${path}:${isTypeOnly ?? false}`; | ||
| } | ||
| function exportKey(path, name, isTypeOnly, asAlias) { | ||
| return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`; | ||
| } | ||
| function importKey(path, name, isTypeOnly) { | ||
| return `${path}:${name ?? ""}:${isTypeOnly ?? false}`; | ||
| } | ||
| /** | ||
| * Computes a multi-level sort key for exports and imports: | ||
| * non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named. | ||
| */ | ||
| function sortKey(node) { | ||
| const isArray = Array.isArray(node.name) ? "1" : "0"; | ||
| const typeOnly = node.isTypeOnly ? "0" : "1"; | ||
| const hasName = node.name != null ? "1" : "0"; | ||
| const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? ""; | ||
| return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`; | ||
| } | ||
| /** | ||
| * Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`. | ||
| * | ||
| * Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order. | ||
| */ | ||
| function combineSources(sources) { | ||
| const seen = /* @__PURE__ */ new Map(); | ||
| for (const source of sources) { | ||
| const key = sourceKey(source); | ||
| if (!seen.has(key)) seen.set(key, source); | ||
| } | ||
| return [...seen.values()]; | ||
| } | ||
| /** | ||
| * Merges `incoming` names into `existing`, preserving order and dropping duplicates. | ||
| * | ||
| * Shared by `combineExports` and `combineImports` for the same-path name-merge case. | ||
| */ | ||
| function mergeNameArrays(existing, incoming) { | ||
| const merged = new Set(existing); | ||
| for (const name of incoming) merged.add(name); | ||
| return [...merged]; | ||
| } | ||
| /** | ||
| * Deduplicates and merges `ExportNode` objects by path and type. | ||
| * | ||
| * Named exports with the same path and `isTypeOnly` flag have their names merged into a single export. | ||
| * Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array. | ||
| */ | ||
| function combineExports(exports) { | ||
| const result = []; | ||
| const namedByPath = /* @__PURE__ */ new Map(); | ||
| const seen = /* @__PURE__ */ new Set(); | ||
| const keyed = exports.map((node) => ({ | ||
| node, | ||
| key: sortKey(node) | ||
| })); | ||
| keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0); | ||
| for (const { node: curr } of keyed) { | ||
| const { name, path, isTypeOnly, asAlias } = curr; | ||
| if (Array.isArray(name)) { | ||
| if (!name.length) continue; | ||
| const key = pathTypeKey(path, isTypeOnly); | ||
| const existing = namedByPath.get(key); | ||
| if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name); | ||
| else { | ||
| const newItem = { | ||
| ...curr, | ||
| name: [...new Set(name)] | ||
| }; | ||
| result.push(newItem); | ||
| namedByPath.set(key, newItem); | ||
| } | ||
| } else { | ||
| const key = exportKey(path, name, isTypeOnly, asAlias); | ||
| if (!seen.has(key)) { | ||
| result.push(curr); | ||
| seen.add(key); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| /** | ||
| * Deduplicates and merges `ImportNode` objects, filtering out unused imports. | ||
| * | ||
| * Retains imports that are referenced in `source` or re-exported. Imports with the same path and | ||
| * `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array. | ||
| */ | ||
| function combineImports(imports, exports, source) { | ||
| const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])); | ||
| const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName); | ||
| const importNameMemo = /* @__PURE__ */ new Map(); | ||
| const canonicalizeName = (n) => { | ||
| if (typeof n === "string") return n; | ||
| const key = `${n.propertyName}:${n.name ?? ""}`; | ||
| if (!importNameMemo.has(key)) importNameMemo.set(key, n); | ||
| return importNameMemo.get(key); | ||
| }; | ||
| const pathsWithUsedNamedImport = /* @__PURE__ */ new Set(); | ||
| for (const node of imports) { | ||
| if (!Array.isArray(node.name)) continue; | ||
| if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path); | ||
| } | ||
| const result = []; | ||
| const namedByPath = /* @__PURE__ */ new Map(); | ||
| const seen = /* @__PURE__ */ new Set(); | ||
| const keyed = imports.map((node) => ({ | ||
| node, | ||
| key: sortKey(node) | ||
| })); | ||
| keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0); | ||
| for (const { node: curr } of keyed) { | ||
| if (curr.path === curr.root) continue; | ||
| const { path, isTypeOnly } = curr; | ||
| let { name } = curr; | ||
| if (Array.isArray(name)) { | ||
| name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName)); | ||
| if (!name.length) continue; | ||
| const key = pathTypeKey(path, isTypeOnly); | ||
| const existing = namedByPath.get(key); | ||
| if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name); | ||
| else { | ||
| const newItem = { | ||
| ...curr, | ||
| name | ||
| }; | ||
| result.push(newItem); | ||
| namedByPath.set(key, newItem); | ||
| } | ||
| } else { | ||
| if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue; | ||
| const key = importKey(path, name, isTypeOnly); | ||
| if (!seen.has(key)) { | ||
| result.push(curr); | ||
| seen.add(key); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| //#endregion | ||
| //#region src/factory.ts | ||
| var factory_exports = /* @__PURE__ */ require_casing.__exportAll({ | ||
| createArrowFunction: () => createArrowFunction, | ||
| createBreak: () => createBreak, | ||
| createConst: () => createConst, | ||
| createContent: () => createContent, | ||
| createExport: () => createExport, | ||
| createFile: () => createFile, | ||
| createFunction: () => createFunction, | ||
| createFunctionParameter: () => createFunctionParameter, | ||
| createFunctionParameters: () => createFunctionParameters, | ||
| createImport: () => createImport, | ||
| createIndexedAccessType: () => createIndexedAccessType, | ||
| createInput: () => createInput, | ||
| createJsx: () => createJsx, | ||
| createObjectBindingPattern: () => createObjectBindingPattern, | ||
| createOperation: () => createOperation, | ||
| createOutput: () => createOutput, | ||
| createParameter: () => createParameter, | ||
| createProperty: () => createProperty, | ||
| createRequestBody: () => createRequestBody, | ||
| createResponse: () => createResponse, | ||
| createSchema: () => createSchema, | ||
| createSource: () => createSource, | ||
| createText: () => createText, | ||
| createType: () => createType, | ||
| createTypeLiteral: () => createTypeLiteral, | ||
| update: () => update | ||
| }); | ||
| /** | ||
| * Identity-preserving node update: returns `node` unchanged when every field in | ||
| * `changes` already equals (by reference) the current value, otherwise a new node | ||
| * with the changes applied. | ||
| * | ||
| * Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the | ||
| * structural sharing in {@link transform} so a no-op rewrite doesn't allocate and | ||
| * downstream passes can detect "nothing changed" by identity. Comparison is | ||
| * shallow: a structurally-equal but newly-allocated array/object counts as a change. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * update(node, { name: node.name }) // -> same `node` reference | ||
| * update(node, { name: 'renamed' }) // -> new node, `name` replaced | ||
| * ``` | ||
| */ | ||
| function update(node, changes) { | ||
| for (const key in changes) if (changes[key] !== node[key]) return { | ||
| ...node, | ||
| ...changes | ||
| }; | ||
| return node; | ||
| } | ||
| /** | ||
| * Creates a fully resolved `FileNode` from a file input descriptor. | ||
| * | ||
| * Computes: | ||
| * - `id` SHA256 hash of the file path | ||
| * - `name` `baseName` without extension | ||
| * - `extname` extension extracted from `baseName` | ||
| * | ||
| * Deduplicates: | ||
| * - `sources` via `combineSources` | ||
| * - `exports` via `combineExports` | ||
| * - `imports` via `combineImports` (also filters unused imports) | ||
| * | ||
| * @throws {Error} when `baseName` has no extension. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const file = createFile({ | ||
| * baseName: 'petStore.ts', | ||
| * path: 'src/models/petStore.ts', | ||
| * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })], | ||
| * imports: [createImport({ name: ['z'], path: 'zod' })], | ||
| * exports: [createExport({ name: ['Pet'], path: './petStore' })], | ||
| * }) | ||
| * // file.id = SHA256 hash of 'src/models/petStore.ts' | ||
| * // file.name = 'petStore' | ||
| * // file.extname = '.ts' | ||
| * ``` | ||
| */ | ||
| function createFile(input) { | ||
| const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : ""); | ||
| if (!extname) throw new Error(`No extname found for ${input.baseName}`); | ||
| const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n"); | ||
| const resolvedExports = input.exports?.length ? combineExports(input.exports) : []; | ||
| const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : []; | ||
| const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name))); | ||
| const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName; | ||
| const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => { | ||
| if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp]; | ||
| const kept = imp.name.filter((item) => !localNames.has(nameOf(item))); | ||
| if (!kept.length) return []; | ||
| return [kept.length === imp.name.length ? imp : { | ||
| ...imp, | ||
| name: kept | ||
| }]; | ||
| }); | ||
| const resolvedSources = input.sources?.length ? combineSources(input.sources) : []; | ||
| return { | ||
| kind: "File", | ||
| ...input, | ||
| id: (0, node_crypto.hash)("sha256", input.path, "hex"), | ||
| name: trimExtName(input.baseName), | ||
| extname, | ||
| imports: resolvedImports, | ||
| exports: resolvedExports, | ||
| sources: resolvedSources, | ||
| meta: input.meta ?? {} | ||
| }; | ||
| } | ||
| //#endregion | ||
| Object.defineProperty(exports, "arrowFunctionDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return arrowFunctionDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "breakDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return breakDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "constDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return constDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "contentDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return contentDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createArrowFunction", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createArrowFunction; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createBreak", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createBreak; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createConst", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createConst; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createContent", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createContent; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createExport", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createExport; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFile", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFile; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFunction", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFunction; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFunctionParameter", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFunctionParameter; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createFunctionParameters", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createFunctionParameters; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createImport", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createImport; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createIndexedAccessType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createIndexedAccessType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createInput", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createInput; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createJsx", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createJsx; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createObjectBindingPattern", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createObjectBindingPattern; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOperation", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createOperation; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOutput", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createOutput; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createParameter", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createParameter; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createProperty", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createProperty; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createRequestBody", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createRequestBody; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createResponse", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createResponse; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createSchema", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createSchema; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createSource", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createSource; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createText", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createText; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createTypeLiteral", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createTypeLiteral; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "defineNode", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return defineNode; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "exportDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return exportDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "extractStringsFromNodes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return extractStringsFromNodes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "factory_exports", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return factory_exports; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "fileDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return fileDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "functionDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return functionDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "functionParameterDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return functionParameterDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "functionParametersDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return functionParametersDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "importDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return importDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "indexedAccessTypeDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return indexedAccessTypeDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "inputDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return inputDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "jsxDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return jsxDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "objectBindingPatternDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return objectBindingPatternDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "operationDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return operationDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "outputDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return outputDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "parameterDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return parameterDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "propertyDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return propertyDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "requestBodyDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return requestBodyDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "responseDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return responseDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "schemaDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return schemaDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "sourceDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return sourceDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "syncOptionality", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return syncOptionality; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "textDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return textDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "typeDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return typeDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "typeLiteralDef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return typeLiteralDef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "update", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return update; | ||
| } | ||
| }); | ||
| //# sourceMappingURL=factory-BmcGBdeg.cjs.map |
Sorry, the diff of this file is too big to display
| const require_casing = require("./casing-BE2R1RXg.cjs"); | ||
| //#region src/guards.ts | ||
| /** | ||
| * Narrows a `SchemaNode` to the variant that matches `type`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ type: 'string' }) | ||
| * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null | ||
| * ``` | ||
| */ | ||
| function narrowSchema(node, type) { | ||
| return node?.type === type ? node : null; | ||
| } | ||
| /** | ||
| * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * if (isHttpOperationNode(node)) { | ||
| * console.log(node.method, node.path) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function isHttpOperationNode(node) { | ||
| return node.protocol === "http" || node.method !== void 0 && node.path !== void 0; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/refs.ts | ||
| const plainStringTypes = new Set([ | ||
| "string", | ||
| "uuid", | ||
| "email", | ||
| "url", | ||
| "datetime" | ||
| ]); | ||
| /** | ||
| * Returns the last path segment of a reference string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * extractRefName('#/components/schemas/Pet') // 'Pet' | ||
| * ``` | ||
| */ | ||
| function extractRefName(ref) { | ||
| return ref.split("/").at(-1) ?? ref; | ||
| } | ||
| /** | ||
| * Builds a PascalCase child schema name by joining a parent name and property name. | ||
| * Returns `null` when there is no parent to nest under. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * childName('Order', 'shipping_address') // 'OrderShippingAddress' | ||
| * childName(undefined, 'params') // null | ||
| * ``` | ||
| */ | ||
| function childName(parentName, propName) { | ||
| return parentName ? require_casing.pascalCase([parentName, propName].join(" ")) : null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any | ||
| * empty parts. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum' | ||
| * ``` | ||
| */ | ||
| function enumPropName(parentName, propName, enumSuffix) { | ||
| return require_casing.pascalCase([ | ||
| parentName, | ||
| propName, | ||
| enumSuffix | ||
| ].filter(Boolean).join(" ")); | ||
| } | ||
| /** | ||
| * Type guard that returns `true` when a schema emits as a plain `string` type. | ||
| * | ||
| * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time` | ||
| * types, returns `true` only when `representation` is `'string'` rather than `'date'`. | ||
| */ | ||
| function isStringType(node) { | ||
| if (plainStringTypes.has(node.type)) return true; | ||
| const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time"); | ||
| if (temporal) return temporal.representation !== "date"; | ||
| return false; | ||
| } | ||
| /** | ||
| * Derives a {@link ParamGroupType} for a query or header group from the resolver. | ||
| * | ||
| * Returns `null` when there is no resolver, no params, or the group name equals the | ||
| * individual param name (so there is no real group to emit). | ||
| */ | ||
| function resolveGroupType({ node, params, group, resolver }) { | ||
| if (!resolver || !params.length) return null; | ||
| const firstParam = params[0]; | ||
| const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam); | ||
| if (groupName === resolver.resolveParamName(node, firstParam)) return null; | ||
| return { | ||
| type: groupName, | ||
| optional: params.every((p) => !p.required) | ||
| }; | ||
| } | ||
| //#endregion | ||
| Object.defineProperty(exports, "buildGroupParam", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildGroupParam; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildJSDoc", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildJSDoc; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildList", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildList; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "buildTypeLiteral", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return buildTypeLiteral; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "caseParams", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return caseParams; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "childName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return childName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "collectUsedSchemaNames", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return collectUsedSchemaNames; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "containsCircularRef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return containsCircularRef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "createOperationParams", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return createOperationParams; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "enumPropName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return enumPropName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "extractRefName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return extractRefName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "extractStringsFromNodes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return extractStringsFromNodes; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "findCircularSchemas", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return findCircularSchemas; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "getNestedAccessor", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return getNestedAccessor; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isHttpOperationNode", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isHttpOperationNode; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isStringType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isStringType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "isValidVarName", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return isValidVarName; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "jsStringEscape", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return jsStringEscape; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "narrowSchema", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return narrowSchema; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "objectKey", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return objectKey; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "resolveGroupType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return resolveGroupType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "resolveParamType", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return resolveParamType; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringify", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return stringify; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "stringifyObject", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return stringifyObject; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "syncSchemaRef", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return syncSchemaRef; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "toRegExpString", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return toRegExpString; | ||
| } | ||
| }); | ||
| Object.defineProperty(exports, "trimQuotes", { | ||
| enumerable: true, | ||
| get: function() { | ||
| return trimQuotes; | ||
| } | ||
| }); | ||
| //# sourceMappingURL=utils-BCtRXfhI.cjs.map |
| {"version":3,"file":"utils-BCtRXfhI.cjs","names":["pascalCase"],"sources":["../src/guards.ts","../src/utils/refs.ts"],"sourcesContent":["import type { HttpOperationNode, OperationNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'\n\n/**\n * Narrows a `SchemaNode` to the variant that matches `type`.\n *\n * @example\n * ```ts\n * const schema = createSchema({ type: 'string' })\n * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null\n * ```\n */\nexport function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {\n return node?.type === type ? (node as SchemaNodeByType[T]) : null\n}\n\n/**\n * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.\n *\n * @example\n * ```ts\n * if (isHttpOperationNode(node)) {\n * console.log(node.method, node.path)\n * }\n * ```\n */\nexport function isHttpOperationNode(node: OperationNode): node is HttpOperationNode {\n return node.protocol === 'http' || (node.method !== undefined && node.path !== undefined)\n}\n","import { pascalCase } from '@internals/utils'\nimport { narrowSchema } from '../guards.ts'\nimport type { OperationNode, ParameterNode, SchemaNode } from '../nodes/index.ts'\nimport type { SchemaType } from '../nodes/schema.ts'\nimport type { OperationParamsResolver, ParamGroupType } from './operationParams.ts'\n\nconst plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)\n\n/**\n * Returns the last path segment of a reference string.\n *\n * @example\n * ```ts\n * extractRefName('#/components/schemas/Pet') // 'Pet'\n * ```\n */\nexport function extractRefName(ref: string): string {\n return ref.split('/').at(-1) ?? ref\n}\n\n/**\n * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.\n *\n * Returns `null` for non-ref nodes or when no name resolves.\n *\n * @example\n * ```ts\n * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })\n * // => 'Pet'\n * ```\n */\nexport function resolveRefName(node: SchemaNode | undefined): string | null {\n if (!node || node.type !== 'ref') return null\n if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null\n\n return node.name ?? node.schema?.name ?? null\n}\n\n/**\n * Builds a PascalCase child schema name by joining a parent name and property name.\n * Returns `null` when there is no parent to nest under.\n *\n * @example\n * ```ts\n * childName('Order', 'shipping_address') // 'OrderShippingAddress'\n * childName(undefined, 'params') // null\n * ```\n */\nexport function childName(parentName: string | null | undefined, propName: string): string | null {\n return parentName ? pascalCase([parentName, propName].join(' ')) : null\n}\n\n/**\n * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any\n * empty parts.\n *\n * @example\n * ```ts\n * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'\n * ```\n */\nexport function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {\n return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))\n}\n\n/**\n * Type guard that returns `true` when a schema emits as a plain `string` type.\n *\n * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`\n * types, returns `true` only when `representation` is `'string'` rather than `'date'`.\n */\nexport function isStringType(node: SchemaNode): boolean {\n if (plainStringTypes.has(node.type)) {\n return true\n }\n\n const temporal = narrowSchema(node, 'date') ?? narrowSchema(node, 'time')\n if (temporal) {\n return temporal.representation !== 'date'\n }\n\n return false\n}\n\n/**\n * Derives a {@link ParamGroupType} for a query or header group from the resolver.\n *\n * Returns `null` when there is no resolver, no params, or the group name equals the\n * individual param name (so there is no real group to emit).\n */\nexport function resolveGroupType({\n node,\n params,\n group,\n resolver,\n}: {\n node: OperationNode\n params: Array<ParameterNode>\n group: 'query' | 'header'\n resolver: OperationParamsResolver | undefined\n}): ParamGroupType | null {\n if (!resolver || !params.length) {\n return null\n }\n const firstParam = params[0]!\n const groupMethod = group === 'query' ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName\n const groupName = groupMethod.call(resolver, node, firstParam)\n if (groupName === resolver.resolveParamName(node, firstParam)) {\n return null\n }\n return { type: groupName, optional: params.every((p) => !p.required) }\n}\n"],"mappings":";;;;;;;;;;;AAWA,SAAgB,aAA2C,MAA8B,MAAqC;CAC5H,OAAO,MAAM,SAAS,OAAQ,OAA+B;AAC/D;;;;;;;;;;;AAYA,SAAgB,oBAAoB,MAAgD;CAClF,OAAO,KAAK,aAAa,UAAW,KAAK,WAAW,KAAA,KAAa,KAAK,SAAS,KAAA;AACjF;;;ACrBA,MAAM,mBAAmB,IAAI,IAAgB;CAAC;CAAU;CAAQ;CAAS;CAAO;AAAU,CAAU;;;;;;;;;AAUpG,SAAgB,eAAe,KAAqB;CAClD,OAAO,IAAI,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;AAClC;;;;;;;;;;;AA8BA,SAAgB,UAAU,YAAuC,UAAiC;CAChG,OAAO,aAAaA,eAAAA,WAAW,CAAC,YAAY,QAAQ,CAAC,CAAC,KAAK,GAAG,CAAC,IAAI;AACrE;;;;;;;;;;AAWA,SAAgB,aAAa,YAAuC,UAAkB,YAA4B;CAChH,OAAOA,eAAAA,WAAW;EAAC;EAAY;EAAU;CAAU,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;AAChF;;;;;;;AAQA,SAAgB,aAAa,MAA2B;CACtD,IAAI,iBAAiB,IAAI,KAAK,IAAI,GAChC,OAAO;CAGT,MAAM,WAAW,aAAa,MAAM,MAAM,KAAK,aAAa,MAAM,MAAM;CACxE,IAAI,UACF,OAAO,SAAS,mBAAmB;CAGrC,OAAO;AACT;;;;;;;AAQA,SAAgB,iBAAiB,EAC/B,MACA,QACA,OACA,YAMwB;CACxB,IAAI,CAAC,YAAY,CAAC,OAAO,QACvB,OAAO;CAET,MAAM,aAAa,OAAO;CAE1B,MAAM,aADc,UAAU,UAAU,SAAS,yBAAyB,SAAS,wBAAA,CACrD,KAAK,UAAU,MAAM,UAAU;CAC7D,IAAI,cAAc,SAAS,iBAAiB,MAAM,UAAU,GAC1D,OAAO;CAET,OAAO;EAAE,MAAM;EAAW,UAAU,OAAO,OAAO,MAAM,CAAC,EAAE,QAAQ;CAAE;AACvE"} |
| import "./chunk-CNktS9qV.js"; | ||
| import { A as contentDef, D as fileDef, E as exportDef, G as typeDef, H as functionDef, K as createProperty, M as arrowFunctionDef, N as breakDef, O as importDef, P as constDef, Q as schemaDef, S as typeLiteralDef, U as jsxDef, W as textDef, X as pascalCase, Y as camelCase, Z as createSchema, _ as createTypeLiteral, b as indexedAccessTypeDef, c as operationDef, f as inputDef, h as createIndexedAccessType, i as parameterDef, k as sourceDef, m as createFunctionParameters, n as responseDef, o as outputDef, p as createFunctionParameter, q as propertyDef, u as requestBodyDef, v as functionParameterDef, x as objectBindingPatternDef, y as functionParametersDef } from "./response-DKxTr522.js"; | ||
| //#region src/constants.ts | ||
| const visitorDepths = { | ||
| shallow: "shallow", | ||
| deep: "deep" | ||
| }; | ||
| /** | ||
| * Schema type discriminators used by all AST schema nodes. | ||
| * | ||
| * Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`). | ||
| * Call `isScalarPrimitive()` to check for the scalar types. | ||
| */ | ||
| const schemaTypes = { | ||
| /** | ||
| * Text value. | ||
| */ | ||
| string: "string", | ||
| /** | ||
| * Floating-point number (`float`, `double`). | ||
| */ | ||
| number: "number", | ||
| /** | ||
| * Whole number (`int32`). Use `bigint` for `int64`. | ||
| */ | ||
| integer: "integer", | ||
| /** | ||
| * 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`. | ||
| */ | ||
| bigint: "bigint", | ||
| /** | ||
| * Boolean value. | ||
| */ | ||
| boolean: "boolean", | ||
| /** | ||
| * Explicit null value. | ||
| */ | ||
| null: "null", | ||
| /** | ||
| * Any value (no type restriction). | ||
| */ | ||
| any: "any", | ||
| /** | ||
| * Unknown value (must be narrowed before usage). | ||
| */ | ||
| unknown: "unknown", | ||
| /** | ||
| * No return value (`void`). | ||
| */ | ||
| void: "void", | ||
| /** | ||
| * Object with named properties. | ||
| */ | ||
| object: "object", | ||
| /** | ||
| * Sequential list of items. | ||
| */ | ||
| array: "array", | ||
| /** | ||
| * Fixed-length list with position-specific items. | ||
| */ | ||
| tuple: "tuple", | ||
| /** | ||
| * "One of" multiple schema members. | ||
| */ | ||
| union: "union", | ||
| /** | ||
| * "All of" multiple schema members. | ||
| */ | ||
| intersection: "intersection", | ||
| /** | ||
| * Enum schema. | ||
| */ | ||
| enum: "enum", | ||
| /** | ||
| * Reference to another schema. | ||
| */ | ||
| ref: "ref", | ||
| /** | ||
| * Calendar date (for example `2026-03-24`). | ||
| */ | ||
| date: "date", | ||
| /** | ||
| * Date-time value (for example `2026-03-24T09:00:00Z`). | ||
| */ | ||
| datetime: "datetime", | ||
| /** | ||
| * Time-only value (for example `09:00:00`). | ||
| */ | ||
| time: "time", | ||
| /** | ||
| * UUID value. | ||
| */ | ||
| uuid: "uuid", | ||
| /** | ||
| * Email address value. | ||
| */ | ||
| email: "email", | ||
| /** | ||
| * URL value. | ||
| */ | ||
| url: "url", | ||
| /** | ||
| * IPv4 address value. | ||
| */ | ||
| ipv4: "ipv4", | ||
| /** | ||
| * IPv6 address value. | ||
| */ | ||
| ipv6: "ipv6", | ||
| /** | ||
| * Binary/blob value. | ||
| */ | ||
| blob: "blob", | ||
| /** | ||
| * Impossible value (`never`). | ||
| */ | ||
| never: "never" | ||
| }; | ||
| /** | ||
| * Scalar primitive schema types used for union simplification and type narrowing. | ||
| */ | ||
| const SCALAR_PRIMITIVE_TYPES = new Set([ | ||
| "string", | ||
| "number", | ||
| "integer", | ||
| "bigint", | ||
| "boolean" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `type` is a scalar primitive that can be assigned without wrapping | ||
| * (for example `string | number | boolean`). | ||
| */ | ||
| function isScalarPrimitive(type) { | ||
| return SCALAR_PRIMITIVE_TYPES.has(type); | ||
| } | ||
| /** | ||
| * HTTP method identifiers used by operation nodes. | ||
| */ | ||
| const httpMethods = { | ||
| get: "GET", | ||
| post: "POST", | ||
| put: "PUT", | ||
| patch: "PATCH", | ||
| delete: "DELETE", | ||
| head: "HEAD", | ||
| options: "OPTIONS", | ||
| trace: "TRACE" | ||
| }; | ||
| /** | ||
| * One indentation level, derived from {@link INDENT_SIZE}. | ||
| */ | ||
| const INDENT = Array.from({ length: 2 }, () => " ").join(""); | ||
| //#endregion | ||
| //#region ../../internals/utils/src/promise.ts | ||
| /** | ||
| * Wraps `factory` with a keyed cache backed by the provided store. | ||
| * | ||
| * Pass a `WeakMap` for object keys (results are GC-eligible when the key is | ||
| * collected) or a `Map` for primitive keys. For multi-argument functions, | ||
| * nest two `memoize` calls — the outer keyed by the first argument, the | ||
| * inner (created once per outer miss) keyed by the second. | ||
| * | ||
| * Because the cache is owned by the caller, it can be shared, inspected, or | ||
| * cleared independently of the memoized function. | ||
| * | ||
| * @example Single WeakMap key | ||
| * ```ts | ||
| * const cache = new WeakMap<SchemaNode, Set<string>>() | ||
| * const getRefs = memoize(cache, (node) => collectRefs(node)) | ||
| * ``` | ||
| * | ||
| * @example Single Map key (primitive) | ||
| * ```ts | ||
| * const cache = new Map<string, Resolver>() | ||
| * const getResolver = memoize(cache, (name) => buildResolver(name)) | ||
| * ``` | ||
| * | ||
| * @example Two-level (object + primitive) | ||
| * ```ts | ||
| * const outer = new WeakMap<Params[], Map<string, Params[]>>() | ||
| * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key))) | ||
| * fn(params)('camelcase') | ||
| * ``` | ||
| */ | ||
| function memoize(store, factory) { | ||
| return (key) => { | ||
| if (store.has(key)) return store.get(key); | ||
| const value = factory(key); | ||
| store.set(key, value); | ||
| return value; | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/reserved.ts | ||
| /** | ||
| * JavaScript and Java reserved words. | ||
| * @link https://github.com/jonschlinkert/reserved/blob/master/index.js | ||
| */ | ||
| const reservedWords = new Set([ | ||
| "abstract", | ||
| "arguments", | ||
| "boolean", | ||
| "break", | ||
| "byte", | ||
| "case", | ||
| "catch", | ||
| "char", | ||
| "class", | ||
| "const", | ||
| "continue", | ||
| "debugger", | ||
| "default", | ||
| "delete", | ||
| "do", | ||
| "double", | ||
| "else", | ||
| "enum", | ||
| "eval", | ||
| "export", | ||
| "extends", | ||
| "false", | ||
| "final", | ||
| "finally", | ||
| "float", | ||
| "for", | ||
| "function", | ||
| "goto", | ||
| "if", | ||
| "implements", | ||
| "import", | ||
| "in", | ||
| "instanceof", | ||
| "int", | ||
| "interface", | ||
| "let", | ||
| "long", | ||
| "native", | ||
| "new", | ||
| "null", | ||
| "package", | ||
| "private", | ||
| "protected", | ||
| "public", | ||
| "return", | ||
| "short", | ||
| "static", | ||
| "super", | ||
| "switch", | ||
| "synchronized", | ||
| "this", | ||
| "throw", | ||
| "throws", | ||
| "transient", | ||
| "true", | ||
| "try", | ||
| "typeof", | ||
| "var", | ||
| "void", | ||
| "volatile", | ||
| "while", | ||
| "with", | ||
| "yield", | ||
| "Array", | ||
| "Date", | ||
| "hasOwnProperty", | ||
| "Infinity", | ||
| "isFinite", | ||
| "isNaN", | ||
| "isPrototypeOf", | ||
| "length", | ||
| "Math", | ||
| "name", | ||
| "NaN", | ||
| "Number", | ||
| "Object", | ||
| "prototype", | ||
| "String", | ||
| "toString", | ||
| "undefined", | ||
| "valueOf" | ||
| ]); | ||
| /** | ||
| * Returns `true` when `name` is a syntactically valid JavaScript variable name. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * isValidVarName('status') // true | ||
| * isValidVarName('class') // false (reserved word) | ||
| * isValidVarName('42foo') // false (starts with digit) | ||
| * ``` | ||
| */ | ||
| function isValidVarName(name) { | ||
| if (!name || reservedWords.has(name)) return false; | ||
| return isIdentifier(name); | ||
| } | ||
| /** | ||
| * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words. | ||
| * | ||
| * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys | ||
| * even though they are not valid variable names, so use this (not {@link isValidVarName}) when | ||
| * deciding whether an object key needs quoting. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * isIdentifier('name') // true | ||
| * isIdentifier('x-total')// false | ||
| * ``` | ||
| */ | ||
| function isIdentifier(name) { | ||
| return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name); | ||
| } | ||
| //#endregion | ||
| //#region ../../internals/utils/src/string.ts | ||
| /** | ||
| * Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping | ||
| * any backslash or single quote in the content. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * singleQuote('foo') // "'foo'" | ||
| * singleQuote("o'clock") // "'o\\'clock'" | ||
| * ``` | ||
| */ | ||
| function singleQuote(value) { | ||
| if (value === void 0 || value === null) return "''"; | ||
| return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/codegen.ts | ||
| /** | ||
| * Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no | ||
| * comments so callers always get a usable string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildJSDoc(['@type string', '@example hello']) | ||
| * // '/**\n * @type string\n * @example hello\n *\/\n ' | ||
| * ``` | ||
| */ | ||
| function buildJSDoc(comments, options = {}) { | ||
| const { indent = " * ", suffix = "\n ", fallback = " " } = options; | ||
| if (comments.length === 0) return fallback; | ||
| return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`; | ||
| } | ||
| /** | ||
| * Indents every non-empty line of `text` by one indent level, leaving blank lines empty. | ||
| */ | ||
| function indentLines(text) { | ||
| if (!text) return ""; | ||
| return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n"); | ||
| } | ||
| /** | ||
| * Renders an object key, quoting it with single quotes only when it is not a valid identifier. | ||
| * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * objectKey('name') // 'name' | ||
| * objectKey('x-total') // "'x-total'" | ||
| * ``` | ||
| */ | ||
| function objectKey(name) { | ||
| return isIdentifier(name) ? name : singleQuote(name); | ||
| } | ||
| /** | ||
| * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one | ||
| * level and closing the brace at column zero. Nested objects built the same way indent cumulatively, | ||
| * so callers never re-parse the generated code. A trailing comma is added per entry to match the | ||
| * formatter's multi-line style. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildObject(['id: z.number()', 'name: z.string()']) | ||
| * // '{\n id: z.number(),\n name: z.string(),\n}' | ||
| * ``` | ||
| */ | ||
| function buildObject(entries) { | ||
| if (entries.length === 0) return "{}"; | ||
| return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`; | ||
| } | ||
| /** | ||
| * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on | ||
| * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented | ||
| * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`, | ||
| * `z.array([…])`, and similar member lists so objects inside them nest correctly. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * buildList(['z.string()', 'z.number()']) | ||
| * // '[z.string(), z.number()]' | ||
| * ``` | ||
| */ | ||
| function buildList(items, brackets = ["[", "]"]) { | ||
| const [open, close] = brackets; | ||
| if (items.length === 0) return `${open}${close}`; | ||
| if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`; | ||
| return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`; | ||
| } | ||
| //#endregion | ||
| //#region src/guards.ts | ||
| /** | ||
| * Narrows a `SchemaNode` to the variant that matches `type`. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ type: 'string' }) | ||
| * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null | ||
| * ``` | ||
| */ | ||
| function narrowSchema(node, type) { | ||
| return node?.type === type ? node : null; | ||
| } | ||
| /** | ||
| * Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * if (isHttpOperationNode(node)) { | ||
| * console.log(node.method, node.path) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function isHttpOperationNode(node) { | ||
| return node.protocol === "http" || node.method !== void 0 && node.path !== void 0; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/refs.ts | ||
| const plainStringTypes = new Set([ | ||
| "string", | ||
| "uuid", | ||
| "email", | ||
| "url", | ||
| "datetime" | ||
| ]); | ||
| /** | ||
| * Returns the last path segment of a reference string. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * extractRefName('#/components/schemas/Pet') // 'Pet' | ||
| * ``` | ||
| */ | ||
| function extractRefName(ref) { | ||
| return ref.split("/").at(-1) ?? ref; | ||
| } | ||
| /** | ||
| * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`. | ||
| * | ||
| * Returns `null` for non-ref nodes or when no name resolves. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) | ||
| * // => 'Pet' | ||
| * ``` | ||
| */ | ||
| function resolveRefName(node) { | ||
| if (!node || node.type !== "ref") return null; | ||
| if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null; | ||
| return node.name ?? node.schema?.name ?? null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase child schema name by joining a parent name and property name. | ||
| * Returns `null` when there is no parent to nest under. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * childName('Order', 'shipping_address') // 'OrderShippingAddress' | ||
| * childName(undefined, 'params') // null | ||
| * ``` | ||
| */ | ||
| function childName(parentName, propName) { | ||
| return parentName ? pascalCase([parentName, propName].join(" ")) : null; | ||
| } | ||
| /** | ||
| * Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any | ||
| * empty parts. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum' | ||
| * ``` | ||
| */ | ||
| function enumPropName(parentName, propName, enumSuffix) { | ||
| return pascalCase([ | ||
| parentName, | ||
| propName, | ||
| enumSuffix | ||
| ].filter(Boolean).join(" ")); | ||
| } | ||
| /** | ||
| * Type guard that returns `true` when a schema emits as a plain `string` type. | ||
| * | ||
| * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time` | ||
| * types, returns `true` only when `representation` is `'string'` rather than `'date'`. | ||
| */ | ||
| function isStringType(node) { | ||
| if (plainStringTypes.has(node.type)) return true; | ||
| const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time"); | ||
| if (temporal) return temporal.representation !== "date"; | ||
| return false; | ||
| } | ||
| /** | ||
| * Derives a {@link ParamGroupType} for a query or header group from the resolver. | ||
| * | ||
| * Returns `null` when there is no resolver, no params, or the group name equals the | ||
| * individual param name (so there is no real group to emit). | ||
| */ | ||
| function resolveGroupType({ node, params, group, resolver }) { | ||
| if (!resolver || !params.length) return null; | ||
| const firstParam = params[0]; | ||
| const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam); | ||
| if (groupName === resolver.resolveParamName(node, firstParam)) return null; | ||
| return { | ||
| type: groupName, | ||
| optional: params.every((p) => !p.required) | ||
| }; | ||
| } | ||
| //#endregion | ||
| //#region src/transformers.ts | ||
| /** | ||
| * Replaces a discriminator property's schema with a string enum of allowed values. | ||
| * | ||
| * If `node` is not an object schema, or if the property does not exist, the input | ||
| * node is returned as-is. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const schema = createSchema({ | ||
| * type: 'object', | ||
| * properties: [createProperty({ name: 'type', required: true, schema: createSchema({ type: 'string' }) })], | ||
| * }) | ||
| * const result = setDiscriminatorEnum({ node: schema, propertyName: 'type', values: ['dog', 'cat'] }) | ||
| * ``` | ||
| */ | ||
| function setDiscriminatorEnum({ node, propertyName, values, enumName }) { | ||
| const objectNode = narrowSchema(node, "object"); | ||
| if (!objectNode?.properties?.length) return node; | ||
| if (!objectNode.properties.some((prop) => prop.name === propertyName)) return node; | ||
| return createSchema({ | ||
| ...objectNode, | ||
| properties: objectNode.properties.map((prop) => { | ||
| if (prop.name !== propertyName) return prop; | ||
| return createProperty({ | ||
| ...prop, | ||
| schema: createSchema({ | ||
| type: "enum", | ||
| primitive: "string", | ||
| enumValues: values, | ||
| name: enumName, | ||
| readOnly: prop.schema.readOnly, | ||
| writeOnly: prop.schema.writeOnly | ||
| }) | ||
| }); | ||
| }) | ||
| }); | ||
| } | ||
| /** | ||
| * Merges adjacent anonymous object members into a single anonymous object member. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const merged = mergeAdjacentObjects([ | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'a', schema: createSchema({ type: 'string' }) })] }), | ||
| * createSchema({ type: 'object', properties: [createProperty({ name: 'b', schema: createSchema({ type: 'number' }) })] }), | ||
| * ]) | ||
| * ``` | ||
| */ | ||
| function* mergeAdjacentObjectsLazy(members) { | ||
| let acc; | ||
| for (const member of members) { | ||
| const objectMember = narrowSchema(member, "object"); | ||
| if (objectMember && !objectMember.name && acc !== void 0) { | ||
| const accObject = narrowSchema(acc, "object"); | ||
| if (accObject && !accObject.name) { | ||
| acc = createSchema({ | ||
| ...accObject, | ||
| properties: [...accObject.properties ?? [], ...objectMember.properties ?? []] | ||
| }); | ||
| continue; | ||
| } | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| acc = member; | ||
| } | ||
| if (acc !== void 0) yield acc; | ||
| } | ||
| /** | ||
| * Removes enum members that are covered by broader scalar primitives in the same union. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const simplified = simplifyUnion([ | ||
| * createSchema({ type: 'enum', primitive: 'string', enumValues: ['active'] }), | ||
| * createSchema({ type: 'string' }), | ||
| * ]) | ||
| * // keeps only string member | ||
| * ``` | ||
| */ | ||
| function simplifyUnion(members) { | ||
| const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type)); | ||
| if (!scalarPrimitives.size) return members; | ||
| return members.filter((member) => { | ||
| const enumNode = narrowSchema(member, "enum"); | ||
| if (!enumNode) return true; | ||
| const primitive = enumNode.primitive; | ||
| if (!primitive) return true; | ||
| if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true; | ||
| if (scalarPrimitives.has(primitive)) return false; | ||
| if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false; | ||
| return true; | ||
| }); | ||
| } | ||
| function setEnumName(propNode, parentName, propName, enumSuffix) { | ||
| const enumNode = narrowSchema(propNode, "enum"); | ||
| if (enumNode?.primitive === "boolean") return { | ||
| ...propNode, | ||
| name: null | ||
| }; | ||
| if (enumNode) return { | ||
| ...propNode, | ||
| name: enumPropName(parentName, propName, enumSuffix) | ||
| }; | ||
| return propNode; | ||
| } | ||
| /** | ||
| * Merges a ref node with its resolved schema, giving usage-site fields precedence. | ||
| * | ||
| * Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override | ||
| * the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ref with description override | ||
| * const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' }) | ||
| * const merged = syncSchemaRef(ref) // merges with resolved Pet schema | ||
| * ``` | ||
| */ | ||
| function syncSchemaRef(node) { | ||
| const ref = narrowSchema(node, "ref"); | ||
| if (!ref) return node; | ||
| if (!ref.schema) return node; | ||
| const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref; | ||
| const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0)); | ||
| return createSchema({ | ||
| ...ref.schema, | ||
| ...definedOverrides | ||
| }); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/strings.ts | ||
| /** | ||
| * Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`. | ||
| * Returns the string unchanged when no balanced quote pair is found. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * trimQuotes('"hello"') // 'hello' | ||
| * trimQuotes('hello') // 'hello' | ||
| * ``` | ||
| */ | ||
| function trimQuotes(text) { | ||
| if (text.length >= 2) { | ||
| const first = text[0]; | ||
| const last = text[text.length - 1]; | ||
| if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1); | ||
| } | ||
| return text; | ||
| } | ||
| /** | ||
| * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first. | ||
| * | ||
| * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated | ||
| * code matches the repo style without a formatter. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * stringify('hello') // "'hello'" | ||
| * stringify('"hello"') // "'hello'" | ||
| * ``` | ||
| */ | ||
| function stringify(value) { | ||
| if (value === void 0 || value === null) return "''"; | ||
| return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`; | ||
| } | ||
| /** | ||
| * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes, | ||
| * and the Unicode line terminators U+2028 and U+2029. | ||
| * | ||
| * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4 | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye' | ||
| * ``` | ||
| */ | ||
| function jsStringEscape(input) { | ||
| return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => { | ||
| switch (character) { | ||
| case "\"": | ||
| case "'": | ||
| case "\\": return `\\${character}`; | ||
| case "\n": return "\\n"; | ||
| case "\r": return "\\r"; | ||
| case "\u2028": return "\\u2028"; | ||
| case "\u2029": return "\\u2029"; | ||
| default: return ""; | ||
| } | ||
| }); | ||
| } | ||
| /** | ||
| * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string. | ||
| * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression. | ||
| * Pass `null` as the second argument to emit a `/pattern/flags` literal instead. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")' | ||
| * toRegExpString('^(?im)foo', null) // '/^foo/im' | ||
| * ``` | ||
| */ | ||
| function toRegExpString(text, func = "RegExp") { | ||
| const raw = trimQuotes(text); | ||
| const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i); | ||
| const replacementTarget = match?.[1] ?? ""; | ||
| const matchedFlags = match?.[2]; | ||
| const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, ""); | ||
| const { source, flags } = new RegExp(cleaned, matchedFlags); | ||
| if (func === null) return `/${source}/${flags}`; | ||
| return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`; | ||
| } | ||
| /** | ||
| * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested | ||
| * objects recurse with fixed indentation, so the result drops straight into an object literal | ||
| * without re-parsing. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * stringifyObject({ foo: 'bar', nested: { a: 1 } }) | ||
| * // 'foo: bar,\nnested: {\n a: 1\n }' | ||
| * ``` | ||
| */ | ||
| function stringifyObject(value) { | ||
| return Object.entries(value).map(([key, val]) => { | ||
| if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`; | ||
| return `${key}: ${val}`; | ||
| }).filter(Boolean).join(",\n"); | ||
| } | ||
| /** | ||
| * Renders a dotted path or string array as an optional-chaining accessor expression rooted at | ||
| * `accessor`. Returns `null` for an empty path. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * getNestedAccessor('pagination.next.id', 'lastPage') | ||
| * // "lastPage?.['pagination']?.['next']?.['id']" | ||
| * ``` | ||
| */ | ||
| function getNestedAccessor(param, accessor) { | ||
| const parts = Array.isArray(param) ? param : param.split("."); | ||
| if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null; | ||
| return `${accessor}?.['${`${parts.join("']?.['")}']`}`; | ||
| } | ||
| //#endregion | ||
| //#region src/registry.ts | ||
| /** | ||
| * Every node definition. Adding a node means adding its `defineNode` to one | ||
| * `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it. | ||
| */ | ||
| const nodeDefs = [ | ||
| inputDef, | ||
| outputDef, | ||
| operationDef, | ||
| requestBodyDef, | ||
| contentDef, | ||
| responseDef, | ||
| schemaDef, | ||
| propertyDef, | ||
| parameterDef, | ||
| functionParameterDef, | ||
| functionParametersDef, | ||
| typeLiteralDef, | ||
| indexedAccessTypeDef, | ||
| objectBindingPatternDef, | ||
| constDef, | ||
| typeDef, | ||
| functionDef, | ||
| arrowFunctionDef, | ||
| textDef, | ||
| breakDef, | ||
| jsxDef, | ||
| importDef, | ||
| exportDef, | ||
| sourceDef, | ||
| fileDef | ||
| ]; | ||
| //#endregion | ||
| //#region src/visitor.ts | ||
| /** | ||
| * Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`). | ||
| * Derived from each definition's `children`. | ||
| */ | ||
| const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : [])); | ||
| /** | ||
| * Maps a node kind to the matching visitor callback name. Derived from each | ||
| * definition's `visitorKey`. | ||
| */ | ||
| const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : [])); | ||
| /** | ||
| * Per-kind builders rerun after children are rebuilt. Derived from each | ||
| * definition's `rebuild` flag. | ||
| */ | ||
| const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : [])); | ||
| /** | ||
| * Creates a small async concurrency limiter. | ||
| * | ||
| * At most `concurrency` tasks are in flight at once. Extra tasks are queued. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const limit = createLimit(2) | ||
| * for (const task of [taskA, taskB, taskC]) { | ||
| * await limit(() => task()) | ||
| * } | ||
| * // only 2 tasks run at the same time | ||
| * ``` | ||
| */ | ||
| function createLimit(concurrency) { | ||
| let active = 0; | ||
| const queue = []; | ||
| function next() { | ||
| if (active < concurrency && queue.length > 0) { | ||
| active++; | ||
| queue.shift()(); | ||
| } | ||
| } | ||
| return function limit(fn) { | ||
| return new Promise((resolve, reject) => { | ||
| queue.push(() => { | ||
| Promise.resolve(fn()).then(resolve, reject).finally(() => { | ||
| active--; | ||
| next(); | ||
| }); | ||
| }); | ||
| next(); | ||
| }); | ||
| }; | ||
| } | ||
| const visitorKeysByKind = VISITOR_KEYS; | ||
| /** | ||
| * Returns `true` when `value` is an AST node (an object carrying a `kind`). | ||
| */ | ||
| function isNode(value) { | ||
| return typeof value === "object" && value !== null && "kind" in value; | ||
| } | ||
| /** | ||
| * Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}. | ||
| * | ||
| * `Schema` children are only included when `recurse` is `true`. Shallow mode skips them. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const children = getChildren(operationNode, true) | ||
| * // returns parameters, the request body, and responses | ||
| * ``` | ||
| */ | ||
| function* getChildren(node, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return; | ||
| const record = node; | ||
| for (const key of keys) { | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) if (isNode(item)) yield item; | ||
| } else if (isNode(value)) yield value; | ||
| } | ||
| } | ||
| /** | ||
| * Runs the visitor callback that matches `node.kind` with the traversal | ||
| * context. The result is a replacement node, a collected value, or `undefined` | ||
| * when no callback is registered for the kind. | ||
| * | ||
| * Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives | ||
| * in one place. `TResult` is the caller's expected return: the same node type | ||
| * for `transform`, the collected value type for `collectLazy`, ignored for `walk`. | ||
| */ | ||
| function applyVisitor(node, visitor, parent) { | ||
| const key = VISITOR_KEY_BY_KIND[node.kind]; | ||
| if (!key) return void 0; | ||
| const fn = visitor[key]; | ||
| return fn?.(node, { parent }); | ||
| } | ||
| /** | ||
| * Async depth-first traversal for side effects. Visitor return values are | ||
| * ignored. Use `transform` when you want to rewrite nodes. | ||
| * | ||
| * Sibling nodes at each depth run concurrently up to `options.concurrency` | ||
| * (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor | ||
| * work. Lower values reduce memory pressure. | ||
| * | ||
| * @example Log every operation | ||
| * ```ts | ||
| * await walk(root, { | ||
| * operation(node) { | ||
| * console.log(node.operationId) | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| * | ||
| * @example Only visit the root node | ||
| * ```ts | ||
| * await walk(root, { depth: 'shallow', input: () => {} }) | ||
| * ``` | ||
| */ | ||
| async function walk(node, options) { | ||
| return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0); | ||
| } | ||
| async function _walk(node, visitor, recurse, limit, parent) { | ||
| await limit(() => applyVisitor(node, visitor, parent)); | ||
| const children = Array.from(getChildren(node, recurse)); | ||
| if (children.length === 0) return; | ||
| await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node))); | ||
| } | ||
| function transform(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse); | ||
| if (rebuilt === node) return node; | ||
| const rebuild = nodeRebuilders[rebuilt.kind]; | ||
| return rebuild ? rebuild(rebuilt) : rebuilt; | ||
| } | ||
| /** | ||
| * Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming | ||
| * each child node and leaving non-node values (e.g. `additionalProperties: true`) intact. | ||
| * `Schema` children are skipped in shallow mode. | ||
| */ | ||
| function transformChildren(node, options, recurse) { | ||
| if (node.kind === "Schema" && !recurse) return node; | ||
| const keys = visitorKeysByKind[node.kind]; | ||
| if (!keys) return node; | ||
| const record = node; | ||
| const childOptions = { | ||
| ...options, | ||
| parent: node | ||
| }; | ||
| let updates; | ||
| for (const key of keys) { | ||
| if (!(key in record)) continue; | ||
| const value = record[key]; | ||
| if (Array.isArray(value)) { | ||
| let changed = false; | ||
| const mapped = value.map((item) => { | ||
| if (!isNode(item)) return item; | ||
| const next = transform(item, childOptions); | ||
| if (next !== item) changed = true; | ||
| return next; | ||
| }); | ||
| if (changed) (updates ??= {})[key] = mapped; | ||
| } else if (isNode(value)) { | ||
| const next = transform(value, childOptions); | ||
| if (next !== value) (updates ??= {})[key] = next; | ||
| } | ||
| } | ||
| return updates ? { | ||
| ...node, | ||
| ...updates | ||
| } : node; | ||
| } | ||
| /** | ||
| * Lazy depth-first collection pass. Yields every non-null value returned by | ||
| * the visitor callbacks. Use `collect` for the eager array form. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids: string[] = [] | ||
| * for (const id of collectLazy<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * })) { | ||
| * ids.push(id) | ||
| * } | ||
| * ``` | ||
| */ | ||
| function* collectLazy(node, options) { | ||
| const { depth, parent, ...visitor } = options; | ||
| const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep; | ||
| const v = applyVisitor(node, visitor, parent); | ||
| if (v != null) yield v; | ||
| for (const child of getChildren(node, recurse)) yield* collectLazy(child, { | ||
| ...options, | ||
| parent: node | ||
| }); | ||
| } | ||
| /** | ||
| * Eager depth-first collection pass. Gathers every non-null value the visitor | ||
| * callbacks return into an array. | ||
| * | ||
| * @example Collect every operationId | ||
| * ```ts | ||
| * const ids = collect<string>(root, { | ||
| * operation(node) { | ||
| * return node.operationId | ||
| * }, | ||
| * }) | ||
| * ``` | ||
| */ | ||
| function collect(node, options) { | ||
| return Array.from(collectLazy(node, options)); | ||
| } | ||
| //#endregion | ||
| //#region src/utils/schemaGraph.ts | ||
| /** | ||
| * Collects every named schema referenced transitively from a node through its ref edges. | ||
| * | ||
| * Refs are followed by name only, so the resolved `node.schema` is never traversed inline. | ||
| * | ||
| * @example Collect refs from a single schema | ||
| * ```ts | ||
| * const names = collectReferencedSchemaNames(petSchema) | ||
| * // → Set { 'Category', 'Tag' } | ||
| * ``` | ||
| * | ||
| * @example Accumulate refs from multiple schemas into one set | ||
| * ```ts | ||
| * const out = new Set<string>() | ||
| * for (const schema of schemas) { | ||
| * collectReferencedSchemaNames(schema, out) | ||
| * } | ||
| * ``` | ||
| */ | ||
| const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => { | ||
| const refs = /* @__PURE__ */ new Set(); | ||
| collect(node, { schema(child) { | ||
| if (child.type === "ref") { | ||
| const name = resolveRefName(child); | ||
| if (name) refs.add(name); | ||
| } | ||
| } }); | ||
| return refs; | ||
| }); | ||
| function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) { | ||
| if (!node) return out; | ||
| for (const name of collectSchemaRefs(node)) out.add(name); | ||
| return out; | ||
| } | ||
| /** | ||
| * Collects the names of all top-level schemas transitively used by a set of operations. | ||
| * | ||
| * An operation uses a schema when its parameters, request body, or responses reference it, directly | ||
| * or through other named schemas. The walk is iterative, so reference cycles are safe. | ||
| * | ||
| * Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated. | ||
| * | ||
| * @example Only generate schemas referenced by included operations | ||
| * ```ts | ||
| * const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null) | ||
| * const allowed = collectUsedSchemaNames(includedOps, schemas) | ||
| * | ||
| * for (const schema of schemas) { | ||
| * if (schema.name && !allowed.has(schema.name)) continue | ||
| * // … generate schema | ||
| * } | ||
| * ``` | ||
| */ | ||
| const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas))); | ||
| function computeUsedSchemaNames(operations, schemas) { | ||
| const schemaMap = /* @__PURE__ */ new Map(); | ||
| for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema); | ||
| const result = /* @__PURE__ */ new Set(); | ||
| function visitSchema(schema) { | ||
| const directRefs = collectReferencedSchemaNames(schema); | ||
| for (const name of directRefs) if (!result.has(name)) { | ||
| result.add(name); | ||
| const namedSchema = schemaMap.get(name); | ||
| if (namedSchema) visitSchema(namedSchema); | ||
| } | ||
| } | ||
| for (const op of operations) for (const schema of collectLazy(op, { | ||
| depth: "shallow", | ||
| schema: (node) => node | ||
| })) visitSchema(schema); | ||
| return result; | ||
| } | ||
| function collectUsedSchemaNames(operations, schemas) { | ||
| return collectUsedSchemaNamesMemo(operations)(schemas); | ||
| } | ||
| const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set(); | ||
| const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => { | ||
| const graph = /* @__PURE__ */ new Map(); | ||
| for (const schema of schemas) { | ||
| if (!schema.name) continue; | ||
| graph.set(schema.name, collectReferencedSchemaNames(schema)); | ||
| } | ||
| const circular = /* @__PURE__ */ new Set(); | ||
| for (const start of graph.keys()) { | ||
| const visited = /* @__PURE__ */ new Set(); | ||
| const stack = [...graph.get(start) ?? []]; | ||
| while (stack.length > 0) { | ||
| const node = stack.pop(); | ||
| if (node === start) { | ||
| circular.add(start); | ||
| break; | ||
| } | ||
| if (visited.has(node)) continue; | ||
| visited.add(node); | ||
| const next = graph.get(node); | ||
| if (next) for (const r of next) stack.push(r); | ||
| } | ||
| } | ||
| return circular; | ||
| }); | ||
| /** | ||
| * Finds every schema that takes part in a circular dependency chain, including direct self-loops. | ||
| * | ||
| * Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so | ||
| * the generated code does not recurse forever. Refs are followed by name only, so the walk stays | ||
| * linear in the size of the schema graph. | ||
| * | ||
| * @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`. | ||
| */ | ||
| function findCircularSchemas(schemas) { | ||
| if (schemas.length === 0) return EMPTY_CIRCULAR_SET; | ||
| return findCircularSchemasMemo(schemas); | ||
| } | ||
| /** | ||
| * Returns `true` when a schema, or anything nested inside it, references a circular schema. | ||
| * | ||
| * Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled | ||
| * on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go. | ||
| * | ||
| * @note Stops at the first matching circular ref. | ||
| */ | ||
| function containsCircularRef(node, { circularSchemas, excludeName }) { | ||
| if (!node || circularSchemas.size === 0) return false; | ||
| for (const _ of collectLazy(node, { schema(child) { | ||
| if (child.type !== "ref") return null; | ||
| const name = resolveRefName(child); | ||
| return name && name !== excludeName && circularSchemas.has(name) ? true : null; | ||
| } })) return true; | ||
| return false; | ||
| } | ||
| //#endregion | ||
| //#region src/utils/operationParams.ts | ||
| /** | ||
| * Applies casing rules to parameter names and returns a new array without mutating the input. | ||
| * | ||
| * Run it before handing parameters to schema builders so output property keys get the right casing | ||
| * while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the | ||
| * original array is returned unchanged. | ||
| */ | ||
| const caseParamsMemo = memoize(/* @__PURE__ */ new WeakMap(), (params) => memoize(/* @__PURE__ */ new Map(), (casing) => params.map((param) => { | ||
| const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name; | ||
| return { | ||
| ...param, | ||
| name: transformed | ||
| }; | ||
| }))); | ||
| function caseParams(params, casing) { | ||
| if (!casing) return params; | ||
| return caseParamsMemo(params)(casing); | ||
| } | ||
| /** | ||
| * Resolves the {@link TypeExpression} for an individual parameter. | ||
| * | ||
| * Without a resolver, it falls back to the schema primitive (a plain type-name string). When the | ||
| * parameter belongs to a named group, it emits an {@link IndexedAccessTypeNode} like | ||
| * `GroupParams['petId']`, otherwise the resolved individual name. | ||
| */ | ||
| function resolveParamType({ node, param, resolver }) { | ||
| if (!resolver) return param.schema.primitive ?? "unknown"; | ||
| const individualName = resolver.resolveParamName(node, param); | ||
| const groupLocation = param.in === "path" || param.in === "query" || param.in === "header" ? param.in : void 0; | ||
| const groupResolvers = { | ||
| path: resolver.resolvePathParamsName, | ||
| query: resolver.resolveQueryParamsName, | ||
| header: resolver.resolveHeaderParamsName | ||
| }; | ||
| const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : void 0; | ||
| if (groupName && groupName !== individualName) return createIndexedAccessType({ | ||
| objectType: groupName, | ||
| indexType: param.name | ||
| }); | ||
| return individualName; | ||
| } | ||
| /** | ||
| * Converts an `OperationNode` into function parameters for code generation. | ||
| * | ||
| * Centralizes parameter grouping logic for all plugins. `paramsType` chooses between one | ||
| * destructured object parameter (`object`) and separate top-level parameters (`inline`), while | ||
| * `pathParamsType` controls how path params render in inline mode. Provide a `resolver` for type | ||
| * name resolution and `extraParams` for plugin-specific trailing parameters such as an `options` object. | ||
| */ | ||
| function createOperationParams(node, options) { | ||
| const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options; | ||
| const dataName = paramNames?.data ?? "data"; | ||
| const paramsName = paramNames?.params ?? "params"; | ||
| const headersName = paramNames?.headers ?? "headers"; | ||
| const pathName = paramNames?.path ?? "pathParams"; | ||
| const wrapType = (type) => typeWrapper ? typeWrapper(type) : type; | ||
| const wrapTypeExpression = (type) => typeof type === "string" ? wrapType(type) : type; | ||
| const casedParams = caseParams(node.parameters, paramsCasing); | ||
| const pathParams = casedParams.filter((p) => p.in === "path"); | ||
| const queryParams = casedParams.filter((p) => p.in === "query"); | ||
| const headerParams = casedParams.filter((p) => p.in === "header"); | ||
| const toProperty = (param) => ({ | ||
| name: param.name, | ||
| type: wrapTypeExpression(resolveParamType({ | ||
| node, | ||
| param, | ||
| resolver | ||
| })), | ||
| optional: !param.required | ||
| }); | ||
| const emptyObjectDefault = (props) => props.every((p) => p.optional) ? "{}" : void 0; | ||
| const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? "unknown") : void 0; | ||
| const bodyProperty = bodyType ? [{ | ||
| name: dataName, | ||
| type: bodyType, | ||
| optional: !(node.requestBody?.required ?? false) | ||
| }] : []; | ||
| const trailingGroups = [{ | ||
| name: paramsName, | ||
| node, | ||
| params: queryParams, | ||
| groupType: resolveGroupType({ | ||
| node, | ||
| params: queryParams, | ||
| group: "query", | ||
| resolver | ||
| }), | ||
| resolver, | ||
| wrapType | ||
| }, { | ||
| name: headersName, | ||
| node, | ||
| params: headerParams, | ||
| groupType: resolveGroupType({ | ||
| node, | ||
| params: headerParams, | ||
| group: "header", | ||
| resolver | ||
| }), | ||
| resolver, | ||
| wrapType | ||
| }]; | ||
| const params = []; | ||
| if (paramsType === "object") { | ||
| const children = [ | ||
| ...pathParams.map(toProperty), | ||
| ...bodyProperty, | ||
| ...trailingGroups.flatMap(buildGroupProperty) | ||
| ]; | ||
| if (children.length) params.push(createFunctionParameter({ | ||
| properties: children, | ||
| default: emptyObjectDefault(children) | ||
| })); | ||
| } else { | ||
| if (pathParamsType === "inlineSpread" && pathParams.length) { | ||
| const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]); | ||
| params.push(createFunctionParameter({ | ||
| name: pathName, | ||
| type: spreadType ? wrapType(spreadType) : void 0, | ||
| rest: true | ||
| })); | ||
| } else if (pathParamsType === "inline") params.push(...pathParams.map((p) => createFunctionParameter(toProperty(p)))); | ||
| else if (pathParams.length) { | ||
| const pathChildren = pathParams.map(toProperty); | ||
| params.push(createFunctionParameter({ | ||
| properties: pathChildren, | ||
| default: pathParamsDefault ?? emptyObjectDefault(pathChildren) | ||
| })); | ||
| } | ||
| params.push(...bodyProperty.map((p) => createFunctionParameter(p))); | ||
| params.push(...trailingGroups.flatMap(buildGroupParam)); | ||
| } | ||
| params.push(...extraParams); | ||
| return createFunctionParameters({ params }); | ||
| } | ||
| /** | ||
| * Builds the property descriptor for a query or header group. | ||
| * Returns an empty array when there are no params to emit. | ||
| * | ||
| * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline | ||
| * {@link TypeLiteralNode} from the individual params. | ||
| */ | ||
| function buildGroupProperty({ name, node, params, groupType, resolver, wrapType }) { | ||
| if (groupType) return [{ | ||
| name, | ||
| type: typeof groupType.type === "string" ? wrapType(groupType.type) : groupType.type, | ||
| optional: groupType.optional | ||
| }]; | ||
| if (params.length) return [{ | ||
| name, | ||
| type: buildTypeLiteral({ | ||
| node, | ||
| params, | ||
| resolver | ||
| }), | ||
| optional: params.every((p) => !p.required) | ||
| }]; | ||
| return []; | ||
| } | ||
| /** | ||
| * Builds a single {@link FunctionParameterNode} for a query or header group. | ||
| * Returns an empty array when there are no params to emit. | ||
| * | ||
| * A pre-resolved `groupType` emits `name: GroupType`. Otherwise it builds an inline | ||
| * {@link TypeLiteralNode} from the individual params. | ||
| */ | ||
| function buildGroupParam(args) { | ||
| return buildGroupProperty(args).map((p) => createFunctionParameter(p)); | ||
| } | ||
| /** | ||
| * Builds a {@link TypeLiteralNode} for an inline anonymous type grouping named fields. | ||
| * | ||
| * Used when query or header parameters have no dedicated group type name. | ||
| * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`). | ||
| */ | ||
| function buildTypeLiteral({ node, params, resolver }) { | ||
| return createTypeLiteral({ members: params.map((p) => ({ | ||
| name: p.name, | ||
| type: resolveParamType({ | ||
| node, | ||
| param: p, | ||
| resolver | ||
| }), | ||
| optional: !p.required | ||
| })) }); | ||
| } | ||
| //#endregion | ||
| export { isHttpOperationNode as A, simplifyUnion as C, extractRefName as D, enumPropName as E, objectKey as F, isValidVarName as I, httpMethods as L, buildJSDoc as M, buildList as N, isStringType as O, buildObject as P, schemaTypes as R, setEnumName as S, childName as T, stringifyObject as _, resolveParamType as a, mergeAdjacentObjectsLazy as b, findCircularSchemas as c, transform as d, walk as f, stringify as g, jsStringEscape as h, createOperationParams as i, narrowSchema as j, resolveGroupType as k, collect as l, getNestedAccessor as m, buildTypeLiteral as n, collectUsedSchemaNames as o, nodeDefs as p, caseParams as r, containsCircularRef as s, buildGroupParam as t, collectLazy as u, toRegExpString as v, syncSchemaRef as w, setDiscriminatorEnum as x, trimQuotes as y }; | ||
| //# sourceMappingURL=utils-SdZU0F3H.js.map |
Sorry, the diff of this file is too big to display
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
988157
10.07%73
1.39%16165
6.67%0
-100%