@kubb/plugin-msw 5.0.0-beta.3 → 5.0.0-beta.31
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/README.md +26 -5
- package/dist/{components-CLQ77DVn.cjs → components-B1Dsj2WT.cjs} +67 -72
- package/dist/components-B1Dsj2WT.cjs.map +1 -0
- package/dist/{components-vO0FIb2i.js → components-BxzfyX2u.js} +64 -63
- package/dist/components-BxzfyX2u.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +5 -5
- package/dist/components.js +1 -1
- package/dist/{generators-CrmMwWE4.cjs → generators-C5AvweCJ.cjs} +61 -31
- package/dist/generators-C5AvweCJ.cjs.map +1 -0
- package/dist/{generators-BPJCs1x1.js → generators-srLe3oqm.js} +63 -33
- package/dist/generators-srLe3oqm.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +13 -1
- package/dist/generators.js +1 -1
- package/dist/index.cjs +90 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +31 -1
- package/dist/index.js +90 -15
- package/dist/index.js.map +1 -1
- package/dist/types-CLAiv8qc.d.ts +103 -0
- package/extension.yaml +680 -0
- package/package.json +11 -14
- package/src/components/Handlers.tsx +1 -1
- package/src/components/Mock.tsx +5 -4
- package/src/components/MockWithFaker.tsx +5 -4
- package/src/components/Response.tsx +1 -1
- package/src/generators/handlersGenerator.tsx +18 -12
- package/src/generators/mswGenerator.tsx +29 -18
- package/src/plugin.ts +34 -18
- package/src/resolvers/resolverMsw.ts +19 -3
- package/src/types.ts +35 -21
- package/src/utils.ts +26 -61
- package/dist/components-CLQ77DVn.cjs.map +0 -1
- package/dist/components-vO0FIb2i.js.map +0 -1
- package/dist/generators-BPJCs1x1.js.map +0 -1
- package/dist/generators-CrmMwWE4.cjs.map +0 -1
- package/dist/types-Dxu0KMQ4.d.ts +0 -89
|
@@ -1,27 +1,34 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import { a as
|
|
3
|
-
import { defineGenerator } from "@kubb/core";
|
|
2
|
+
import { a as Handlers, i as resolveFakerMeta, n as MockWithFaker, o as getOperationSuccessResponses, r as Mock, s as resolveResponseTypes, t as Response } from "./components-BxzfyX2u.js";
|
|
3
|
+
import { ast, defineGenerator } from "@kubb/core";
|
|
4
4
|
import { pluginFakerName } from "@kubb/plugin-faker";
|
|
5
5
|
import { pluginTsName } from "@kubb/plugin-ts";
|
|
6
|
-
import { File,
|
|
6
|
+
import { File, jsxRendererSync } from "@kubb/renderer-jsx";
|
|
7
7
|
import { jsx, jsxs } from "@kubb/renderer-jsx/jsx-runtime";
|
|
8
8
|
//#region src/generators/handlersGenerator.tsx
|
|
9
|
+
/**
|
|
10
|
+
* Aggregate generator enabled by `pluginMsw({ handlers: true })`. Emits a
|
|
11
|
+
* `handlers.ts` file that re-exports every generated handler grouped by HTTP
|
|
12
|
+
* method, ready to spread into `setupServer(...handlers)` or
|
|
13
|
+
* `setupWorker(...handlers)`.
|
|
14
|
+
*/
|
|
9
15
|
const handlersGenerator = defineGenerator({
|
|
10
16
|
name: "plugin-msw",
|
|
11
|
-
renderer:
|
|
17
|
+
renderer: jsxRendererSync,
|
|
12
18
|
operations(nodes, ctx) {
|
|
13
|
-
const { resolver, config, root
|
|
14
|
-
const { output, group
|
|
19
|
+
const { resolver, config, root } = ctx;
|
|
20
|
+
const { output, group } = ctx.options;
|
|
21
|
+
const handlersName = resolver.resolveHandlersName();
|
|
15
22
|
const file = resolver.resolveFile({
|
|
16
|
-
name: "
|
|
23
|
+
name: resolver.resolvePathName(handlersName, "file"),
|
|
17
24
|
extname: ".ts"
|
|
18
25
|
}, {
|
|
19
26
|
root,
|
|
20
27
|
output,
|
|
21
|
-
group
|
|
28
|
+
group: group ?? void 0
|
|
22
29
|
});
|
|
23
30
|
const imports = nodes.map((node) => {
|
|
24
|
-
const operationName =
|
|
31
|
+
const operationName = resolver.resolveHandlerName(node);
|
|
25
32
|
const operationFile = resolver.resolveFile({
|
|
26
33
|
name: resolver.resolveName(node.operationId),
|
|
27
34
|
extname: ".ts",
|
|
@@ -30,7 +37,7 @@ const handlersGenerator = defineGenerator({
|
|
|
30
37
|
}, {
|
|
31
38
|
root,
|
|
32
39
|
output,
|
|
33
|
-
group
|
|
40
|
+
group: group ?? void 0
|
|
34
41
|
});
|
|
35
42
|
return /* @__PURE__ */ jsx(File.Import, {
|
|
36
43
|
name: [operationName],
|
|
@@ -38,21 +45,29 @@ const handlersGenerator = defineGenerator({
|
|
|
38
45
|
path: operationFile.path
|
|
39
46
|
}, operationFile.path);
|
|
40
47
|
});
|
|
41
|
-
const handlers = nodes.map((node) => `${
|
|
48
|
+
const handlers = nodes.map((node) => `${resolver.resolveHandlerName(node)}()`);
|
|
42
49
|
return /* @__PURE__ */ jsxs(File, {
|
|
43
50
|
baseName: file.baseName,
|
|
44
51
|
path: file.path,
|
|
45
52
|
meta: file.meta,
|
|
46
|
-
banner: resolver.resolveBanner(
|
|
53
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
47
54
|
output,
|
|
48
|
-
config
|
|
55
|
+
config,
|
|
56
|
+
file: {
|
|
57
|
+
path: file.path,
|
|
58
|
+
baseName: file.baseName
|
|
59
|
+
}
|
|
49
60
|
}),
|
|
50
|
-
footer: resolver.resolveFooter(
|
|
61
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
51
62
|
output,
|
|
52
|
-
config
|
|
63
|
+
config,
|
|
64
|
+
file: {
|
|
65
|
+
path: file.path,
|
|
66
|
+
baseName: file.baseName
|
|
67
|
+
}
|
|
53
68
|
}),
|
|
54
69
|
children: [imports, /* @__PURE__ */ jsx(Handlers, {
|
|
55
|
-
name:
|
|
70
|
+
name: handlersName,
|
|
56
71
|
handlers
|
|
57
72
|
})]
|
|
58
73
|
});
|
|
@@ -60,15 +75,22 @@ const handlersGenerator = defineGenerator({
|
|
|
60
75
|
});
|
|
61
76
|
//#endregion
|
|
62
77
|
//#region src/generators/mswGenerator.tsx
|
|
78
|
+
/**
|
|
79
|
+
* Built-in operation generator for `@kubb/plugin-msw`. Emits one MSW handler
|
|
80
|
+
* per OpenAPI operation. With `parser: 'faker'` the handler returns a value
|
|
81
|
+
* from `@kubb/plugin-faker`; with `parser: 'data'` it returns a typed empty
|
|
82
|
+
* payload for tests to fill in.
|
|
83
|
+
*/
|
|
63
84
|
const mswGenerator = defineGenerator({
|
|
64
85
|
name: "msw",
|
|
65
|
-
renderer:
|
|
86
|
+
renderer: jsxRendererSync,
|
|
66
87
|
operation(node, ctx) {
|
|
67
|
-
|
|
68
|
-
const {
|
|
88
|
+
if (!ast.isHttpOperationNode(node)) return null;
|
|
89
|
+
const { driver, resolver, config, root } = ctx;
|
|
90
|
+
const { output, parser, baseURL, group } = ctx.options;
|
|
69
91
|
const fileName = resolver.resolveName(node.operationId);
|
|
70
92
|
const mock = {
|
|
71
|
-
name:
|
|
93
|
+
name: resolver.resolveHandlerName(node),
|
|
72
94
|
file: resolver.resolveFile({
|
|
73
95
|
name: fileName,
|
|
74
96
|
extname: ".ts",
|
|
@@ -77,16 +99,16 @@ const mswGenerator = defineGenerator({
|
|
|
77
99
|
}, {
|
|
78
100
|
root,
|
|
79
101
|
output,
|
|
80
|
-
group
|
|
102
|
+
group: group ?? void 0
|
|
81
103
|
})
|
|
82
104
|
};
|
|
83
|
-
const fakerPlugin = parser === "faker" ? driver.getPlugin(pluginFakerName) :
|
|
105
|
+
const fakerPlugin = parser === "faker" ? driver.getPlugin(pluginFakerName) : null;
|
|
84
106
|
const faker = parser === "faker" && fakerPlugin ? resolveFakerMeta(node, {
|
|
85
107
|
root,
|
|
86
108
|
fakerResolver: driver.getResolver(pluginFakerName),
|
|
87
109
|
fakerOutput: fakerPlugin.options?.output ?? output,
|
|
88
|
-
fakerGroup: fakerPlugin.options?.group
|
|
89
|
-
}) :
|
|
110
|
+
fakerGroup: fakerPlugin.options?.group ?? null
|
|
111
|
+
}) : null;
|
|
90
112
|
const pluginTs = driver.getPlugin(pluginTsName);
|
|
91
113
|
if (!pluginTs) return null;
|
|
92
114
|
const tsResolver = driver.getResolver(pluginTsName);
|
|
@@ -99,24 +121,32 @@ const mswGenerator = defineGenerator({
|
|
|
99
121
|
}, {
|
|
100
122
|
root,
|
|
101
123
|
output: pluginTs.options?.output ?? output,
|
|
102
|
-
group: pluginTs.options?.group
|
|
124
|
+
group: pluginTs.options?.group ?? void 0
|
|
103
125
|
}),
|
|
104
126
|
responseName: tsResolver.resolveResponseName(node)
|
|
105
127
|
};
|
|
106
|
-
const types =
|
|
107
|
-
const hasSuccessSchema =
|
|
108
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) :
|
|
128
|
+
const types = resolveResponseTypes(node, tsResolver);
|
|
129
|
+
const hasSuccessSchema = getOperationSuccessResponses(node).some((response) => !!response.content?.[0]?.schema);
|
|
130
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null;
|
|
109
131
|
return /* @__PURE__ */ jsxs(File, {
|
|
110
132
|
baseName: mock.file.baseName,
|
|
111
133
|
path: mock.file.path,
|
|
112
134
|
meta: mock.file.meta,
|
|
113
|
-
banner: resolver.resolveBanner(
|
|
135
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
114
136
|
output,
|
|
115
|
-
config
|
|
137
|
+
config,
|
|
138
|
+
file: {
|
|
139
|
+
path: mock.file.path,
|
|
140
|
+
baseName: mock.file.baseName
|
|
141
|
+
}
|
|
116
142
|
}),
|
|
117
|
-
footer: resolver.resolveFooter(
|
|
143
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
118
144
|
output,
|
|
119
|
-
config
|
|
145
|
+
config,
|
|
146
|
+
file: {
|
|
147
|
+
path: mock.file.path,
|
|
148
|
+
baseName: mock.file.baseName
|
|
149
|
+
}
|
|
120
150
|
}),
|
|
121
151
|
children: [
|
|
122
152
|
/* @__PURE__ */ jsx(File.Import, {
|
|
@@ -173,4 +203,4 @@ const mswGenerator = defineGenerator({
|
|
|
173
203
|
//#endregion
|
|
174
204
|
export { handlersGenerator as n, mswGenerator as t };
|
|
175
205
|
|
|
176
|
-
//# sourceMappingURL=generators-
|
|
206
|
+
//# sourceMappingURL=generators-srLe3oqm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generators-srLe3oqm.js","names":[],"sources":["../src/generators/handlersGenerator.tsx","../src/generators/mswGenerator.tsx"],"sourcesContent":["import { defineGenerator } from '@kubb/core'\nimport { File, jsxRendererSync } from '@kubb/renderer-jsx'\nimport { Handlers } from '../components/Handlers.tsx'\nimport type { PluginMsw } from '../types'\n\n/**\n * Aggregate generator enabled by `pluginMsw({ handlers: true })`. Emits a\n * `handlers.ts` file that re-exports every generated handler grouped by HTTP\n * method, ready to spread into `setupServer(...handlers)` or\n * `setupWorker(...handlers)`.\n */\nexport const handlersGenerator = defineGenerator<PluginMsw>({\n name: 'plugin-msw',\n renderer: jsxRendererSync,\n operations(nodes, ctx) {\n const { resolver, config, root } = ctx\n const { output, group } = ctx.options\n\n const handlersName = resolver.resolveHandlersName()\n const file = resolver.resolveFile({ name: resolver.resolvePathName(handlersName, 'file'), extname: '.ts' }, { root, output, group: group ?? undefined })\n\n const imports = nodes.map((node) => {\n const operationName = resolver.resolveHandlerName(node)\n const operationFile = resolver.resolveFile(\n { name: resolver.resolveName(node.operationId), extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },\n { root, output, group: group ?? undefined },\n )\n\n return <File.Import key={operationFile.path} name={[operationName]} root={file.path} path={operationFile.path} />\n })\n\n const handlers = nodes.map((node) => `${resolver.resolveHandlerName(node)}()`)\n\n return (\n <File\n baseName={file.baseName}\n path={file.path}\n meta={file.meta}\n banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: file.path, baseName: file.baseName } })}\n footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: file.path, baseName: file.baseName } })}\n >\n {imports}\n <Handlers name={handlersName} handlers={handlers} />\n </File>\n )\n },\n})\n","import { getOperationSuccessResponses, resolveResponseTypes } from '@internals/shared'\nimport { ast, defineGenerator } from '@kubb/core'\nimport { pluginFakerName } from '@kubb/plugin-faker'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { File, jsxRendererSync } from '@kubb/renderer-jsx'\nimport { Mock, MockWithFaker, Response } from '../components'\nimport type { PluginMsw } from '../types'\nimport { resolveFakerMeta } from '../utils.ts'\n\n/**\n * Built-in operation generator for `@kubb/plugin-msw`. Emits one MSW handler\n * per OpenAPI operation. With `parser: 'faker'` the handler returns a value\n * from `@kubb/plugin-faker`; with `parser: 'data'` it returns a typed empty\n * payload for tests to fill in.\n */\nexport const mswGenerator = defineGenerator<PluginMsw>({\n name: 'msw',\n renderer: jsxRendererSync,\n operation(node, ctx) {\n if (!ast.isHttpOperationNode(node)) return null\n const { driver, resolver, config, root } = ctx\n const { output, parser, baseURL, group } = ctx.options\n\n const fileName = resolver.resolveName(node.operationId)\n const mock = {\n name: resolver.resolveHandlerName(node),\n file: resolver.resolveFile(\n { name: fileName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },\n { root, output, group: group ?? undefined },\n ),\n }\n\n const fakerPlugin = parser === 'faker' ? driver.getPlugin(pluginFakerName) : null\n const faker =\n parser === 'faker' && fakerPlugin\n ? resolveFakerMeta(node, {\n root,\n fakerResolver: driver.getResolver(pluginFakerName),\n fakerOutput: fakerPlugin.options?.output ?? output,\n fakerGroup: fakerPlugin.options?.group ?? null,\n })\n : null\n\n const pluginTs = driver.getPlugin(pluginTsName)\n if (!pluginTs) return null\n const tsResolver = driver.getResolver(pluginTsName)\n\n const type = {\n file: tsResolver.resolveFile(\n { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },\n { root, output: pluginTs.options?.output ?? output, group: pluginTs.options?.group ?? undefined },\n ),\n responseName: tsResolver.resolveResponseName(node),\n }\n\n const types = resolveResponseTypes(node, tsResolver)\n const successResponses = getOperationSuccessResponses(node)\n const hasSuccessSchema = successResponses.some((response) => !!response.content?.[0]?.schema)\n\n const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null\n\n return (\n <File\n baseName={mock.file.baseName}\n path={mock.file.path}\n meta={mock.file.meta}\n banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: mock.file.path, baseName: mock.file.baseName } })}\n footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: mock.file.path, baseName: mock.file.baseName } })}\n >\n <File.Import name={['http']} path=\"msw\" />\n <File.Import name={['HttpResponseResolver']} isTypeOnly path=\"msw\" />\n <File.Import\n name={Array.from(new Set([type.responseName, ...types.map((t) => t[1]), ...(requestName ? [requestName] : [])]))}\n path={type.file.path}\n root={mock.file.path}\n isTypeOnly\n />\n {parser === 'faker' && faker && <File.Import name={[faker.name]} root={mock.file.path} path={faker.file.path} />}\n\n {types\n .filter(([code]) => code !== 'default')\n .map(([code, typeName]) => {\n const response = node.responses.find((item) => item.statusCode === String(code))\n if (!response) return null\n return <Response key={typeName} typeName={typeName} response={response} name={mock.name} />\n })}\n\n {parser === 'faker' && faker && hasSuccessSchema ? (\n <MockWithFaker name={mock.name} typeName={type.responseName} requestTypeName={requestName} fakerName={faker.name} node={node} baseURL={baseURL} />\n ) : (\n <Mock name={mock.name} typeName={type.responseName} requestTypeName={requestName} node={node} baseURL={baseURL} />\n )}\n </File>\n )\n },\n})\n"],"mappings":";;;;;;;;;;;;;;AAWA,MAAa,oBAAoB,gBAA2B;CAC1D,MAAM;CACN,UAAU;CACV,WAAW,OAAO,KAAK;EACrB,MAAM,EAAE,UAAU,QAAQ,SAAS;EACnC,MAAM,EAAE,QAAQ,UAAU,IAAI;EAE9B,MAAM,eAAe,SAAS,qBAAqB;EACnD,MAAM,OAAO,SAAS,YAAY;GAAE,MAAM,SAAS,gBAAgB,cAAc,OAAO;GAAE,SAAS;GAAO,EAAE;GAAE;GAAM;GAAQ,OAAO,SAAS,KAAA;GAAW,CAAC;EAExJ,MAAM,U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AU;IAAE,CAAC;aAL5H;IAOE,oBAAC,KAAK,QAAN;KAAa,MAAM,CAAC,OAAO;KAAE,MAAK;KAAQ,CAAA;IAC1C,oBAAC,KAAK,QAAN;KAAa,MAAM,CAAC,uBAAuB;KAAE,YAAA;KAAW,MAAK;KAAQ,CAAA;IACrE,oBAAC,KAAK,QAAN;KACE,MAAM,MAAM,KAAK,IAAI,IAAI;MAAC,KAAK;MAAc,GAAG,MAAM,KAAK,MAAM,EAAE,GAAG;MAAE,GAAI,cAAc,CAAC,YAAY,GAAG,EAAE;MAAE,CAAC,CAAC;KAChH,MAAM,KAAK,KAAK;KAChB,MAAM,KAAK,KAAK;KAChB,YAAA;KACA,CAAA;IACD,WAAW,WAAW,SAAS,oBAAC,KAAK,QAAN;KAAa,MAAM,CAAC,MAAM,KAAK;KAAE,MAAM,KAAK,KAAK;KAAM,MAAM,MAAM,KAAK;KAAQ,CAAA;IAE/G,MACE,QAAQ,CAAC,UAAU,SAAS,UAAU,CACtC,KAAK,CAAC,MAAM,cAAc;KACzB,MAAM,WAAW,KAAK,UAAU,MAAM,SAAS,KAAK,eAAe,OAAO,KAAK,CAAC;KAChF,IAAI,CAAC,UAAU,OAAO;KACtB,OAAO,oBAAC,UAAD;MAAmC;MAAoB;MAAU,MAAM,KAAK;MAAQ,EAArE,SAAqE;MAC3F;IAEH,WAAW,WAAW,SAAS,mBAC9B,oBAAC,eAAD;KAAe,MAAM,KAAK;KAAM,UAAU,KAAK;KAAc,iBAAiB;KAAa,WAAW,MAAM;KAAY;KAAe;KAAW,CAAA,GAElJ,oBAAC,MAAD;KAAM,MAAM,KAAK;KAAM,UAAU,KAAK;KAAc,iBAAiB;KAAmB;KAAe;KAAW,CAAA;IAE/G;;;CAGZ,CAAC"}
|
package/dist/generators.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_generators = require("./generators-
|
|
2
|
+
const require_generators = require("./generators-C5AvweCJ.cjs");
|
|
3
3
|
exports.handlersGenerator = require_generators.handlersGenerator;
|
|
4
4
|
exports.mswGenerator = require_generators.mswGenerator;
|
package/dist/generators.d.ts
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { n as PluginMsw } from "./types-
|
|
2
|
+
import { n as PluginMsw } from "./types-CLAiv8qc.js";
|
|
3
3
|
import * as _$_kubb_core0 from "@kubb/core";
|
|
4
4
|
|
|
5
5
|
//#region src/generators/handlersGenerator.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Aggregate generator enabled by `pluginMsw({ handlers: true })`. Emits a
|
|
8
|
+
* `handlers.ts` file that re-exports every generated handler grouped by HTTP
|
|
9
|
+
* method, ready to spread into `setupServer(...handlers)` or
|
|
10
|
+
* `setupWorker(...handlers)`.
|
|
11
|
+
*/
|
|
6
12
|
declare const handlersGenerator: _$_kubb_core0.Generator<PluginMsw, unknown>;
|
|
7
13
|
//#endregion
|
|
8
14
|
//#region src/generators/mswGenerator.d.ts
|
|
15
|
+
/**
|
|
16
|
+
* Built-in operation generator for `@kubb/plugin-msw`. Emits one MSW handler
|
|
17
|
+
* per OpenAPI operation. With `parser: 'faker'` the handler returns a value
|
|
18
|
+
* from `@kubb/plugin-faker`; with `parser: 'data'` it returns a typed empty
|
|
19
|
+
* payload for tests to fill in.
|
|
20
|
+
*/
|
|
9
21
|
declare const mswGenerator: _$_kubb_core0.Generator<PluginMsw, unknown>;
|
|
10
22
|
//#endregion
|
|
11
23
|
export { handlersGenerator, mswGenerator };
|
package/dist/generators.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as handlersGenerator, t as mswGenerator } from "./generators-
|
|
1
|
+
import { n as handlersGenerator, t as mswGenerator } from "./generators-srLe3oqm.js";
|
|
2
2
|
export { handlersGenerator, mswGenerator };
|
package/dist/index.cjs
CHANGED
|
@@ -2,18 +2,58 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
-
const require_components = require("./components-
|
|
6
|
-
const require_generators = require("./generators-
|
|
5
|
+
const require_components = require("./components-B1Dsj2WT.cjs");
|
|
6
|
+
const require_generators = require("./generators-C5AvweCJ.cjs");
|
|
7
7
|
let _kubb_core = require("@kubb/core");
|
|
8
8
|
let _kubb_plugin_faker = require("@kubb/plugin-faker");
|
|
9
9
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
10
|
+
//#region ../../internals/shared/src/group.ts
|
|
11
|
+
/**
|
|
12
|
+
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
13
|
+
* shared default naming so every plugin groups output consistently:
|
|
14
|
+
*
|
|
15
|
+
* - `path` groups use the second path segment (`/pet/findByStatus` → `pet`).
|
|
16
|
+
* - other groups use `${camelCase(group)}${suffix}` (e.g. `petController`).
|
|
17
|
+
*
|
|
18
|
+
* Returns `null` when grouping is disabled, matching the per-plugin convention.
|
|
19
|
+
*
|
|
20
|
+
* @param group - The user-supplied group option, or `undefined` to disable grouping.
|
|
21
|
+
* @param options.suffix - Appended to non-`path` group names, e.g. `'Controller'` or `'Requests'`.
|
|
22
|
+
* @param options.honorName - When `true`, a user-provided `group.name` overrides the default namer.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* createGroupConfig(group, { suffix: 'Controller' }) // plugin-ts, plugin-zod
|
|
27
|
+
* createGroupConfig(group, { suffix: 'Controller', honorName: true }) // plugin-faker, plugin-client, …
|
|
28
|
+
* createGroupConfig(group, { suffix: 'Requests', honorName: true }) // plugin-cypress, plugin-mcp
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function createGroupConfig(group, options) {
|
|
32
|
+
if (!group) return null;
|
|
33
|
+
const defaultName = (ctx) => {
|
|
34
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
35
|
+
return `${require_components.camelCase(ctx.group)}${options.suffix}`;
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
...group,
|
|
39
|
+
name: options.honorName && group.name ? group.name : defaultName
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
10
43
|
//#region src/resolvers/resolverMsw.ts
|
|
11
44
|
/**
|
|
12
|
-
*
|
|
45
|
+
* Default resolver used by `@kubb/plugin-msw`. Decides the names and file
|
|
46
|
+
* paths for every generated MSW handler. Function names get a `Handler`
|
|
47
|
+
* suffix; the aggregate export is always `handlers`.
|
|
13
48
|
*
|
|
14
|
-
*
|
|
49
|
+
* @example Resolve a handler name
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { resolverMsw } from '@kubb/plugin-msw'
|
|
52
|
+
*
|
|
53
|
+
* resolverMsw.resolveName('addPet') // 'addPetHandler'
|
|
54
|
+
* ```
|
|
15
55
|
*/
|
|
16
|
-
const resolverMsw = (0, _kubb_core.defineResolver)((
|
|
56
|
+
const resolverMsw = (0, _kubb_core.defineResolver)(() => ({
|
|
17
57
|
name: "default",
|
|
18
58
|
pluginName: "plugin-msw",
|
|
19
59
|
default(name, type) {
|
|
@@ -21,27 +61,63 @@ const resolverMsw = (0, _kubb_core.defineResolver)((_ctx) => ({
|
|
|
21
61
|
},
|
|
22
62
|
resolveName(name) {
|
|
23
63
|
return require_components.camelCase(name, { suffix: "handler" });
|
|
64
|
+
},
|
|
65
|
+
resolvePathName(name, type) {
|
|
66
|
+
return this.default(name, type);
|
|
67
|
+
},
|
|
68
|
+
resolveHandlerName(node) {
|
|
69
|
+
return this.resolveName(node.operationId);
|
|
70
|
+
},
|
|
71
|
+
resolveHandlersName() {
|
|
72
|
+
return "handlers";
|
|
24
73
|
}
|
|
25
74
|
}));
|
|
26
75
|
//#endregion
|
|
27
76
|
//#region src/plugin.ts
|
|
77
|
+
/**
|
|
78
|
+
* Canonical plugin name for `@kubb/plugin-msw`. Used for driver lookups and
|
|
79
|
+
* cross-plugin dependency references.
|
|
80
|
+
*/
|
|
28
81
|
const pluginMswName = "plugin-msw";
|
|
82
|
+
/**
|
|
83
|
+
* Generates MSW request handlers from an OpenAPI spec. Drop them into your
|
|
84
|
+
* test setup or service worker to mock the API end-to-end. Request path,
|
|
85
|
+
* method, status, and response body all stay in sync with the spec. Combine
|
|
86
|
+
* with `@kubb/plugin-faker` (via `parser: 'faker'`) to seed handlers with
|
|
87
|
+
* realistic data.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* import { defineConfig } from 'kubb'
|
|
92
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
93
|
+
* import { pluginMsw } from '@kubb/plugin-msw'
|
|
94
|
+
*
|
|
95
|
+
* export default defineConfig({
|
|
96
|
+
* input: { path: './petStore.yaml' },
|
|
97
|
+
* output: { path: './src/gen' },
|
|
98
|
+
* plugins: [
|
|
99
|
+
* pluginTs(),
|
|
100
|
+
* pluginMsw({
|
|
101
|
+
* output: { path: './handlers' },
|
|
102
|
+
* handlers: true,
|
|
103
|
+
* }),
|
|
104
|
+
* ],
|
|
105
|
+
* })
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
29
108
|
const pluginMsw = (0, _kubb_core.definePlugin)((options) => {
|
|
30
109
|
const { output = {
|
|
31
110
|
path: "handlers",
|
|
32
111
|
barrelType: "named"
|
|
33
|
-
}, group, exclude = [], include, override = [],
|
|
34
|
-
const groupConfig = group
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return `${require_components.camelCase(ctx.group)}Controller`;
|
|
39
|
-
}
|
|
40
|
-
} : void 0;
|
|
112
|
+
}, group, exclude = [], include, override = [], handlers = false, parser = "data", baseURL, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
|
|
113
|
+
const groupConfig = createGroupConfig(group, {
|
|
114
|
+
suffix: "Controller",
|
|
115
|
+
honorName: true
|
|
116
|
+
});
|
|
41
117
|
return {
|
|
42
118
|
name: pluginMswName,
|
|
43
119
|
options,
|
|
44
|
-
dependencies: [_kubb_plugin_ts.pluginTsName, parser === "faker" ? _kubb_plugin_faker.pluginFakerName :
|
|
120
|
+
dependencies: [_kubb_plugin_ts.pluginTsName, parser === "faker" ? _kubb_plugin_faker.pluginFakerName : null].filter((dependency) => Boolean(dependency)),
|
|
45
121
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
46
122
|
const resolver = userResolver ? {
|
|
47
123
|
...resolverMsw,
|
|
@@ -56,7 +132,6 @@ const pluginMsw = (0, _kubb_core.definePlugin)((options) => {
|
|
|
56
132
|
include,
|
|
57
133
|
override,
|
|
58
134
|
handlers,
|
|
59
|
-
transformers,
|
|
60
135
|
resolver
|
|
61
136
|
});
|
|
62
137
|
ctx.setResolver(resolver);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["camelCase","camelCase","pluginTsName","pluginFakerName","mswGenerator","handlersGenerator"],"sources":["../src/resolvers/resolverMsw.ts","../src/plugin.ts"],"sourcesContent":["import { camelCase } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginMsw } from '../types.ts'\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["camelCase","camelCase","pluginTsName","pluginFakerName","mswGenerator","handlersGenerator"],"sources":["../../../internals/shared/src/group.ts","../src/resolvers/resolverMsw.ts","../src/plugin.ts"],"sourcesContent":["import { camelCase } from '@internals/utils'\nimport type { Group } from '@kubb/core'\n\n/**\n * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the\n * shared default naming so every plugin groups output consistently:\n *\n * - `path` groups use the second path segment (`/pet/findByStatus` → `pet`).\n * - other groups use `${camelCase(group)}${suffix}` (e.g. `petController`).\n *\n * Returns `null` when grouping is disabled, matching the per-plugin convention.\n *\n * @param group - The user-supplied group option, or `undefined` to disable grouping.\n * @param options.suffix - Appended to non-`path` group names, e.g. `'Controller'` or `'Requests'`.\n * @param options.honorName - When `true`, a user-provided `group.name` overrides the default namer.\n *\n * @example\n * ```ts\n * createGroupConfig(group, { suffix: 'Controller' }) // plugin-ts, plugin-zod\n * createGroupConfig(group, { suffix: 'Controller', honorName: true }) // plugin-faker, plugin-client, …\n * createGroupConfig(group, { suffix: 'Requests', honorName: true }) // plugin-cypress, plugin-mcp\n * ```\n */\nexport function createGroupConfig(group: Group | undefined, options: { suffix: string; honorName?: boolean }): Group | null {\n if (!group) {\n return null\n }\n\n const defaultName = (ctx: { group: string }): string => {\n if (group.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n\n return `${camelCase(ctx.group)}${options.suffix}`\n }\n\n return {\n ...group,\n name: options.honorName && group.name ? group.name : defaultName,\n } satisfies Group\n}\n","import { camelCase } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginMsw } from '../types.ts'\n\n/**\n * Default resolver used by `@kubb/plugin-msw`. Decides the names and file\n * paths for every generated MSW handler. Function names get a `Handler`\n * suffix; the aggregate export is always `handlers`.\n *\n * @example Resolve a handler name\n * ```ts\n * import { resolverMsw } from '@kubb/plugin-msw'\n *\n * resolverMsw.resolveName('addPet') // 'addPetHandler'\n * ```\n */\nexport const resolverMsw = defineResolver<PluginMsw>(() => ({\n name: 'default',\n pluginName: 'plugin-msw',\n default(name, type) {\n return camelCase(name, { isFile: type === 'file' })\n },\n resolveName(name) {\n return camelCase(name, { suffix: 'handler' })\n },\n resolvePathName(name, type) {\n return this.default(name, type)\n },\n resolveHandlerName(node) {\n return this.resolveName(node.operationId)\n },\n resolveHandlersName() {\n return 'handlers'\n },\n}))\n","import { createGroupConfig } from '@internals/shared'\nimport { definePlugin } from '@kubb/core'\nimport { pluginFakerName } from '@kubb/plugin-faker'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { handlersGenerator, mswGenerator } from './generators'\nimport { resolverMsw } from './resolvers/resolverMsw.ts'\nimport type { PluginMsw } from './types.ts'\n\n/**\n * Canonical plugin name for `@kubb/plugin-msw`. Used for driver lookups and\n * cross-plugin dependency references.\n */\nexport const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']\n\n/**\n * Generates MSW request handlers from an OpenAPI spec. Drop them into your\n * test setup or service worker to mock the API end-to-end. Request path,\n * method, status, and response body all stay in sync with the spec. Combine\n * with `@kubb/plugin-faker` (via `parser: 'faker'`) to seed handlers with\n * realistic data.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { pluginTs } from '@kubb/plugin-ts'\n * import { pluginMsw } from '@kubb/plugin-msw'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * plugins: [\n * pluginTs(),\n * pluginMsw({\n * output: { path: './handlers' },\n * handlers: true,\n * }),\n * ],\n * })\n * ```\n */\nexport const pluginMsw = definePlugin<PluginMsw>((options) => {\n const {\n output = { path: 'handlers', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n handlers = false,\n parser = 'data',\n baseURL,\n resolver: userResolver,\n transformer: userTransformer,\n generators: userGenerators = [],\n } = options\n\n const groupConfig = createGroupConfig(group, { suffix: 'Controller', honorName: true })\n\n return {\n name: pluginMswName,\n options,\n dependencies: [pluginTsName, parser === 'faker' ? pluginFakerName : null].filter((dependency): dependency is string => Boolean(dependency)),\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n const resolver = userResolver ? { ...resolverMsw, ...userResolver } : resolverMsw\n\n ctx.setOptions({\n output,\n parser,\n baseURL,\n group: groupConfig,\n exclude,\n include,\n override,\n handlers,\n resolver,\n })\n ctx.setResolver(resolver)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n\n ctx.addGenerator(mswGenerator)\n if (handlers) {\n ctx.addGenerator(handlersGenerator)\n }\n for (const gen of userGenerators) {\n ctx.addGenerator(gen)\n }\n },\n },\n }\n})\n\nexport default pluginMsw\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;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I,OAAO,EACL,oBAAoB,KAAK;GACvB,MAAM,WAAW,eAAe;IAAE,GAAG;IAAa,GAAG;IAAc,GAAG;GAEtE,IAAI,WAAW;IACb;IACA;IACA;IACA,OAAO;IACP;IACA;IACA;IACA;IACA;IACD,CAAC;GACF,IAAI,YAAY,SAAS;GACzB,IAAI,iBACF,IAAI,eAAe,gBAAgB;GAGrC,IAAI,aAAaC,mBAAAA,aAAa;GAC9B,IAAI,UACF,IAAI,aAAaC,mBAAAA,kBAAkB;GAErC,KAAK,MAAM,OAAO,gBAChB,IAAI,aAAa,IAAI;KAG1B;EACF;EACD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,39 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { n as PluginMsw, t as Options } from "./types-
|
|
2
|
+
import { n as PluginMsw, t as Options } from "./types-CLAiv8qc.js";
|
|
3
3
|
import * as _$_kubb_core0 from "@kubb/core";
|
|
4
4
|
|
|
5
5
|
//#region src/plugin.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Canonical plugin name for `@kubb/plugin-msw`. Used for driver lookups and
|
|
8
|
+
* cross-plugin dependency references.
|
|
9
|
+
*/
|
|
6
10
|
declare const pluginMswName = "plugin-msw";
|
|
11
|
+
/**
|
|
12
|
+
* Generates MSW request handlers from an OpenAPI spec. Drop them into your
|
|
13
|
+
* test setup or service worker to mock the API end-to-end. Request path,
|
|
14
|
+
* method, status, and response body all stay in sync with the spec. Combine
|
|
15
|
+
* with `@kubb/plugin-faker` (via `parser: 'faker'`) to seed handlers with
|
|
16
|
+
* realistic data.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { defineConfig } from 'kubb'
|
|
21
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
22
|
+
* import { pluginMsw } from '@kubb/plugin-msw'
|
|
23
|
+
*
|
|
24
|
+
* export default defineConfig({
|
|
25
|
+
* input: { path: './petStore.yaml' },
|
|
26
|
+
* output: { path: './src/gen' },
|
|
27
|
+
* plugins: [
|
|
28
|
+
* pluginTs(),
|
|
29
|
+
* pluginMsw({
|
|
30
|
+
* output: { path: './handlers' },
|
|
31
|
+
* handlers: true,
|
|
32
|
+
* }),
|
|
33
|
+
* ],
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
7
37
|
declare const pluginMsw: (options?: Options | undefined) => _$_kubb_core0.Plugin<PluginMsw>;
|
|
8
38
|
//#endregion
|
|
9
39
|
export { type PluginMsw, pluginMsw as default, pluginMsw, pluginMswName };
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,56 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import {
|
|
3
|
-
import { n as handlersGenerator, t as mswGenerator } from "./generators-
|
|
2
|
+
import { c as camelCase } from "./components-BxzfyX2u.js";
|
|
3
|
+
import { n as handlersGenerator, t as mswGenerator } from "./generators-srLe3oqm.js";
|
|
4
4
|
import { definePlugin, defineResolver } from "@kubb/core";
|
|
5
5
|
import { pluginFakerName } from "@kubb/plugin-faker";
|
|
6
6
|
import { pluginTsName } from "@kubb/plugin-ts";
|
|
7
|
+
//#region ../../internals/shared/src/group.ts
|
|
8
|
+
/**
|
|
9
|
+
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
10
|
+
* shared default naming so every plugin groups output consistently:
|
|
11
|
+
*
|
|
12
|
+
* - `path` groups use the second path segment (`/pet/findByStatus` → `pet`).
|
|
13
|
+
* - other groups use `${camelCase(group)}${suffix}` (e.g. `petController`).
|
|
14
|
+
*
|
|
15
|
+
* Returns `null` when grouping is disabled, matching the per-plugin convention.
|
|
16
|
+
*
|
|
17
|
+
* @param group - The user-supplied group option, or `undefined` to disable grouping.
|
|
18
|
+
* @param options.suffix - Appended to non-`path` group names, e.g. `'Controller'` or `'Requests'`.
|
|
19
|
+
* @param options.honorName - When `true`, a user-provided `group.name` overrides the default namer.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* createGroupConfig(group, { suffix: 'Controller' }) // plugin-ts, plugin-zod
|
|
24
|
+
* createGroupConfig(group, { suffix: 'Controller', honorName: true }) // plugin-faker, plugin-client, …
|
|
25
|
+
* createGroupConfig(group, { suffix: 'Requests', honorName: true }) // plugin-cypress, plugin-mcp
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
function createGroupConfig(group, options) {
|
|
29
|
+
if (!group) return null;
|
|
30
|
+
const defaultName = (ctx) => {
|
|
31
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
32
|
+
return `${camelCase(ctx.group)}${options.suffix}`;
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
...group,
|
|
36
|
+
name: options.honorName && group.name ? group.name : defaultName
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
7
40
|
//#region src/resolvers/resolverMsw.ts
|
|
8
41
|
/**
|
|
9
|
-
*
|
|
42
|
+
* Default resolver used by `@kubb/plugin-msw`. Decides the names and file
|
|
43
|
+
* paths for every generated MSW handler. Function names get a `Handler`
|
|
44
|
+
* suffix; the aggregate export is always `handlers`.
|
|
10
45
|
*
|
|
11
|
-
*
|
|
46
|
+
* @example Resolve a handler name
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { resolverMsw } from '@kubb/plugin-msw'
|
|
49
|
+
*
|
|
50
|
+
* resolverMsw.resolveName('addPet') // 'addPetHandler'
|
|
51
|
+
* ```
|
|
12
52
|
*/
|
|
13
|
-
const resolverMsw = defineResolver((
|
|
53
|
+
const resolverMsw = defineResolver(() => ({
|
|
14
54
|
name: "default",
|
|
15
55
|
pluginName: "plugin-msw",
|
|
16
56
|
default(name, type) {
|
|
@@ -18,27 +58,63 @@ const resolverMsw = defineResolver((_ctx) => ({
|
|
|
18
58
|
},
|
|
19
59
|
resolveName(name) {
|
|
20
60
|
return camelCase(name, { suffix: "handler" });
|
|
61
|
+
},
|
|
62
|
+
resolvePathName(name, type) {
|
|
63
|
+
return this.default(name, type);
|
|
64
|
+
},
|
|
65
|
+
resolveHandlerName(node) {
|
|
66
|
+
return this.resolveName(node.operationId);
|
|
67
|
+
},
|
|
68
|
+
resolveHandlersName() {
|
|
69
|
+
return "handlers";
|
|
21
70
|
}
|
|
22
71
|
}));
|
|
23
72
|
//#endregion
|
|
24
73
|
//#region src/plugin.ts
|
|
74
|
+
/**
|
|
75
|
+
* Canonical plugin name for `@kubb/plugin-msw`. Used for driver lookups and
|
|
76
|
+
* cross-plugin dependency references.
|
|
77
|
+
*/
|
|
25
78
|
const pluginMswName = "plugin-msw";
|
|
79
|
+
/**
|
|
80
|
+
* Generates MSW request handlers from an OpenAPI spec. Drop them into your
|
|
81
|
+
* test setup or service worker to mock the API end-to-end. Request path,
|
|
82
|
+
* method, status, and response body all stay in sync with the spec. Combine
|
|
83
|
+
* with `@kubb/plugin-faker` (via `parser: 'faker'`) to seed handlers with
|
|
84
|
+
* realistic data.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { defineConfig } from 'kubb'
|
|
89
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
90
|
+
* import { pluginMsw } from '@kubb/plugin-msw'
|
|
91
|
+
*
|
|
92
|
+
* export default defineConfig({
|
|
93
|
+
* input: { path: './petStore.yaml' },
|
|
94
|
+
* output: { path: './src/gen' },
|
|
95
|
+
* plugins: [
|
|
96
|
+
* pluginTs(),
|
|
97
|
+
* pluginMsw({
|
|
98
|
+
* output: { path: './handlers' },
|
|
99
|
+
* handlers: true,
|
|
100
|
+
* }),
|
|
101
|
+
* ],
|
|
102
|
+
* })
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
26
105
|
const pluginMsw = definePlugin((options) => {
|
|
27
106
|
const { output = {
|
|
28
107
|
path: "handlers",
|
|
29
108
|
barrelType: "named"
|
|
30
|
-
}, group, exclude = [], include, override = [],
|
|
31
|
-
const groupConfig = group
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return `${camelCase(ctx.group)}Controller`;
|
|
36
|
-
}
|
|
37
|
-
} : void 0;
|
|
109
|
+
}, group, exclude = [], include, override = [], handlers = false, parser = "data", baseURL, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
|
|
110
|
+
const groupConfig = createGroupConfig(group, {
|
|
111
|
+
suffix: "Controller",
|
|
112
|
+
honorName: true
|
|
113
|
+
});
|
|
38
114
|
return {
|
|
39
115
|
name: pluginMswName,
|
|
40
116
|
options,
|
|
41
|
-
dependencies: [pluginTsName, parser === "faker" ? pluginFakerName :
|
|
117
|
+
dependencies: [pluginTsName, parser === "faker" ? pluginFakerName : null].filter((dependency) => Boolean(dependency)),
|
|
42
118
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
43
119
|
const resolver = userResolver ? {
|
|
44
120
|
...resolverMsw,
|
|
@@ -53,7 +129,6 @@ const pluginMsw = definePlugin((options) => {
|
|
|
53
129
|
include,
|
|
54
130
|
override,
|
|
55
131
|
handlers,
|
|
56
|
-
transformers,
|
|
57
132
|
resolver
|
|
58
133
|
});
|
|
59
134
|
ctx.setResolver(resolver);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/resolvers/resolverMsw.ts","../src/plugin.ts"],"sourcesContent":["import { camelCase } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginMsw } from '../types.ts'\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../internals/shared/src/group.ts","../src/resolvers/resolverMsw.ts","../src/plugin.ts"],"sourcesContent":["import { camelCase } from '@internals/utils'\nimport type { Group } from '@kubb/core'\n\n/**\n * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the\n * shared default naming so every plugin groups output consistently:\n *\n * - `path` groups use the second path segment (`/pet/findByStatus` → `pet`).\n * - other groups use `${camelCase(group)}${suffix}` (e.g. `petController`).\n *\n * Returns `null` when grouping is disabled, matching the per-plugin convention.\n *\n * @param group - The user-supplied group option, or `undefined` to disable grouping.\n * @param options.suffix - Appended to non-`path` group names, e.g. `'Controller'` or `'Requests'`.\n * @param options.honorName - When `true`, a user-provided `group.name` overrides the default namer.\n *\n * @example\n * ```ts\n * createGroupConfig(group, { suffix: 'Controller' }) // plugin-ts, plugin-zod\n * createGroupConfig(group, { suffix: 'Controller', honorName: true }) // plugin-faker, plugin-client, …\n * createGroupConfig(group, { suffix: 'Requests', honorName: true }) // plugin-cypress, plugin-mcp\n * ```\n */\nexport function createGroupConfig(group: Group | undefined, options: { suffix: string; honorName?: boolean }): Group | null {\n if (!group) {\n return null\n }\n\n const defaultName = (ctx: { group: string }): string => {\n if (group.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n\n return `${camelCase(ctx.group)}${options.suffix}`\n }\n\n return {\n ...group,\n name: options.honorName && group.name ? group.name : defaultName,\n } satisfies Group\n}\n","import { camelCase } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginMsw } from '../types.ts'\n\n/**\n * Default resolver used by `@kubb/plugin-msw`. Decides the names and file\n * paths for every generated MSW handler. Function names get a `Handler`\n * suffix; the aggregate export is always `handlers`.\n *\n * @example Resolve a handler name\n * ```ts\n * import { resolverMsw } from '@kubb/plugin-msw'\n *\n * resolverMsw.resolveName('addPet') // 'addPetHandler'\n * ```\n */\nexport const resolverMsw = defineResolver<PluginMsw>(() => ({\n name: 'default',\n pluginName: 'plugin-msw',\n default(name, type) {\n return camelCase(name, { isFile: type === 'file' })\n },\n resolveName(name) {\n return camelCase(name, { suffix: 'handler' })\n },\n resolvePathName(name, type) {\n return this.default(name, type)\n },\n resolveHandlerName(node) {\n return this.resolveName(node.operationId)\n },\n resolveHandlersName() {\n return 'handlers'\n },\n}))\n","import { createGroupConfig } from '@internals/shared'\nimport { definePlugin } from '@kubb/core'\nimport { pluginFakerName } from '@kubb/plugin-faker'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { handlersGenerator, mswGenerator } from './generators'\nimport { resolverMsw } from './resolvers/resolverMsw.ts'\nimport type { PluginMsw } from './types.ts'\n\n/**\n * Canonical plugin name for `@kubb/plugin-msw`. Used for driver lookups and\n * cross-plugin dependency references.\n */\nexport const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']\n\n/**\n * Generates MSW request handlers from an OpenAPI spec. Drop them into your\n * test setup or service worker to mock the API end-to-end. Request path,\n * method, status, and response body all stay in sync with the spec. Combine\n * with `@kubb/plugin-faker` (via `parser: 'faker'`) to seed handlers with\n * realistic data.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { pluginTs } from '@kubb/plugin-ts'\n * import { pluginMsw } from '@kubb/plugin-msw'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * plugins: [\n * pluginTs(),\n * pluginMsw({\n * output: { path: './handlers' },\n * handlers: true,\n * }),\n * ],\n * })\n * ```\n */\nexport const pluginMsw = definePlugin<PluginMsw>((options) => {\n const {\n output = { path: 'handlers', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n handlers = false,\n parser = 'data',\n baseURL,\n resolver: userResolver,\n transformer: userTransformer,\n generators: userGenerators = [],\n } = options\n\n const groupConfig = createGroupConfig(group, { suffix: 'Controller', honorName: true })\n\n return {\n name: pluginMswName,\n options,\n dependencies: [pluginTsName, parser === 'faker' ? pluginFakerName : null].filter((dependency): dependency is string => Boolean(dependency)),\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n const resolver = userResolver ? { ...resolverMsw, ...userResolver } : resolverMsw\n\n ctx.setOptions({\n output,\n parser,\n baseURL,\n group: groupConfig,\n exclude,\n include,\n override,\n handlers,\n resolver,\n })\n ctx.setResolver(resolver)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n\n ctx.addGenerator(mswGenerator)\n if (handl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