@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
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
|
+
import { camelCase } from '@kubb/core/transformers'
|
|
5
|
+
import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
6
|
+
|
|
7
|
+
import { pluginFakerName } from '@kubb/plugin-faker'
|
|
8
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
|
+
|
|
10
|
+
import type { Plugin } from '@kubb/core'
|
|
11
|
+
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
12
|
+
import { handlersGenerator, mswGenerator } from './generators'
|
|
13
|
+
import type { PluginMsw } from './types.ts'
|
|
14
|
+
|
|
15
|
+
export const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']
|
|
16
|
+
|
|
17
|
+
export const pluginMsw = createPlugin<PluginMsw>((options) => {
|
|
18
|
+
const {
|
|
19
|
+
output = { path: 'handlers', barrelType: 'named' },
|
|
20
|
+
group,
|
|
21
|
+
exclude = [],
|
|
22
|
+
include,
|
|
23
|
+
override = [],
|
|
24
|
+
transformers = {},
|
|
25
|
+
handlers = false,
|
|
26
|
+
parser = 'data',
|
|
27
|
+
generators = [mswGenerator, handlers ? handlersGenerator : undefined].filter(Boolean),
|
|
28
|
+
} = options
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
name: pluginMswName,
|
|
32
|
+
options: {
|
|
33
|
+
output,
|
|
34
|
+
parser,
|
|
35
|
+
},
|
|
36
|
+
pre: [pluginOasName, pluginTsName, parser === 'faker' ? pluginFakerName : undefined].filter(Boolean),
|
|
37
|
+
resolvePath(baseName, pathMode, options) {
|
|
38
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
39
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
40
|
+
|
|
41
|
+
if (options?.tag && group?.type === 'tag') {
|
|
42
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
43
|
+
|
|
44
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (mode === 'single') {
|
|
48
|
+
/**
|
|
49
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
50
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
51
|
+
*/
|
|
52
|
+
return path.resolve(root, output.path)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return path.resolve(root, output.path, baseName)
|
|
56
|
+
},
|
|
57
|
+
resolveName(name, type) {
|
|
58
|
+
const resolvedName = camelCase(name, {
|
|
59
|
+
suffix: type ? 'handler' : undefined,
|
|
60
|
+
isFile: type === 'file',
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (type) {
|
|
64
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return resolvedName
|
|
68
|
+
},
|
|
69
|
+
async buildStart() {
|
|
70
|
+
const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
|
|
71
|
+
|
|
72
|
+
const oas = await swaggerPlugin.context.getOas()
|
|
73
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
74
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
75
|
+
|
|
76
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
77
|
+
oas,
|
|
78
|
+
pluginManager: this.pluginManager,
|
|
79
|
+
plugin: this.plugin,
|
|
80
|
+
contentType: swaggerPlugin.context.contentType,
|
|
81
|
+
exclude,
|
|
82
|
+
include,
|
|
83
|
+
override,
|
|
84
|
+
mode,
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const files = await operationGenerator.build(...generators)
|
|
88
|
+
await this.addFile(...files)
|
|
89
|
+
|
|
90
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
91
|
+
type: output.barrelType ?? 'named',
|
|
92
|
+
root,
|
|
93
|
+
output,
|
|
94
|
+
files: this.fileManager.files,
|
|
95
|
+
meta: {
|
|
96
|
+
pluginKey: this.plugin.key,
|
|
97
|
+
},
|
|
98
|
+
logger: this.logger,
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
await this.addFile(...barrelFiles)
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
|
|
3
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
4
|
+
|
|
5
|
+
export type Options = {
|
|
6
|
+
/**
|
|
7
|
+
* Specify the export location for the files and define the behavior of the output
|
|
8
|
+
* @default { path: 'mocks', barrelType: 'named' }
|
|
9
|
+
*/
|
|
10
|
+
output?: Output
|
|
11
|
+
/**
|
|
12
|
+
* Group the MSW mocks based on the provided name.
|
|
13
|
+
*/
|
|
14
|
+
group?: Group
|
|
15
|
+
/**
|
|
16
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
17
|
+
*/
|
|
18
|
+
exclude?: Array<Exclude>
|
|
19
|
+
/**
|
|
20
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
21
|
+
*/
|
|
22
|
+
include?: Array<Include>
|
|
23
|
+
/**
|
|
24
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
25
|
+
*/
|
|
26
|
+
override?: Array<Override<ResolvedOptions>>
|
|
27
|
+
transformers?: {
|
|
28
|
+
/**
|
|
29
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
30
|
+
*/
|
|
31
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create `handlers.ts` file with all handlers grouped by methods.
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
handlers?: boolean
|
|
38
|
+
/**
|
|
39
|
+
* Which parser should be used before returning the data to the `Response` of MSW.
|
|
40
|
+
* - `'faker'` will use `@kubb/plugin-faker` to generate the data for the response
|
|
41
|
+
* - `'data'` will use your custom data to generate the data for the response
|
|
42
|
+
* @default 'data'
|
|
43
|
+
*/
|
|
44
|
+
parser?: 'data' | 'faker'
|
|
45
|
+
/**
|
|
46
|
+
* Define some generators next to the msw generators
|
|
47
|
+
*/
|
|
48
|
+
generators?: Array<Generator<PluginMsw>>
|
|
49
|
+
}
|
|
50
|
+
type ResolvedOptions = {
|
|
51
|
+
output: Output
|
|
52
|
+
parser: NonNullable<Options['parser']>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, never, ResolvePathOptions>
|