@kubb/plugin-msw 5.0.0-alpha.8 → 5.0.0-beta.10
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 +26 -7
- package/dist/components-BYvgvrY7.cjs +569 -0
- package/dist/components-BYvgvrY7.cjs.map +1 -0
- package/dist/components-Cm17DMTE.js +510 -0
- package/dist/components-Cm17DMTE.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +17 -21
- package/dist/components.js +1 -1
- package/dist/generators-D9gvdP7Z.js +177 -0
- package/dist/generators-D9gvdP7Z.js.map +1 -0
- package/dist/generators-rZ99WaWQ.cjs +187 -0
- package/dist/generators-rZ99WaWQ.cjs.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +4 -471
- package/dist/generators.js +1 -1
- package/dist/index.cjs +62 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +59 -65
- package/dist/index.js.map +1 -1
- package/dist/types-ItsHsMvC.d.ts +94 -0
- package/extension.yaml +233 -0
- package/package.json +57 -58
- package/src/components/Handlers.tsx +3 -3
- package/src/components/Mock.tsx +37 -28
- package/src/components/MockWithFaker.tsx +37 -24
- package/src/components/Response.tsx +23 -17
- package/src/generators/handlersGenerator.tsx +19 -19
- package/src/generators/mswGenerator.tsx +50 -60
- package/src/index.ts +1 -1
- package/src/plugin.ts +47 -86
- package/src/resolvers/resolverMsw.ts +28 -0
- package/src/types.ts +55 -27
- package/src/utils.ts +58 -0
- package/dist/components-8XBwMbFa.cjs +0 -343
- package/dist/components-8XBwMbFa.cjs.map +0 -1
- package/dist/components-DgtTZkWX.js +0 -277
- package/dist/components-DgtTZkWX.js.map +0 -1
- package/dist/generators-CY1SNd5X.cjs +0 -171
- package/dist/generators-CY1SNd5X.cjs.map +0 -1
- package/dist/generators-CvyZTxOm.js +0 -161
- package/dist/generators-CvyZTxOm.js.map +0 -1
- package/dist/types-MdHRNpgi.d.ts +0 -68
package/dist/index.js
CHANGED
|
@@ -1,84 +1,78 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import {
|
|
3
|
-
import { n as handlersGenerator, t as mswGenerator } from "./generators-
|
|
4
|
-
import
|
|
5
|
-
import { createPlugin, getBarrelFiles, getMode } from "@kubb/core";
|
|
2
|
+
import { c as camelCase } from "./components-Cm17DMTE.js";
|
|
3
|
+
import { n as handlersGenerator, t as mswGenerator } from "./generators-D9gvdP7Z.js";
|
|
4
|
+
import { definePlugin, defineResolver } from "@kubb/core";
|
|
6
5
|
import { pluginFakerName } from "@kubb/plugin-faker";
|
|
7
|
-
import { OperationGenerator, pluginOasName } from "@kubb/plugin-oas";
|
|
8
6
|
import { pluginTsName } from "@kubb/plugin-ts";
|
|
7
|
+
//#region src/resolvers/resolverMsw.ts
|
|
8
|
+
/**
|
|
9
|
+
* Naming convention resolver for MSW plugin.
|
|
10
|
+
*
|
|
11
|
+
* Provides default naming helpers using camelCase with a `handler` suffix.
|
|
12
|
+
*/
|
|
13
|
+
const resolverMsw = defineResolver(() => ({
|
|
14
|
+
name: "default",
|
|
15
|
+
pluginName: "plugin-msw",
|
|
16
|
+
default(name, type) {
|
|
17
|
+
return camelCase(name, { isFile: type === "file" });
|
|
18
|
+
},
|
|
19
|
+
resolveName(name) {
|
|
20
|
+
return camelCase(name, { suffix: "handler" });
|
|
21
|
+
},
|
|
22
|
+
resolvePathName(name, type) {
|
|
23
|
+
return this.default(name, type);
|
|
24
|
+
},
|
|
25
|
+
resolveHandlerName(node) {
|
|
26
|
+
return this.resolveName(node.operationId);
|
|
27
|
+
},
|
|
28
|
+
resolveHandlersName() {
|
|
29
|
+
return "handlers";
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
//#endregion
|
|
9
33
|
//#region src/plugin.ts
|
|
10
34
|
const pluginMswName = "plugin-msw";
|
|
11
|
-
const pluginMsw =
|
|
35
|
+
const pluginMsw = definePlugin((options) => {
|
|
12
36
|
const { output = {
|
|
13
37
|
path: "handlers",
|
|
14
38
|
barrelType: "named"
|
|
15
|
-
}, group, exclude = [], include, override = [],
|
|
39
|
+
}, group, exclude = [], include, override = [], handlers = false, parser = "data", baseURL, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
|
|
40
|
+
const groupConfig = group ? {
|
|
41
|
+
...group,
|
|
42
|
+
name: group.name ? group.name : (ctx) => {
|
|
43
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
44
|
+
return `${camelCase(ctx.group)}Controller`;
|
|
45
|
+
}
|
|
46
|
+
} : void 0;
|
|
16
47
|
return {
|
|
17
48
|
name: pluginMswName,
|
|
18
|
-
options
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
|
31
|
-
if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single")
|
|
32
|
-
/**
|
|
33
|
-
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
34
|
-
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
35
|
-
*/
|
|
36
|
-
return path.resolve(root, output.path);
|
|
37
|
-
if (group && (options?.group?.path || options?.group?.tag)) {
|
|
38
|
-
const groupName = group?.name ? group.name : (ctx) => {
|
|
39
|
-
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
40
|
-
return `${camelCase(ctx.group)}Controller`;
|
|
41
|
-
};
|
|
42
|
-
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
|
|
43
|
-
}
|
|
44
|
-
return path.resolve(root, output.path, baseName);
|
|
45
|
-
},
|
|
46
|
-
resolveName(name, type) {
|
|
47
|
-
const resolvedName = camelCase(name, {
|
|
48
|
-
suffix: type ? "handler" : void 0,
|
|
49
|
-
isFile: type === "file"
|
|
50
|
-
});
|
|
51
|
-
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
52
|
-
return resolvedName;
|
|
53
|
-
},
|
|
54
|
-
async install() {
|
|
55
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
|
56
|
-
const mode = getMode(path.resolve(root, output.path));
|
|
57
|
-
const oas = await this.getOas();
|
|
58
|
-
const files = await new OperationGenerator(this.plugin.options, {
|
|
59
|
-
fabric: this.fabric,
|
|
60
|
-
oas,
|
|
61
|
-
driver: this.driver,
|
|
62
|
-
events: this.events,
|
|
63
|
-
plugin: this.plugin,
|
|
64
|
-
contentType,
|
|
49
|
+
options,
|
|
50
|
+
dependencies: [pluginTsName, parser === "faker" ? pluginFakerName : void 0].filter((dependency) => Boolean(dependency)),
|
|
51
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
52
|
+
const resolver = userResolver ? {
|
|
53
|
+
...resolverMsw,
|
|
54
|
+
...userResolver
|
|
55
|
+
} : resolverMsw;
|
|
56
|
+
ctx.setOptions({
|
|
57
|
+
output,
|
|
58
|
+
parser,
|
|
59
|
+
baseURL,
|
|
60
|
+
group: groupConfig,
|
|
65
61
|
exclude,
|
|
66
62
|
include,
|
|
67
63
|
override,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
await this.upsertFile(...files);
|
|
71
|
-
const barrelFiles = await getBarrelFiles(this.fabric.files, {
|
|
72
|
-
type: output.barrelType ?? "named",
|
|
73
|
-
root,
|
|
74
|
-
output,
|
|
75
|
-
meta: { pluginName: this.plugin.name }
|
|
64
|
+
handlers,
|
|
65
|
+
resolver
|
|
76
66
|
});
|
|
77
|
-
|
|
78
|
-
|
|
67
|
+
ctx.setResolver(resolver);
|
|
68
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
69
|
+
ctx.addGenerator(mswGenerator);
|
|
70
|
+
if (handlers) ctx.addGenerator(handlersGenerator);
|
|
71
|
+
for (const gen of userGenerators) ctx.addGenerator(gen);
|
|
72
|
+
} }
|
|
79
73
|
};
|
|
80
74
|
});
|
|
81
75
|
//#endregion
|
|
82
|
-
export { pluginMsw, pluginMswName };
|
|
76
|
+
export { pluginMsw as default, pluginMsw, pluginMswName };
|
|
83
77
|
|
|
84
78
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["import
|
|
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 * Naming convention resolver for MSW plugin.\n *\n * Provides default naming helpers using camelCase with a `handler` suffix.\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 { camelCase } from '@internals/utils'\nimport { definePlugin, type Group } 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\nexport const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']\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 = group\n ? ({\n ...group,\n name: group.name\n ? group.name\n : (ctx: { group: string }) => {\n if (group.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n },\n } satisfies Group)\n : undefined\n\n return {\n name: pluginMswName,\n options,\n dependencies: [pluginTsName, parser === 'faker' ? pluginFakerName : undefined].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":";;;;;;;;;;;;AASA,MAAa,cAAc,sBAAiC;CAC1D,MAAM;CACN,YAAY;CACZ,QAAQ,MAAM,MAAM;EAClB,OAAO,UAAU,MAAM,EAAE,QAAQ,SAAS,QAAQ,CAAC;;CAErD,YAAY,MAAM;EAChB,OAAO,UAAU,MAAM,EAAE,QAAQ,WAAW,CAAC;;CAE/C,gBAAgB,MAAM,MAAM;EAC1B,OAAO,KAAK,QAAQ,MAAM,KAAK;;CAEjC,mBAAmB,MAAM;EACvB,OAAO,KAAK,YAAY,KAAK,YAAY;;CAE3C,sBAAsB;EACpB,OAAO;;CAEV,EAAE;;;ACnBH,MAAa,gBAAgB;AAE7B,MAAa,YAAY,cAAyB,YAAY;CAC5D,MAAM,EACJ,SAAS;EAAE,MAAM;EAAY,YAAY;EAAS,EAClD,OACA,UAAU,EAAE,EACZ,SACA,WAAW,EAAE,EACb,WAAW,OACX,SAAS,QACT,SACA,UAAU,cACV,aAAa,iBACb,YAAY,iBAAiB,EAAE,KAC7B;CAEJ,MAAM,cAAc,QACf;EACC,GAAG;EACH,MAAM,MAAM,OACR,MAAM,QACL,QAA2B;GAC1B,IAAI,MAAM,SAAS,QACjB,OAAO,GAAG,IAAI,MAAM,MAAM,IAAI,CAAC;GAEjC,OAAO,GAAG,UAAU,IAAI,MAAM,CAAC;;EAEtC,GACD,KAAA;CAEJ,OAAO;EACL,MAAM;EACN;EACA,cAAc,CAAC,cAAc,WAAW,UAAU,kBAAkB,KAAA,EAAU,CAAC,QAAQ,eAAqC,QAAQ,WAAW,CAAC;EAChJ,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,aAAa,aAAa;GAC9B,IAAI,UACF,IAAI,aAAa,kBAAkB;GAErC,KAAK,MAAM,OAAO,gBAChB,IAAI,aAAa,IAAI;KAG1B;EACF;EACD"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
+
import { Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
|
|
3
|
+
|
|
4
|
+
//#region src/types.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Resolver for MSW that provides naming methods for handler functions.
|
|
7
|
+
*/
|
|
8
|
+
type ResolverMsw = Resolver & {
|
|
9
|
+
/**
|
|
10
|
+
* Resolves the base handler function name for an operation.
|
|
11
|
+
*/
|
|
12
|
+
resolveName(this: ResolverMsw, name: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Resolves the output file name for an MSW handler module.
|
|
15
|
+
*/
|
|
16
|
+
resolvePathName(this: ResolverMsw, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
|
|
17
|
+
/**
|
|
18
|
+
* Resolves the handler function name for an operation.
|
|
19
|
+
*/
|
|
20
|
+
resolveHandlerName(this: ResolverMsw, node: ast.OperationNode): string;
|
|
21
|
+
/**
|
|
22
|
+
* Resolves the exported handlers collection name.
|
|
23
|
+
*/
|
|
24
|
+
resolveHandlersName(this: ResolverMsw): string;
|
|
25
|
+
};
|
|
26
|
+
type Options = {
|
|
27
|
+
/**
|
|
28
|
+
* Specify the export location for the files and define the behavior of the output
|
|
29
|
+
* @default { path: 'handlers', barrelType: 'named' }
|
|
30
|
+
*/
|
|
31
|
+
output?: Output;
|
|
32
|
+
baseURL?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Group the MSW mocks based on the provided name.
|
|
35
|
+
*/
|
|
36
|
+
group?: Group;
|
|
37
|
+
/**
|
|
38
|
+
* Tags, operations, or paths to exclude from generation.
|
|
39
|
+
*/
|
|
40
|
+
exclude?: Array<Exclude>;
|
|
41
|
+
/**
|
|
42
|
+
* Tags, operations, or paths to include in generation.
|
|
43
|
+
*/
|
|
44
|
+
include?: Array<Include>;
|
|
45
|
+
/**
|
|
46
|
+
* Override options for specific tags, operations, or paths.
|
|
47
|
+
*/
|
|
48
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
49
|
+
/**
|
|
50
|
+
* Override naming conventions for function names and types.
|
|
51
|
+
*/
|
|
52
|
+
resolver?: Partial<ResolverMsw> & ThisType<ResolverMsw>;
|
|
53
|
+
/**
|
|
54
|
+
* AST visitor to transform generated nodes.
|
|
55
|
+
*/
|
|
56
|
+
transformer?: ast.Visitor;
|
|
57
|
+
/**
|
|
58
|
+
* Create `handlers.ts` file with all handlers grouped by methods.
|
|
59
|
+
* @default false
|
|
60
|
+
*/
|
|
61
|
+
handlers?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Which parser to use for generating response data.
|
|
64
|
+
*
|
|
65
|
+
* @default 'data'
|
|
66
|
+
*/
|
|
67
|
+
parser?: 'data' | 'faker';
|
|
68
|
+
/**
|
|
69
|
+
* Additional generators alongside the default generators.
|
|
70
|
+
*/
|
|
71
|
+
generators?: Array<Generator<PluginMsw>>;
|
|
72
|
+
};
|
|
73
|
+
type ResolvedOptions = {
|
|
74
|
+
output: Output;
|
|
75
|
+
group: Group | undefined;
|
|
76
|
+
exclude: NonNullable<Options['exclude']>;
|
|
77
|
+
include: Options['include'];
|
|
78
|
+
override: NonNullable<Options['override']>;
|
|
79
|
+
parser: NonNullable<Options['parser']>;
|
|
80
|
+
baseURL: Options['baseURL'] | undefined;
|
|
81
|
+
handlers: boolean;
|
|
82
|
+
resolver: ResolverMsw;
|
|
83
|
+
};
|
|
84
|
+
type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, ResolverMsw>;
|
|
85
|
+
declare global {
|
|
86
|
+
namespace Kubb {
|
|
87
|
+
interface PluginRegistry {
|
|
88
|
+
'plugin-msw': PluginMsw;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
export { PluginMsw as n, Options as t };
|
|
94
|
+
//# sourceMappingURL=types-ItsHsMvC.d.ts.map
|
package/extension.yaml
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
$schema: https://kubb.dev/schemas/extension.json
|
|
2
|
+
kind: plugin
|
|
3
|
+
id: plugin-msw
|
|
4
|
+
name: MSW
|
|
5
|
+
description: Generate Mock Service Worker (MSW) handlers from OpenAPI specifications.
|
|
6
|
+
category: mocks
|
|
7
|
+
type: official
|
|
8
|
+
npmPackage: '@kubb/plugin-msw'
|
|
9
|
+
docsPath: /plugins/plugin-msw
|
|
10
|
+
repo: https://github.com/kubb-labs/plugins
|
|
11
|
+
maintainers:
|
|
12
|
+
- name: Stijn Van Hulle
|
|
13
|
+
github: stijnvanhulle
|
|
14
|
+
compatibility:
|
|
15
|
+
kubb: '>=5.0.0'
|
|
16
|
+
node: '>=22'
|
|
17
|
+
tags:
|
|
18
|
+
- msw
|
|
19
|
+
- mock-service-worker
|
|
20
|
+
- api-mocking
|
|
21
|
+
- mocks
|
|
22
|
+
- testing
|
|
23
|
+
- codegen
|
|
24
|
+
- openapi
|
|
25
|
+
dependencies:
|
|
26
|
+
- plugin-ts
|
|
27
|
+
- plugin-faker
|
|
28
|
+
resources:
|
|
29
|
+
documentation: https://kubb.dev/plugins/plugin-msw
|
|
30
|
+
repository: https://github.com/kubb-labs/plugins
|
|
31
|
+
issues: https://github.com/kubb-labs/plugins/issues
|
|
32
|
+
changelog: https://github.com/kubb-labs/plugins/blob/main/packages/plugin-msw/CHANGELOG.md
|
|
33
|
+
codesandbox: https://codesandbox.io/p/github/kubb-labs/plugins/main/examples/msw
|
|
34
|
+
featured: false
|
|
35
|
+
icon:
|
|
36
|
+
light: https://kubb.dev/feature/msw.svg
|
|
37
|
+
intro: |
|
|
38
|
+
# @kubb/plugin-msw
|
|
39
|
+
|
|
40
|
+
Generate [MSW](https://mswjs.io/) API mock handlers from your OpenAPI schema. Mock API requests in development and tests with type-safe handlers.
|
|
41
|
+
options:
|
|
42
|
+
- name: output
|
|
43
|
+
type: Output
|
|
44
|
+
required: false
|
|
45
|
+
default: "{ path: 'handlers', barrel: { type: 'named' } }"
|
|
46
|
+
description: Specify the export location for the files and define the behavior of the output.
|
|
47
|
+
properties:
|
|
48
|
+
- name: path
|
|
49
|
+
type: string
|
|
50
|
+
required: true
|
|
51
|
+
description: Output directory or file for the generated code, relative to the global `output.path`.
|
|
52
|
+
tip: |
|
|
53
|
+
if `output.path` is a file, `group` cannot be used.
|
|
54
|
+
default: "'handlers'"
|
|
55
|
+
- name: barrel
|
|
56
|
+
type: "{ type: 'named' | 'all', nested?: boolean } | false"
|
|
57
|
+
required: false
|
|
58
|
+
default: "{ type: 'named' }"
|
|
59
|
+
description: "Configure barrel file export strategy. Use `type` to control named vs. wildcard exports; set `nested: true` to generate hierarchical barrels in subdirectories."
|
|
60
|
+
examples:
|
|
61
|
+
- name: all
|
|
62
|
+
files:
|
|
63
|
+
- lang: typescript
|
|
64
|
+
code: |
|
|
65
|
+
export * from './gen/petService.ts'
|
|
66
|
+
twoslash: false
|
|
67
|
+
- name: named
|
|
68
|
+
files:
|
|
69
|
+
- lang: typescript
|
|
70
|
+
code: |
|
|
71
|
+
export { PetService } from './gen/petService.ts'
|
|
72
|
+
twoslash: false
|
|
73
|
+
- name: 'false'
|
|
74
|
+
files:
|
|
75
|
+
- lang: typescript
|
|
76
|
+
code: ''
|
|
77
|
+
twoslash: false
|
|
78
|
+
- name: banner
|
|
79
|
+
type: 'string | ((node: RootNode) => string)'
|
|
80
|
+
required: false
|
|
81
|
+
description: Add a banner comment at the top of every generated file. Accepts a static string or a function that receives the `RootNode` and returns a string.
|
|
82
|
+
- name: footer
|
|
83
|
+
type: 'string | ((node: RootNode) => string)'
|
|
84
|
+
required: false
|
|
85
|
+
description: Add a footer comment at the end of every generated file. Accepts a static string or a function that receives the `RootNode` and returns a string.
|
|
86
|
+
- name: override
|
|
87
|
+
type: boolean
|
|
88
|
+
required: false
|
|
89
|
+
default: 'false'
|
|
90
|
+
description: Whether Kubb overrides existing external files that can be generated if they already exist.
|
|
91
|
+
- name: handlers
|
|
92
|
+
type: boolean
|
|
93
|
+
required: false
|
|
94
|
+
default: 'false'
|
|
95
|
+
description: Create a `handlers.ts` file with all handlers grouped by methods.
|
|
96
|
+
- name: baseURL
|
|
97
|
+
type: string
|
|
98
|
+
required: false
|
|
99
|
+
description: Sets a custom base URL for all generated calls. When not set, the base URL is automatically taken from the OAS spec via the adapter (e.g. the `servers[0].url` field).
|
|
100
|
+
- name: group
|
|
101
|
+
type: Group
|
|
102
|
+
required: false
|
|
103
|
+
description: |
|
|
104
|
+
Grouping combines files in a folder based on a specific `type`.
|
|
105
|
+
examples:
|
|
106
|
+
- name: kubb.config.ts
|
|
107
|
+
files:
|
|
108
|
+
- lang: typescript
|
|
109
|
+
code: |
|
|
110
|
+
group: {
|
|
111
|
+
type: 'tag',
|
|
112
|
+
name({ group }) {
|
|
113
|
+
return `${group}Controller`
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
twoslash: false
|
|
117
|
+
body: |
|
|
118
|
+
With the configuration above, the generator emits:
|
|
119
|
+
|
|
120
|
+
```text
|
|
121
|
+
.
|
|
122
|
+
├── src/
|
|
123
|
+
│ └── petController/
|
|
124
|
+
│ │ ├── addPet.ts
|
|
125
|
+
│ │ └── getPet.ts
|
|
126
|
+
│ └── storeController/
|
|
127
|
+
│ ├── createStore.ts
|
|
128
|
+
│ └── getStoreById.ts
|
|
129
|
+
├── petStore.yaml
|
|
130
|
+
├── kubb.config.ts
|
|
131
|
+
└── package.json
|
|
132
|
+
```
|
|
133
|
+
properties:
|
|
134
|
+
- name: type
|
|
135
|
+
type: "'tag'"
|
|
136
|
+
required: true
|
|
137
|
+
description: Specify the property to group files by. Required when `group` is defined.
|
|
138
|
+
note: |
|
|
139
|
+
`Required: true*` means this is required only when the `group` option is used. The `group` option itself is optional.
|
|
140
|
+
body: |
|
|
141
|
+
- `'tag'`: Uses the first tag from `operation.getTags().at(0)?.name`
|
|
142
|
+
- name: name
|
|
143
|
+
type: '(context: GroupContext) => string'
|
|
144
|
+
required: false
|
|
145
|
+
default: (ctx) => `${ctx.group}Controller`
|
|
146
|
+
description: Return the name of a group based on the group name. This is used for file and identifier generation.
|
|
147
|
+
- name: parser
|
|
148
|
+
type: "'data' | 'faker'"
|
|
149
|
+
required: false
|
|
150
|
+
default: "'data'"
|
|
151
|
+
description: Choose which parser to use when generating response data.
|
|
152
|
+
body: |
|
|
153
|
+
- `'faker'` will use `@kubb/plugin-faker` to generate the data for the response.
|
|
154
|
+
- `'data'` will use your custom data to generate the data for the response.
|
|
155
|
+
- name: include
|
|
156
|
+
type: Array<Include>
|
|
157
|
+
required: false
|
|
158
|
+
description: Array containing include parameters to include tags, operations, methods, paths, or content types.
|
|
159
|
+
codeBlock:
|
|
160
|
+
lang: typescript
|
|
161
|
+
title: Include
|
|
162
|
+
code: |
|
|
163
|
+
export type Include = {
|
|
164
|
+
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
165
|
+
pattern: string | RegExp
|
|
166
|
+
}
|
|
167
|
+
- name: exclude
|
|
168
|
+
type: Array<Exclude>
|
|
169
|
+
required: false
|
|
170
|
+
description: Array containing exclude parameters to exclude or skip tags, operations, methods, paths, or content types.
|
|
171
|
+
codeBlock:
|
|
172
|
+
lang: typescript
|
|
173
|
+
title: Exclude
|
|
174
|
+
code: |
|
|
175
|
+
export type Exclude = {
|
|
176
|
+
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
177
|
+
pattern: string | RegExp
|
|
178
|
+
}
|
|
179
|
+
- name: override
|
|
180
|
+
type: Array<Override>
|
|
181
|
+
required: false
|
|
182
|
+
description: Array containing override parameters to override `options` based on tags, operations, methods, paths, or content types.
|
|
183
|
+
codeBlock:
|
|
184
|
+
lang: typescript
|
|
185
|
+
title: Override
|
|
186
|
+
code: |
|
|
187
|
+
export type Override = {
|
|
188
|
+
type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
|
|
189
|
+
pattern: string | RegExp
|
|
190
|
+
options: PluginOptions
|
|
191
|
+
}
|
|
192
|
+
- name: generators
|
|
193
|
+
type: Array<Generator<PluginMsw>>
|
|
194
|
+
required: false
|
|
195
|
+
experimental: true
|
|
196
|
+
description: |
|
|
197
|
+
Define additional generators next to the built-in generators.
|
|
198
|
+
|
|
199
|
+
See [Generators](https://kubb.dev/docs/5.x/guides/creating-plugins) for more information on how to use generators.
|
|
200
|
+
examples:
|
|
201
|
+
- name: kubb.config.ts
|
|
202
|
+
files:
|
|
203
|
+
- lang: typescript
|
|
204
|
+
code: |
|
|
205
|
+
import { defineConfig } from 'kubb'
|
|
206
|
+
import { pluginMsw } from '@kubb/plugin-msw'
|
|
207
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
208
|
+
|
|
209
|
+
export default defineConfig({
|
|
210
|
+
input: {
|
|
211
|
+
path: './petStore.yaml',
|
|
212
|
+
},
|
|
213
|
+
output: {
|
|
214
|
+
path: './src/gen',
|
|
215
|
+
},
|
|
216
|
+
plugins: [
|
|
217
|
+
pluginTs(),
|
|
218
|
+
pluginMsw({
|
|
219
|
+
output: {
|
|
220
|
+
path: './mocks',
|
|
221
|
+
barrel: { type: 'named' },
|
|
222
|
+
banner: '/* eslint-disable no-alert, no-console */',
|
|
223
|
+
footer: '',
|
|
224
|
+
},
|
|
225
|
+
group: {
|
|
226
|
+
type: 'tag',
|
|
227
|
+
name: ({ group }) => `${group}Service`,
|
|
228
|
+
},
|
|
229
|
+
handlers: true,
|
|
230
|
+
}),
|
|
231
|
+
],
|
|
232
|
+
})
|
|
233
|
+
twoslash: false
|
package/package.json
CHANGED
|
@@ -1,49 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-msw",
|
|
3
|
-
"version": "5.0.0-
|
|
4
|
-
"description": "Mock Service Worker (MSW) handlers
|
|
3
|
+
"version": "5.0.0-beta.10",
|
|
4
|
+
"description": "Generate Mock Service Worker (MSW) request handlers from your OpenAPI specification. Intercept HTTP requests in the browser or Node.js for seamless frontend development and testing.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"msw",
|
|
7
|
-
"mock-service-worker",
|
|
8
6
|
"api-mocking",
|
|
7
|
+
"code-generation",
|
|
8
|
+
"codegen",
|
|
9
|
+
"kubb",
|
|
10
|
+
"mock-service-worker",
|
|
9
11
|
"mocking",
|
|
10
12
|
"mocks",
|
|
11
|
-
"
|
|
12
|
-
"development",
|
|
13
|
-
"interceptor",
|
|
14
|
-
"typescript",
|
|
13
|
+
"msw",
|
|
15
14
|
"openapi",
|
|
15
|
+
"service-worker",
|
|
16
16
|
"swagger",
|
|
17
|
-
"
|
|
18
|
-
"code-generator",
|
|
19
|
-
"codegen",
|
|
20
|
-
"plugins",
|
|
21
|
-
"kubb"
|
|
17
|
+
"typescript"
|
|
22
18
|
],
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "stijnvanhulle",
|
|
23
21
|
"repository": {
|
|
24
22
|
"type": "git",
|
|
25
|
-
"url": "git+https://github.com/kubb-labs/
|
|
23
|
+
"url": "git+https://github.com/kubb-labs/plugins.git",
|
|
26
24
|
"directory": "packages/plugin-msw"
|
|
27
25
|
},
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"dist",
|
|
29
|
+
"extension.yaml",
|
|
30
|
+
"!/**/**.test.**",
|
|
31
|
+
"!/**/__tests__/**",
|
|
32
|
+
"!/**/__snapshots__/**"
|
|
33
|
+
],
|
|
31
34
|
"type": "module",
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"require": "./dist/index.cjs"
|
|
36
|
-
},
|
|
37
|
-
"./components": {
|
|
38
|
-
"import": "./dist/components.js",
|
|
39
|
-
"require": "./dist/components.cjs"
|
|
40
|
-
},
|
|
41
|
-
"./generators": {
|
|
42
|
-
"import": "./dist/generators.js",
|
|
43
|
-
"require": "./dist/generators.cjs"
|
|
44
|
-
},
|
|
45
|
-
"./package.json": "./package.json"
|
|
46
|
-
},
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"main": "./dist/index.cjs",
|
|
37
|
+
"module": "./dist/index.js",
|
|
47
38
|
"types": "./dist/index.d.ts",
|
|
48
39
|
"typesVersions": {
|
|
49
40
|
"*": {
|
|
@@ -61,13 +52,38 @@
|
|
|
61
52
|
]
|
|
62
53
|
}
|
|
63
54
|
},
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"
|
|
70
|
-
|
|
55
|
+
"exports": {
|
|
56
|
+
".": {
|
|
57
|
+
"import": "./dist/index.js",
|
|
58
|
+
"require": "./dist/index.cjs"
|
|
59
|
+
},
|
|
60
|
+
"./components": {
|
|
61
|
+
"import": "./dist/components.js",
|
|
62
|
+
"require": "./dist/components.cjs"
|
|
63
|
+
},
|
|
64
|
+
"./generators": {
|
|
65
|
+
"import": "./dist/generators.js",
|
|
66
|
+
"require": "./dist/generators.cjs"
|
|
67
|
+
},
|
|
68
|
+
"./package.json": "./package.json"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public",
|
|
72
|
+
"registry": "https://registry.npmjs.org/"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"@kubb/core": "5.0.0-beta.10",
|
|
76
|
+
"@kubb/renderer-jsx": "5.0.0-beta.10",
|
|
77
|
+
"@kubb/plugin-faker": "5.0.0-beta.10",
|
|
78
|
+
"@kubb/plugin-ts": "5.0.0-beta.10"
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@internals/shared": "0.0.0",
|
|
82
|
+
"@internals/utils": "0.0.0"
|
|
83
|
+
},
|
|
84
|
+
"peerDependencies": {
|
|
85
|
+
"@kubb/renderer-jsx": "5.0.0-beta.10"
|
|
86
|
+
},
|
|
71
87
|
"size-limit": [
|
|
72
88
|
{
|
|
73
89
|
"path": "./dist/*.js",
|
|
@@ -75,31 +91,14 @@
|
|
|
75
91
|
"gzip": true
|
|
76
92
|
}
|
|
77
93
|
],
|
|
78
|
-
"dependencies": {
|
|
79
|
-
"@kubb/react-fabric": "0.14.0",
|
|
80
|
-
"@kubb/core": "5.0.0-alpha.8",
|
|
81
|
-
"@kubb/oas": "5.0.0-alpha.8",
|
|
82
|
-
"@kubb/plugin-faker": "5.0.0-alpha.8",
|
|
83
|
-
"@kubb/plugin-oas": "5.0.0-alpha.8",
|
|
84
|
-
"@kubb/plugin-ts": "5.0.0-alpha.8"
|
|
85
|
-
},
|
|
86
|
-
"devDependencies": {
|
|
87
|
-
"@internals/utils": "0.0.0"
|
|
88
|
-
},
|
|
89
94
|
"engines": {
|
|
90
95
|
"node": ">=22"
|
|
91
96
|
},
|
|
92
|
-
"publishConfig": {
|
|
93
|
-
"access": "public",
|
|
94
|
-
"registry": "https://registry.npmjs.org/"
|
|
95
|
-
},
|
|
96
|
-
"main": "./dist/index.cjs",
|
|
97
|
-
"module": "./dist/index.js",
|
|
98
97
|
"scripts": {
|
|
99
98
|
"build": "tsdown && size-limit",
|
|
100
99
|
"clean": "npx rimraf ./dist",
|
|
101
|
-
"lint": "
|
|
102
|
-
"lint:fix": "
|
|
100
|
+
"lint": "oxlint .",
|
|
101
|
+
"lint:fix": "oxlint --fix .",
|
|
103
102
|
"release": "pnpm publish --no-git-check",
|
|
104
103
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
105
104
|
"start": "tsdown --watch",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { File } from '@kubb/
|
|
2
|
-
import type {
|
|
1
|
+
import { File } from '@kubb/renderer-jsx'
|
|
2
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
3
3
|
|
|
4
4
|
type HandlersProps = {
|
|
5
5
|
/**
|
|
@@ -10,7 +10,7 @@ type HandlersProps = {
|
|
|
10
10
|
handlers: string[]
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export function Handlers({ name, handlers }: HandlersProps):
|
|
13
|
+
export function Handlers({ name, handlers }: HandlersProps): KubbReactNode {
|
|
14
14
|
return (
|
|
15
15
|
<File.Source name={name} isIndexable isExportable>
|
|
16
16
|
{`export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, '')} as const`}
|