@kubb/core 5.0.0-beta.1 → 5.0.0-beta.100
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/LICENSE +17 -10
- package/README.md +25 -158
- package/dist/index.cjs +2262 -1130
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +107 -295
- package/dist/index.js +2253 -1122
- package/dist/index.js.map +1 -1
- package/dist/mocks.cjs +81 -32
- package/dist/mocks.cjs.map +1 -1
- package/dist/mocks.d.ts +37 -14
- package/dist/mocks.js +83 -36
- package/dist/mocks.js.map +1 -1
- package/dist/types-DM7-MGjZ.d.ts +2838 -0
- package/dist/usingCtx-BNggxUEL.js +597 -0
- package/dist/usingCtx-BNggxUEL.js.map +1 -0
- package/dist/usingCtx-CZyLSqds.cjs +705 -0
- package/dist/usingCtx-CZyLSqds.cjs.map +1 -0
- package/package.json +7 -35
- package/dist/PluginDriver-BXibeQk-.cjs +0 -1036
- package/dist/PluginDriver-BXibeQk-.cjs.map +0 -1
- package/dist/PluginDriver-DV3p2Hky.js +0 -945
- package/dist/PluginDriver-DV3p2Hky.js.map +0 -1
- package/dist/types-CuNocrbJ.d.ts +0 -2148
- package/src/FileManager.ts +0 -115
- package/src/FileProcessor.ts +0 -86
- package/src/Kubb.ts +0 -300
- package/src/PluginDriver.ts +0 -424
- package/src/constants.ts +0 -35
- package/src/createAdapter.ts +0 -32
- package/src/createKubb.ts +0 -548
- package/src/createRenderer.ts +0 -57
- package/src/createStorage.ts +0 -70
- package/src/defineGenerator.ts +0 -87
- package/src/defineLogger.ts +0 -19
- package/src/defineMiddleware.ts +0 -62
- package/src/defineParser.ts +0 -44
- package/src/definePlugin.ts +0 -83
- package/src/defineResolver.ts +0 -521
- package/src/devtools.ts +0 -59
- package/src/index.ts +0 -20
- package/src/mocks.ts +0 -178
- package/src/renderNode.ts +0 -35
- package/src/storages/fsStorage.ts +0 -114
- package/src/storages/memoryStorage.ts +0 -55
- package/src/types.ts +0 -1296
- package/src/utils/diagnostics.ts +0 -18
- package/src/utils/isInputPath.ts +0 -10
- package/src/utils/packageJSON.ts +0 -99
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/dist/mocks.cjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_usingCtx = require("./usingCtx-CZyLSqds.cjs");
|
|
3
3
|
let node_path = require("node:path");
|
|
4
|
+
node_path = require_usingCtx.__toESM(node_path, 1);
|
|
4
5
|
let _kubb_ast = require("@kubb/ast");
|
|
6
|
+
let vitest = require("vitest");
|
|
5
7
|
//#region src/mocks.ts
|
|
6
8
|
/**
|
|
7
|
-
|
|
8
|
-
* Creates a minimal `PluginDriver` mock for unit tests.
|
|
9
|
+
* Creates a minimal `KubbDriver` mock for unit tests.
|
|
9
10
|
*/
|
|
10
11
|
function createMockedPluginDriver(options = {}) {
|
|
12
|
+
const fileManager = new require_usingCtx.FileManager();
|
|
11
13
|
return {
|
|
12
14
|
config: options?.config ?? {
|
|
13
15
|
root: ".",
|
|
@@ -17,26 +19,38 @@ function createMockedPluginDriver(options = {}) {
|
|
|
17
19
|
return options?.plugin;
|
|
18
20
|
},
|
|
19
21
|
getResolver: (_pluginName) => options?.plugin?.resolver,
|
|
20
|
-
fileManager
|
|
22
|
+
fileManager,
|
|
23
|
+
async dispatch({ result, renderer }) {
|
|
24
|
+
try {
|
|
25
|
+
var _usingCtx$1 = require_usingCtx._usingCtx();
|
|
26
|
+
if (!result) return;
|
|
27
|
+
if (Array.isArray(result)) {
|
|
28
|
+
fileManager.upsert(...result);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (!renderer) return;
|
|
32
|
+
const instance = _usingCtx$1.u(renderer());
|
|
33
|
+
await instance.render(result);
|
|
34
|
+
fileManager.upsert(...instance.files);
|
|
35
|
+
} catch (_) {
|
|
36
|
+
_usingCtx$1.e = _;
|
|
37
|
+
} finally {
|
|
38
|
+
_usingCtx$1.d();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
21
41
|
};
|
|
22
42
|
}
|
|
23
43
|
/**
|
|
24
44
|
* Creates a minimal `Adapter` mock for unit tests.
|
|
25
|
-
* `parse` returns an empty `InputNode` by default
|
|
26
|
-
* `getImports` returns `[]` by default.
|
|
45
|
+
* `parse` returns an empty `InputNode` by default. Override via `options.parse`.
|
|
27
46
|
*/
|
|
28
47
|
function createMockedAdapter(options = {}) {
|
|
29
|
-
const inputNode = options.inputNode ?? null;
|
|
30
48
|
return {
|
|
31
49
|
name: options.name ?? "oas",
|
|
32
50
|
options: options.resolvedOptions ?? {},
|
|
33
|
-
|
|
34
|
-
parse: options.parse ?? (async () => (
|
|
35
|
-
|
|
36
|
-
schemas: [],
|
|
37
|
-
operations: []
|
|
38
|
-
})),
|
|
39
|
-
getImports: options.getImports ?? ((_node, _resolve) => [])
|
|
51
|
+
document: null,
|
|
52
|
+
parse: options.parse ?? (async () => _kubb_ast.ast.factory.createInput()),
|
|
53
|
+
validate: async () => {}
|
|
40
54
|
};
|
|
41
55
|
}
|
|
42
56
|
/**
|
|
@@ -50,7 +64,7 @@ function createMockedPlugin(params) {
|
|
|
50
64
|
name: params.name,
|
|
51
65
|
options: params.options,
|
|
52
66
|
resolver: params.resolver,
|
|
53
|
-
|
|
67
|
+
macros: params.macros,
|
|
54
68
|
dependencies: params.dependencies,
|
|
55
69
|
hooks: {}
|
|
56
70
|
};
|
|
@@ -60,24 +74,21 @@ function createMockedPluginContext(opts) {
|
|
|
60
74
|
return {
|
|
61
75
|
config: opts.config,
|
|
62
76
|
root,
|
|
63
|
-
getMode: (output) => require_PluginDriver.PluginDriver.getMode((0, node_path.resolve)(root, output.path)),
|
|
64
77
|
adapter: opts.adapter,
|
|
65
78
|
resolver: opts.resolver,
|
|
66
79
|
plugin: opts.plugin,
|
|
67
80
|
driver: opts.driver,
|
|
68
81
|
getResolver: (name) => opts.driver.getResolver(name),
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
operations: []
|
|
82
|
+
meta: opts.meta ?? {
|
|
83
|
+
circularNames: [],
|
|
84
|
+
enumNames: []
|
|
73
85
|
},
|
|
74
86
|
addFile: async (...files) => opts.driver.fileManager.add(...files),
|
|
75
87
|
upsertFile: async (...files) => opts.driver.fileManager.upsert(...files),
|
|
76
|
-
hooks: opts.driver.hooks ??
|
|
88
|
+
hooks: opts.driver.hooks ?? new require_usingCtx.Hookable(),
|
|
77
89
|
warn: (msg) => console.warn(msg),
|
|
78
90
|
error: (msg) => console.error(msg),
|
|
79
|
-
info: (msg) => console.info(msg)
|
|
80
|
-
openInStudio: async () => {}
|
|
91
|
+
info: (msg) => console.info(msg)
|
|
81
92
|
};
|
|
82
93
|
}
|
|
83
94
|
/**
|
|
@@ -92,11 +103,15 @@ function createMockedPluginContext(opts) {
|
|
|
92
103
|
async function renderGeneratorSchema(generator, node, opts) {
|
|
93
104
|
if (!generator.schema) return;
|
|
94
105
|
const context = createMockedPluginContext(opts);
|
|
95
|
-
const transformedNode = opts.plugin.
|
|
96
|
-
|
|
106
|
+
const transformedNode = opts.plugin.macros?.length ? (0, _kubb_ast.applyMacros)(node, opts.plugin.macros) : node;
|
|
107
|
+
const result = await generator.schema(transformedNode, {
|
|
97
108
|
...context,
|
|
98
109
|
options: opts.options
|
|
99
|
-
})
|
|
110
|
+
});
|
|
111
|
+
await opts.driver.dispatch({
|
|
112
|
+
result,
|
|
113
|
+
renderer: generator.renderer
|
|
114
|
+
});
|
|
100
115
|
}
|
|
101
116
|
/**
|
|
102
117
|
* Renders a generator's `operation` method in a test context.
|
|
@@ -110,11 +125,15 @@ async function renderGeneratorSchema(generator, node, opts) {
|
|
|
110
125
|
async function renderGeneratorOperation(generator, node, opts) {
|
|
111
126
|
if (!generator.operation) return;
|
|
112
127
|
const context = createMockedPluginContext(opts);
|
|
113
|
-
const transformedNode = opts.plugin.
|
|
114
|
-
|
|
128
|
+
const transformedNode = opts.plugin.macros?.length ? (0, _kubb_ast.applyMacros)(node, opts.plugin.macros) : node;
|
|
129
|
+
const result = await generator.operation(transformedNode, {
|
|
115
130
|
...context,
|
|
116
131
|
options: opts.options
|
|
117
|
-
})
|
|
132
|
+
});
|
|
133
|
+
await opts.driver.dispatch({
|
|
134
|
+
result,
|
|
135
|
+
renderer: generator.renderer
|
|
136
|
+
});
|
|
118
137
|
}
|
|
119
138
|
/**
|
|
120
139
|
* Renders a generator's `operations` method in a test context.
|
|
@@ -128,16 +147,46 @@ async function renderGeneratorOperation(generator, node, opts) {
|
|
|
128
147
|
async function renderGeneratorOperations(generator, nodes, opts) {
|
|
129
148
|
if (!generator.operations) return;
|
|
130
149
|
const context = createMockedPluginContext(opts);
|
|
131
|
-
const transformedNodes = opts.plugin.
|
|
132
|
-
|
|
150
|
+
const transformedNodes = opts.plugin.macros?.length ? nodes.map((n) => (0, _kubb_ast.applyMacros)(n, opts.plugin.macros)) : nodes;
|
|
151
|
+
const result = await generator.operations(transformedNodes, {
|
|
133
152
|
...context,
|
|
134
153
|
options: opts.options
|
|
135
|
-
})
|
|
154
|
+
});
|
|
155
|
+
await opts.driver.dispatch({
|
|
156
|
+
result,
|
|
157
|
+
renderer: generator.renderer
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Renders the driver's collected `FileNode`s to source and asserts each against a file snapshot.
|
|
162
|
+
* Pair it with the `renderGenerator*` helpers to snapshot a generator's output.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
167
|
+
* await matchFiles(driver.fileManager.files, { parsers, format })
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
async function matchFiles(files, options = {}) {
|
|
171
|
+
if (!files?.length) return;
|
|
172
|
+
const { parsers = /* @__PURE__ */ new Map(), format, pre } = options;
|
|
173
|
+
const fileManager = new require_usingCtx.FileManager();
|
|
174
|
+
const processed = /* @__PURE__ */ new Map();
|
|
175
|
+
for (const file of files) {
|
|
176
|
+
if (!file?.path || processed.has(file.path)) continue;
|
|
177
|
+
const parsed = await fileManager.parse(file, { parsers });
|
|
178
|
+
const code = file.baseName.endsWith(".json") || !format ? parsed : await format(parsed);
|
|
179
|
+
processed.set(file.path, code);
|
|
180
|
+
const snapshotPath = node_path.default.join("__snapshots__", ...pre ? [require_usingCtx.camelCase(pre)] : [], file.baseName);
|
|
181
|
+
await (0, vitest.expect)(code).toMatchFileSnapshot(snapshotPath);
|
|
182
|
+
}
|
|
183
|
+
return processed;
|
|
136
184
|
}
|
|
137
185
|
//#endregion
|
|
138
186
|
exports.createMockedAdapter = createMockedAdapter;
|
|
139
187
|
exports.createMockedPlugin = createMockedPlugin;
|
|
140
188
|
exports.createMockedPluginDriver = createMockedPluginDriver;
|
|
189
|
+
exports.matchFiles = matchFiles;
|
|
141
190
|
exports.renderGeneratorOperation = renderGeneratorOperation;
|
|
142
191
|
exports.renderGeneratorOperations = renderGeneratorOperations;
|
|
143
192
|
exports.renderGeneratorSchema = renderGeneratorSchema;
|
package/dist/mocks.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mocks.cjs","names":["FileManager","PluginDriver","applyHookResult"],"sources":["../src/mocks.ts"],"sourcesContent":["import { resolve } from 'node:path'\nimport type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'\nimport { transform } from '@kubb/ast'\nimport { FileManager } from './FileManager.ts'\nimport { PluginDriver } from './PluginDriver.ts'\nimport { applyHookResult } from './renderNode.ts'\nimport type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'\n\n/**\n\n * Creates a minimal `PluginDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {\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: new FileManager(),\n } as unknown as PluginDriver\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 * `getImports` returns `[]` by default.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n inputNode?: Adapter<TOptions>['inputNode']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n const inputNode = options.inputNode ?? null\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n inputNode,\n parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),\n getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),\n } as Adapter<TOptions>\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 PluginFactoryOptions = PluginFactoryOptions>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n transformer?: Visitor\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n transformer: params.transformer,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {\n config: Config\n adapter: Adapter\n driver: PluginDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginFactoryOptions>(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 getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),\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 inputNode: { kind: 'Input', schemas: [], operations: [] },\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 ?? ({} as never),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n openInStudio: async () => {},\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\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 PluginFactoryOptions>(\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.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\n}\n"],"mappings":";;;;;;;;;AAYA,SAAgB,yBAAyB,UAAyE,EAAE,EAAgB;AAClI,QAAO;EACL,QAAQ,SAAS,UAAU;GACzB,MAAM;GACN,QAAQ,EACN,MAAM,UACP;GACF;EACD,UAAU,aAAmD;AAC3D,UAAO,SAAS;;EAElB,cAAc,gBAAwB,SAAS,QAAQ;EACvD,aAAa,IAAIA,qBAAAA,aAAa;EAC/B;;;;;;;AAQH,SAAgB,oBACd,UAMI,EAAE,EACa;CACnB,MAAM,YAAY,QAAQ,aAAa;AACvC,QAAO;EACL,MAAO,QAAQ,QAAQ;EACvB,SAAU,QAAQ,mBAAmB,EAAE;EACvC;EACA,OAAO,QAAQ,UAAU,aAAa;GAAE,MAAM;GAAkB,SAAS,EAAE;GAAE,YAAY,EAAE;GAAE;EAC7F,YAAY,QAAQ,gBAAgB,OAAmB,aAAqE,EAAE;EAC/H;;;;;;;;AASH,SAAgB,mBAAiF,QAMlE;AAC7B,QAAO;EACL,MAAM,OA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UAAW;CAC1B,MAAM,UAAU,0BAA0B,KAAK;CAC/C,MAAM,kBAAkB,KAAK,OAAO,eAAA,GAAA,UAAA,WAAwB,MAAM,KAAK,OAAO,YAAY,GAAG;AAK7F,OAAMA,qBAAAA,gBAAgB,MAJD,UAAU,UAAU,iBAAiB;EACxD,GAAG;EACH,SAAS,KAAK;EACf,CAAC,EAC4B,KAAK,QAAQ,UAAU,YAAY,KAAA,EAAU;;;;;;;;;;;AAY7E,eAAsB,0BACpB,WACA,OACA,MACe;AACf,KAAI,CAAC,UAAU,WAAY;CAC3B,MAAM,UAAU,0BAA0B,KAAK;CAC/C,MAAM,mBAAmB,KAAK,OAAO,cAAc,MAAM,KAAK,OAAA,GAAA,UAAA,WAAgB,GAAG,KAAK,OAAO,YAAa,CAAC,GAAG;AAK9G,OAAMA,qBAAAA,gBAAgB,MAJD,UAAU,WAAW,kBAAkB;EAC1D,GAAG;EACH,SAAS,KAAK;EACf,CAAC,EAC4B,KAAK,QAAQ,UAAU,YAAY,KAAA,EAAU"}
|
|
1
|
+
{"version":3,"file":"mocks.cjs","names":["FileManager","ast","Hookable","path","camelCase"],"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 } from 'vitest'\nimport type { Parser } from './defineParser.ts'\nimport { FileManager } from './FileManager.ts'\nimport { Hookable } from './Hookable.ts'\nimport type { KubbDriver } from './KubbDriver.ts'\nimport type {\n Adapter,\n AdapterFactoryOptions,\n Config,\n Generator,\n GeneratorContext,\n KubbHooks,\n NormalizedPlugin,\n PluginFactoryOptions,\n RendererFactory,\n} from './types.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 async dispatch({ result, renderer }: { result: unknown; renderer?: RendererFactory | null }): Promise<void> {\n if (!result) return\n\n if (Array.isArray(result)) {\n fileManager.upsert(...(result as Array<FileNode>))\n return\n }\n\n if (!renderer) return\n\n using instance = renderer()\n await instance.render(result)\n fileManager.upsert(...instance.files)\n },\n } as unknown as KubbDriver\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 PluginFactoryOptions = PluginFactoryOptions>(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 PluginFactoryOptions> = {\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 PluginFactoryOptions>(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 meta: opts.meta ?? { circularNames: [], enumNames: [] },\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\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\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 PluginFactoryOptions>(\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.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":";;;;;;;;;;AAwBA,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;EACA,MAAM,SAAS,EAAE,QAAQ,YAAmF;;;IAC1G,IAAI,CAAC,QAAQ;IAEb,IAAI,MAAM,QAAQ,MAAM,GAAG;KACzB,YAAY,OAAO,GAAI,MAA0B;KACjD;IACF;IAEA,IAAI,CAAC,UAAU;IAEf,MAAM,WAAA,YAAA,EAAW,SAAS,CAAA;IAC1B,MAAM,SAAS,OAAO,MAAM;IAC5B,YAAY,OAAO,GAAG,SAAS,KAAK;;;;;;EACtC;CACF;AACF;;;;;AAMA,SAAgB,oBACd,UAII,CAAC,GACc;CAQnB,OAAO;EANL,MAAO,QAAQ,QAAQ;EACvB,SAAU,QAAQ,mBAAmB,CAAC;EACtC,UAAU;EACV,OAAO,QAAQ,UAAU,YAAYC,UAAAA,IAAI,QAAQ,YAAY;EAC7D,UAAU,YAAY,CAAC;CAEZ;AACf;;;;;;;AAQA,SAAgB,mBAAiF,QAMlE;CAC7B,OAAO;EACL,MAAM,OAAO;EACb,SAAS,OAAO;EAChB,UAAU,OAAO;EACjB,QAAQ,OAAO;EACf,cAAc,OAAO;EACrB,OAAO,CAAC;CACV;AACF;AAYA,SAAS,0BAAiE,MAAqF;CAC7J,MAAM,QAAA,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.d.ts
CHANGED
|
@@ -1,28 +1,23 @@
|
|
|
1
|
-
import { t as __name } from "./
|
|
2
|
-
import {
|
|
3
|
-
import { OperationNode, SchemaNode
|
|
4
|
-
|
|
1
|
+
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
+
import { A as Generator, F as Parser, N as KubbDriver, Rt as Adapter, W as NormalizedPlugin, X as PluginFactoryOptions, r as Config, zt as AdapterFactoryOptions } from "./types-DM7-MGjZ.js";
|
|
3
|
+
import { FileNode, InputMeta, Macro, OperationNode, SchemaNode } from "@kubb/ast";
|
|
5
4
|
//#region src/mocks.d.ts
|
|
6
5
|
/**
|
|
7
|
-
|
|
8
|
-
* Creates a minimal `PluginDriver` mock for unit tests.
|
|
6
|
+
* Creates a minimal `KubbDriver` mock for unit tests.
|
|
9
7
|
*/
|
|
10
8
|
declare function createMockedPluginDriver(options?: {
|
|
11
9
|
name?: string;
|
|
12
10
|
plugin?: NormalizedPlugin;
|
|
13
11
|
config?: Config;
|
|
14
|
-
}):
|
|
12
|
+
}): KubbDriver;
|
|
15
13
|
/**
|
|
16
14
|
* Creates a minimal `Adapter` mock for unit tests.
|
|
17
|
-
* `parse` returns an empty `InputNode` by default
|
|
18
|
-
* `getImports` returns `[]` by default.
|
|
15
|
+
* `parse` returns an empty `InputNode` by default. Override via `options.parse`.
|
|
19
16
|
*/
|
|
20
17
|
declare function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(options?: {
|
|
21
18
|
name?: TOptions['name'];
|
|
22
19
|
resolvedOptions?: TOptions['resolvedOptions'];
|
|
23
|
-
inputNode?: Adapter<TOptions>['inputNode'];
|
|
24
20
|
parse?: Adapter<TOptions>['parse'];
|
|
25
|
-
getImports?: Adapter<TOptions>['getImports'];
|
|
26
21
|
}): Adapter<TOptions>;
|
|
27
22
|
/**
|
|
28
23
|
* Creates a minimal plugin mock for unit tests.
|
|
@@ -34,13 +29,14 @@ declare function createMockedPlugin<TOptions extends PluginFactoryOptions = Plug
|
|
|
34
29
|
name: TOptions['name'];
|
|
35
30
|
options: TOptions['resolvedOptions'];
|
|
36
31
|
resolver?: TOptions['resolver'];
|
|
37
|
-
|
|
32
|
+
macros?: Array<Macro>;
|
|
38
33
|
dependencies?: Array<string>;
|
|
39
34
|
}): NormalizedPlugin<TOptions>;
|
|
40
35
|
type RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {
|
|
41
36
|
config: Config;
|
|
42
37
|
adapter: Adapter;
|
|
43
|
-
|
|
38
|
+
meta?: InputMeta;
|
|
39
|
+
driver: KubbDriver;
|
|
44
40
|
plugin: NormalizedPlugin<TOptions>;
|
|
45
41
|
options: TOptions['resolvedOptions'];
|
|
46
42
|
resolver: TOptions['resolver'];
|
|
@@ -75,6 +71,33 @@ declare function renderGeneratorOperation<TOptions extends PluginFactoryOptions>
|
|
|
75
71
|
* ```
|
|
76
72
|
*/
|
|
77
73
|
declare function renderGeneratorOperations<TOptions extends PluginFactoryOptions>(generator: Generator<TOptions>, nodes: Array<OperationNode>, opts: RenderGeneratorOptions<TOptions>): Promise<void>;
|
|
74
|
+
type MatchFilesOptions = {
|
|
75
|
+
/**
|
|
76
|
+
* Parsers indexed by file extension, used to render each `FileNode` to source.
|
|
77
|
+
* Without a matching parser the file's raw content is used.
|
|
78
|
+
*/
|
|
79
|
+
parsers?: Map<FileNode['extname'], Parser>;
|
|
80
|
+
/**
|
|
81
|
+
* Formatter applied to non-JSON output before snapshotting, e.g. prettier. When
|
|
82
|
+
* omitted the parsed source is snapshotted as-is.
|
|
83
|
+
*/
|
|
84
|
+
format?: (source?: string) => string | Promise<string>;
|
|
85
|
+
/**
|
|
86
|
+
* Subfolder under `__snapshots__`, camelCased. Useful to keep variant snapshots apart.
|
|
87
|
+
*/
|
|
88
|
+
pre?: string;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Renders the driver's collected `FileNode`s to source and asserts each against a file snapshot.
|
|
92
|
+
* Pair it with the `renderGenerator*` helpers to snapshot a generator's output.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
97
|
+
* await matchFiles(driver.fileManager.files, { parsers, format })
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function matchFiles(files: Array<FileNode> | undefined, options?: MatchFilesOptions): Promise<Map<string, string> | undefined>;
|
|
78
101
|
//#endregion
|
|
79
|
-
export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
|
|
102
|
+
export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, matchFiles, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
|
|
80
103
|
//# sourceMappingURL=mocks.d.ts.map
|
package/dist/mocks.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import { n as
|
|
3
|
-
import { resolve } from "node:path";
|
|
4
|
-
import {
|
|
1
|
+
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
+
import { f as camelCase, n as FileManager, r as Hookable, t as _usingCtx } from "./usingCtx-BNggxUEL.js";
|
|
3
|
+
import path, { resolve } from "node:path";
|
|
4
|
+
import { applyMacros, ast } from "@kubb/ast";
|
|
5
|
+
import { expect } from "vitest";
|
|
5
6
|
//#region src/mocks.ts
|
|
6
7
|
/**
|
|
7
|
-
|
|
8
|
-
* Creates a minimal `PluginDriver` mock for unit tests.
|
|
8
|
+
* Creates a minimal `KubbDriver` mock for unit tests.
|
|
9
9
|
*/
|
|
10
10
|
function createMockedPluginDriver(options = {}) {
|
|
11
|
+
const fileManager = new FileManager();
|
|
11
12
|
return {
|
|
12
13
|
config: options?.config ?? {
|
|
13
14
|
root: ".",
|
|
@@ -17,26 +18,38 @@ function createMockedPluginDriver(options = {}) {
|
|
|
17
18
|
return options?.plugin;
|
|
18
19
|
},
|
|
19
20
|
getResolver: (_pluginName) => options?.plugin?.resolver,
|
|
20
|
-
fileManager
|
|
21
|
+
fileManager,
|
|
22
|
+
async dispatch({ result, renderer }) {
|
|
23
|
+
try {
|
|
24
|
+
var _usingCtx$1 = _usingCtx();
|
|
25
|
+
if (!result) return;
|
|
26
|
+
if (Array.isArray(result)) {
|
|
27
|
+
fileManager.upsert(...result);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!renderer) return;
|
|
31
|
+
const instance = _usingCtx$1.u(renderer());
|
|
32
|
+
await instance.render(result);
|
|
33
|
+
fileManager.upsert(...instance.files);
|
|
34
|
+
} catch (_) {
|
|
35
|
+
_usingCtx$1.e = _;
|
|
36
|
+
} finally {
|
|
37
|
+
_usingCtx$1.d();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
21
40
|
};
|
|
22
41
|
}
|
|
23
42
|
/**
|
|
24
43
|
* Creates a minimal `Adapter` mock for unit tests.
|
|
25
|
-
* `parse` returns an empty `InputNode` by default
|
|
26
|
-
* `getImports` returns `[]` by default.
|
|
44
|
+
* `parse` returns an empty `InputNode` by default. Override via `options.parse`.
|
|
27
45
|
*/
|
|
28
46
|
function createMockedAdapter(options = {}) {
|
|
29
|
-
const inputNode = options.inputNode ?? null;
|
|
30
47
|
return {
|
|
31
48
|
name: options.name ?? "oas",
|
|
32
49
|
options: options.resolvedOptions ?? {},
|
|
33
|
-
|
|
34
|
-
parse: options.parse ?? (async () => (
|
|
35
|
-
|
|
36
|
-
schemas: [],
|
|
37
|
-
operations: []
|
|
38
|
-
})),
|
|
39
|
-
getImports: options.getImports ?? ((_node, _resolve) => [])
|
|
50
|
+
document: null,
|
|
51
|
+
parse: options.parse ?? (async () => ast.factory.createInput()),
|
|
52
|
+
validate: async () => {}
|
|
40
53
|
};
|
|
41
54
|
}
|
|
42
55
|
/**
|
|
@@ -50,7 +63,7 @@ function createMockedPlugin(params) {
|
|
|
50
63
|
name: params.name,
|
|
51
64
|
options: params.options,
|
|
52
65
|
resolver: params.resolver,
|
|
53
|
-
|
|
66
|
+
macros: params.macros,
|
|
54
67
|
dependencies: params.dependencies,
|
|
55
68
|
hooks: {}
|
|
56
69
|
};
|
|
@@ -60,24 +73,21 @@ function createMockedPluginContext(opts) {
|
|
|
60
73
|
return {
|
|
61
74
|
config: opts.config,
|
|
62
75
|
root,
|
|
63
|
-
getMode: (output) => PluginDriver.getMode(resolve(root, output.path)),
|
|
64
76
|
adapter: opts.adapter,
|
|
65
77
|
resolver: opts.resolver,
|
|
66
78
|
plugin: opts.plugin,
|
|
67
79
|
driver: opts.driver,
|
|
68
80
|
getResolver: (name) => opts.driver.getResolver(name),
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
operations: []
|
|
81
|
+
meta: opts.meta ?? {
|
|
82
|
+
circularNames: [],
|
|
83
|
+
enumNames: []
|
|
73
84
|
},
|
|
74
85
|
addFile: async (...files) => opts.driver.fileManager.add(...files),
|
|
75
86
|
upsertFile: async (...files) => opts.driver.fileManager.upsert(...files),
|
|
76
|
-
hooks: opts.driver.hooks ??
|
|
87
|
+
hooks: opts.driver.hooks ?? new Hookable(),
|
|
77
88
|
warn: (msg) => console.warn(msg),
|
|
78
89
|
error: (msg) => console.error(msg),
|
|
79
|
-
info: (msg) => console.info(msg)
|
|
80
|
-
openInStudio: async () => {}
|
|
90
|
+
info: (msg) => console.info(msg)
|
|
81
91
|
};
|
|
82
92
|
}
|
|
83
93
|
/**
|
|
@@ -92,11 +102,15 @@ function createMockedPluginContext(opts) {
|
|
|
92
102
|
async function renderGeneratorSchema(generator, node, opts) {
|
|
93
103
|
if (!generator.schema) return;
|
|
94
104
|
const context = createMockedPluginContext(opts);
|
|
95
|
-
const transformedNode = opts.plugin.
|
|
96
|
-
|
|
105
|
+
const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node;
|
|
106
|
+
const result = await generator.schema(transformedNode, {
|
|
97
107
|
...context,
|
|
98
108
|
options: opts.options
|
|
99
|
-
})
|
|
109
|
+
});
|
|
110
|
+
await opts.driver.dispatch({
|
|
111
|
+
result,
|
|
112
|
+
renderer: generator.renderer
|
|
113
|
+
});
|
|
100
114
|
}
|
|
101
115
|
/**
|
|
102
116
|
* Renders a generator's `operation` method in a test context.
|
|
@@ -110,11 +124,15 @@ async function renderGeneratorSchema(generator, node, opts) {
|
|
|
110
124
|
async function renderGeneratorOperation(generator, node, opts) {
|
|
111
125
|
if (!generator.operation) return;
|
|
112
126
|
const context = createMockedPluginContext(opts);
|
|
113
|
-
const transformedNode = opts.plugin.
|
|
114
|
-
|
|
127
|
+
const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node;
|
|
128
|
+
const result = await generator.operation(transformedNode, {
|
|
115
129
|
...context,
|
|
116
130
|
options: opts.options
|
|
117
|
-
})
|
|
131
|
+
});
|
|
132
|
+
await opts.driver.dispatch({
|
|
133
|
+
result,
|
|
134
|
+
renderer: generator.renderer
|
|
135
|
+
});
|
|
118
136
|
}
|
|
119
137
|
/**
|
|
120
138
|
* Renders a generator's `operations` method in a test context.
|
|
@@ -128,13 +146,42 @@ async function renderGeneratorOperation(generator, node, opts) {
|
|
|
128
146
|
async function renderGeneratorOperations(generator, nodes, opts) {
|
|
129
147
|
if (!generator.operations) return;
|
|
130
148
|
const context = createMockedPluginContext(opts);
|
|
131
|
-
const transformedNodes = opts.plugin.
|
|
132
|
-
|
|
149
|
+
const transformedNodes = opts.plugin.macros?.length ? nodes.map((n) => applyMacros(n, opts.plugin.macros)) : nodes;
|
|
150
|
+
const result = await generator.operations(transformedNodes, {
|
|
133
151
|
...context,
|
|
134
152
|
options: opts.options
|
|
135
|
-
})
|
|
153
|
+
});
|
|
154
|
+
await opts.driver.dispatch({
|
|
155
|
+
result,
|
|
156
|
+
renderer: generator.renderer
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Renders the driver's collected `FileNode`s to source and asserts each against a file snapshot.
|
|
161
|
+
* Pair it with the `renderGenerator*` helpers to snapshot a generator's output.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* await renderGeneratorSchema(typeGenerator, node, { config, adapter, driver, plugin, options, resolver })
|
|
166
|
+
* await matchFiles(driver.fileManager.files, { parsers, format })
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
async function matchFiles(files, options = {}) {
|
|
170
|
+
if (!files?.length) return;
|
|
171
|
+
const { parsers = /* @__PURE__ */ new Map(), format, pre } = options;
|
|
172
|
+
const fileManager = new FileManager();
|
|
173
|
+
const processed = /* @__PURE__ */ new Map();
|
|
174
|
+
for (const file of files) {
|
|
175
|
+
if (!file?.path || processed.has(file.path)) continue;
|
|
176
|
+
const parsed = await fileManager.parse(file, { parsers });
|
|
177
|
+
const code = file.baseName.endsWith(".json") || !format ? parsed : await format(parsed);
|
|
178
|
+
processed.set(file.path, code);
|
|
179
|
+
const snapshotPath = path.join("__snapshots__", ...pre ? [camelCase(pre)] : [], file.baseName);
|
|
180
|
+
await expect(code).toMatchFileSnapshot(snapshotPath);
|
|
181
|
+
}
|
|
182
|
+
return processed;
|
|
136
183
|
}
|
|
137
184
|
//#endregion
|
|
138
|
-
export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
|
|
185
|
+
export { createMockedAdapter, createMockedPlugin, createMockedPluginDriver, matchFiles, renderGeneratorOperation, renderGeneratorOperations, renderGeneratorSchema };
|
|
139
186
|
|
|
140
187
|
//# sourceMappingURL=mocks.js.map
|
package/dist/mocks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mocks.js","names":[],"sources":["../src/mocks.ts"],"sourcesContent":["import { resolve } from 'node:path'\nimport type { FileNode, OperationNode, SchemaNode, Visitor } from '@kubb/ast'\nimport { transform } from '@kubb/ast'\nimport { FileManager } from './FileManager.ts'\nimport { PluginDriver } from './PluginDriver.ts'\nimport { applyHookResult } from './renderNode.ts'\nimport type { Adapter, AdapterFactoryOptions, Config, Generator, GeneratorContext, NormalizedPlugin, PluginFactoryOptions } from './types.ts'\n\n/**\n\n * Creates a minimal `PluginDriver` mock for unit tests.\n */\nexport function createMockedPluginDriver(options: { name?: string; plugin?: NormalizedPlugin; config?: Config } = {}): PluginDriver {\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: new FileManager(),\n } as unknown as PluginDriver\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 * `getImports` returns `[]` by default.\n */\nexport function createMockedAdapter<TOptions extends AdapterFactoryOptions = AdapterFactoryOptions>(\n options: {\n name?: TOptions['name']\n resolvedOptions?: TOptions['resolvedOptions']\n inputNode?: Adapter<TOptions>['inputNode']\n parse?: Adapter<TOptions>['parse']\n getImports?: Adapter<TOptions>['getImports']\n } = {},\n): Adapter<TOptions> {\n const inputNode = options.inputNode ?? null\n return {\n name: (options.name ?? 'oas') as TOptions['name'],\n options: (options.resolvedOptions ?? {}) as TOptions['resolvedOptions'],\n inputNode,\n parse: options.parse ?? (async () => ({ kind: 'Input' as const, schemas: [], operations: [] })),\n getImports: options.getImports ?? ((_node: SchemaNode, _resolve: (schemaName: string) => { name: string; path: string }) => []),\n } as Adapter<TOptions>\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 PluginFactoryOptions = PluginFactoryOptions>(params: {\n name: TOptions['name']\n options: TOptions['resolvedOptions']\n resolver?: TOptions['resolver']\n transformer?: Visitor\n dependencies?: Array<string>\n}): NormalizedPlugin<TOptions> {\n return {\n name: params.name,\n options: params.options,\n resolver: params.resolver,\n transformer: params.transformer,\n dependencies: params.dependencies,\n hooks: {},\n } as unknown as NormalizedPlugin<TOptions>\n}\n\ntype RenderGeneratorOptions<TOptions extends PluginFactoryOptions> = {\n config: Config\n adapter: Adapter\n driver: PluginDriver\n plugin: NormalizedPlugin<TOptions>\n options: TOptions['resolvedOptions']\n resolver: TOptions['resolver']\n}\n\nfunction createMockedPluginContext<TOptions extends PluginFactoryOptions>(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 getMode: (output: { path: string }) => PluginDriver.getMode(resolve(root, output.path)),\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 inputNode: { kind: 'Input', schemas: [], operations: [] },\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 ?? ({} as never),\n warn: (msg: string) => console.warn(msg),\n error: (msg: string) => console.error(msg),\n info: (msg: string) => console.info(msg),\n openInStudio: async () => {},\n } as unknown as Omit<GeneratorContext<TOptions>, 'options'>\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.transformer ? transform(node, opts.plugin.transformer) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\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 PluginFactoryOptions>(\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.transformer ? nodes.map((n) => transform(n, opts.plugin.transformer!)) : nodes\n const result = await generator.operations(transformedNodes, {\n ...context,\n options: opts.options,\n })\n await applyHookResult(result, opts.driver, generator.renderer ?? undefined)\n}\n"],"mappings":";;;;;;;;;AAYA,SAAgB,yBAAyB,UAAyE,EAAE,EAAgB;AAClI,QAAO;EACL,QAAQ,SAAS,UAAU;GACzB,MAAM;GACN,QAAQ,EACN,MAAM,UACP;GACF;EACD,UAAU,aAAmD;AAC3D,UAAO,SAAS;;EAElB,cAAc,gBAAwB,SAAS,QAAQ;EACvD,aAAa,IAAI,aAAa;EAC/B;;;;;;;AAQH,SAAgB,oBACd,UAMI,EAAE,EACa;CACnB,MAAM,YAAY,QAAQ,aAAa;AACvC,QAAO;EACL,MAAO,QAAQ,QAAQ;EACvB,SAAU,QAAQ,mBAAmB,EAAE;EAC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ACrD,GAAG;EACH,SAAS,KAAK;EACf,CAAC,EAC4B,KAAK,QAAQ,UAAU,YAAY,KAAA,EAAU;;;;;;;;;;;AAY7E,eAAsB,yBACpB,WACA,MACA,MACe;AACf,KAAI,CAAC,UAAU,UAAW;CAC1B,MAAM,UAAU,0BAA0B,KAAK;CAC/C,MAAM,kBAAkB,KAAK,OAAO,cAAc,UAAU,MAAM,KAAK,OAAO,YAAY,GAAG;AAK7F,OAAM,gBAAgB,MAJD,UAAU,UAAU,iBAAiB;EACxD,GAAG;EACH,SAAS,KAAK;EACf,CAAC,EAC4B,KAAK,QAAQ,UAAU,YAAY,KAAA,EAAU;;;;;;;;;;;AAY7E,eAAsB,0BACpB,WACA,OACA,MACe;AACf,KAAI,CAAC,UAAU,WAAY;CAC3B,MAAM,UAAU,0BAA0B,KAAK;CAC/C,MAAM,mBAAmB,KAAK,OAAO,cAAc,MAAM,KAAK,MAAM,UAAU,GAAG,KAAK,OAAO,YAAa,CAAC,GAAG;AAK9G,OAAM,gBAAgB,MAJD,UAAU,WAAW,kBAAkB;EAC1D,GAAG;EACH,SAAS,KAAK;EACf,CAAC,EAC4B,KAAK,QAAQ,UAAU,YAAY,KAAA,EAAU"}
|
|
1
|
+
{"version":3,"file":"mocks.js","names":[],"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 } from 'vitest'\nimport type { Parser } from './defineParser.ts'\nimport { FileManager } from './FileManager.ts'\nimport { Hookable } from './Hookable.ts'\nimport type { KubbDriver } from './KubbDriver.ts'\nimport type {\n Adapter,\n AdapterFactoryOptions,\n Config,\n Generator,\n GeneratorContext,\n KubbHooks,\n NormalizedPlugin,\n PluginFactoryOptions,\n RendererFactory,\n} from './types.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 async dispatch({ result, renderer }: { result: unknown; renderer?: RendererFactory | null }): Promise<void> {\n if (!result) return\n\n if (Array.isArray(result)) {\n fileManager.upsert(...(result as Array<FileNode>))\n return\n }\n\n if (!renderer) return\n\n using instance = renderer()\n await instance.render(result)\n fileManager.upsert(...instance.files)\n },\n } as unknown as KubbDriver\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 PluginFactoryOptions = PluginFactoryOptions>(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 PluginFactoryOptions> = {\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 PluginFactoryOptions>(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 meta: opts.meta ?? { circularNames: [], enumNames: [] },\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\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: SchemaNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.schema) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const result = await generator.schema(transformedNode, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\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 PluginFactoryOptions>(\n generator: Generator<TOptions>,\n node: OperationNode,\n opts: RenderGeneratorOptions<TOptions>,\n): Promise<void> {\n if (!generator.operation) return\n const context = createMockedPluginContext(opts)\n const transformedNode = opts.plugin.macros?.length ? applyMacros(node, opts.plugin.macros) : node\n const result = await generator.operation(transformedNode, {\n ...context,\n options: opts.options,\n })\n await opts.driver.dispatch({ result, renderer: generator.renderer })\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 PluginFactoryOptions>(\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.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":";;;;;;;;;AAwBA,SAAgB,yBAAyB,UAAyE,CAAC,GAAe;CAChI,MAAM,cAAc,IAAI,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;EACA,MAAM,SAAS,EAAE,QAAQ,YAAmF;;;IAC1G,IAAI,CAAC,QAAQ;IAEb,IAAI,MAAM,QAAQ,MAAM,GAAG;KACzB,YAAY,OAAO,GAAI,MAA0B;KACjD;IACF;IAEA,IAAI,CAAC,UAAU;IAEf,MAAM,WAAA,YAAA,EAAW,SAAS,CAAA;IAC1B,MAAM,SAAS,OAAO,MAAM;IAC5B,YAAY,OAAO,GAAG,SAAS,KAAK;;;;;;EACtC;CACF;AACF;;;;;AAMA,SAAgB,oBACd,UAII,CAAC,GACc;CAQnB,OAAO;EANL,MAAO,QAAQ,QAAQ;EACvB,SAAU,QAAQ,mBAAmB,CAAC;EACtC,UAAU;EACV,OAAO,QAAQ,UAAU,YAAY,IAAI,QAAQ,YAAY;EAC7D,UAAU,YAAY,CAAC;CAEZ;AACf;;;;;;;AAQA,SAAgB,mBAAiF,QAMlE;CAC7B,OAAO;EACL,MAAM,OAAO;EACb,SAAS,OAAO;EAChB,UAAU,OAAO;EACjB,QAAQ,OAAO;EACf,cAAc,OAAO;EACrB,OAAO,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