@kubb/core 5.9.3 → 5.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{KubbDriver-C0hejO6L.js → Hookable-Cs4D9tMH.js} +147 -293
- package/dist/Hookable-Cs4D9tMH.js.map +1 -0
- package/dist/{KubbDriver-CrzfVOkH.cjs → Hookable-D0nxPjEd.cjs} +144 -308
- package/dist/Hookable-D0nxPjEd.cjs.map +1 -0
- package/dist/{createAdapter-DfbfcqcJ.d.ts → createAdapter-D55JE9lX.d.ts} +16 -76
- package/dist/index.cjs +91 -140
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -28
- package/dist/index.js +33 -81
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +9 -9
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.js +1 -1
- package/package.json +2 -2
- package/dist/KubbDriver-C0hejO6L.js.map +0 -1
- package/dist/KubbDriver-CrzfVOkH.cjs.map +0 -1
package/dist/mocks.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_Hookable = require("./Hookable-D0nxPjEd.cjs");
|
|
3
3
|
let node_path = require("node:path");
|
|
4
|
-
node_path =
|
|
4
|
+
node_path = require_Hookable.__toESM(node_path, 1);
|
|
5
5
|
let _kubb_ast = require("@kubb/ast");
|
|
6
6
|
let vitest = require("vitest");
|
|
7
7
|
//#region src/mocks.ts
|
|
@@ -9,7 +9,7 @@ let vitest = require("vitest");
|
|
|
9
9
|
* Creates a minimal `KubbDriver` mock for unit tests.
|
|
10
10
|
*/
|
|
11
11
|
function createMockedPluginDriver(options = {}) {
|
|
12
|
-
const fileManager = new
|
|
12
|
+
const fileManager = new require_Hookable.FileManager();
|
|
13
13
|
return {
|
|
14
14
|
config: options?.config ?? {
|
|
15
15
|
root: ".",
|
|
@@ -36,7 +36,7 @@ function createMockedSetupContext({ plugins = [], options = {} } = {}) {
|
|
|
36
36
|
injectFile: vitest.vi.fn(),
|
|
37
37
|
requirePlugin: vitest.vi.fn((pluginName) => {
|
|
38
38
|
const plugin = registered.find((entry) => entry.name === pluginName);
|
|
39
|
-
if (!plugin) throw
|
|
39
|
+
if (!plugin) throw require_Hookable.pluginNotFoundError({ pluginName });
|
|
40
40
|
return plugin;
|
|
41
41
|
})
|
|
42
42
|
};
|
|
@@ -82,7 +82,7 @@ function createMockedPluginContext(opts) {
|
|
|
82
82
|
getResolver: (name) => opts.driver.getResolver(name),
|
|
83
83
|
requirePlugin: (pluginName) => {
|
|
84
84
|
const plugin = opts.driver.getPlugin(pluginName);
|
|
85
|
-
if (!plugin) throw
|
|
85
|
+
if (!plugin) throw require_Hookable.pluginNotFoundError({
|
|
86
86
|
pluginName,
|
|
87
87
|
requiredBy: opts.plugin.name
|
|
88
88
|
});
|
|
@@ -92,10 +92,10 @@ function createMockedPluginContext(opts) {
|
|
|
92
92
|
circularNames: [],
|
|
93
93
|
enumNames: []
|
|
94
94
|
},
|
|
95
|
-
cache:
|
|
95
|
+
cache: require_Hookable.createNodeCache(),
|
|
96
96
|
addFile: async (...files) => opts.driver.fileManager.add(...files),
|
|
97
97
|
upsertFile: async (...files) => opts.driver.fileManager.upsert(...files),
|
|
98
|
-
hooks: opts.driver.hooks ?? new
|
|
98
|
+
hooks: opts.driver.hooks ?? new require_Hookable.Hookable(),
|
|
99
99
|
warn: (msg) => console.warn(msg),
|
|
100
100
|
error: (msg) => console.error(msg),
|
|
101
101
|
info: (msg) => console.info(msg)
|
|
@@ -184,14 +184,14 @@ async function renderGeneratorOperations(generator, nodes, opts) {
|
|
|
184
184
|
async function matchFiles(files, options = {}) {
|
|
185
185
|
if (!files?.length) return;
|
|
186
186
|
const { parsers = /* @__PURE__ */ new Map(), format, pre } = options;
|
|
187
|
-
const fileManager = new
|
|
187
|
+
const fileManager = new require_Hookable.FileManager();
|
|
188
188
|
const processed = /* @__PURE__ */ new Map();
|
|
189
189
|
for (const file of files) {
|
|
190
190
|
if (!file?.path || processed.has(file.path)) continue;
|
|
191
191
|
const parsed = await fileManager.parse(file, { parsers });
|
|
192
192
|
const code = file.baseName.endsWith(".json") || !format ? parsed : await format(parsed);
|
|
193
193
|
processed.set(file.path, code);
|
|
194
|
-
const snapshotPath = node_path.default.join("__snapshots__", ...pre ? [
|
|
194
|
+
const snapshotPath = node_path.default.join("__snapshots__", ...pre ? [require_Hookable.camelCase(pre)] : [], file.baseName);
|
|
195
195
|
await (0, vitest.expect)(code).toMatchFileSnapshot(snapshotPath);
|
|
196
196
|
}
|
|
197
197
|
return processed;
|
package/dist/mocks.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mocks.cjs","names":["FileManager","vi","pluginNotFoundError","ast","resolve","createNodeCache","Hookable","applyMacros","path","camelCase","expect"],"sources":["../src/mocks.ts"],"sourcesContent":["import path, { resolve } from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport type { FileNode, InputMeta, Macro, OperationNode, SchemaNode } from '@kubb/ast'\nimport { applyMacros, ast } from '@kubb/ast'\nimport { expect, vi } from 'vitest'\nimport type { Parser } from './defineParser.ts'\nimport { FileManager } from './FileManager.ts'\nimport { Hookable } from './Hookable.ts'\nimport { createNodeCache } from './nodeCache.ts'\nimport { type KubbDriver, pluginNotFoundError } from './KubbDriver.ts'\nimport type { Config, KubbHooks } from './types.ts'\nimport type { Adapter, AdapterFactoryOptions } from './createAdapter.ts'\nimport type { Generator, GeneratorContext } from './defineGenerator.ts'\nimport type { KubbPluginSetupContext, NormalizedPlugin, Plugin, PluginType } from './definePlugin.ts'\n\n/**\n * Creates a minimal `KubbDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): KubbDriver {\n const fileManager = new FileManager()\n\n return {\n config: options?.config ?? {\n root: '.',\n output: {\n path: './path',\n },\n },\n getPlugin(_pluginName: string): NormalizedPlugin | undefined {\n return options?.plugin\n },\n getResolver: (_pluginName: string) => options?.plugin?.resolver,\n fileManager,\n } as unknown as KubbDriver\n}\n\n/** Creates a `kubb:plugin:setup` context for unit tests. `requirePlugin` fails for names `plugins` does not list. */\nexport function createMockedSetupContext<TFactory extends PluginType = PluginType>({\n plugins = [],\n options = {} as TFactory['options'],\n}: { plugins?: Array<string>; options?: TFactory['options'] } = {}): KubbPluginSetupContext<TFactory> {\n const registered = plugins.map((name) => ({ name }) as unknown as Plugin)\n\n return {\n config: { plugins: registered } as unknown as Config,\n options,\n addGenerator: vi.fn(),\n setResolver: vi.fn(),\n addMacro: vi.fn(),\n setMacros: vi.fn(),\n setOptions: vi.fn(),\n injectFile: vi.fn(),\n requirePlugin: vi.fn((pluginName: string) => {\n const plugin = registered.find((entry) => entry.name === pluginName)\n if (!plugin) throw pluginNotFoundError({ pluginName })\n return plugin\n }) as KubbPluginSetupContext<TFactory>['requirePlugin'],\n }\n}\n\n/**\n * Creates a minimal `Adapter` mock for unit tests.\n * `parse` returns an empty `InputNode` by default. Override via `options.parse`.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n parse?: Adapter<TOptions>['parse']\n } = {},\n): Adapter<TOptions> {\n const adapter: Adapter<TOptions> = {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n document: null,\n parse: options.parse ?? (async () => ast.factory.createInput()),\n validate: async () => {},\n }\n return adapter\n}\n\n/**\n * Creates a minimal plugin mock for unit tests.\n *\n * @example\n * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`\n */\nexport function createMockedPlugin<TOptions extends PluginType = PluginType>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n macros?: Array<Macro>\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n macros: params.macros,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginType> = {\n config: Config\n adapter: Adapter\n meta?: InputMeta\n driver: KubbDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginType>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {\n const root = resolve(opts.config.root, opts.config.output.path)\n\n return {\n config: opts.config,\n root,\n adapter: opts.adapter,\n resolver: opts.resolver,\n plugin: opts.plugin,\n driver: opts.driver,\n getResolver: (name: string) => opts.driver.getResolver(name),\n requirePlugin: (pluginName: string) => {\n const plugin = opts.driver.getPlugin(pluginName)\n if (!plugin) throw pluginNotFoundError({ pluginName, requiredBy: opts.plugin.name })\n return plugin\n },\n meta: opts.meta ?? { circularNames: [], enumNames: [] },\n cache: createNodeCache(),\n addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),\n upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),\n hooks: opts.driver.hooks ?? new Hookable<KubbHooks>(),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\n}\n\nasync function renderNode<TOptions extends PluginType, TNode extends SchemaNode | OperationNode>({\n generator,\n node,\n opts,\n generate,\n}: {\n generator: Generator<TOptions>\n node: TNode\n opts: RenderGeneratorOptions<TOptions>\n generate: (node: TNode, ctx: GeneratorContext<TOptions>) => ReturnType<NonNullable<Generator<TOptions>['schema']>>\n}): Promise<void> {\n const ctx = { ...createMockedPluginContext(opts), options: opts.options }\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const matches = generator.match ? await generator.match(transformedNode, ctx) : true\n if (!matches) return\n await opts.driver.fileManager.dispatch({ result: await generate(transformedNode, ctx), renderer: generator.renderer })\n}\n\n/**\n * Renders a generator's `schema` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorSchema<TOptions extends PluginType>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n await renderNode({ generator, node, opts, generate: (transformedNode, ctx) => generator.schema!(transformedNode, ctx) })\n}\n\n/**\n * Renders a generator's `operation` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperation<TOptions extends PluginType>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n await renderNode({ generator, node, opts, generate: (transformedNode, ctx) => generator.operation!(transformedNode, ctx) })\n}\n\n/**\n * Renders a generator's `operations` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperations<TOptions extends PluginType>(\n generator: Generator<TOptions>,\n nodes: Array<OperationNode>,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operations) return\n const context = createMockedPluginContext(opts)\n const transformedNodes = opts.plugin.macros?.length ? nodes.map((n) => applyMacros(n, opts.plugin.macros!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await opts.driver.fileManager.dispatch({ result, renderer: generator.renderer })\n}\n\ntype MatchFilesOptions = {\n /**\n * Parsers indexed by file extension, used to render each `FileNode` to source.\n * Without a matching parser the file's raw content is used.\n */\n parsers?: Map<FileNode['extname'], Parser>\n /**\n * Formatter applied to non-JSON output before snapshotting, e.g. prettier. When\n * omitted the parsed source is snapshotted as-is.\n */\n format?: (source?: string) => string | Promise<string>\n /**\n * Subfolder under `__snapshots__`, camelCased. Useful to keep variant snapshots apart.\n */\n pre?: string\n}\n\n/**\n * Renders the driver's collected `FileNode`s to source and asserts each against a file snapshot.\n * Pair it with the `renderGenerator*` helpers to snapshot a generator's output.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files, { parsers, format })\n * ```\n */\nexport async function matchFiles(files: Array<FileNode> | undefined, options: MatchFilesOptions = {}): Promise<Map<string, string> | undefined> {\n if (!files?.length) return\n\n const { parsers = new Map(), format, pre } = options\n const fileManager = new FileManager()\n const processed = new Map<string, string>()\n\n for (const file of files) {\n if (!file?.path || processed.has(file.path)) {\n continue\n }\n\n const parsed = await fileManager.parse(file, { parsers })\n const code = file.baseName.endsWith('.json') || !format ? parsed : await format(parsed)\n\n processed.set(file.path, code)\n\n const snapshotPath = path.join('__snapshots__', ...(pre ? [camelCase(pre)] : []), file.baseName)\n await expect(code).toMatchFileSnapshot(snapshotPath)\n }\n\n return processed\n}\n"],"mappings":";;;;;;;;;;AAkBA,SAAgB,yBAAyB,UAAyE,CAAC,GAAe;CAChI,MAAM,cAAc,IAAIA,mBAAAA,YAAY;CAEpC,OAAO;EACL,QAAQ,SAAS,UAAU;GACzB,MAAM;GACN,QAAQ,EACN,MAAM,SACR;EACF;EACA,UAAU,aAAmD;GAC3D,OAAO,SAAS;EAClB;EACA,cAAc,gBAAwB,SAAS,QAAQ;EACvD;CACF;AACF;;AAGA,SAAgB,yBAAmE,EACjF,UAAU,CAAC,GACX,UAAU,CAAC,MACmD,CAAC,GAAqC;CACpG,MAAM,aAAa,QAAQ,KAAK,UAAU,EAAE,K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ources":["../src/mocks.ts"],"sourcesContent":["import path, { resolve } from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport type { FileNode, InputMeta, Macro, OperationNode, SchemaNode } from '@kubb/ast'\nimport { applyMacros, ast } from '@kubb/ast'\nimport { expect, vi } from 'vitest'\nimport type { Parser } from './defineParser.ts'\nimport { FileManager } from './FileManager.ts'\nimport { Hookable } from './Hookable.ts'\nimport { createNodeCache } from './nodeCache.ts'\nimport { type KubbDriver, pluginNotFoundError } from './KubbDriver.ts'\nimport type { Config, KubbHooks } from './types.ts'\nimport type { Adapter, AdapterFactoryOptions } from './createAdapter.ts'\nimport type { Generator, GeneratorContext } from './defineGenerator.ts'\nimport type { KubbPluginSetupContext, NormalizedPlugin, Plugin, PluginType } from './definePlugin.ts'\n\n/**\n * Creates a minimal `KubbDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): KubbDriver {\n const fileManager = new FileManager()\n\n return {\n config: options?.config ?? {\n root: '.',\n output: {\n path: './path',\n },\n },\n getPlugin(_pluginName: string): NormalizedPlugin | undefined {\n return options?.plugin\n },\n getResolver: (_pluginName: string) => options?.plugin?.resolver,\n fileManager,\n } as unknown as KubbDriver\n}\n\n/** Creates a `kubb:plugin:setup` context for unit tests. `requirePlugin` fails for names `plugins` does not list. */\nexport function createMockedSetupContext<TFactory extends PluginType = PluginType>({\n plugins = [],\n options = {} as TFactory['options'],\n}: { plugins?: Array<string>; options?: TFactory['options'] } = {}): KubbPluginSetupContext<TFactory> {\n const registered = plugins.map((name) => ({ name }) as unknown as Plugin)\n\n return {\n config: { plugins: registered } as unknown as Config,\n options,\n addGenerator: vi.fn(),\n setResolver: vi.fn(),\n addMacro: vi.fn(),\n setMacros: vi.fn(),\n setOptions: vi.fn(),\n injectFile: vi.fn(),\n requirePlugin: vi.fn((pluginName: string) => {\n const plugin = registered.find((entry) => entry.name === pluginName)\n if (!plugin) throw pluginNotFoundError({ pluginName })\n return plugin\n }) as KubbPluginSetupContext<TFactory>['requirePlugin'],\n }\n}\n\n/**\n * Creates a minimal `Adapter` mock for unit tests.\n * `parse` returns an empty `InputNode` by default. Override via `options.parse`.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n parse?: Adapter<TOptions>['parse']\n } = {},\n): Adapter<TOptions> {\n const adapter: Adapter<TOptions> = {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n document: null,\n parse: options.parse ?? (async () => ast.factory.createInput()),\n validate: async () => {},\n }\n return adapter\n}\n\n/**\n * Creates a minimal plugin mock for unit tests.\n *\n * @example\n * `const plugin = createMockedPlugin<PluginTs>({ name: '@kubb/plugin-ts', options })`\n */\nexport function createMockedPlugin<TOptions extends PluginType = PluginType>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n macros?: Array<Macro>\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n macros: params.macros,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginType> = {\n config: Config\n adapter: Adapter\n meta?: InputMeta\n driver: KubbDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginType>(opts: RenderGeneratorOptions<TOptions>): Omit<GeneratorContext<TOptions>, 'options'> {\n const root = resolve(opts.config.root, opts.config.output.path)\n\n return {\n config: opts.config,\n root,\n adapter: opts.adapter,\n resolver: opts.resolver,\n plugin: opts.plugin,\n driver: opts.driver,\n getResolver: (name: string) => opts.driver.getResolver(name),\n requirePlugin: (pluginName: string) => {\n const plugin = opts.driver.getPlugin(pluginName)\n if (!plugin) throw pluginNotFoundError({ pluginName, requiredBy: opts.plugin.name })\n return plugin\n },\n meta: opts.meta ?? { circularNames: [], enumNames: [] },\n cache: createNodeCache(),\n addFile: async (...files: Array<FileNode>) => opts.driver.fileManager.add(...files),\n upsertFile: async (...files: Array<FileNode>) => opts.driver.fileManager.upsert(...files),\n hooks: opts.driver.hooks ?? new Hookable<KubbHooks>(),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\n}\n\nasync function renderNode<TOptions extends PluginType, TNode extends SchemaNode | OperationNode>({\n generator,\n node,\n opts,\n generate,\n}: {\n generator: Generator<TOptions>\n node: TNode\n opts: RenderGeneratorOptions<TOptions>\n generate: (node: TNode, ctx: GeneratorContext<TOptions>) => ReturnType<NonNullable<Generator<TOptions>['schema']>>\n}): Promise<void> {\n const ctx = { ...createMockedPluginContext(opts), options: opts.options }\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const matches = generator.match ? await generator.match(transformedNode, ctx) : true\n if (!matches) return\n await opts.driver.fileManager.dispatch({ result: await generate(transformedNode, ctx), renderer: generator.renderer })\n}\n\n/**\n * Renders a generator's `schema` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorSchema<TOptions extends PluginType>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n await renderNode({ generator, node, opts, generate: (transformedNode, ctx) => generator.schema!(transformedNode, ctx) })\n}\n\n/**\n * Renders a generator's `operation` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperation(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperation<TOptions extends PluginType>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n await renderNode({ generator, node, opts, generate: (transformedNode, ctx) => generator.operation!(transformedNode, ctx) })\n}\n\n/**\n * Renders a generator's `operations` method in a test context.\n *\n * @example\n * ```ts\n * await renderGeneratorOperations(classClientGenerator, nodes, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files)\n * ```\n */\nexport async function renderGeneratorOperations<TOptions extends PluginType>(\n generator: Generator<TOptions>,\n nodes: Array<OperationNode>,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operations) return\n const context = createMockedPluginContext(opts)\n const transformedNodes = opts.plugin.macros?.length ? nodes.map((n) => applyMacros(n, opts.plugin.macros!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await opts.driver.fileManager.dispatch({ result, renderer: generator.renderer })\n}\n\ntype MatchFilesOptions = {\n /**\n * Parsers indexed by file extension, used to render each `FileNode` to source.\n * Without a matching parser the file's raw content is used.\n */\n parsers?: Map<FileNode['extname'], Parser>\n /**\n * Formatter applied to non-JSON output before snapshotting, e.g. prettier. When\n * omitted the parsed source is snapshotted as-is.\n */\n format?: (source?: string) => string | Promise<string>\n /**\n * Subfolder under `__snapshots__`, camelCased. Useful to keep variant snapshots apart.\n */\n pre?: string\n}\n\n/**\n * Renders the driver's collected `FileNode`s to source and asserts each against a file snapshot.\n * Pair it with the `renderGenerator*` helpers to snapshot a generator's output.\n *\n * @example\n * ```ts\n * await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })\n * await matchFiles(driver.fileManager.files, { parsers, format })\n * ```\n */\nexport async function matchFiles(files: Array<FileNode> | undefined, options: MatchFilesOptions = {}): Promise<Map<string, string> | undefined> {\n if (!files?.length) return\n\n const { parsers = new Map(), format, pre } = options\n const fileManager = new FileManager()\n const processed = new Map<string, string>()\n\n for (const file of files) {\n if (!file?.path || processed.has(file.path)) {\n continue\n }\n\n const parsed = await fileManager.parse(file, { parsers })\n const code = file.baseName.endsWith('.json') || !format ? parsed : await format(parsed)\n\n processed.set(file.path, code)\n\n const snapshotPath = path.join('__snapshots__', ...(pre ? [camelCase(pre)] : []), file.baseName)\n await expect(code).toMatchFileSnapshot(snapshotPath)\n }\n\n return processed\n}\n"],"mappings":";;;;;;;;;;AAkBA,SAAgB,yBAAyB,UAAyE,CAAC,GAAe;CAChI,MAAM,cAAc,IAAIA,iBAAAA,YAAY;CAEpC,OAAO;EACL,QAAQ,SAAS,UAAU;GACzB,MAAM;GACN,QAAQ,EACN,MAAM,SACR;EACF;EACA,UAAU,aAAmD;GAC3D,OAAO,SAAS;EAClB;EACA,cAAc,gBAAwB,SAAS,QAAQ;EACvD;CACF;AACF;;AAGA,SAAgB,yBAAmE,EACjF,UAAU,CAAC,GACX,UAAU,CAAC,MACmD,CAAC,GAAqC;CACpG,MAAM,aAAa,QAAQ,KAAK,UAAU,EAAE,KAAK,EAAuB;CAExE,OAAO;EACL,QAAQ,EAAE,SAAS,WAAW;EAC9B;EACA,cAAcC,OAAAA,GAAG,GAAG;EACpB,aAAaA,OAAAA,GAAG,GAAG;EACnB,UAAUA,OAAAA,GAAG,GAAG;EAChB,WAAWA,OAAAA,GAAG,GAAG;EACjB,YAAYA,OAAAA,GAAG,GAAG;EAClB,YAAYA,OAAAA,GAAG,GAAG;EAClB,eAAeA,OAAAA,GAAG,IAAI,eAAuB;GAC3C,MAAM,SAAS,WAAW,MAAM,UAAU,MAAM,SAAS,UAAU;GACnE,IAAI,CAAC,QAAQ,MAAMC,iBAAAA,oBAAoB,EAAE,WAAW,CAAC;GACrD,OAAO;EACT,CAAC;CACH;AACF;;;;;AAMA,SAAgB,oBACd,UAII,CAAC,GACc;CAQnB,OAAO;EANL,MAAO,QAAQ,QAAQ;EACvB,SAAU,QAAQ,mBAAmB,CAAC;EACtC,UAAU;EACV,OAAO,QAA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YAAY;CACpC,MAAM,4BAAY,IAAI,IAAoB;CAE1C,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,CAAC,MAAM,QAAQ,UAAU,IAAI,KAAK,IAAI,GACxC;EAGF,MAAM,SAAS,MAAM,YAAY,MAAM,MAAM,EAAE,QAAQ,CAAC;EACxD,MAAM,OAAO,KAAK,SAAS,SAAS,OAAO,KAAK,CAAC,SAAS,SAAS,MAAM,OAAO,MAAM;EAEtF,UAAU,IAAI,KAAK,MAAM,IAAI;EAE7B,MAAM,eAAeQ,UAAAA,QAAK,KAAK,iBAAiB,GAAI,MAAM,CAACC,iBAAAA,UAAU,GAAG,CAAC,IAAI,CAAC,GAAI,KAAK,QAAQ;EAC/F,OAAA,GAAMC,OAAAA,OAAAA,CAAO,IAAI,CAAC,CAAC,oBAAoB,YAAY;CACrD;CAEA,OAAO;AACT"}
|
package/dist/mocks.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-CRm0XQPb.js";
|
|
2
|
-
import { C as NormalizedPlugin, M as PluginType, Mt as KubbDriver, X as Generator,
|
|
2
|
+
import { C as NormalizedPlugin, M as PluginType, Mt as KubbDriver, X as Generator, i as AdapterFactoryOptions, in as Parser, nt as Config, t as Adapter, x as KubbPluginSetupContext } from "./createAdapter-D55JE9lX.js";
|
|
3
3
|
import { FileNode, InputMeta, Macro, OperationNode, SchemaNode } from "@kubb/ast";
|
|
4
4
|
//#region src/mocks.d.ts
|
|
5
5
|
/**
|
package/dist/mocks.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import "./rolldown-runtime-CRm0XQPb.js";
|
|
2
|
-
import {
|
|
2
|
+
import { I as camelCase, a as FileManager, i as createNodeCache, r as pluginNotFoundError, t as Hookable } from "./Hookable-Cs4D9tMH.js";
|
|
3
3
|
import path, { resolve } from "node:path";
|
|
4
4
|
import { applyMacros, ast } from "@kubb/ast";
|
|
5
5
|
import { expect, vi } from "vitest";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/core",
|
|
3
|
-
"version": "5.9.
|
|
3
|
+
"version": "5.9.5",
|
|
4
4
|
"description": "Core engine for Kubb. Provides the plugin driver, file manager and build orchestration used by every Kubb plugin.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generator",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
"registry": "https://registry.npmjs.org/"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@kubb/ast": "5.9.
|
|
76
|
+
"@kubb/ast": "5.9.5",
|
|
77
77
|
"tinyexec": "^1.3.1",
|
|
78
78
|
"unstorage": "^1.17.5",
|
|
79
79
|
"verkit": "^0.5.0"
|