@kubb/plugin-msw 3.0.0-alpha.9 → 3.0.0-beta.1
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/README.md +14 -5
- package/dist/chunk-6F4YUN2R.cjs +138 -0
- package/dist/chunk-6F4YUN2R.cjs.map +1 -0
- package/dist/chunk-6M2QU6UL.js +22 -0
- package/dist/chunk-6M2QU6UL.js.map +1 -0
- package/dist/chunk-GODA4G5K.cjs +25 -0
- package/dist/chunk-GODA4G5K.cjs.map +1 -0
- package/dist/chunk-TW2VMA3H.js +129 -0
- package/dist/chunk-TW2VMA3H.js.map +1 -0
- package/dist/components.cjs +11 -4
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +7 -70
- package/dist/components.d.ts +7 -70
- package/dist/components.js +2 -8
- package/dist/components.js.map +1 -1
- 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 +11 -128
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -86
- package/dist/index.d.ts +3 -86
- package/dist/index.js +3 -133
- package/dist/index.js.map +1 -1
- package/dist/types-B3_ksuJz.d.cts +46 -0
- package/dist/types-B3_ksuJz.d.ts +46 -0
- package/package.json +22 -17
- package/src/components/Handlers.tsx +19 -0
- package/src/components/Mock.tsx +8 -86
- package/src/components/index.ts +1 -1
- package/src/generators/__snapshots__/createPet.ts +9 -0
- package/src/generators/__snapshots__/deletePet.ts +9 -0
- package/src/generators/__snapshots__/getPets.ts +9 -0
- package/src/generators/__snapshots__/handlers.ts +3 -0
- package/src/generators/__snapshots__/showPetById.ts +9 -0
- package/src/generators/handlersGenerator.tsx +32 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/mswGenerator.tsx +38 -0
- package/src/plugin.ts +34 -37
- package/src/types.ts +15 -58
- package/dist/chunk-3KHLTEUD.js +0 -104
- package/dist/chunk-3KHLTEUD.js.map +0 -1
- package/dist/chunk-BD7UJBXM.cjs +0 -104
- package/dist/chunk-BD7UJBXM.cjs.map +0 -1
- package/src/OperationGenerator.tsx +0 -66
- package/src/components/Operations.tsx +0 -94
- package/src/components/__snapshots__/Mock/Pets.ts +0 -7
- package/src/components/__snapshots__/Mock/showPetsById.ts +0 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-msw",
|
|
3
|
-
"version": "3.0.0-
|
|
3
|
+
"version": "3.0.0-beta.1",
|
|
4
4
|
"description": "Generator swagger",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -40,6 +40,11 @@
|
|
|
40
40
|
"require": "./dist/components.cjs",
|
|
41
41
|
"default": "./dist/components.cjs"
|
|
42
42
|
},
|
|
43
|
+
"./generators": {
|
|
44
|
+
"import": "./dist/generators.js",
|
|
45
|
+
"require": "./dist/generators.cjs",
|
|
46
|
+
"default": "./dist/generators.cjs"
|
|
47
|
+
},
|
|
43
48
|
"./package.json": "./package.json",
|
|
44
49
|
"./*": "./*"
|
|
45
50
|
},
|
|
@@ -56,6 +61,9 @@
|
|
|
56
61
|
],
|
|
57
62
|
"components": [
|
|
58
63
|
"./dist/components.d.ts"
|
|
64
|
+
],
|
|
65
|
+
"generators": [
|
|
66
|
+
"./dist/generators.d.ts"
|
|
59
67
|
]
|
|
60
68
|
}
|
|
61
69
|
},
|
|
@@ -66,27 +74,24 @@
|
|
|
66
74
|
"!/**/__tests__/**"
|
|
67
75
|
],
|
|
68
76
|
"dependencies": {
|
|
69
|
-
"@kubb/core": "3.0.0-
|
|
70
|
-
"@kubb/fs": "3.0.0-
|
|
71
|
-
"@kubb/oas": "3.0.0-
|
|
72
|
-
"@kubb/
|
|
73
|
-
"@kubb/plugin-
|
|
74
|
-
"@kubb/plugin-
|
|
75
|
-
"@kubb/
|
|
76
|
-
"@kubb/react": "3.0.0-alpha.9"
|
|
77
|
+
"@kubb/core": "3.0.0-beta.1",
|
|
78
|
+
"@kubb/fs": "3.0.0-beta.1",
|
|
79
|
+
"@kubb/oas": "3.0.0-beta.1",
|
|
80
|
+
"@kubb/plugin-faker": "3.0.0-beta.1",
|
|
81
|
+
"@kubb/plugin-oas": "3.0.0-beta.1",
|
|
82
|
+
"@kubb/plugin-ts": "3.0.0-beta.1",
|
|
83
|
+
"@kubb/react": "3.0.0-beta.1"
|
|
77
84
|
},
|
|
78
85
|
"devDependencies": {
|
|
79
|
-
"@types/react": "^18.3.
|
|
80
|
-
"@types/react-dom": "^18.3.0",
|
|
86
|
+
"@types/react": "^18.3.11",
|
|
81
87
|
"react": "^18.3.1",
|
|
82
|
-
"tsup": "^8.
|
|
83
|
-
"typescript": "^5.
|
|
84
|
-
"@kubb/config-
|
|
85
|
-
"@kubb/config-
|
|
86
|
-
"@kubb/config-tsup": "3.0.0-alpha.9"
|
|
88
|
+
"tsup": "^8.3.0",
|
|
89
|
+
"typescript": "^5.6.2",
|
|
90
|
+
"@kubb/config-ts": "3.0.0-beta.1",
|
|
91
|
+
"@kubb/config-tsup": "3.0.0-beta.1"
|
|
87
92
|
},
|
|
88
93
|
"peerDependencies": {
|
|
89
|
-
"@kubb/react": "3.0.0-
|
|
94
|
+
"@kubb/react": "3.0.0-beta.1"
|
|
90
95
|
},
|
|
91
96
|
"engines": {
|
|
92
97
|
"node": ">=20"
|
|
@@ -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
|
+
}
|
package/src/components/Mock.tsx
CHANGED
|
@@ -1,38 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { File, useApp } from '@kubb/react'
|
|
3
|
-
import { pluginFakerName } from '@kubb/plugin-faker'
|
|
4
|
-
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
1
|
+
import { File } from '@kubb/react'
|
|
5
2
|
|
|
6
3
|
import type { HttpMethod } from '@kubb/oas'
|
|
7
4
|
import type { ReactNode } from 'react'
|
|
8
|
-
import type { FileMeta, PluginMsw } from '../types.ts'
|
|
9
5
|
|
|
10
|
-
type
|
|
6
|
+
type Props = {
|
|
11
7
|
/**
|
|
12
8
|
* Name of the function
|
|
13
9
|
*/
|
|
14
10
|
name: string
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
*/
|
|
11
|
+
fakerName: string
|
|
12
|
+
url: string
|
|
18
13
|
method: HttpMethod
|
|
19
|
-
/**
|
|
20
|
-
* Path of the mock
|
|
21
|
-
*/
|
|
22
|
-
path: URLPath
|
|
23
|
-
/**
|
|
24
|
-
* Name of the import for the mock(this is a function).
|
|
25
|
-
* @example createPet
|
|
26
|
-
*/
|
|
27
|
-
responseName: string
|
|
28
14
|
}
|
|
29
15
|
|
|
30
|
-
function
|
|
16
|
+
export function Mock({ name, fakerName, url, method }: Props): ReactNode {
|
|
31
17
|
return (
|
|
32
|
-
<File.Source name={name} isExportable
|
|
18
|
+
<File.Source name={name} isIndexable isExportable>
|
|
33
19
|
{`
|
|
34
|
-
export const ${name} = http.${method}('*${
|
|
35
|
-
return new Response(JSON.stringify(${
|
|
20
|
+
export const ${name} = http.${method}('*${url}', function handler(info) {
|
|
21
|
+
return new Response(JSON.stringify(${fakerName}()), {
|
|
36
22
|
headers: {
|
|
37
23
|
'Content-Type': 'application/json',
|
|
38
24
|
},
|
|
@@ -42,67 +28,3 @@ function Template({ name, method, path, responseName }: TemplateProps): ReactNod
|
|
|
42
28
|
</File.Source>
|
|
43
29
|
)
|
|
44
30
|
}
|
|
45
|
-
|
|
46
|
-
const defaultTemplates = { default: Template } as const
|
|
47
|
-
|
|
48
|
-
type Props = {
|
|
49
|
-
/**
|
|
50
|
-
* This will make it possible to override the default behaviour.
|
|
51
|
-
*/
|
|
52
|
-
Template?: React.ComponentType<React.ComponentProps<typeof Template>>
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function Mock({ Template = defaultTemplates.default }: Props): ReactNode {
|
|
56
|
-
const { pluginManager } = useApp<PluginMsw>()
|
|
57
|
-
const { getSchemas, getName } = useOperationManager()
|
|
58
|
-
const operation = useOperation()
|
|
59
|
-
|
|
60
|
-
const schemas = getSchemas(operation)
|
|
61
|
-
const name = getName(operation, { type: 'function' })
|
|
62
|
-
const responseName = pluginManager.resolveName({
|
|
63
|
-
pluginKey: [pluginFakerName],
|
|
64
|
-
name: schemas.response.name,
|
|
65
|
-
type: 'type',
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
return <Template name={name} responseName={responseName} method={operation.method} path={new URLPath(operation.path)} />
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
type FileProps = {
|
|
72
|
-
/**
|
|
73
|
-
* This will make it possible to override the default behaviour.
|
|
74
|
-
*/
|
|
75
|
-
templates?: typeof defaultTemplates
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
Mock.File = function ({ templates = defaultTemplates }: FileProps): ReactNode {
|
|
79
|
-
const {
|
|
80
|
-
pluginManager,
|
|
81
|
-
plugin: {
|
|
82
|
-
options: { extName },
|
|
83
|
-
},
|
|
84
|
-
} = useApp<PluginMsw>()
|
|
85
|
-
const { getSchemas, getFile } = useOperationManager()
|
|
86
|
-
const operation = useOperation()
|
|
87
|
-
|
|
88
|
-
const schemas = getSchemas(operation)
|
|
89
|
-
const file = getFile(operation)
|
|
90
|
-
const fileFaker = getFile(operation, { pluginKey: [pluginFakerName] })
|
|
91
|
-
const responseName = pluginManager.resolveName({
|
|
92
|
-
pluginKey: [pluginFakerName],
|
|
93
|
-
name: schemas.response.name,
|
|
94
|
-
type: 'function',
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
const Template = templates.default
|
|
98
|
-
|
|
99
|
-
return (
|
|
100
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
101
|
-
<File.Import name={['http']} path={'msw'} />
|
|
102
|
-
{fileFaker && responseName && <File.Import name={[responseName]} root={file.path} path={fileFaker.path} />}
|
|
103
|
-
<Mock Template={Template} />
|
|
104
|
-
</File>
|
|
105
|
-
)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
Mock.templates = defaultTemplates
|
package/src/components/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { Mock } from './Mock.tsx'
|
|
2
|
-
export {
|
|
2
|
+
export { Handlers } from './Handlers.tsx'
|
|
@@ -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,38 @@
|
|
|
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 { File, useApp } from '@kubb/react'
|
|
6
|
+
import { Mock } from '../components'
|
|
7
|
+
import type { PluginMsw } from '../types'
|
|
8
|
+
|
|
9
|
+
export const mswGenerator = createReactGenerator<PluginMsw>({
|
|
10
|
+
name: 'msw',
|
|
11
|
+
Operation({ operation }) {
|
|
12
|
+
const {
|
|
13
|
+
plugin: {
|
|
14
|
+
options: { output },
|
|
15
|
+
},
|
|
16
|
+
} = useApp<PluginMsw>()
|
|
17
|
+
const { getSchemaName, getName, getFile } = useOperationManager()
|
|
18
|
+
|
|
19
|
+
const mock = {
|
|
20
|
+
name: getName(operation, { type: 'function' }),
|
|
21
|
+
file: getFile(operation),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const faker = {
|
|
25
|
+
name: getSchemaName(operation, 'response', { pluginKey: [pluginFakerName], type: 'function' }),
|
|
26
|
+
file: getFile(operation, { pluginKey: [pluginFakerName] }),
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<File baseName={mock.file.baseName} path={mock.file.path} meta={mock.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
31
|
+
<File.Import name={['http']} path={'msw'} />
|
|
32
|
+
{faker.file && faker.name && <File.Import name={[faker.name]} root={mock.file.path} path={faker.file.path} />}
|
|
33
|
+
|
|
34
|
+
<Mock name={mock.name} fakerName={faker.name} method={operation.method} url={new URLPath(operation.path).toURLPath()} />
|
|
35
|
+
</File>
|
|
36
|
+
)
|
|
37
|
+
},
|
|
38
|
+
})
|
package/src/plugin.ts
CHANGED
|
@@ -1,45 +1,48 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
|
|
3
|
-
import { FileManager, PluginManager, createPlugin } from '@kubb/core'
|
|
3
|
+
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
4
|
import { camelCase } from '@kubb/core/transformers'
|
|
5
5
|
import { renderTemplate } from '@kubb/core/utils'
|
|
6
|
-
import { pluginOasName } from '@kubb/plugin-oas'
|
|
6
|
+
import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
7
7
|
|
|
8
8
|
import { pluginFakerName } from '@kubb/plugin-faker'
|
|
9
9
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
10
10
|
|
|
11
|
-
import { OperationGenerator } from './OperationGenerator.tsx'
|
|
12
|
-
import { Mock, Operations } from './components/index.ts'
|
|
13
|
-
|
|
14
11
|
import type { Plugin } from '@kubb/core'
|
|
15
12
|
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
13
|
+
import { handlersGenerator, mswGenerator } from './generators'
|
|
16
14
|
import type { PluginMsw } from './types.ts'
|
|
17
15
|
|
|
18
16
|
export const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']
|
|
19
17
|
|
|
20
18
|
export const pluginMsw = createPlugin<PluginMsw>((options) => {
|
|
21
|
-
const {
|
|
22
|
-
|
|
19
|
+
const {
|
|
20
|
+
output = { path: 'handlers', barrelType: 'named' },
|
|
21
|
+
group,
|
|
22
|
+
exclude = [],
|
|
23
|
+
include,
|
|
24
|
+
override = [],
|
|
25
|
+
transformers = {},
|
|
26
|
+
handlers = false,
|
|
27
|
+
generators = [mswGenerator, handlers ? handlersGenerator : undefined].filter(Boolean),
|
|
28
|
+
} = options
|
|
23
29
|
|
|
24
30
|
return {
|
|
25
31
|
name: pluginMswName,
|
|
26
|
-
output: {
|
|
27
|
-
exportType: 'barrelNamed',
|
|
28
|
-
...output,
|
|
29
|
-
},
|
|
30
32
|
options: {
|
|
31
|
-
|
|
32
|
-
templates: {
|
|
33
|
-
operations: Operations.templates,
|
|
34
|
-
mock: Mock.templates,
|
|
35
|
-
...templates,
|
|
36
|
-
},
|
|
33
|
+
output,
|
|
37
34
|
},
|
|
38
35
|
pre: [pluginOasName, pluginTsName, pluginFakerName],
|
|
39
36
|
resolvePath(baseName, pathMode, options) {
|
|
40
37
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
41
38
|
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
42
39
|
|
|
40
|
+
if (options?.tag && group?.type === 'tag') {
|
|
41
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
42
|
+
|
|
43
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
44
|
+
}
|
|
45
|
+
|
|
43
46
|
if (mode === 'single') {
|
|
44
47
|
/**
|
|
45
48
|
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
@@ -48,12 +51,6 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
|
|
|
48
51
|
return path.resolve(root, output.path)
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
if (options?.tag && group?.type === 'tag') {
|
|
52
|
-
const tag = camelCase(options.tag)
|
|
53
|
-
|
|
54
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
54
|
return path.resolve(root, output.path, baseName)
|
|
58
55
|
},
|
|
59
56
|
resolveName(name, type) {
|
|
@@ -61,6 +58,7 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
|
|
|
61
58
|
suffix: type ? 'handler' : undefined,
|
|
62
59
|
isFile: type === 'file',
|
|
63
60
|
})
|
|
61
|
+
|
|
64
62
|
if (type) {
|
|
65
63
|
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
66
64
|
}
|
|
@@ -85,22 +83,21 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
|
|
|
85
83
|
mode,
|
|
86
84
|
})
|
|
87
85
|
|
|
88
|
-
const files = await operationGenerator.build()
|
|
86
|
+
const files = await operationGenerator.build(...generators)
|
|
89
87
|
await this.addFile(...files)
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
89
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
90
|
+
type: output.barrelType ?? 'named',
|
|
91
|
+
root,
|
|
92
|
+
output,
|
|
93
|
+
files: this.fileManager.files,
|
|
94
|
+
meta: {
|
|
95
|
+
pluginKey: this.plugin.key,
|
|
96
|
+
},
|
|
97
|
+
logger: this.logger,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
await this.addFile(...barrelFiles)
|
|
104
101
|
},
|
|
105
102
|
}
|
|
106
103
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,58 +1,16 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type * as KubbFile from '@kubb/fs/types'
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
3
2
|
|
|
4
|
-
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
5
|
-
import type { Mock, Operations } from './components/index.ts'
|
|
6
|
-
|
|
7
|
-
type Templates = {
|
|
8
|
-
operations?: typeof Operations.templates | false
|
|
9
|
-
mock?: typeof Mock.templates | false
|
|
10
|
-
}
|
|
3
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
11
4
|
|
|
12
5
|
export type Options = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* @default 'mocks'
|
|
18
|
-
*/
|
|
19
|
-
path: string
|
|
20
|
-
/**
|
|
21
|
-
* Name to be used for the `export * as {{exportAs}} from './'`
|
|
22
|
-
*/
|
|
23
|
-
exportAs?: string
|
|
24
|
-
/**
|
|
25
|
-
* Add an extension to the generated imports and exports, default it will not use an extension
|
|
26
|
-
*/
|
|
27
|
-
extName?: KubbFile.Extname
|
|
28
|
-
/**
|
|
29
|
-
* Define what needs to exported, here you can also disable the export of barrel files
|
|
30
|
-
* @default `'barrel'`
|
|
31
|
-
*/
|
|
32
|
-
exportType?: 'barrel' | 'barrelNamed' | false
|
|
33
|
-
}
|
|
6
|
+
/**
|
|
7
|
+
* @default 'mocks'
|
|
8
|
+
*/
|
|
9
|
+
output?: Output
|
|
34
10
|
/**
|
|
35
11
|
* Group the MSW mocks based on the provided name.
|
|
36
12
|
*/
|
|
37
|
-
group?:
|
|
38
|
-
/**
|
|
39
|
-
* Tag will group based on the operation tag inside the Swagger file
|
|
40
|
-
*/
|
|
41
|
-
type: 'tag'
|
|
42
|
-
/**
|
|
43
|
-
* Relative path to save the grouped MSW mocks.
|
|
44
|
-
*
|
|
45
|
-
* `{{tag}}` will be replaced by the current tagName.
|
|
46
|
-
* @example `${output}/{{tag}}Controller` => `mocks/PetController`
|
|
47
|
-
* @default `${output}/{{tag}}Controller`
|
|
48
|
-
*/
|
|
49
|
-
output?: string
|
|
50
|
-
/**
|
|
51
|
-
* Name to be used for the `export * as {{exportAs}} from './`
|
|
52
|
-
* @default `"{{tag}}Handlers"`
|
|
53
|
-
*/
|
|
54
|
-
exportAs?: string
|
|
55
|
-
}
|
|
13
|
+
group?: Group
|
|
56
14
|
/**
|
|
57
15
|
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
58
16
|
*/
|
|
@@ -72,18 +30,17 @@ export type Options = {
|
|
|
72
30
|
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
73
31
|
}
|
|
74
32
|
/**
|
|
75
|
-
*
|
|
33
|
+
* Create `handlers.ts` file with all handlers grouped by methods.
|
|
34
|
+
* @default `false`
|
|
35
|
+
*/
|
|
36
|
+
handlers?: boolean
|
|
37
|
+
/**
|
|
38
|
+
* Define some generators next to the msw generators
|
|
76
39
|
*/
|
|
77
|
-
|
|
40
|
+
generators?: Array<Generator<PluginMsw>>
|
|
78
41
|
}
|
|
79
42
|
type ResolvedOptions = {
|
|
80
|
-
|
|
81
|
-
templates: NonNullable<Templates>
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export type FileMeta = {
|
|
85
|
-
pluginKey?: Plugin['key']
|
|
86
|
-
tag?: string
|
|
43
|
+
output: Output
|
|
87
44
|
}
|
|
88
45
|
|
|
89
46
|
export type PluginMsw = PluginFactoryOptions<'plugin-msw', Options, ResolvedOptions, never, ResolvePathOptions>
|
package/dist/chunk-3KHLTEUD.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
// src/components/Mock.tsx
|
|
2
|
-
import { URLPath } from "@kubb/core/utils";
|
|
3
|
-
import { File, useApp } from "@kubb/react";
|
|
4
|
-
import { pluginFakerName } from "@kubb/plugin-faker";
|
|
5
|
-
import { useOperation, useOperationManager } from "@kubb/plugin-oas/hooks";
|
|
6
|
-
import { jsx, jsxs } from "@kubb/react/jsx-runtime";
|
|
7
|
-
function Template({ name, method, path, responseName }) {
|
|
8
|
-
return /* @__PURE__ */ jsx(File.Source, { name, isExportable: true, isIndexable: true, children: `
|
|
9
|
-
export const ${name} = http.${method}('*${path.toURLPath()}', function handler(info) {
|
|
10
|
-
return new Response(JSON.stringify(${responseName}()), {
|
|
11
|
-
headers: {
|
|
12
|
-
'Content-Type': 'application/json',
|
|
13
|
-
},
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
` });
|
|
17
|
-
}
|
|
18
|
-
var defaultTemplates = { default: Template };
|
|
19
|
-
function Mock({ Template: Template3 = defaultTemplates.default }) {
|
|
20
|
-
const { pluginManager } = useApp();
|
|
21
|
-
const { getSchemas, getName } = useOperationManager();
|
|
22
|
-
const operation = useOperation();
|
|
23
|
-
const schemas = getSchemas(operation);
|
|
24
|
-
const name = getName(operation, { type: "function" });
|
|
25
|
-
const responseName = pluginManager.resolveName({
|
|
26
|
-
pluginKey: [pluginFakerName],
|
|
27
|
-
name: schemas.response.name,
|
|
28
|
-
type: "type"
|
|
29
|
-
});
|
|
30
|
-
return /* @__PURE__ */ jsx(Template3, { name, responseName, method: operation.method, path: new URLPath(operation.path) });
|
|
31
|
-
}
|
|
32
|
-
Mock.File = function({ templates = defaultTemplates }) {
|
|
33
|
-
const {
|
|
34
|
-
pluginManager,
|
|
35
|
-
plugin: {
|
|
36
|
-
options: { extName }
|
|
37
|
-
}
|
|
38
|
-
} = useApp();
|
|
39
|
-
const { getSchemas, getFile } = useOperationManager();
|
|
40
|
-
const operation = useOperation();
|
|
41
|
-
const schemas = getSchemas(operation);
|
|
42
|
-
const file = getFile(operation);
|
|
43
|
-
const fileFaker = getFile(operation, { pluginKey: [pluginFakerName] });
|
|
44
|
-
const responseName = pluginManager.resolveName({
|
|
45
|
-
pluginKey: [pluginFakerName],
|
|
46
|
-
name: schemas.response.name,
|
|
47
|
-
type: "function"
|
|
48
|
-
});
|
|
49
|
-
const Template3 = templates.default;
|
|
50
|
-
return /* @__PURE__ */ jsxs(File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
51
|
-
/* @__PURE__ */ jsx(File.Import, { name: ["http"], path: "msw" }),
|
|
52
|
-
fileFaker && responseName && /* @__PURE__ */ jsx(File.Import, { name: [responseName], root: file.path, path: fileFaker.path }),
|
|
53
|
-
/* @__PURE__ */ jsx(Mock, { Template: Template3 })
|
|
54
|
-
] });
|
|
55
|
-
};
|
|
56
|
-
Mock.templates = defaultTemplates;
|
|
57
|
-
|
|
58
|
-
// src/components/Operations.tsx
|
|
59
|
-
import { useOperationManager as useOperationManager2, useOperations } from "@kubb/plugin-oas/hooks";
|
|
60
|
-
import { File as File2, useApp as useApp2 } from "@kubb/react";
|
|
61
|
-
import { jsx as jsx2, jsxs as jsxs2 } from "@kubb/react/jsx-runtime";
|
|
62
|
-
function Template2({ name, handlers }) {
|
|
63
|
-
return /* @__PURE__ */ jsx2(File2.Source, { name, isExportable: true, isIndexable: true, children: `export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, "")} as const` });
|
|
64
|
-
}
|
|
65
|
-
function RootTemplate({ children }) {
|
|
66
|
-
const {
|
|
67
|
-
pluginManager,
|
|
68
|
-
plugin: { key: pluginKey }
|
|
69
|
-
} = useApp2();
|
|
70
|
-
const { getName, getFile } = useOperationManager2();
|
|
71
|
-
const file = pluginManager.getFile({ name: "handlers", extName: ".ts", pluginKey });
|
|
72
|
-
const operations = useOperations();
|
|
73
|
-
const imports = operations.map((operation) => {
|
|
74
|
-
const operationFile = getFile(operation, { pluginKey });
|
|
75
|
-
const operationName = getName(operation, { pluginKey, type: "function" });
|
|
76
|
-
return /* @__PURE__ */ jsx2(File2.Import, { name: [operationName], root: file.path, path: operationFile.path }, operationFile.path);
|
|
77
|
-
}).filter(Boolean);
|
|
78
|
-
return /* @__PURE__ */ jsxs2(File2, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
79
|
-
imports,
|
|
80
|
-
children
|
|
81
|
-
] });
|
|
82
|
-
}
|
|
83
|
-
var defaultTemplates2 = { default: Template2, root: RootTemplate };
|
|
84
|
-
function Operations({ Template: Template3 = defaultTemplates2.default }) {
|
|
85
|
-
const {
|
|
86
|
-
plugin: { key: pluginKey }
|
|
87
|
-
} = useApp2();
|
|
88
|
-
const operations = useOperations();
|
|
89
|
-
const { getName } = useOperationManager2();
|
|
90
|
-
return /* @__PURE__ */ jsx2(Template3, { name: "handlers", handlers: operations.map((operation) => getName(operation, { type: "function", pluginKey })) });
|
|
91
|
-
}
|
|
92
|
-
Operations.File = function(props) {
|
|
93
|
-
const templates = { ...defaultTemplates2, ...props.templates };
|
|
94
|
-
const Template3 = templates.default;
|
|
95
|
-
const RootTemplate2 = templates.root;
|
|
96
|
-
return /* @__PURE__ */ jsx2(RootTemplate2, { children: /* @__PURE__ */ jsx2(Operations, { Template: Template3 }) });
|
|
97
|
-
};
|
|
98
|
-
Operations.templates = defaultTemplates2;
|
|
99
|
-
|
|
100
|
-
export {
|
|
101
|
-
Mock,
|
|
102
|
-
Operations
|
|
103
|
-
};
|
|
104
|
-
//# sourceMappingURL=chunk-3KHLTEUD.js.map
|