@kubb/plugin-msw 5.0.0-alpha.9 → 5.0.0-beta.3

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.
Files changed (43) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +1 -3
  3. package/dist/components-CLQ77DVn.cjs +584 -0
  4. package/dist/components-CLQ77DVn.cjs.map +1 -0
  5. package/dist/components-vO0FIb2i.js +519 -0
  6. package/dist/components-vO0FIb2i.js.map +1 -0
  7. package/dist/components.cjs +1 -1
  8. package/dist/components.d.ts +17 -21
  9. package/dist/components.js +1 -1
  10. package/dist/generators-BPJCs1x1.js +176 -0
  11. package/dist/generators-BPJCs1x1.js.map +1 -0
  12. package/dist/generators-CrmMwWE4.cjs +186 -0
  13. package/dist/generators-CrmMwWE4.cjs.map +1 -0
  14. package/dist/generators.cjs +1 -1
  15. package/dist/generators.d.ts +4 -500
  16. package/dist/generators.js +1 -1
  17. package/dist/index.cjs +54 -65
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +51 -65
  21. package/dist/index.js.map +1 -1
  22. package/dist/types-Dxu0KMQ4.d.ts +89 -0
  23. package/package.json +59 -57
  24. package/src/components/Handlers.tsx +3 -3
  25. package/src/components/Mock.tsx +36 -28
  26. package/src/components/MockWithFaker.tsx +36 -24
  27. package/src/components/Response.tsx +23 -17
  28. package/src/generators/handlersGenerator.tsx +18 -18
  29. package/src/generators/mswGenerator.tsx +49 -60
  30. package/src/index.ts +1 -1
  31. package/src/plugin.ts +48 -85
  32. package/src/resolvers/resolverMsw.ts +19 -0
  33. package/src/types.ts +45 -22
  34. package/src/utils.ts +109 -0
  35. package/dist/components-8XBwMbFa.cjs +0 -343
  36. package/dist/components-8XBwMbFa.cjs.map +0 -1
  37. package/dist/components-DgtTZkWX.js +0 -277
  38. package/dist/components-DgtTZkWX.js.map +0 -1
  39. package/dist/generators-CY1SNd5X.cjs +0 -171
  40. package/dist/generators-CY1SNd5X.cjs.map +0 -1
  41. package/dist/generators-CvyZTxOm.js +0 -161
  42. package/dist/generators-CvyZTxOm.js.map +0 -1
  43. package/dist/types-MdHRNpgi.d.ts +0 -68
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { n as PluginMsw, t as Options } from "./types-MdHRNpgi.js";
3
- import * as _kubb_core0 from "@kubb/core";
2
+ import { n as PluginMsw, t as Options } from "./types-Dxu0KMQ4.js";
3
+ import * as _$_kubb_core0 from "@kubb/core";
4
4
 
5
5
  //#region src/plugin.d.ts
6
6
  declare const pluginMswName = "plugin-msw";
7
- declare const pluginMsw: (options?: Options | undefined) => _kubb_core0.UserPluginWithLifeCycle<PluginMsw>;
7
+ declare const pluginMsw: (options?: Options | undefined) => _$_kubb_core0.Plugin<PluginMsw>;
8
8
  //#endregion
9
- export { type PluginMsw, pluginMsw, pluginMswName };
9
+ export { type PluginMsw, pluginMsw as default, pluginMsw, pluginMswName };
10
10
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,84 +1,70 @@
1
1
  import "./chunk--u3MIqq1.js";
2
- import { a as camelCase } from "./components-DgtTZkWX.js";
3
- import { n as handlersGenerator, t as mswGenerator } from "./generators-CvyZTxOm.js";
4
- import path from "node:path";
5
- import { createPlugin, getBarrelFiles, getMode } from "@kubb/core";
2
+ import { l as camelCase } from "./components-vO0FIb2i.js";
3
+ import { n as handlersGenerator, t as mswGenerator } from "./generators-BPJCs1x1.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((_ctx) => ({
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
+ }));
23
+ //#endregion
9
24
  //#region src/plugin.ts
10
25
  const pluginMswName = "plugin-msw";
11
- const pluginMsw = createPlugin((options) => {
26
+ const pluginMsw = definePlugin((options) => {
12
27
  const { output = {
13
28
  path: "handlers",
14
29
  barrelType: "named"
15
- }, group, exclude = [], include, override = [], transformers = {}, handlers = false, parser = "data", generators = [mswGenerator, handlers ? handlersGenerator : void 0].filter(Boolean), contentType, baseURL } = options;
30
+ }, group, exclude = [], include, override = [], transformers = {}, handlers = false, parser = "data", baseURL, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [] } = options;
31
+ const groupConfig = group ? {
32
+ ...group,
33
+ name: group.name ? group.name : (ctx) => {
34
+ if (group.type === "path") return `${ctx.group.split("/")[1]}`;
35
+ return `${camelCase(ctx.group)}Controller`;
36
+ }
37
+ } : void 0;
16
38
  return {
17
39
  name: pluginMswName,
18
- options: {
19
- output,
20
- parser,
21
- group,
22
- baseURL
23
- },
24
- pre: [
25
- pluginOasName,
26
- pluginTsName,
27
- parser === "faker" ? pluginFakerName : void 0
28
- ].filter(Boolean),
29
- resolvePath(baseName, pathMode, options) {
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,
40
+ options,
41
+ dependencies: [pluginTsName, parser === "faker" ? pluginFakerName : void 0].filter(Boolean),
42
+ hooks: { "kubb:plugin:setup"(ctx) {
43
+ const resolver = userResolver ? {
44
+ ...resolverMsw,
45
+ ...userResolver
46
+ } : resolverMsw;
47
+ ctx.setOptions({
48
+ output,
49
+ parser,
50
+ baseURL,
51
+ group: groupConfig,
65
52
  exclude,
66
53
  include,
67
54
  override,
68
- mode
69
- }).build(...generators);
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 }
55
+ handlers,
56
+ transformers,
57
+ resolver
76
58
  });
77
- await this.upsertFile(...barrelFiles);
78
- }
59
+ ctx.setResolver(resolver);
60
+ if (userTransformer) ctx.setTransformer(userTransformer);
61
+ ctx.addGenerator(mswGenerator);
62
+ if (handlers) ctx.addGenerator(handlersGenerator);
63
+ for (const gen of userGenerators) ctx.addGenerator(gen);
64
+ } }
79
65
  };
80
66
  });
81
67
  //#endregion
82
- export { pluginMsw, pluginMswName };
68
+ export { pluginMsw as default, pluginMsw, pluginMswName };
83
69
 
84
70
  //# 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 path from 'node:path'\nimport { camelCase } from '@internals/utils'\nimport { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'\nimport { pluginFakerName } from '@kubb/plugin-faker'\nimport { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { handlersGenerator, mswGenerator } from './generators'\nimport type { PluginMsw } from './types.ts'\n\nexport const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']\n\nexport const pluginMsw = createPlugin<PluginMsw>((options) => {\n const {\n output = { path: 'handlers', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n transformers = {},\n handlers = false,\n parser = 'data',\n generators = [mswGenerator, handlers ? handlersGenerator : undefined].filter(Boolean),\n contentType,\n baseURL,\n } = options\n\n return {\n name: pluginMswName,\n options: {\n output,\n parser,\n group,\n baseURL,\n },\n pre: [pluginOasName, pluginTsName, parser === 'faker' ? pluginFakerName : undefined].filter(Boolean),\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = pathMode ?? getMode(path.resolve(root, output.path))\n\n if (mode === 'single') {\n /**\n * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n */\n return path.resolve(root, output.path)\n }\n\n if (group && (options?.group?.path || options?.group?.tag)) {\n const groupName: Group['name'] = group?.name\n ? group.name\n : (ctx) => {\n if (group?.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n }\n\n return path.resolve(\n root,\n output.path,\n groupName({\n group: group.type === 'path' ? options.group.path! : options.group.tag!,\n }),\n baseName,\n )\n }\n\n return path.resolve(root, output.path, baseName)\n },\n resolveName(name, type) {\n const resolvedName = camelCase(name, {\n suffix: type ? 'handler' : undefined,\n isFile: type === 'file',\n })\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async install() {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = getMode(path.resolve(root, output.path))\n const oas = await this.getOas()\n\n const operationGenerator = new OperationGenerator(this.plugin.options, {\n fabric: this.fabric,\n oas,\n driver: this.driver,\n events: this.events,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n })\n\n const files = await operationGenerator.build(...generators)\n await this.upsertFile(...files)\n\n const barrelFiles = await getBarrelFiles(this.fabric.files, {\n type: output.barrelType ?? 'named',\n root,\n output,\n meta: {\n pluginName: this.plugin.name,\n },\n })\n\n await this.upsertFile(...barrelFiles)\n },\n }\n})\n"],"mappings":";;;;;;;;;AASA,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,eAAe,EAAE,EACjB,WAAW,OACX,SAAS,QACT,aAAa,CAAC,cAAc,WAAW,oBAAoB,KAAA,EAAU,CAAC,OAAO,QAAQ,EACrF,aACA,YACE;AAEJ,QAAO;EACL,MAAM;EACN,SAAS;GACP;GACA;GACA;GACA;GACD;EACD,KAAK;GAAC;GAAe;GAAc,WAAW,UAAU,kBAAkB,KAAA;GAAU,CAAC,OAAO,QAAQ;EACpG,YAAY,UAAU,UAAU,SAAS;GACvC,MAAM,OAAO,KAAK,QAAQ,KAAK,OAAO,MAAM,KAAK,OAAO,OAAO,KAAK;AAGpE,QAFa,YAAY,QAAQ,KAAK,QAAQ,MAAM,OAAO,KAAK,CAAC,MAEpD;;;;;AAKX,UAAO,KAAK,QAAQ,MAAM,OAAO,KAAK;AAGxC,OAAI,UAAU,SAAS,OAAO,QAAQ,SAAS,OAAO,MAAM;IAC1D,MAAM,YAA2B,OAAO,OACpC,MAAM,QACL,QAAQ;AACP,SAAI,OAAO,SAAS,OAClB,QAAO,GAAG,IAAI,MAAM,MAAM,IAAI,CAAC;AAEjC,YAAO,GAAG,UAAU,IAAI,MAAM,CAAC;;AAGrC,WAAO,KAAK,QACV,MACA,OAAO,MACP,UAAU,EACR,OAAO,MAAM,SAAS,SAAS,QAAQ,MAAM,OAAQ,QAAQ,MAAM,KACpE,CAAC,EACF,SACD;;AAGH,UAAO,KAAK,QAAQ,MAAM,OAAO,MAAM,SAAS;;EAElD,YAAY,MAAM,MAAM;GACtB,MAAM,eAAe,UAAU,MAAM;IACnC,QAAQ,OAAO,YAAY,KAAA;IAC3B,QAAQ,SAAS;IAClB,CAAC;AAEF,OAAI,KACF,QAAO,cAAc,OAAO,cAAc,KAAK,IAAI;AAGrD,UAAO;;EAET,MAAM,UAAU;GACd,MAAM,OAAO,KAAK,QAAQ,KAAK,OAAO,MAAM,KAAK,OAAO,OAAO,KAAK;GACpE,MAAM,OAAO,QAAQ,KAAK,QAAQ,MAAM,OAAO,KAAK,CAAC;GACrD,MAAM,MAAM,MAAM,KAAK,QAAQ;GAe/B,MAAM,QAAQ,MAba,IAAI,mBAAmB,KAAK,OAAO,SAAS;IACrE,QAAQ,KAAK;IACb;IACA,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb;IACA;IACA;IACA;IACA;IACD,CAAC,CAEqC,MAAM,GAAG,WAAW;AAC3D,SAAM,KAAK,WAAW,GAAG,MAAM;GAE/B,MAAM,cAAc,MAAM,eAAe,KAAK,OAAO,OAAO;IAC1D,MAAM,OAAO,cAAc;IAC3B;IACA;IACA,MAAM,EACJ,YAAY,KAAK,OAAO,MACzB;IACF,CAAC;AAEF,SAAM,KAAK,WAAW,GAAG,YAAY;;EAExC;EACD"}
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>((_ctx) => ({\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}))\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 transformers = {},\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(Boolean),\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 transformers,\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,gBAA2B,UAAU;CAC9D,MAAM;CACN,YAAY;CACZ,QAAQ,MAAM,MAAM;AAClB,SAAO,UAAU,MAAM,EAAE,QAAQ,SAAS,QAAQ,CAAC;;CAErD,YAAY,MAAM;AAChB,SAAO,UAAU,MAAM,EAAE,QAAQ,WAAW,CAAC;;CAEhD,EAAE;;;ACVH,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,eAAe,EAAE,EACjB,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;AAC1B,OAAI,MAAM,SAAS,OACjB,QAAO,GAAG,IAAI,MAAM,MAAM,IAAI,CAAC;AAEjC,UAAO,GAAG,UAAU,IAAI,MAAM,CAAC;;EAEtC,GACD,KAAA;AAEJ,QAAO;EACL,MAAM;EACN;EACA,cAAc,CAAC,cAAc,WAAW,UAAU,kBAAkB,KAAA,EAAU,CAAC,OAAO,QAAQ;EAC9F,OAAO,EACL,oBAAoB,KAAK;GACvB,MAAM,WAAW,eAAe;IAAE,GAAG;IAAa,GAAG;IAAc,GAAG;AAEtE,OAAI,WAAW;IACb;IACA;IACA;IACA,OAAO;IACP;IACA;IACA;IACA;IACA;IACA;IACD,CAAC;AACF,OAAI,YAAY,SAAS;AACzB,OAAI,gBACF,KAAI,eAAe,gBAAgB;AAGrC,OAAI,aAAa,aAAa;AAC9B,OAAI,SACF,KAAI,aAAa,kBAAkB;AAErC,QAAK,MAAM,OAAO,eAChB,KAAI,aAAa,IAAI;KAG1B;EACF;EACD"}
@@ -0,0 +1,89 @@
1
+ import { t as __name } from "./chunk--u3MIqq1.js";
2
+ import { Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, ResolveNameParams, 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 handler function name for an operation.
11
+ */
12
+ resolveName(this: ResolverMsw, name: string): string;
13
+ };
14
+ type Options = {
15
+ /**
16
+ * Specify the export location for the files and define the behavior of the output
17
+ * @default { path: 'handlers', barrelType: 'named' }
18
+ */
19
+ output?: Output;
20
+ baseURL?: string;
21
+ /**
22
+ * Group the MSW mocks based on the provided name.
23
+ */
24
+ group?: Group;
25
+ /**
26
+ * Tags, operations, or paths to exclude from generation.
27
+ */
28
+ exclude?: Array<Exclude>;
29
+ /**
30
+ * Tags, operations, or paths to include in generation.
31
+ */
32
+ include?: Array<Include>;
33
+ /**
34
+ * Override options for specific tags, operations, or paths.
35
+ */
36
+ override?: Array<Override<ResolvedOptions>>;
37
+ transformers?: {
38
+ /**
39
+ * Override the default naming for handlers.
40
+ */
41
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
42
+ };
43
+ /**
44
+ * Override naming conventions for function names and types.
45
+ */
46
+ resolver?: Partial<ResolverMsw> & ThisType<ResolverMsw>;
47
+ /**
48
+ * AST visitor to transform generated nodes.
49
+ */
50
+ transformer?: ast.Visitor;
51
+ /**
52
+ * Create `handlers.ts` file with all handlers grouped by methods.
53
+ * @default false
54
+ */
55
+ handlers?: boolean;
56
+ /**
57
+ * Which parser to use for generating response data.
58
+ *
59
+ * @default 'data'
60
+ */
61
+ parser?: 'data' | 'faker';
62
+ /**
63
+ * Additional generators alongside the default generators.
64
+ */
65
+ generators?: Array<Generator<PluginMsw>>;
66
+ };
67
+ type ResolvedOptions = {
68
+ output: Output;
69
+ group: Group | undefined;
70
+ exclude: NonNullable<Options['exclude']>;
71
+ include: Options['include'];
72
+ override: NonNullable<Options['override']>;
73
+ parser: NonNullable<Options['parser']>;
74
+ baseURL: Options['baseURL'] | undefined;
75
+ handlers: boolean;
76
+ transformers: NonNullable<Options['transformers']>;
77
+ resolver: ResolverMsw;
78
+ };
79
+ type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, ResolverMsw>;
80
+ declare global {
81
+ namespace Kubb {
82
+ interface PluginRegistry {
83
+ 'plugin-msw': PluginMsw;
84
+ }
85
+ }
86
+ }
87
+ //#endregion
88
+ export { PluginMsw as n, Options as t };
89
+ //# sourceMappingURL=types-Dxu0KMQ4.d.ts.map
package/package.json CHANGED
@@ -1,49 +1,44 @@
1
1
  {
2
2
  "name": "@kubb/plugin-msw",
3
- "version": "5.0.0-alpha.9",
3
+ "version": "5.0.0-beta.3",
4
4
  "description": "Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.",
5
5
  "keywords": [
6
- "msw",
7
- "mock-service-worker",
8
6
  "api-mocking",
9
- "mocking",
10
- "mocks",
11
- "testing",
7
+ "code-generator",
8
+ "codegen",
12
9
  "development",
13
10
  "interceptor",
14
- "typescript",
15
- "openapi",
16
- "swagger",
11
+ "kubb",
12
+ "mock-service-worker",
13
+ "mocking",
14
+ "mocks",
15
+ "msw",
17
16
  "oas",
18
- "code-generator",
19
- "codegen",
17
+ "openapi",
20
18
  "plugins",
21
- "kubb"
19
+ "swagger",
20
+ "testing",
21
+ "typescript"
22
22
  ],
23
+ "license": "MIT",
24
+ "author": "stijnvanhulle",
23
25
  "repository": {
24
26
  "type": "git",
25
- "url": "git+https://github.com/kubb-labs/kubb.git",
27
+ "url": "git+https://github.com/kubb-labs/plugins.git",
26
28
  "directory": "packages/plugin-msw"
27
29
  },
28
- "license": "MIT",
29
- "author": "stijnvanhulle",
30
- "sideEffects": false,
30
+ "files": [
31
+ "src",
32
+ "dist",
33
+ "plugin.json",
34
+ "!/**/**.test.**",
35
+ "!/**/__tests__/**",
36
+ "!/**/__snapshots__/**"
37
+ ],
31
38
  "type": "module",
32
- "exports": {
33
- ".": {
34
- "import": "./dist/index.js",
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
- },
39
+ "sideEffects": false,
40
+ "main": "./dist/index.cjs",
41
+ "module": "./dist/index.js",
47
42
  "types": "./dist/index.d.ts",
48
43
  "typesVersions": {
49
44
  "*": {
@@ -61,13 +56,37 @@
61
56
  ]
62
57
  }
63
58
  },
64
- "files": [
65
- "src",
66
- "dist",
67
- "!/**/**.test.**",
68
- "!/**/__tests__/**",
69
- "!/**/__snapshots__/**"
70
- ],
59
+ "exports": {
60
+ ".": {
61
+ "import": "./dist/index.js",
62
+ "require": "./dist/index.cjs"
63
+ },
64
+ "./components": {
65
+ "import": "./dist/components.js",
66
+ "require": "./dist/components.cjs"
67
+ },
68
+ "./generators": {
69
+ "import": "./dist/generators.js",
70
+ "require": "./dist/generators.cjs"
71
+ },
72
+ "./package.json": "./package.json"
73
+ },
74
+ "publishConfig": {
75
+ "access": "public",
76
+ "registry": "https://registry.npmjs.org/"
77
+ },
78
+ "dependencies": {
79
+ "@kubb/core": "5.0.0-beta.3",
80
+ "@kubb/renderer-jsx": "5.0.0-beta.3",
81
+ "@kubb/plugin-faker": "5.0.0-beta.3",
82
+ "@kubb/plugin-ts": "5.0.0-beta.3"
83
+ },
84
+ "devDependencies": {
85
+ "@internals/utils": "0.0.0"
86
+ },
87
+ "peerDependencies": {
88
+ "@kubb/renderer-jsx": "5.0.0-beta.3"
89
+ },
71
90
  "size-limit": [
72
91
  {
73
92
  "path": "./dist/*.js",
@@ -75,31 +94,14 @@
75
94
  "gzip": true
76
95
  }
77
96
  ],
78
- "dependencies": {
79
- "@kubb/react-fabric": "0.14.0",
80
- "@kubb/core": "5.0.0-alpha.9",
81
- "@kubb/oas": "5.0.0-alpha.9",
82
- "@kubb/plugin-oas": "5.0.0-alpha.9",
83
- "@kubb/plugin-faker": "5.0.0-alpha.9",
84
- "@kubb/plugin-ts": "5.0.0-alpha.9"
85
- },
86
- "devDependencies": {
87
- "@internals/utils": "0.0.0"
88
- },
89
97
  "engines": {
90
98
  "node": ">=22"
91
99
  },
92
- "publishConfig": {
93
- "access": "public",
94
- "registry": "https://registry.npmjs.org/"
95
- },
96
- "main": "./dist/index.cjs",
97
- "module": "./dist/index.js",
98
100
  "scripts": {
99
101
  "build": "tsdown && size-limit",
100
102
  "clean": "npx rimraf ./dist",
101
- "lint": "bun biome lint .",
102
- "lint:fix": "bun biome lint --fix --unsafe .",
103
+ "lint": "oxlint .",
104
+ "lint:fix": "oxlint --fix .",
103
105
  "release": "pnpm publish --no-git-check",
104
106
  "release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
105
107
  "start": "tsdown --watch",
@@ -1,5 +1,5 @@
1
- import { File } from '@kubb/react-fabric'
2
- import type { FabricReactNode } from '@kubb/react-fabric/types'
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): FabricReactNode {
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`}
@@ -1,48 +1,56 @@
1
1
  import { URLPath } from '@internals/utils'
2
- import type { OasTypes, Operation } from '@kubb/oas'
3
- import { File, Function, FunctionParams } from '@kubb/react-fabric'
4
- import type { FabricReactNode } from '@kubb/react-fabric/types'
2
+ import { ast } from '@kubb/core'
3
+ import { functionPrinter } from '@kubb/plugin-ts'
4
+ import { File, Function } from '@kubb/renderer-jsx'
5
+ import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
+ import { getContentType, getMswMethod, getMswUrl, getPrimarySuccessResponse, hasResponseSchema } from '../utils.ts'
5
7
 
6
8
  type Props = {
7
- /**
8
- * Name of the function
9
- */
10
9
  name: string
11
10
  typeName: string
12
- fakerName: string
11
+ requestTypeName?: string
13
12
  baseURL: string | undefined
14
- operation: Operation
13
+ node: ast.OperationNode
15
14
  }
16
15
 
17
- export function Mock({ baseURL = '', name, typeName, operation }: Props): FabricReactNode {
18
- const method = operation.method
19
- const successStatusCodes = operation.getResponseStatusCodes().filter((code) => code.startsWith('2'))
20
- const statusCode = successStatusCodes.length > 0 ? Number(successStatusCodes[0]) : 200
16
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
21
17
 
22
- const responseObject = operation.getResponseByStatusCode(statusCode) as OasTypes.ResponseObject
23
- const contentType = Object.keys(responseObject.content || {})?.[0]
24
- const url = new URLPath(operation.path).toURLPath().replace(/([^/]):/g, '$1\\\\:')
18
+ export function Mock({ baseURL = '', name, typeName, requestTypeName, node }: Props): KubbReactNode {
19
+ const method = getMswMethod(node)
20
+ const successResponse = getPrimarySuccessResponse(node)
21
+ const statusCode = successResponse ? Number(successResponse.statusCode) : 200
22
+ const contentType = getContentType(successResponse)
23
+ const url = new URLPath(getMswUrl(node)).toURLPath()
25
24
 
26
25
  const headers = [contentType ? `'Content-Type': '${contentType}'` : undefined].filter(Boolean)
26
+ const responseHasSchema = hasResponseSchema(successResponse)
27
+ const dataType = responseHasSchema ? typeName : 'string | number | boolean | null | object'
27
28
 
28
- const hasResponseSchema = contentType && responseObject?.content?.[contentType]?.schema !== undefined
29
+ const callbackType = requestTypeName
30
+ ? `HttpResponseResolver<Record<string, string>, ${requestTypeName}, any>`
31
+ : `((info: Parameters<Parameters<typeof http.${method}>[1]>[0]) => Response | Promise<Response>)`
29
32
 
30
- // If no response schema, uses any type but function to avoid overriding callback
31
- const dataType = hasResponseSchema ? typeName : 'string | number | boolean | null | object'
33
+ const params = declarationPrinter.print(
34
+ ast.createFunctionParameters({
35
+ params: [
36
+ ast.createFunctionParameter({
37
+ name: 'data',
38
+ type: ast.createParamsType({
39
+ variant: 'reference',
40
+ name: `${dataType} | ${callbackType}`,
41
+ }),
42
+ optional: true,
43
+ }),
44
+ ],
45
+ }),
46
+ )
32
47
 
33
- const params = FunctionParams.factory({
34
- data: {
35
- type: `${dataType} | ((
36
- info: Parameters<Parameters<typeof http.${method}>[1]>[0],
37
- ) => Response | Promise<Response>)`,
38
- optional: true,
39
- },
40
- })
48
+ const httpCall = requestTypeName ? `http.${method}<Record<string, string>, ${requestTypeName}, any>` : `http.${method}`
41
49
 
42
50
  return (
43
51
  <File.Source name={name} isIndexable isExportable>
44
- <Function name={name} export params={params.toConstructor()}>
45
- {`return http.${method}(\`${baseURL}${url.replace(/([^/]):/g, '$1\\\\:')}\`, function handler(info) {
52
+ <Function name={name} export params={params ?? ''}>
53
+ {`return ${httpCall}(\`${baseURL}${url.replace(/([^/]):/g, '$1\\\\:')}\`, function handler(info) {
46
54
  if(typeof data === 'function') return data(info)
47
55
 
48
56
  return new Response(JSON.stringify(data), {
@@ -1,43 +1,55 @@
1
1
  import { URLPath } from '@internals/utils'
2
- import type { OasTypes, Operation } from '@kubb/oas'
3
- import { File, Function, FunctionParams } from '@kubb/react-fabric'
4
- import type { FabricReactNode } from '@kubb/react-fabric/types'
2
+ import { ast } from '@kubb/core'
3
+ import { functionPrinter } from '@kubb/plugin-ts'
4
+ import { File, Function } from '@kubb/renderer-jsx'
5
+ import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
+ import { getContentType, getMswMethod, getMswUrl, getPrimarySuccessResponse } from '../utils.ts'
5
7
 
6
8
  type Props = {
7
- /**
8
- * Name of the function
9
- */
10
9
  name: string
11
10
  typeName: string
11
+ requestTypeName?: string
12
12
  fakerName: string
13
13
  baseURL: string | undefined
14
- operation: Operation
14
+ node: ast.OperationNode
15
15
  }
16
16
 
17
- export function MockWithFaker({ baseURL = '', name, fakerName, typeName, operation }: Props): FabricReactNode {
18
- const method = operation.method
19
- const successStatusCodes = operation.getResponseStatusCodes().filter((code) => code.startsWith('2'))
20
- const statusCode = successStatusCodes.length > 0 ? Number(successStatusCodes[0]) : 200
17
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
21
18
 
22
- const responseObject = operation.getResponseByStatusCode(statusCode) as OasTypes.ResponseObject
23
- const contentType = Object.keys(responseObject.content || {})?.[0]
24
- const url = new URLPath(operation.path).toURLPath().replace(/([^/]):/g, '$1\\\\:')
19
+ export function MockWithFaker({ baseURL = '', name, fakerName, typeName, requestTypeName, node }: Props): KubbReactNode {
20
+ const method = getMswMethod(node)
21
+ const successResponse = getPrimarySuccessResponse(node)
22
+ const statusCode = successResponse ? Number(successResponse.statusCode) : 200
23
+ const contentType = getContentType(successResponse)
24
+ const url = new URLPath(getMswUrl(node)).toURLPath()
25
25
 
26
26
  const headers = [contentType ? `'Content-Type': '${contentType}'` : undefined].filter(Boolean)
27
27
 
28
- const params = FunctionParams.factory({
29
- data: {
30
- type: `${typeName} | ((
31
- info: Parameters<Parameters<typeof http.${method}>[1]>[0],
32
- ) => Response | Promise<Response>)`,
33
- optional: true,
34
- },
35
- })
28
+ const callbackType = requestTypeName
29
+ ? `HttpResponseResolver<Record<string, string>, ${requestTypeName}, any>`
30
+ : `((info: Parameters<Parameters<typeof http.${method}>[1]>[0]) => Response | Promise<Response>)`
31
+
32
+ const params = declarationPrinter.print(
33
+ ast.createFunctionParameters({
34
+ params: [
35
+ ast.createFunctionParameter({
36
+ name: 'data',
37
+ type: ast.createParamsType({
38
+ variant: 'reference',
39
+ name: `${typeName} | ${callbackType}`,
40
+ }),
41
+ optional: true,
42
+ }),
43
+ ],
44
+ }),
45
+ )
46
+
47
+ const httpCall = requestTypeName ? `http.${method}<Record<string, string>, ${requestTypeName}, any>` : `http.${method}`
36
48
 
37
49
  return (
38
50
  <File.Source name={name} isIndexable isExportable>
39
- <Function name={name} export params={params.toConstructor()}>
40
- {`return http.${method}('${baseURL}${url.replace(/([^/]):/g, '$1\\\\:')}', function handler(info) {
51
+ <Function name={name} export params={params ?? ''}>
52
+ {`return ${httpCall}('${baseURL}${url.replace(/([^/]):/g, '$1\\\\:')}', function handler(info) {
41
53
  if(typeof data === 'function') return data(info)
42
54
 
43
55
  return new Response(JSON.stringify(data || ${fakerName}(data)), {