@kubb/plugin-faker 3.0.0-alpha.2 → 3.0.0-alpha.21
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-4AVWJQH5.js +113 -0
- package/dist/chunk-4AVWJQH5.js.map +1 -0
- package/dist/chunk-4I5F76DK.cjs +310 -0
- package/dist/chunk-4I5F76DK.cjs.map +1 -0
- package/dist/chunk-JVUPDWN2.js +304 -0
- package/dist/chunk-JVUPDWN2.js.map +1 -0
- package/dist/chunk-WYCU2YG6.cjs +115 -0
- package/dist/chunk-WYCU2YG6.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 +51 -504
- 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 +34 -494
- package/dist/index.js.map +1 -1
- package/dist/types-dutOIgtG.d.cts +108 -0
- package/dist/types-dutOIgtG.d.ts +108 -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 +24 -14
- package/src/plugin.ts +20 -33
- package/src/types.ts +22 -47
- package/src/OperationGenerator.tsx +0 -30
- package/src/SchemaGenerator.tsx +0 -31
- package/src/components/OperationSchema.tsx +0 -84
- package/src/components/Schema.tsx +0 -146
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { faker } from "@faker-js/faker";
|
|
2
|
+
|
|
3
|
+
export function pet(data: NonNullable<Partial<Pet>> = {}) {
|
|
4
|
+
return {
|
|
5
|
+
...{ "id": faker.string.fromCharacters("abc"), "name": faker.string.alpha({ casing: "lower" }), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), faker.helpers.fromRegExp(new RegExp("\\b[1-9]\\b"))]), "shipDate": faker.date.anytime(), "shipTime": faker.date.anytime() },
|
|
6
|
+
...data
|
|
7
|
+
};
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import RandExp from "randexp";
|
|
2
|
+
import { faker } from "@faker-js/faker";
|
|
3
|
+
|
|
4
|
+
export function pet(data: NonNullable<Partial<Pet>> = {}) {
|
|
5
|
+
return {
|
|
6
|
+
...{ "id": faker.number.int(), "name": faker.string.alpha(), "tag": faker.string.alpha(), "code": faker.helpers.arrayElement<any>([faker.string.alpha(), new RandExp("\\b[1-9]\\b").gen()]), "shipDate": faker.date.anytime(), "shipTime": faker.date.anytime() },
|
|
7
|
+
...data
|
|
8
|
+
};
|
|
9
|
+
}
|
|
@@ -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) ?? '',
|
|
@@ -155,7 +165,7 @@ type ParserOptions = {
|
|
|
155
165
|
|
|
156
166
|
seed?: number | number[]
|
|
157
167
|
regexGenerator?: 'faker' | 'randexp'
|
|
158
|
-
|
|
168
|
+
canOverride?: boolean
|
|
159
169
|
dateParser?: Options['dateParser']
|
|
160
170
|
mapper?: Record<string, string>
|
|
161
171
|
}
|
|
@@ -168,15 +178,15 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
168
178
|
}
|
|
169
179
|
|
|
170
180
|
if (isKeyword(current, schemaKeywords.union)) {
|
|
171
|
-
return fakerKeywordMapper.union(current.args.map((schema) => parse(current, schema, { ...options,
|
|
181
|
+
return fakerKeywordMapper.union(current.args.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
172
182
|
}
|
|
173
183
|
|
|
174
184
|
if (isKeyword(current, schemaKeywords.and)) {
|
|
175
|
-
return fakerKeywordMapper.and(current.args.map((schema) => parse(current, schema, { ...options,
|
|
185
|
+
return fakerKeywordMapper.and(current.args.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
176
186
|
}
|
|
177
187
|
|
|
178
188
|
if (isKeyword(current, schemaKeywords.array)) {
|
|
179
|
-
return fakerKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, { ...options,
|
|
189
|
+
return fakerKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
180
190
|
}
|
|
181
191
|
|
|
182
192
|
if (isKeyword(current, schemaKeywords.enum)) {
|
|
@@ -195,7 +205,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
195
205
|
throw new Error(`Name not defined for keyword ${current.keyword}`)
|
|
196
206
|
}
|
|
197
207
|
|
|
198
|
-
if (options.
|
|
208
|
+
if (options.canOverride) {
|
|
199
209
|
return `${current.args.name}(data)`
|
|
200
210
|
}
|
|
201
211
|
|
|
@@ -220,7 +230,7 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
220
230
|
return `"${name}": ${joinItems(
|
|
221
231
|
schemas
|
|
222
232
|
.sort(schemaKeywordsorter)
|
|
223
|
-
.map((schema) => parse(current, schema, { ...options,
|
|
233
|
+
.map((schema) => parse(current, schema, { ...options, canOverride: false }))
|
|
224
234
|
.filter(Boolean),
|
|
225
235
|
)}`
|
|
226
236
|
})
|
|
@@ -231,10 +241,10 @@ export function parse(parent: Schema | undefined, current: Schema, options: Pars
|
|
|
231
241
|
|
|
232
242
|
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
233
243
|
if (Array.isArray(current.args.items)) {
|
|
234
|
-
return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, { ...options,
|
|
244
|
+
return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, { ...options, canOverride: false })).filter(Boolean))
|
|
235
245
|
}
|
|
236
246
|
|
|
237
|
-
return parse(current, current.args.items, { ...options,
|
|
247
|
+
return parse(current, current.args.items, { ...options, canOverride: false })
|
|
238
248
|
}
|
|
239
249
|
|
|
240
250
|
if (isKeyword(current, schemaKeywords.const)) {
|
package/src/plugin.ts
CHANGED
|
@@ -3,15 +3,13 @@ import path from 'node:path'
|
|
|
3
3
|
import { FileManager, 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']
|
|
@@ -26,9 +24,9 @@ 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',
|
|
32
30
|
regexGenerator = 'faker',
|
|
33
31
|
} = options
|
|
34
32
|
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
@@ -36,10 +34,13 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
36
34
|
return {
|
|
37
35
|
name: pluginFakerName,
|
|
38
36
|
options: {
|
|
39
|
-
|
|
37
|
+
output: {
|
|
38
|
+
exportType: 'barrelNamed',
|
|
39
|
+
...output,
|
|
40
|
+
},
|
|
40
41
|
transformers,
|
|
41
|
-
dateType,
|
|
42
42
|
seed,
|
|
43
|
+
dateType,
|
|
43
44
|
unknownType,
|
|
44
45
|
dateParser,
|
|
45
46
|
mapper,
|
|
@@ -97,7 +98,7 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
97
98
|
output: output.path,
|
|
98
99
|
})
|
|
99
100
|
|
|
100
|
-
const schemaFiles = await schemaGenerator.build()
|
|
101
|
+
const schemaFiles = await schemaGenerator.build(fakerGenerator)
|
|
101
102
|
await this.addFile(...schemaFiles)
|
|
102
103
|
|
|
103
104
|
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
@@ -111,36 +112,22 @@ export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
|
111
112
|
mode,
|
|
112
113
|
})
|
|
113
114
|
|
|
114
|
-
const operationFiles = await operationGenerator.build()
|
|
115
|
+
const operationFiles = await operationGenerator.build(fakerGenerator)
|
|
115
116
|
await this.addFile(...operationFiles)
|
|
116
|
-
},
|
|
117
|
-
async buildEnd() {
|
|
118
|
-
if (this.config.output.write === false) {
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
117
|
|
|
122
|
-
|
|
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',
|
|
118
|
+
if (this.config.output.exportType) {
|
|
119
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
131
120
|
root,
|
|
132
121
|
output,
|
|
122
|
+
files: this.fileManager.files,
|
|
123
|
+
meta: {
|
|
124
|
+
pluginKey: this.plugin.key,
|
|
125
|
+
},
|
|
126
|
+
logger: this.logger,
|
|
133
127
|
})
|
|
134
128
|
|
|
135
|
-
await this.addFile(...
|
|
129
|
+
await this.addFile(...barrelFiles)
|
|
136
130
|
}
|
|
137
|
-
|
|
138
|
-
await this.fileManager.addIndexes({
|
|
139
|
-
root,
|
|
140
|
-
output,
|
|
141
|
-
meta: { pluginKey: this.plugin.key },
|
|
142
|
-
logger: this.logger,
|
|
143
|
-
})
|
|
144
131
|
},
|
|
145
132
|
}
|
|
146
133
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,32 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type * as KubbFile from '@kubb/fs/types'
|
|
1
|
+
import type { Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
3
2
|
|
|
4
3
|
import type { SchemaObject } from '@kubb/oas'
|
|
5
4
|
import type { Exclude, 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
|
*/
|
|
@@ -74,26 +55,14 @@ export type Options = {
|
|
|
74
55
|
* - Schema with format 'time' will use ISO time format (HH:mm:ss[.SSSSSS])
|
|
75
56
|
* - `'dayjs'` will use `dayjs(faker.date.anytime()).format("HH:mm:ss")`.
|
|
76
57
|
* - `undefined` will use `faker.date.anytime().toString()`
|
|
77
|
-
* * @default
|
|
58
|
+
* * @default 'faker'
|
|
78
59
|
*/
|
|
79
|
-
dateParser?: 'dayjs' | 'moment' | (string & {})
|
|
60
|
+
dateParser?: 'faker' | 'dayjs' | 'moment' | (string & {})
|
|
80
61
|
/**
|
|
81
62
|
* Which type to use when the Swagger/OpenAPI file is not providing more information
|
|
82
63
|
* @default 'any'
|
|
83
64
|
*/
|
|
84
65
|
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
66
|
/**
|
|
98
67
|
* Choose which generator to use when using Regexp.
|
|
99
68
|
*
|
|
@@ -108,23 +77,29 @@ export type Options = {
|
|
|
108
77
|
* The use of Seed is intended to allow for consistent values in a test.
|
|
109
78
|
*/
|
|
110
79
|
seed?: number | number[]
|
|
80
|
+
transformers?: {
|
|
81
|
+
/**
|
|
82
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
83
|
+
*/
|
|
84
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
85
|
+
/**
|
|
86
|
+
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
87
|
+
* TODO TODO add docs
|
|
88
|
+
* @beta
|
|
89
|
+
*/
|
|
90
|
+
schema?: (props: { schema?: SchemaObject; name?: string; parentName?: string }, defaultSchemas: Schema[]) => Schema[] | undefined
|
|
91
|
+
}
|
|
111
92
|
}
|
|
112
93
|
|
|
113
94
|
type ResolvedOptions = {
|
|
114
|
-
|
|
95
|
+
output: Output
|
|
96
|
+
override: NonNullable<Options['override']>
|
|
115
97
|
dateType: NonNullable<Options['dateType']>
|
|
116
|
-
dateParser: Options['dateParser']
|
|
98
|
+
dateParser: NonNullable<Options['dateParser']>
|
|
117
99
|
unknownType: NonNullable<Options['unknownType']>
|
|
118
100
|
transformers: NonNullable<Options['transformers']>
|
|
119
|
-
override: NonNullable<Options['override']>
|
|
120
101
|
seed: NonNullable<Options['seed']> | undefined
|
|
121
102
|
mapper: NonNullable<Options['mapper']>
|
|
122
103
|
regexGenerator: NonNullable<Options['regexGenerator']>
|
|
123
104
|
}
|
|
124
|
-
|
|
125
|
-
export type FileMeta = {
|
|
126
|
-
pluginKey?: Plugin['key']
|
|
127
|
-
tag?: string
|
|
128
|
-
}
|
|
129
|
-
|
|
130
105
|
export type PluginFaker = PluginFactoryOptions<'plugin-faker', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
@@ -1,30 +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
|
-
root.render(
|
|
19
|
-
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
20
|
-
<Oas oas={oas} operations={[operation]} generator={this}>
|
|
21
|
-
<Oas.Operation operation={operation}>
|
|
22
|
-
<OperationSchema.File />
|
|
23
|
-
</Oas.Operation>
|
|
24
|
-
</Oas>
|
|
25
|
-
</App>,
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
return root.files
|
|
29
|
-
}
|
|
30
|
-
}
|
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
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { Oas } from '@kubb/plugin-oas/components'
|
|
2
|
-
import { useOas, useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
3
|
-
import { File, Parser, useApp } from '@kubb/react'
|
|
4
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
5
|
-
|
|
6
|
-
import { SchemaGenerator } from '../SchemaGenerator.tsx'
|
|
7
|
-
|
|
8
|
-
import type { OperationSchema as OperationSchemaType } from '@kubb/plugin-oas'
|
|
9
|
-
import type { ReactNode } from 'react'
|
|
10
|
-
import type { FileMeta, PluginFaker } from '../types.ts'
|
|
11
|
-
import { Schema } from './Schema.tsx'
|
|
12
|
-
|
|
13
|
-
type Props = {
|
|
14
|
-
description?: string
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function OperationSchema({ description }: Props): ReactNode {
|
|
18
|
-
return <Schema withData={false} description={description} />
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type FileProps = {}
|
|
22
|
-
|
|
23
|
-
OperationSchema.File = function ({}: FileProps): ReactNode {
|
|
24
|
-
const { plugin, pluginManager, mode } = useApp<PluginFaker>()
|
|
25
|
-
|
|
26
|
-
const oas = useOas()
|
|
27
|
-
const { getSchemas, getFile } = useOperationManager()
|
|
28
|
-
const operation = useOperation()
|
|
29
|
-
|
|
30
|
-
const file = getFile(operation)
|
|
31
|
-
const schemas = getSchemas(operation)
|
|
32
|
-
const generator = new SchemaGenerator(plugin.options, {
|
|
33
|
-
oas,
|
|
34
|
-
plugin,
|
|
35
|
-
pluginManager,
|
|
36
|
-
mode,
|
|
37
|
-
override: plugin.options.override,
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean)
|
|
41
|
-
|
|
42
|
-
const mapItem = ({ name, schema, description, ...options }: OperationSchemaType, i: number) => {
|
|
43
|
-
// used for this.options.typed
|
|
44
|
-
const typeName = pluginManager.resolveName({
|
|
45
|
-
name,
|
|
46
|
-
pluginKey: [pluginTsName],
|
|
47
|
-
type: 'type',
|
|
48
|
-
})
|
|
49
|
-
const typeFileName = pluginManager.resolveName({
|
|
50
|
-
name: options.operationName || name,
|
|
51
|
-
pluginKey: [pluginTsName],
|
|
52
|
-
type: 'file',
|
|
53
|
-
})
|
|
54
|
-
const typePath = pluginManager.resolvePath({
|
|
55
|
-
baseName: typeFileName,
|
|
56
|
-
pluginKey: [pluginTsName],
|
|
57
|
-
options: { tag: options.operation?.getTags()[0]?.name },
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
const tree = generator.parse({ schema, name })
|
|
61
|
-
|
|
62
|
-
return (
|
|
63
|
-
<Oas.Schema key={i} name={name} value={schema} tree={tree}>
|
|
64
|
-
{typeName && typePath && <File.Import extName={plugin.options.extName} isTypeOnly root={file.path} path={typePath} name={[typeName]} />}
|
|
65
|
-
{plugin.options.dateParser && <File.Import path={plugin.options.dateParser} name={plugin.options.dateParser} />}
|
|
66
|
-
|
|
67
|
-
{mode === 'split' && <Oas.Schema.Imports extName={plugin.options.extName} />}
|
|
68
|
-
<File.Source>
|
|
69
|
-
<OperationSchema description={description} />
|
|
70
|
-
</File.Source>
|
|
71
|
-
</Oas.Schema>
|
|
72
|
-
)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return (
|
|
76
|
-
<Parser language="typescript">
|
|
77
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
78
|
-
<File.Import name={['faker']} path="@faker-js/faker" />
|
|
79
|
-
{plugin.options.regexGenerator === 'randexp' && <File.Import name={'RandExp'} path={'randexp'} />}
|
|
80
|
-
{items.map(mapItem)}
|
|
81
|
-
</File>
|
|
82
|
-
</Parser>
|
|
83
|
-
)
|
|
84
|
-
}
|