@kubb/plugin-msw 5.0.0-alpha.8 → 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.
- package/LICENSE +17 -10
- package/README.md +1 -3
- package/dist/components-CLQ77DVn.cjs +584 -0
- package/dist/components-CLQ77DVn.cjs.map +1 -0
- package/dist/components-vO0FIb2i.js +519 -0
- package/dist/components-vO0FIb2i.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-BPJCs1x1.js +176 -0
- package/dist/generators-BPJCs1x1.js.map +1 -0
- package/dist/generators-CrmMwWE4.cjs +186 -0
- package/dist/generators-CrmMwWE4.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 +54 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +51 -65
- package/dist/index.js.map +1 -1
- package/dist/types-Dxu0KMQ4.d.ts +89 -0
- package/package.json +59 -57
- package/src/components/Handlers.tsx +3 -3
- package/src/components/Mock.tsx +36 -28
- package/src/components/MockWithFaker.tsx +36 -24
- package/src/components/Response.tsx +23 -17
- package/src/generators/handlersGenerator.tsx +18 -18
- package/src/generators/mswGenerator.tsx +49 -60
- package/src/index.ts +1 -1
- package/src/plugin.ts +48 -85
- package/src/resolvers/resolverMsw.ts +19 -0
- package/src/types.ts +45 -22
- package/src/utils.ts +109 -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,70 @@
|
|
|
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 { 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 =
|
|
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",
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
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
|
|
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-
|
|
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
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"testing",
|
|
7
|
+
"code-generator",
|
|
8
|
+
"codegen",
|
|
12
9
|
"development",
|
|
13
10
|
"interceptor",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
11
|
+
"kubb",
|
|
12
|
+
"mock-service-worker",
|
|
13
|
+
"mocking",
|
|
14
|
+
"mocks",
|
|
15
|
+
"msw",
|
|
17
16
|
"oas",
|
|
18
|
-
"
|
|
19
|
-
"codegen",
|
|
17
|
+
"openapi",
|
|
20
18
|
"plugins",
|
|
21
|
-
"
|
|
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/
|
|
27
|
+
"url": "git+https://github.com/kubb-labs/plugins.git",
|
|
26
28
|
"directory": "packages/plugin-msw"
|
|
27
29
|
},
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"dist",
|
|
33
|
+
"plugin.json",
|
|
34
|
+
"!/**/**.test.**",
|
|
35
|
+
"!/**/__tests__/**",
|
|
36
|
+
"!/**/__snapshots__/**"
|
|
37
|
+
],
|
|
31
38
|
"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
|
-
},
|
|
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
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"
|
|
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.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
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": "
|
|
102
|
-
"lint:fix": "
|
|
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/
|
|
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`}
|
package/src/components/Mock.tsx
CHANGED
|
@@ -1,48 +1,56 @@
|
|
|
1
1
|
import { URLPath } from '@internals/utils'
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
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
|
-
|
|
11
|
+
requestTypeName?: string
|
|
13
12
|
baseURL: string | undefined
|
|
14
|
-
|
|
13
|
+
node: ast.OperationNode
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
|
|
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
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
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
|
|
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
|
-
|
|
31
|
-
|
|
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
|
|
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
|
|
45
|
-
{`return
|
|
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
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
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
|
-
|
|
14
|
+
node: ast.OperationNode
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
|
|
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
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
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
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
40
|
-
{`return
|
|
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)), {
|
|
@@ -1,34 +1,40 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
3
|
+
import { File, Function } from '@kubb/renderer-jsx'
|
|
4
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
5
|
+
import { getContentType, hasResponseSchema } from '../utils.ts'
|
|
4
6
|
|
|
5
7
|
type Props = {
|
|
6
8
|
typeName: string
|
|
7
|
-
operation: Operation
|
|
8
9
|
name: string
|
|
9
|
-
|
|
10
|
+
response: ast.ResponseNode
|
|
11
|
+
key?: string | number | null
|
|
10
12
|
}
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
const responseObject = operation.getResponseByStatusCode(statusCode) as OasTypes.ResponseObject
|
|
14
|
-
const contentType = Object.keys(responseObject.content || {})?.[0]
|
|
14
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
15
15
|
|
|
16
|
+
export function Response({ name, typeName, response }: Props): KubbReactNode {
|
|
17
|
+
const statusCode = Number(response.statusCode)
|
|
18
|
+
const contentType = getContentType(response)
|
|
16
19
|
const headers = [contentType ? `'Content-Type': '${contentType}'` : undefined].filter(Boolean)
|
|
17
20
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
const params = declarationPrinter.print(
|
|
22
|
+
ast.createFunctionParameters({
|
|
23
|
+
params: [
|
|
24
|
+
ast.createFunctionParameter({
|
|
25
|
+
name: 'data',
|
|
26
|
+
type: ast.createParamsType({ variant: 'reference', name: typeName }),
|
|
27
|
+
optional: !hasResponseSchema(response),
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
}),
|
|
31
|
+
)
|
|
26
32
|
|
|
27
33
|
const responseName = `${name}Response${statusCode}`
|
|
28
34
|
|
|
29
35
|
return (
|
|
30
36
|
<File.Source name={responseName} isIndexable isExportable>
|
|
31
|
-
<Function name={responseName} export params={params
|
|
37
|
+
<Function name={responseName} export params={params ?? ''}>
|
|
32
38
|
{`
|
|
33
39
|
return new Response(JSON.stringify(data), {
|
|
34
40
|
status: ${statusCode},
|