@kubb/plugin-msw 0.0.0-canary-20241104172400
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 +21 -0
- package/README.md +53 -0
- package/dist/chunk-IUWTMC3G.js +155 -0
- package/dist/chunk-IUWTMC3G.js.map +1 -0
- package/dist/chunk-QXK7PR6Z.cjs +164 -0
- package/dist/chunk-QXK7PR6Z.cjs.map +1 -0
- package/dist/chunk-XLGN5MEF.cjs +45 -0
- package/dist/chunk-XLGN5MEF.cjs.map +1 -0
- package/dist/chunk-YG3WMS3Q.js +41 -0
- package/dist/chunk-YG3WMS3Q.js.map +1 -0
- package/dist/components.cjs +20 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +37 -0
- package/dist/components.d.ts +37 -0
- package/dist/components.js +3 -0
- package/dist/components.js.map +1 -0
- package/dist/generators.cjs +17 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +9 -0
- package/dist/generators.d.ts +9 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +17 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types-BaK4s79H.d.cts +55 -0
- package/dist/types-BaK4s79H.d.ts +55 -0
- package/package.json +114 -0
- package/src/components/Handlers.tsx +19 -0
- package/src/components/Mock.tsx +38 -0
- package/src/components/MockWithFaker.tsx +38 -0
- package/src/components/index.ts +3 -0
- package/src/generators/__snapshots__/createPet.ts +11 -0
- package/src/generators/__snapshots__/deletePet.ts +11 -0
- package/src/generators/__snapshots__/getPets.ts +11 -0
- package/src/generators/__snapshots__/getPetsFaker.ts +11 -0
- package/src/generators/__snapshots__/handlers.ts +3 -0
- package/src/generators/__snapshots__/showPetById.ts +11 -0
- package/src/generators/handlersGenerator.tsx +32 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/mswGenerator.tsx +64 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +104 -0
- package/src/types.ts +55 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { HttpMethod } from '@kubb/oas';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type Props$1 = {
|
|
5
|
+
/**
|
|
6
|
+
* Name of the function
|
|
7
|
+
*/
|
|
8
|
+
name: string;
|
|
9
|
+
typeName: string;
|
|
10
|
+
fakerName: string;
|
|
11
|
+
url: string;
|
|
12
|
+
method: HttpMethod;
|
|
13
|
+
};
|
|
14
|
+
declare function Mock({ name, fakerName, typeName, url, method }: Props$1): ReactNode;
|
|
15
|
+
|
|
16
|
+
type HandlersProps = {
|
|
17
|
+
/**
|
|
18
|
+
* Name of the function
|
|
19
|
+
*/
|
|
20
|
+
name: string;
|
|
21
|
+
handlers: string[];
|
|
22
|
+
};
|
|
23
|
+
declare function Handlers({ name, handlers }: HandlersProps): ReactNode;
|
|
24
|
+
|
|
25
|
+
type Props = {
|
|
26
|
+
/**
|
|
27
|
+
* Name of the function
|
|
28
|
+
*/
|
|
29
|
+
name: string;
|
|
30
|
+
typeName: string;
|
|
31
|
+
fakerName: string;
|
|
32
|
+
url: string;
|
|
33
|
+
method: HttpMethod;
|
|
34
|
+
};
|
|
35
|
+
declare function MockWithFaker({ name, fakerName, typeName, url, method }: Props): ReactNode;
|
|
36
|
+
|
|
37
|
+
export { Handlers, Mock, MockWithFaker };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"components.js"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkQXK7PR6Z_cjs = require('./chunk-QXK7PR6Z.cjs');
|
|
4
|
+
require('./chunk-XLGN5MEF.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, "handlersGenerator", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return chunkQXK7PR6Z_cjs.handlersGenerator; }
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "mswGenerator", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return chunkQXK7PR6Z_cjs.mswGenerator; }
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=generators.cjs.map
|
|
17
|
+
//# sourceMappingURL=generators.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"generators.cjs"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as _kubb_plugin_oas from '@kubb/plugin-oas';
|
|
2
|
+
import { P as PluginMsw } from './types-BaK4s79H.cjs';
|
|
3
|
+
import '@kubb/core';
|
|
4
|
+
|
|
5
|
+
declare const mswGenerator: _kubb_plugin_oas.Generator<PluginMsw>;
|
|
6
|
+
|
|
7
|
+
declare const handlersGenerator: _kubb_plugin_oas.Generator<PluginMsw>;
|
|
8
|
+
|
|
9
|
+
export { handlersGenerator, mswGenerator };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as _kubb_plugin_oas from '@kubb/plugin-oas';
|
|
2
|
+
import { P as PluginMsw } from './types-BaK4s79H.js';
|
|
3
|
+
import '@kubb/core';
|
|
4
|
+
|
|
5
|
+
declare const mswGenerator: _kubb_plugin_oas.Generator<PluginMsw>;
|
|
6
|
+
|
|
7
|
+
declare const handlersGenerator: _kubb_plugin_oas.Generator<PluginMsw>;
|
|
8
|
+
|
|
9
|
+
export { handlersGenerator, mswGenerator };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"generators.js"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkQXK7PR6Z_cjs = require('./chunk-QXK7PR6Z.cjs');
|
|
4
|
+
require('./chunk-XLGN5MEF.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, "pluginMsw", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return chunkQXK7PR6Z_cjs.pluginMsw; }
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "pluginMswName", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return chunkQXK7PR6Z_cjs.pluginMswName; }
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=index.cjs.map
|
|
17
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as _kubb_core from '@kubb/core';
|
|
2
|
+
import { O as Options, P as PluginMsw } from './types-BaK4s79H.cjs';
|
|
3
|
+
import '@kubb/plugin-oas';
|
|
4
|
+
|
|
5
|
+
declare const pluginMswName = "plugin-msw";
|
|
6
|
+
declare const pluginMsw: (options?: Options | undefined) => _kubb_core.UserPluginWithLifeCycle<PluginMsw>;
|
|
7
|
+
|
|
8
|
+
export { PluginMsw, pluginMsw, pluginMswName };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as _kubb_core from '@kubb/core';
|
|
2
|
+
import { O as Options, P as PluginMsw } from './types-BaK4s79H.js';
|
|
3
|
+
import '@kubb/plugin-oas';
|
|
4
|
+
|
|
5
|
+
declare const pluginMswName = "plugin-msw";
|
|
6
|
+
declare const pluginMsw: (options?: Options | undefined) => _kubb_core.UserPluginWithLifeCycle<PluginMsw>;
|
|
7
|
+
|
|
8
|
+
export { PluginMsw, pluginMsw, pluginMswName };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { PluginFactoryOptions, Output, Group, ResolveNameParams } from '@kubb/core';
|
|
2
|
+
import { ResolvePathOptions, Exclude, Include, Override, Generator } from '@kubb/plugin-oas';
|
|
3
|
+
|
|
4
|
+
type Options = {
|
|
5
|
+
/**
|
|
6
|
+
* Specify the export location for the files and define the behavior of the output
|
|
7
|
+
* @default { path: 'mocks', barrelType: 'named' }
|
|
8
|
+
*/
|
|
9
|
+
output?: Output;
|
|
10
|
+
/**
|
|
11
|
+
* Group the MSW mocks based on the provided name.
|
|
12
|
+
*/
|
|
13
|
+
group?: Group;
|
|
14
|
+
/**
|
|
15
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
16
|
+
*/
|
|
17
|
+
exclude?: Array<Exclude>;
|
|
18
|
+
/**
|
|
19
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
20
|
+
*/
|
|
21
|
+
include?: Array<Include>;
|
|
22
|
+
/**
|
|
23
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
24
|
+
*/
|
|
25
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
26
|
+
transformers?: {
|
|
27
|
+
/**
|
|
28
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
29
|
+
*/
|
|
30
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Create `handlers.ts` file with all handlers grouped by methods.
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
handlers?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Which parser should be used before returning the data to the `Response` of MSW.
|
|
39
|
+
* - `'faker'` will use `@kubb/plugin-faker` to generate the data for the response
|
|
40
|
+
* - `'data'` will use your custom data to generate the data for the response
|
|
41
|
+
* @default 'data'
|
|
42
|
+
*/
|
|
43
|
+
parser?: 'data' | 'faker';
|
|
44
|
+
/**
|
|
45
|
+
* Define some generators next to the msw generators
|
|
46
|
+
*/
|
|
47
|
+
generators?: Array<Generator<PluginMsw>>;
|
|
48
|
+
};
|
|
49
|
+
type ResolvedOptions = {
|
|
50
|
+
output: Output;
|
|
51
|
+
parser: NonNullable<Options['parser']>;
|
|
52
|
+
};
|
|
53
|
+
type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
54
|
+
|
|
55
|
+
export type { Options as O, PluginMsw as P };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { PluginFactoryOptions, Output, Group, ResolveNameParams } from '@kubb/core';
|
|
2
|
+
import { ResolvePathOptions, Exclude, Include, Override, Generator } from '@kubb/plugin-oas';
|
|
3
|
+
|
|
4
|
+
type Options = {
|
|
5
|
+
/**
|
|
6
|
+
* Specify the export location for the files and define the behavior of the output
|
|
7
|
+
* @default { path: 'mocks', barrelType: 'named' }
|
|
8
|
+
*/
|
|
9
|
+
output?: Output;
|
|
10
|
+
/**
|
|
11
|
+
* Group the MSW mocks based on the provided name.
|
|
12
|
+
*/
|
|
13
|
+
group?: Group;
|
|
14
|
+
/**
|
|
15
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
16
|
+
*/
|
|
17
|
+
exclude?: Array<Exclude>;
|
|
18
|
+
/**
|
|
19
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
20
|
+
*/
|
|
21
|
+
include?: Array<Include>;
|
|
22
|
+
/**
|
|
23
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
24
|
+
*/
|
|
25
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
26
|
+
transformers?: {
|
|
27
|
+
/**
|
|
28
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
29
|
+
*/
|
|
30
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Create `handlers.ts` file with all handlers grouped by methods.
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
handlers?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Which parser should be used before returning the data to the `Response` of MSW.
|
|
39
|
+
* - `'faker'` will use `@kubb/plugin-faker` to generate the data for the response
|
|
40
|
+
* - `'data'` will use your custom data to generate the data for the response
|
|
41
|
+
* @default 'data'
|
|
42
|
+
*/
|
|
43
|
+
parser?: 'data' | 'faker';
|
|
44
|
+
/**
|
|
45
|
+
* Define some generators next to the msw generators
|
|
46
|
+
*/
|
|
47
|
+
generators?: Array<Generator<PluginMsw>>;
|
|
48
|
+
};
|
|
49
|
+
type ResolvedOptions = {
|
|
50
|
+
output: Output;
|
|
51
|
+
parser: NonNullable<Options['parser']>;
|
|
52
|
+
};
|
|
53
|
+
type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
54
|
+
|
|
55
|
+
export type { Options as O, PluginMsw as P };
|
package/package.json
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kubb/plugin-msw",
|
|
3
|
+
"version": "0.0.0-canary-20241104172400",
|
|
4
|
+
"description": "Generator swagger",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"plugins",
|
|
8
|
+
"kubb",
|
|
9
|
+
"codegen",
|
|
10
|
+
"swagger",
|
|
11
|
+
"openapi"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git://github.com/kubb-labs/kubb.git",
|
|
16
|
+
"directory": "packages/plugin-msw"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Stijn Van Hulle <stijn@stijnvanhulle.be",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./dist/index.js",
|
|
25
|
+
"require": "./dist/index.cjs",
|
|
26
|
+
"default": "./dist/index.cjs"
|
|
27
|
+
},
|
|
28
|
+
"./utils": {
|
|
29
|
+
"import": "./dist/utils.js",
|
|
30
|
+
"require": "./dist/utils.cjs",
|
|
31
|
+
"default": "./dist/utils.cjs"
|
|
32
|
+
},
|
|
33
|
+
"./hooks": {
|
|
34
|
+
"import": "./dist/hooks.js",
|
|
35
|
+
"require": "./dist/hooks.cjs",
|
|
36
|
+
"default": "./dist/hooks.cjs"
|
|
37
|
+
},
|
|
38
|
+
"./components": {
|
|
39
|
+
"import": "./dist/components.js",
|
|
40
|
+
"require": "./dist/components.cjs",
|
|
41
|
+
"default": "./dist/components.cjs"
|
|
42
|
+
},
|
|
43
|
+
"./generators": {
|
|
44
|
+
"import": "./dist/generators.js",
|
|
45
|
+
"require": "./dist/generators.cjs",
|
|
46
|
+
"default": "./dist/generators.cjs"
|
|
47
|
+
},
|
|
48
|
+
"./package.json": "./package.json",
|
|
49
|
+
"./*": "./*"
|
|
50
|
+
},
|
|
51
|
+
"main": "dist/index.cjs",
|
|
52
|
+
"module": "dist/index.js",
|
|
53
|
+
"types": "./dist/index.d.ts",
|
|
54
|
+
"typesVersions": {
|
|
55
|
+
"*": {
|
|
56
|
+
"utils": [
|
|
57
|
+
"./dist/utils.d.ts"
|
|
58
|
+
],
|
|
59
|
+
"hooks": [
|
|
60
|
+
"./dist/hooks.d.ts"
|
|
61
|
+
],
|
|
62
|
+
"components": [
|
|
63
|
+
"./dist/components.d.ts"
|
|
64
|
+
],
|
|
65
|
+
"generators": [
|
|
66
|
+
"./dist/generators.d.ts"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
"files": [
|
|
71
|
+
"src",
|
|
72
|
+
"dist",
|
|
73
|
+
"!/**/**.test.**",
|
|
74
|
+
"!/**/__tests__/**"
|
|
75
|
+
],
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"@kubb/core": "0.0.0-canary-20241104172400",
|
|
78
|
+
"@kubb/fs": "0.0.0-canary-20241104172400",
|
|
79
|
+
"@kubb/oas": "0.0.0-canary-20241104172400",
|
|
80
|
+
"@kubb/plugin-faker": "0.0.0-canary-20241104172400",
|
|
81
|
+
"@kubb/plugin-oas": "0.0.0-canary-20241104172400",
|
|
82
|
+
"@kubb/plugin-ts": "0.0.0-canary-20241104172400",
|
|
83
|
+
"@kubb/react": "0.0.0-canary-20241104172400"
|
|
84
|
+
},
|
|
85
|
+
"devDependencies": {
|
|
86
|
+
"@types/react": "^18.3.12",
|
|
87
|
+
"react": "^18.3.1",
|
|
88
|
+
"tsup": "^8.3.5",
|
|
89
|
+
"typescript": "^5.6.3",
|
|
90
|
+
"@kubb/config-ts": "0.0.0-canary-20241104172400",
|
|
91
|
+
"@kubb/config-tsup": "0.0.0-canary-20241104172400"
|
|
92
|
+
},
|
|
93
|
+
"peerDependencies": {
|
|
94
|
+
"@kubb/react": "0.0.0-canary-20241104172400"
|
|
95
|
+
},
|
|
96
|
+
"engines": {
|
|
97
|
+
"node": ">=20"
|
|
98
|
+
},
|
|
99
|
+
"publishConfig": {
|
|
100
|
+
"access": "public",
|
|
101
|
+
"registry": "https://registry.npmjs.org/"
|
|
102
|
+
},
|
|
103
|
+
"scripts": {
|
|
104
|
+
"build": "tsup",
|
|
105
|
+
"clean": "npx rimraf ./dist",
|
|
106
|
+
"lint": "bun biome lint .",
|
|
107
|
+
"lint:fix": "bun biome lint --apply-unsafe .",
|
|
108
|
+
"release": "pnpm publish --no-git-check",
|
|
109
|
+
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
110
|
+
"start": "tsup --watch",
|
|
111
|
+
"test": "vitest --passWithNoTests",
|
|
112
|
+
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { File } from '@kubb/react'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
3
|
+
|
|
4
|
+
type HandlersProps = {
|
|
5
|
+
/**
|
|
6
|
+
* Name of the function
|
|
7
|
+
*/
|
|
8
|
+
name: string
|
|
9
|
+
// custom
|
|
10
|
+
handlers: string[]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function Handlers({ name, handlers }: HandlersProps): ReactNode {
|
|
14
|
+
return (
|
|
15
|
+
<File.Source name={name} isIndexable isExportable>
|
|
16
|
+
{`export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, '')} as const`}
|
|
17
|
+
</File.Source>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
2
|
+
|
|
3
|
+
import type { HttpMethod } from '@kubb/oas'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
|
|
6
|
+
type Props = {
|
|
7
|
+
/**
|
|
8
|
+
* Name of the function
|
|
9
|
+
*/
|
|
10
|
+
name: string
|
|
11
|
+
typeName: string
|
|
12
|
+
fakerName: string
|
|
13
|
+
url: string
|
|
14
|
+
method: HttpMethod
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function Mock({ name, fakerName, typeName, url, method }: Props): ReactNode {
|
|
18
|
+
const params = FunctionParams.factory({
|
|
19
|
+
data: {
|
|
20
|
+
type: typeName,
|
|
21
|
+
optional: true,
|
|
22
|
+
},
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<File.Source name={name} isIndexable isExportable>
|
|
27
|
+
<Function name={name} export params={params.toConstructor()}>
|
|
28
|
+
{`return http.${method}('*${url}', function handler(info) {
|
|
29
|
+
return new Response(JSON.stringify(data), {
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/json',
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
})`}
|
|
35
|
+
</Function>
|
|
36
|
+
</File.Source>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
2
|
+
|
|
3
|
+
import type { HttpMethod } from '@kubb/oas'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
|
|
6
|
+
type Props = {
|
|
7
|
+
/**
|
|
8
|
+
* Name of the function
|
|
9
|
+
*/
|
|
10
|
+
name: string
|
|
11
|
+
typeName: string
|
|
12
|
+
fakerName: string
|
|
13
|
+
url: string
|
|
14
|
+
method: HttpMethod
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function MockWithFaker({ name, fakerName, typeName, url, method }: Props): ReactNode {
|
|
18
|
+
const params = FunctionParams.factory({
|
|
19
|
+
data: {
|
|
20
|
+
type: typeName,
|
|
21
|
+
optional: true,
|
|
22
|
+
},
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<File.Source name={name} isIndexable isExportable>
|
|
27
|
+
<Function name={name} export params={params.toConstructor()}>
|
|
28
|
+
{`return http.${method}('*${url}', function handler(info) {
|
|
29
|
+
return new Response(JSON.stringify(${fakerName}(data)), {
|
|
30
|
+
headers: {
|
|
31
|
+
'Content-Type': 'application/json',
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
})`}
|
|
35
|
+
</Function>
|
|
36
|
+
</File.Source>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { http } from "msw";
|
|
2
|
+
|
|
3
|
+
export function createPets(data?: CreatePetsMutationResponse) {
|
|
4
|
+
return http.post("*/pets", function handler(info) {
|
|
5
|
+
return new Response(JSON.stringify(data), {
|
|
6
|
+
headers: {
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { http } from "msw";
|
|
2
|
+
|
|
3
|
+
export function deletePetsPetid(data?: DeletePetsPetidMutationResponse) {
|
|
4
|
+
return http.delete("*/pets/:petId", function handler(info) {
|
|
5
|
+
return new Response(JSON.stringify(data), {
|
|
6
|
+
headers: {
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { http } from "msw";
|
|
2
|
+
|
|
3
|
+
export function listPets(data?: ListPetsQueryResponse) {
|
|
4
|
+
return http.get("*/pets", function handler(info) {
|
|
5
|
+
return new Response(JSON.stringify(data), {
|
|
6
|
+
headers: {
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { http } from "msw";
|
|
2
|
+
|
|
3
|
+
export function listPets(data?: ListPetsQueryResponse) {
|
|
4
|
+
return http.get("*/pets", function handler(info) {
|
|
5
|
+
return new Response(JSON.stringify(listPetsQueryResponse(data)), {
|
|
6
|
+
headers: {
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { http } from "msw";
|
|
2
|
+
|
|
3
|
+
export function showPetById(data?: ShowPetByIdQueryResponse) {
|
|
4
|
+
return http.get("*/pets/:petId", function handler(info) {
|
|
5
|
+
return new Response(JSON.stringify(data), {
|
|
6
|
+
headers: {
|
|
7
|
+
"Content-Type": "application/json",
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
2
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
3
|
+
import { File, useApp } from '@kubb/react'
|
|
4
|
+
import { Handlers } from '../components/Handlers.tsx'
|
|
5
|
+
import { pluginMswName } from '../plugin.ts'
|
|
6
|
+
import type { PluginMsw } from '../types'
|
|
7
|
+
|
|
8
|
+
export const handlersGenerator = createReactGenerator<PluginMsw>({
|
|
9
|
+
name: 'plugin-msw',
|
|
10
|
+
Operations({ operations }) {
|
|
11
|
+
const { pluginManager, plugin } = useApp<PluginMsw>()
|
|
12
|
+
const { getName, getFile } = useOperationManager()
|
|
13
|
+
|
|
14
|
+
const file = pluginManager.getFile({ name: 'handlers', extname: '.ts', pluginKey: [pluginMswName] })
|
|
15
|
+
|
|
16
|
+
const imports = operations.map((operation) => {
|
|
17
|
+
const operationFile = getFile(operation, { pluginKey: [pluginMswName] })
|
|
18
|
+
const operationName = getName(operation, { pluginKey: [pluginMswName], type: 'function' })
|
|
19
|
+
|
|
20
|
+
return <File.Import key={operationFile.path} name={[operationName]} root={file.path} path={operationFile.path} />
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const handlers = operations.map((operation) => `${getName(operation, { type: 'function', pluginKey: [pluginMswName] })}()`)
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<File baseName={file.baseName} path={file.path} meta={file.meta} banner={plugin.options.output?.banner} footer={plugin.options.output?.footer}>
|
|
27
|
+
{imports}
|
|
28
|
+
<Handlers name={'handlers'} handlers={handlers} />
|
|
29
|
+
</File>
|
|
30
|
+
)
|
|
31
|
+
},
|
|
32
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { URLPath } from '@kubb/core/utils'
|
|
2
|
+
import { pluginFakerName } from '@kubb/plugin-faker'
|
|
3
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
4
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
|
+
import { File, useApp } from '@kubb/react'
|
|
7
|
+
import { Mock, MockWithFaker } from '../components'
|
|
8
|
+
import type { PluginMsw } from '../types'
|
|
9
|
+
|
|
10
|
+
export const mswGenerator = createReactGenerator<PluginMsw>({
|
|
11
|
+
name: 'msw',
|
|
12
|
+
Operation({ operation }) {
|
|
13
|
+
const {
|
|
14
|
+
plugin: {
|
|
15
|
+
options: { output, parser },
|
|
16
|
+
},
|
|
17
|
+
} = useApp<PluginMsw>()
|
|
18
|
+
const { getSchemas, getName, getFile } = useOperationManager()
|
|
19
|
+
|
|
20
|
+
const mock = {
|
|
21
|
+
name: getName(operation, { type: 'function' }),
|
|
22
|
+
file: getFile(operation),
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const faker = {
|
|
26
|
+
file: getFile(operation, { pluginKey: [pluginFakerName] }),
|
|
27
|
+
schemas: getSchemas(operation, { pluginKey: [pluginFakerName], type: 'function' }),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const type = {
|
|
31
|
+
file: getFile(operation, { pluginKey: [pluginTsName] }),
|
|
32
|
+
schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<File baseName={mock.file.baseName} path={mock.file.path} meta={mock.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
37
|
+
<File.Import name={['http']} path={'msw'} />
|
|
38
|
+
<File.Import name={[type.schemas.response.name]} path={type.file.path} root={mock.file.path} isTypeOnly />
|
|
39
|
+
{parser === 'faker' && faker.file && faker.schemas.response && (
|
|
40
|
+
<File.Import name={[faker.schemas.response.name]} root={mock.file.path} path={faker.file.path} />
|
|
41
|
+
)}
|
|
42
|
+
|
|
43
|
+
{parser === 'faker' && (
|
|
44
|
+
<MockWithFaker
|
|
45
|
+
name={mock.name}
|
|
46
|
+
typeName={type.schemas.response.name}
|
|
47
|
+
fakerName={faker.schemas.response.name}
|
|
48
|
+
method={operation.method}
|
|
49
|
+
url={new URLPath(operation.path).toURLPath()}
|
|
50
|
+
/>
|
|
51
|
+
)}
|
|
52
|
+
{parser === 'data' && (
|
|
53
|
+
<Mock
|
|
54
|
+
name={mock.name}
|
|
55
|
+
typeName={type.schemas.response.name}
|
|
56
|
+
fakerName={faker.schemas.response.name}
|
|
57
|
+
method={operation.method}
|
|
58
|
+
url={new URLPath(operation.path).toURLPath()}
|
|
59
|
+
/>
|
|
60
|
+
)}
|
|
61
|
+
</File>
|
|
62
|
+
)
|
|
63
|
+
},
|
|
64
|
+
})
|
package/src/index.ts
ADDED