@kubb/plugin-faker 3.0.0-alpha.3 → 3.0.0-alpha.31
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 +13 -4
- package/dist/chunk-HTZ2R6MK.js +305 -0
- package/dist/chunk-HTZ2R6MK.js.map +1 -0
- package/dist/chunk-KBSFZ5OP.cjs +311 -0
- package/dist/chunk-KBSFZ5OP.cjs.map +1 -0
- package/dist/chunk-QFJUM25Z.js +113 -0
- package/dist/chunk-QFJUM25Z.js.map +1 -0
- package/dist/chunk-U6LOSTOE.cjs +115 -0
- package/dist/chunk-U6LOSTOE.cjs.map +1 -0
- package/dist/components.cjs +6 -565
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +15 -18
- package/dist/components.d.ts +15 -18
- package/dist/components.js +2 -570
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +13 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +8 -0
- package/dist/generators.d.ts +8 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +48 -505
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -125
- package/dist/index.d.ts +4 -125
- package/dist/index.js +35 -499
- package/dist/index.js.map +1 -1
- package/dist/types-C1bqlbxh.d.cts +94 -0
- package/dist/types-C1bqlbxh.d.ts +94 -0
- package/package.json +20 -16
- package/src/components/Faker.tsx +80 -0
- package/src/components/index.ts +1 -2
- package/src/generators/__snapshots__/createPet.ts +26 -0
- package/src/generators/__snapshots__/createPetSeed.ts +30 -0
- package/src/generators/__snapshots__/createPetUnknownTypeAny.ts +26 -0
- package/src/generators/__snapshots__/deletePet.ts +3 -0
- package/src/generators/__snapshots__/enumNames.ts +5 -0
- package/src/generators/__snapshots__/enumVarNames.ts +5 -0
- package/src/generators/__snapshots__/getPets.ts +29 -0
- package/src/generators/__snapshots__/pet.ts +8 -0
- package/src/generators/__snapshots__/petWithDateString.ts +8 -0
- package/src/generators/__snapshots__/petWithDayjs.ts +9 -0
- package/src/generators/__snapshots__/petWithMapper.ts +8 -0
- package/src/generators/__snapshots__/petWithRandExp.ts +9 -0
- package/src/generators/__snapshots__/pets.ts +8 -0
- package/src/generators/__snapshots__/showPetById.ts +29 -0
- package/src/generators/fakerGenerator.tsx +140 -0
- package/src/generators/index.ts +1 -0
- package/src/parser/index.ts +25 -14
- package/src/plugin.ts +26 -43
- package/src/types.ts +28 -67
- package/src/OperationGenerator.tsx +0 -31
- package/src/SchemaGenerator.tsx +0 -31
- package/src/components/OperationSchema.tsx +0 -84
- package/src/components/Schema.tsx +0 -146
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { faker } from "@faker-js/faker";
|
|
2
|
+
|
|
3
|
+
export function showPetByIdPathParams(data: NonNullable<Partial<ShowPetByIdPathParams>> = {}) {
|
|
4
|
+
return {
|
|
5
|
+
...{ "petId": faker.string.alpha(), "testId": faker.string.alpha() },
|
|
6
|
+
...data
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @description Expected response to a valid request
|
|
12
|
+
*/
|
|
13
|
+
export function showPetById200() {
|
|
14
|
+
return pet();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @description unexpected error
|
|
19
|
+
*/
|
|
20
|
+
export function showPetByIdError() {
|
|
21
|
+
return error();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @description Expected response to a valid request
|
|
26
|
+
*/
|
|
27
|
+
export function showPetByIdQueryResponse() {
|
|
28
|
+
return pet();
|
|
29
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { type OperationSchema as OperationSchemaType, SchemaGenerator, createReactGenerator, schemaKeywords } from '@kubb/plugin-oas'
|
|
2
|
+
import { Oas } from '@kubb/plugin-oas/components'
|
|
3
|
+
import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
|
|
4
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
5
|
+
import { File, useApp } from '@kubb/react'
|
|
6
|
+
import { Faker } from '../components'
|
|
7
|
+
import type { PluginFaker } from '../types'
|
|
8
|
+
|
|
9
|
+
export const fakerGenerator = createReactGenerator<PluginFaker>({
|
|
10
|
+
name: 'faker',
|
|
11
|
+
Operation({ operation, options }) {
|
|
12
|
+
const { dateParser, regexGenerator, seed, mapper } = options
|
|
13
|
+
|
|
14
|
+
const { plugin, pluginManager, mode } = useApp<PluginFaker>()
|
|
15
|
+
const oas = useOas()
|
|
16
|
+
const { getSchemas, getFile } = useOperationManager()
|
|
17
|
+
const schemaManager = useSchemaManager()
|
|
18
|
+
|
|
19
|
+
const file = getFile(operation)
|
|
20
|
+
const schemas = getSchemas(operation)
|
|
21
|
+
const schemaGenerator = new SchemaGenerator(options, {
|
|
22
|
+
oas,
|
|
23
|
+
plugin,
|
|
24
|
+
pluginManager,
|
|
25
|
+
mode,
|
|
26
|
+
override: options.override,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
|
|
30
|
+
.flat()
|
|
31
|
+
.filter(Boolean)
|
|
32
|
+
|
|
33
|
+
const mapOperationSchema = ({ name, schema, description, ...options }: OperationSchemaType, i: number) => {
|
|
34
|
+
const tree = schemaGenerator.parse({ schema, name })
|
|
35
|
+
const imports = schemaManager.getImports(tree)
|
|
36
|
+
|
|
37
|
+
const faker = {
|
|
38
|
+
name: schemaManager.getName(name, { type: 'function' }),
|
|
39
|
+
file: schemaManager.getFile(name),
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const type = {
|
|
43
|
+
name: schemaManager.getName(name, { type: 'type', pluginKey: [pluginTsName] }),
|
|
44
|
+
file: schemaManager.getFile(options.operationName || name, { pluginKey: [pluginTsName], tag: options.operation?.getTags()[0]?.name }),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const canOverride = tree.some(
|
|
48
|
+
({ keyword }) =>
|
|
49
|
+
keyword === schemaKeywords.array ||
|
|
50
|
+
keyword === schemaKeywords.and ||
|
|
51
|
+
keyword === schemaKeywords.object ||
|
|
52
|
+
keyword === schemaKeywords.union ||
|
|
53
|
+
keyword === schemaKeywords.tuple,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<Oas.Schema key={i} name={name} value={schema} tree={tree}>
|
|
58
|
+
{canOverride && <File.Import isTypeOnly root={file.path} path={type.file.path} name={[type.name]} />}
|
|
59
|
+
{imports.map((imp, index) => (
|
|
60
|
+
<File.Import key={index} root={file.path} path={imp.path} name={imp.name} />
|
|
61
|
+
))}
|
|
62
|
+
<Faker
|
|
63
|
+
name={faker.name}
|
|
64
|
+
typeName={type.name}
|
|
65
|
+
description={description}
|
|
66
|
+
tree={tree}
|
|
67
|
+
regexGenerator={regexGenerator}
|
|
68
|
+
dateParser={dateParser}
|
|
69
|
+
mapper={mapper}
|
|
70
|
+
seed={seed}
|
|
71
|
+
canOverride={canOverride}
|
|
72
|
+
/>
|
|
73
|
+
</Oas.Schema>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<File baseName={file.baseName} path={file.path} meta={file.meta} banner={plugin.options.output?.banner} footer={plugin.options.output?.footer}>
|
|
79
|
+
<File.Import name={['faker']} path="@faker-js/faker" />
|
|
80
|
+
{regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
|
|
81
|
+
{dateParser !== 'faker' && <File.Import path={dateParser} name={dateParser} />}
|
|
82
|
+
{operationSchemas.map(mapOperationSchema)}
|
|
83
|
+
</File>
|
|
84
|
+
)
|
|
85
|
+
},
|
|
86
|
+
Schema({ schema, options }) {
|
|
87
|
+
const { dateParser, regexGenerator, seed, mapper } = options
|
|
88
|
+
|
|
89
|
+
const { getName, getFile, getImports } = useSchemaManager()
|
|
90
|
+
const {
|
|
91
|
+
plugin: {
|
|
92
|
+
options: { output },
|
|
93
|
+
},
|
|
94
|
+
} = useApp<PluginFaker>()
|
|
95
|
+
const imports = getImports(schema.tree)
|
|
96
|
+
|
|
97
|
+
const faker = {
|
|
98
|
+
name: getName(schema.name, { type: 'function' }),
|
|
99
|
+
file: getFile(schema.name),
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const type = {
|
|
103
|
+
name: getName(schema.name, { type: 'type', pluginKey: [pluginTsName] }),
|
|
104
|
+
file: getFile(schema.name, { pluginKey: [pluginTsName] }),
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const canOverride = schema.tree.some(
|
|
108
|
+
({ keyword }) =>
|
|
109
|
+
keyword === schemaKeywords.array ||
|
|
110
|
+
keyword === schemaKeywords.and ||
|
|
111
|
+
keyword === schemaKeywords.object ||
|
|
112
|
+
keyword === schemaKeywords.union ||
|
|
113
|
+
keyword === schemaKeywords.tuple,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<File baseName={faker.file.baseName} path={faker.file.path} meta={faker.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
118
|
+
<File.Import name={['faker']} path="@faker-js/faker" />
|
|
119
|
+
{regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
|
|
120
|
+
{dateParser !== 'faker' && <File.Import path={dateParser} name={dateParser} />}
|
|
121
|
+
<File.Import isTypeOnly root={faker.file.path} path={type.file.path} name={[type.name]} />
|
|
122
|
+
{imports.map((imp, index) => (
|
|
123
|
+
<File.Import key={index} root={faker.file.path} path={imp.path} name={imp.name} />
|
|
124
|
+
))}
|
|
125
|
+
|
|
126
|
+
<Faker
|
|
127
|
+
name={faker.name}
|
|
128
|
+
typeName={type.name}
|
|
129
|
+
description={schema.value.description}
|
|
130
|
+
tree={schema.tree}
|
|
131
|
+
regexGenerator={regexGenerator}
|
|
132
|
+
dateParser={dateParser}
|
|
133
|
+
mapper={mapper}
|
|
134
|
+
seed={seed}
|
|
135
|
+
canOverride={canOverride}
|
|
136
|
+
/>
|
|
137
|
+
</File>
|
|
138
|
+
)
|
|
139
|
+
},
|
|
140
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { fakerGenerator } from './fakerGenerator.tsx'
|
package/src/parser/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { SchemaGenerator, isKeyword, schemaKeywords } from '@kubb/plugin-oas'
|
|
|
4
4
|
import type { Schema, SchemaKeywordBase, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
|
|
5
5
|
import type { Options } from '../types.ts'
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const fakerKeywordMapper = {
|
|
8
8
|
any: () => 'undefined',
|
|
9
9
|
unknown: () => 'unknown',
|
|
10
10
|
number: (min?: number, max?: number) => {
|
|
@@ -68,13 +68,18 @@ export const fakerKeywordMapper = {
|
|
|
68
68
|
* Type `'string'` ISO date format (YYYY-MM-DD)
|
|
69
69
|
* @default ISO date format (YYYY-MM-DD)
|
|
70
70
|
*/
|
|
71
|
-
date: (type: 'date' | 'string' = 'string', parser
|
|
71
|
+
date: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {
|
|
72
72
|
if (type === 'string') {
|
|
73
|
-
if (parser) {
|
|
73
|
+
if (parser !== 'faker') {
|
|
74
74
|
return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`
|
|
75
75
|
}
|
|
76
76
|
return 'faker.date.anytime().toString()'
|
|
77
77
|
}
|
|
78
|
+
|
|
79
|
+
if (parser !== 'faker') {
|
|
80
|
+
throw new Error(`type '${type}' and parser '${parser}' can not work together`)
|
|
81
|
+
}
|
|
82
|
+
|
|
78
83
|
return 'faker.date.anytime()'
|
|
79
84
|
},
|
|
80
85
|
/**
|
|
@@ -82,13 +87,18 @@ export const fakerKeywordMapper = {
|
|
|
82
87
|
* Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
|
|
83
88
|
* @default ISO time format (HH:mm:ss[.SSSSSS])
|
|
84
89
|
*/
|
|
85
|
-
time: (type: 'date' | 'string' = 'string', parser
|
|
90
|
+
time: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {
|
|
86
91
|
if (type === 'string') {
|
|
87
|
-
if (parser) {
|
|
92
|
+
if (parser !== 'faker') {
|
|
88
93
|
return `${parser}(faker.date.anytime()).format("HH:mm:ss")`
|
|
89
94
|
}
|
|
90
95
|
return 'faker.date.anytime().toString()'
|
|
91
96
|
}
|
|
97
|
+
|
|
98
|
+
if (parser !== 'faker') {
|
|
99
|
+
throw new Error(`type '${type}' and parser '${parser}' can not work together`)
|
|
100
|
+
}
|
|
101
|
+
|
|
92
102
|
return 'faker.date.anytime()'
|
|
93
103
|
},
|
|
94
104
|
uuid: () => 'faker.string.uuid()',
|
|
@@ -107,7 +117,7 @@ export const fakerKeywordMapper = {
|
|
|
107
117
|
lastName: () => 'faker.person.lastName()',
|
|
108
118
|
password: () => 'faker.internet.password()',
|
|
109
119
|
phone: () => 'faker.phone.number()',
|
|
110
|
-
blob: () => 'faker.image.
|
|
120
|
+
blob: () => 'faker.image.url() as unknown as Blob',
|
|
111
121
|
default: undefined,
|
|
112
122
|
describe: undefined,
|
|
113
123
|
const: (value?: string | number) => (value as string) ?? '',
|
|
@@ -117,6 +127,7 @@ export const fakerKeywordMapper = {
|
|
|
117
127
|
nullish: undefined,
|
|
118
128
|
optional: undefined,
|
|
119
129
|
readOnly: undefined,
|
|
130
|
+
writeOnly: undefined,
|
|
120
131
|
strict: undefined,
|
|
121
132
|
deprecated: undefined,
|
|
122
133
|
example: undefined,
|
|
@@ -155,7 +166,7 @@ type ParserOptions = {
|
|
|
155
166
|
|
|
156
167
|
seed?: number | number[]
|
|
157
168
|
regexGenerator?: 'faker' | 'randexp'
|
|
158
|
-
|
|
169
|
+
canOverride?: boolean
|
|
159
170
|
dateParser?: Options['dateParser']
|
|
160
171
|
mapper?: Record<string, string>
|
|
161
172
|
}
|
|
@@ -168,15 +179,15 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
168
179
|
}
|
|
169
180
|
|
|
170
181
|
if (isKeyword(current, schemaKeywords.union)) {
|
|
171
|
-
return fakerKeywordMapper.union(current.args.map((schema) => parse(current, schema, { ...options,
|
|
182
|
+
return fakerKeywordMapper.union(current.args.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
172
183
|
}
|
|
173
184
|
|
|
174
185
|
if (isKeyword(current, schemaKeywords.and)) {
|
|
175
|
-
return fakerKeywordMapper.and(current.args.map((schema) => parse(current, schema, { ...options,
|
|
186
|
+
return fakerKeywordMapper.and(current.args.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
176
187
|
}
|
|
177
188
|
|
|
178
189
|
if (isKeyword(current, schemaKeywords.array)) {
|
|
179
|
-
return fakerKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, { ...options,
|
|
190
|
+
return fakerKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
180
191
|
}
|
|
181
192
|
|
|
182
193
|
if (isKeyword(current, schemaKeywords.enum)) {
|
|
@@ -195,7 +206,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
195
206
|
throw new Error(`Name not defined for keyword ${current.keyword}`)
|
|
196
207
|
}
|
|
197
208
|
|
|
198
|
-
if (options.
|
|
209
|
+
if (options.canOverride) {
|
|
199
210
|
return `${current.args.name}(data)`
|
|
200
211
|
}
|
|
201
212
|
|
|
@@ -220,7 +231,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
220
231
|
return `"${name}": ${joinItems(
|
|
221
232
|
schemas
|
|
222
233
|
.sort(schemaKeywordsorter)
|
|
223
|
-
.map((schema) => parse(current, schema, { ...options,
|
|
234
|
+
.map((schema) => parse(current, schema, { ...options, canOverride: false }))
|
|
224
235
|
.filter(Boolean),
|
|
225
236
|
)}`
|
|
226
237
|
})
|
|
@@ -231,10 +242,10 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
231
242
|
|
|
232
243
|
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
233
244
|
if (Array.isArray(current.args.items)) {
|
|
234
|
-
return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, { ...options,
|
|
245
|
+
return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
235
246
|
}
|
|
236
247
|
|
|
237
|
-
return parse(current, current.args.items, { ...options,
|
|
248
|
+
return parse(current, current.args.items, { ...options, canOverride: false })
|
|
238
249
|
}
|
|
239
250
|
|
|
240
251
|
if (isKeyword(current, schemaKeywords.const)) {
|
package/src/plugin.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
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'
|
|
7
|
-
import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
|
|
8
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
|
+
import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
9
7
|
|
|
10
|
-
import {
|
|
11
|
-
import { SchemaGenerator } from './SchemaGenerator.tsx'
|
|
8
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
12
9
|
|
|
13
10
|
import type { Plugin } from '@kubb/core'
|
|
14
11
|
import type { PluginOas } from '@kubb/plugin-oas'
|
|
12
|
+
import { fakerGenerator } from './generators/fakerGenerator.tsx'
|
|
15
13
|
import type { PluginFaker } from './types.ts'
|
|
16
14
|
|
|
17
15
|
export const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']
|
|
18
16
|
|
|
19
17
|
export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
20
18
|
const {
|
|
21
|
-
output = { path: 'mocks' },
|
|
19
|
+
output = { path: 'mocks', barrelType: 'named' },
|
|
22
20
|
seed,
|
|
23
21
|
group,
|
|
24
22
|
exclude = [],
|
|
@@ -26,20 +24,20 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
26
24
|
override = [],
|
|
27
25
|
transformers = {},
|
|
28
26
|
mapper = {},
|
|
29
|
-
dateType = 'string',
|
|
30
27
|
unknownType = 'any',
|
|
31
|
-
|
|
28
|
+
dateType = 'string',
|
|
29
|
+
dateParser = 'faker',
|
|
30
|
+
generators = [fakerGenerator].filter(Boolean),
|
|
32
31
|
regexGenerator = 'faker',
|
|
33
32
|
} = options
|
|
34
|
-
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
35
33
|
|
|
36
34
|
return {
|
|
37
35
|
name: pluginFakerName,
|
|
38
36
|
options: {
|
|
39
|
-
|
|
37
|
+
output,
|
|
40
38
|
transformers,
|
|
41
|
-
dateType,
|
|
42
39
|
seed,
|
|
40
|
+
dateType,
|
|
43
41
|
unknownType,
|
|
44
42
|
dateParser,
|
|
45
43
|
mapper,
|
|
@@ -51,6 +49,12 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
51
49
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
52
50
|
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
53
51
|
|
|
52
|
+
if (options?.tag && group?.type === 'tag') {
|
|
53
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
54
|
+
|
|
55
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
56
|
+
}
|
|
57
|
+
|
|
54
58
|
if (mode === 'single') {
|
|
55
59
|
/**
|
|
56
60
|
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
@@ -59,12 +63,6 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
59
63
|
return path.resolve(root, output.path)
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
if (options?.tag && group?.type === 'tag') {
|
|
63
|
-
const tag = camelCase(options.tag)
|
|
64
|
-
|
|
65
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
66
|
return path.resolve(root, output.path, baseName)
|
|
69
67
|
},
|
|
70
68
|
resolveName(name, type) {
|
|
@@ -97,7 +95,7 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
97
95
|
output: output.path,
|
|
98
96
|
})
|
|
99
97
|
|
|
100
|
-
const schemaFiles = await schemaGenerator.build()
|
|
98
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
101
99
|
await this.addFile(...schemaFiles)
|
|
102
100
|
|
|
103
101
|
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
@@ -111,36 +109,21 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
111
109
|
mode,
|
|
112
110
|
})
|
|
113
111
|
|
|
114
|
-
const operationFiles = await operationGenerator.build()
|
|
112
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
115
113
|
await this.addFile(...operationFiles)
|
|
116
|
-
},
|
|
117
|
-
async buildEnd() {
|
|
118
|
-
if (this.config.output.write === false) {
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
114
|
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
if (group?.type === 'tag') {
|
|
125
|
-
const rootFiles = await getGroupedByTagFiles({
|
|
126
|
-
logger: this.logger,
|
|
127
|
-
files: this.fileManager.files,
|
|
128
|
-
plugin: this.plugin,
|
|
129
|
-
template,
|
|
130
|
-
exportAs: group.exportAs || '{{tag}}Mocks',
|
|
131
|
-
root,
|
|
132
|
-
output,
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
await this.addFile(...rootFiles)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
await this.fileManager.addIndexes({
|
|
115
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
116
|
+
type: output.barrelType ?? 'named',
|
|
139
117
|
root,
|
|
140
118
|
output,
|
|
141
|
-
|
|
119
|
+
files: this.fileManager.files,
|
|
120
|
+
meta: {
|
|
121
|
+
pluginKey: this.plugin.key,
|
|
122
|
+
},
|
|
142
123
|
logger: this.logger,
|
|
143
124
|
})
|
|
125
|
+
|
|
126
|
+
await this.addFile(...barrelFiles)
|
|
144
127
|
},
|
|
145
128
|
}
|
|
146
129
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,54 +1,17 @@
|
|
|
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
3
|
import type { SchemaObject } from '@kubb/oas'
|
|
5
|
-
import type { Exclude, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
|
|
4
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
|
|
6
5
|
|
|
7
6
|
export type Options = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @default 'mocks'
|
|
13
|
-
*/
|
|
14
|
-
path: string
|
|
15
|
-
/**
|
|
16
|
-
* Name to be used for the `export * as {{exportAs}} from './'`
|
|
17
|
-
*/
|
|
18
|
-
exportAs?: string
|
|
19
|
-
/**
|
|
20
|
-
* Add an extension to the generated imports and exports, default it will not use an extension
|
|
21
|
-
*/
|
|
22
|
-
extName?: KubbFile.Extname
|
|
23
|
-
/**
|
|
24
|
-
* Define what needs to exported, here you can also disable the export of barrel files
|
|
25
|
-
* @default `'barrel'`
|
|
26
|
-
*/
|
|
27
|
-
exportType?: 'barrel' | 'barrelNamed' | false
|
|
28
|
-
}
|
|
29
|
-
|
|
7
|
+
/**
|
|
8
|
+
* @default 'handlers'
|
|
9
|
+
*/
|
|
10
|
+
output?: Output
|
|
30
11
|
/**
|
|
31
12
|
* Group the Faker mocks based on the provided name.
|
|
32
13
|
*/
|
|
33
|
-
group?:
|
|
34
|
-
/**
|
|
35
|
-
* Tag will group based on the operation tag inside the Swagger file
|
|
36
|
-
*/
|
|
37
|
-
type: 'tag'
|
|
38
|
-
/**
|
|
39
|
-
* Relative path to save the grouped Faker mocks.
|
|
40
|
-
*
|
|
41
|
-
* `{{tag}}` will be replaced by the current tagName.
|
|
42
|
-
* @example `${output}/{{tag}}Controller` => `mocks/PetController`
|
|
43
|
-
* @default `${output}/{{tag}}Controller`
|
|
44
|
-
*/
|
|
45
|
-
output?: string
|
|
46
|
-
/**
|
|
47
|
-
* Name to be used for the `export * as {{exportAs}} from './`
|
|
48
|
-
* @default `"{{tag}}Mocks"`
|
|
49
|
-
*/
|
|
50
|
-
exportAs?: string
|
|
51
|
-
}
|
|
14
|
+
group?: Group
|
|
52
15
|
/**
|
|
53
16
|
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
54
17
|
*/
|
|
@@ -74,26 +37,14 @@ export type Options = {
|
|
|
74
37
|
* - Schema with format 'time' will use ISO time format (HH:mm:ss[.SSSSSS])
|
|
75
38
|
* - `'dayjs'` will use `dayjs(faker.date.anytime()).format("HH:mm:ss")`.
|
|
76
39
|
* - `undefined` will use `faker.date.anytime().toString()`
|
|
77
|
-
* * @default
|
|
40
|
+
* * @default 'faker'
|
|
78
41
|
*/
|
|
79
|
-
dateParser?: 'dayjs' | 'moment' | (string & {})
|
|
42
|
+
dateParser?: 'faker' | 'dayjs' | 'moment' | (string & {})
|
|
80
43
|
/**
|
|
81
44
|
* Which type to use when the Swagger/OpenAPI file is not providing more information
|
|
82
45
|
* @default 'any'
|
|
83
46
|
*/
|
|
84
47
|
unknownType?: 'any' | 'unknown'
|
|
85
|
-
transformers?: {
|
|
86
|
-
/**
|
|
87
|
-
* Customize the names based on the type that is provided by the plugin.
|
|
88
|
-
*/
|
|
89
|
-
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
90
|
-
/**
|
|
91
|
-
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
92
|
-
* TODO TODO add docs
|
|
93
|
-
* @beta
|
|
94
|
-
*/
|
|
95
|
-
schema?: (props: { schema?: SchemaObject; name?: string; parentName?: string }, defaultSchemas: Schema[]) => Schema[] | undefined
|
|
96
|
-
}
|
|
97
48
|
/**
|
|
98
49
|
* Choose which generator to use when using Regexp.
|
|
99
50
|
*
|
|
@@ -108,23 +59,33 @@ export type Options = {
|
|
|
108
59
|
* The use of Seed is intended to allow for consistent values in a test.
|
|
109
60
|
*/
|
|
110
61
|
seed?: number | number[]
|
|
62
|
+
transformers?: {
|
|
63
|
+
/**
|
|
64
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
65
|
+
*/
|
|
66
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
67
|
+
/**
|
|
68
|
+
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
69
|
+
* TODO TODO add docs
|
|
70
|
+
* @beta
|
|
71
|
+
*/
|
|
72
|
+
schema?: (props: { schema?: SchemaObject; name?: string; parentName?: string }, defaultSchemas: Schema[]) => Schema[] | undefined
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Define some generators next to the faker generators
|
|
76
|
+
*/
|
|
77
|
+
generators?: Array<Generator<PluginFaker>>
|
|
111
78
|
}
|
|
112
79
|
|
|
113
80
|
type ResolvedOptions = {
|
|
114
|
-
|
|
81
|
+
output: Output
|
|
82
|
+
override: NonNullable<Options['override']>
|
|
115
83
|
dateType: NonNullable<Options['dateType']>
|
|
116
|
-
dateParser: Options['dateParser']
|
|
84
|
+
dateParser: NonNullable<Options['dateParser']>
|
|
117
85
|
unknownType: NonNullable<Options['unknownType']>
|
|
118
86
|
transformers: NonNullable<Options['transformers']>
|
|
119
|
-
override: NonNullable<Options['override']>
|
|
120
87
|
seed: NonNullable<Options['seed']> | undefined
|
|
121
88
|
mapper: NonNullable<Options['mapper']>
|
|
122
89
|
regexGenerator: NonNullable<Options['regexGenerator']>
|
|
123
90
|
}
|
|
124
|
-
|
|
125
|
-
export type FileMeta = {
|
|
126
|
-
pluginKey?: Plugin['key']
|
|
127
|
-
tag?: string
|
|
128
|
-
}
|
|
129
|
-
|
|
130
91
|
export type PluginFaker = PluginFactoryOptions<'plugin-faker', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { OperationGenerator as Generator } from '@kubb/plugin-oas'
|
|
2
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
3
|
-
import { App, createRoot } from '@kubb/react'
|
|
4
|
-
|
|
5
|
-
import { OperationSchema } from './components/OperationSchema.tsx'
|
|
6
|
-
|
|
7
|
-
import type { Operation } from '@kubb/oas'
|
|
8
|
-
import type { OperationMethodResult } from '@kubb/plugin-oas'
|
|
9
|
-
import type { FileMeta, PluginFaker } from './types.ts'
|
|
10
|
-
|
|
11
|
-
export class OperationGenerator extends Generator<PluginFaker['resolvedOptions'], PluginFaker> {
|
|
12
|
-
async operation(operation: Operation, options: PluginFaker['resolvedOptions']): OperationMethodResult<FileMeta> {
|
|
13
|
-
const { oas, pluginManager, plugin, mode } = this.context
|
|
14
|
-
|
|
15
|
-
const root = createRoot({
|
|
16
|
-
logger: pluginManager.logger,
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
root.render(
|
|
20
|
-
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
21
|
-
<Oas oas={oas} operations={[operation]} generator={this}>
|
|
22
|
-
<Oas.Operation operation={operation}>
|
|
23
|
-
<OperationSchema.File />
|
|
24
|
-
</Oas.Operation>
|
|
25
|
-
</Oas>
|
|
26
|
-
</App>,
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
return root.files
|
|
30
|
-
}
|
|
31
|
-
}
|
package/src/SchemaGenerator.tsx
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { SchemaObject } from '@kubb/oas'
|
|
2
|
-
import { SchemaGenerator as Generator } from '@kubb/plugin-oas'
|
|
3
|
-
import type { SchemaMethodResult } from '@kubb/plugin-oas'
|
|
4
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
5
|
-
import { App, createRoot } from '@kubb/react'
|
|
6
|
-
import { Schema } from './components/Schema.tsx'
|
|
7
|
-
import type { FileMeta, PluginFaker } from './types.ts'
|
|
8
|
-
|
|
9
|
-
export class SchemaGenerator extends Generator<PluginFaker['resolvedOptions'], PluginFaker> {
|
|
10
|
-
async schema(name: string, schema: SchemaObject, options: PluginFaker['resolvedOptions']): SchemaMethodResult<FileMeta> {
|
|
11
|
-
const { oas, pluginManager, plugin, mode, output } = this.context
|
|
12
|
-
|
|
13
|
-
const root = createRoot({
|
|
14
|
-
logger: pluginManager.logger,
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const tree = this.parse({ schema, name })
|
|
18
|
-
|
|
19
|
-
root.render(
|
|
20
|
-
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
21
|
-
<Oas oas={oas}>
|
|
22
|
-
<Oas.Schema name={name} value={schema} tree={tree}>
|
|
23
|
-
<Schema.File />
|
|
24
|
-
</Oas.Schema>
|
|
25
|
-
</Oas>
|
|
26
|
-
</App>,
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
return root.files
|
|
30
|
-
}
|
|
31
|
-
}
|