@kubb/plugin-faker 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-FCIZHQ4X.cjs +339 -0
- package/dist/chunk-FCIZHQ4X.cjs.map +1 -0
- package/dist/chunk-OOL6ZKDZ.cjs +115 -0
- package/dist/chunk-OOL6ZKDZ.cjs.map +1 -0
- package/dist/chunk-PLVSF3YP.js +333 -0
- package/dist/chunk-PLVSF3YP.js.map +1 -0
- package/dist/chunk-ZOJMDISS.js +113 -0
- package/dist/chunk-ZOJMDISS.js.map +1 -0
- package/dist/components.cjs +12 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +20 -0
- package/dist/components.d.ts +20 -0
- package/dist/components.js +3 -0
- package/dist/components.js.map +1 -0
- 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 +115 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/dist/types-Cq8z4Gto.d.cts +95 -0
- package/dist/types-Cq8z4Gto.d.ts +95 -0
- package/package.json +98 -0
- package/src/components/Faker.tsx +82 -0
- package/src/components/index.ts +1 -0
- 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 +26 -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 +26 -0
- package/src/generators/fakerGenerator.tsx +140 -0
- package/src/generators/index.ts +1 -0
- package/src/index.ts +2 -0
- package/src/parser/index.ts +351 -0
- package/src/plugin.ts +128 -0
- package/src/types.ts +92 -0
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
|
2
|
+
import { SchemaGenerator, type SchemaTree, isKeyword, schemaKeywords } from '@kubb/plugin-oas'
|
|
3
|
+
|
|
4
|
+
import type { Schema, SchemaKeywordBase, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
|
|
5
|
+
import type { Options } from '../types.ts'
|
|
6
|
+
|
|
7
|
+
const fakerKeywordMapper = {
|
|
8
|
+
any: () => 'undefined',
|
|
9
|
+
unknown: () => 'unknown',
|
|
10
|
+
number: (min?: number, max?: number) => {
|
|
11
|
+
if (max !== undefined && min !== undefined) {
|
|
12
|
+
return `faker.number.float({ min: ${min}, max: ${max} })`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (min !== undefined) {
|
|
16
|
+
return `faker.number.float({ min: ${min} })`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (max !== undefined) {
|
|
20
|
+
return `faker.number.float({ max: ${max} })`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return 'faker.number.float()'
|
|
24
|
+
},
|
|
25
|
+
integer: (min?: number, max?: number) => {
|
|
26
|
+
if (max !== undefined && min !== undefined) {
|
|
27
|
+
return `faker.number.int({ min: ${min}, max: ${max} })`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (min !== undefined) {
|
|
31
|
+
return `faker.number.int({ min: ${min} })`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (max !== undefined) {
|
|
35
|
+
return `faker.number.int({ max: ${max} })`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return 'faker.number.int()'
|
|
39
|
+
},
|
|
40
|
+
string: (min?: number, max?: number) => {
|
|
41
|
+
if (max !== undefined && min !== undefined) {
|
|
42
|
+
return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (min !== undefined) {
|
|
46
|
+
return `faker.string.alpha({ length: { min: ${min} } })`
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (max !== undefined) {
|
|
50
|
+
return `faker.string.alpha({ length: { max: ${max} } })`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return 'faker.string.alpha()'
|
|
54
|
+
},
|
|
55
|
+
boolean: () => 'faker.datatype.boolean()',
|
|
56
|
+
undefined: () => 'undefined',
|
|
57
|
+
null: () => 'null',
|
|
58
|
+
array: (items: string[] = [], min?: number, max?: number) => {
|
|
59
|
+
if (items.length > 1) {
|
|
60
|
+
return `faker.helpers.arrayElements([${items.join(', ')}]) as any`
|
|
61
|
+
}
|
|
62
|
+
const item = items.at(0)
|
|
63
|
+
|
|
64
|
+
if (min !== undefined && max !== undefined) {
|
|
65
|
+
return `faker.helpers.multiple(() => (${item}), { count: { min: ${min}, max: ${max} }}) as any`
|
|
66
|
+
}
|
|
67
|
+
if (min !== undefined) {
|
|
68
|
+
return `faker.helpers.multiple(() => (${item}), { count: ${min} }) as any`
|
|
69
|
+
}
|
|
70
|
+
if (max !== undefined) {
|
|
71
|
+
return `faker.helpers.multiple(() => (${item}), { count: { min: 0, max: ${max} }}) as any`
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return `faker.helpers.multiple(() => (${item})) as any`
|
|
75
|
+
},
|
|
76
|
+
tuple: (items: string[] = []) => `faker.helpers.arrayElements([${items.join(', ')}]) as any`,
|
|
77
|
+
enum: (items: Array<string | number> = []) => `faker.helpers.arrayElement<any>([${items.join(', ')}])`,
|
|
78
|
+
union: (items: string[] = []) => `faker.helpers.arrayElement<any>([${items.join(', ')}])`,
|
|
79
|
+
/**
|
|
80
|
+
* ISO 8601
|
|
81
|
+
*/
|
|
82
|
+
datetime: () => 'faker.date.anytime().toISOString()',
|
|
83
|
+
/**
|
|
84
|
+
* Type `'date'` Date
|
|
85
|
+
* Type `'string'` ISO date format (YYYY-MM-DD)
|
|
86
|
+
* @default ISO date format (YYYY-MM-DD)
|
|
87
|
+
*/
|
|
88
|
+
date: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {
|
|
89
|
+
if (type === 'string') {
|
|
90
|
+
if (parser !== 'faker') {
|
|
91
|
+
return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`
|
|
92
|
+
}
|
|
93
|
+
return 'faker.date.anytime().toString()'
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (parser !== 'faker') {
|
|
97
|
+
throw new Error(`type '${type}' and parser '${parser}' can not work together`)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return 'faker.date.anytime()'
|
|
101
|
+
},
|
|
102
|
+
/**
|
|
103
|
+
* Type `'date'` Date
|
|
104
|
+
* Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
|
|
105
|
+
* @default ISO time format (HH:mm:ss[.SSSSSS])
|
|
106
|
+
*/
|
|
107
|
+
time: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {
|
|
108
|
+
if (type === 'string') {
|
|
109
|
+
if (parser !== 'faker') {
|
|
110
|
+
return `${parser}(faker.date.anytime()).format("HH:mm:ss")`
|
|
111
|
+
}
|
|
112
|
+
return 'faker.date.anytime().toString()'
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (parser !== 'faker') {
|
|
116
|
+
throw new Error(`type '${type}' and parser '${parser}' can not work together`)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return 'faker.date.anytime()'
|
|
120
|
+
},
|
|
121
|
+
uuid: () => 'faker.string.uuid()',
|
|
122
|
+
url: () => 'faker.internet.url()',
|
|
123
|
+
and: (items: string[] = []) => `Object.assign({}, ${items.join(', ')})`,
|
|
124
|
+
object: () => 'object',
|
|
125
|
+
ref: () => 'ref',
|
|
126
|
+
matches: (value = '', regexGenerator: 'faker' | 'randexp' = 'faker') => {
|
|
127
|
+
if (regexGenerator === 'randexp') {
|
|
128
|
+
return `${transformers.toRegExpString(value, 'RandExp')}.gen()`
|
|
129
|
+
}
|
|
130
|
+
return `faker.helpers.fromRegExp(${transformers.toRegExpString(value)})`
|
|
131
|
+
},
|
|
132
|
+
email: () => 'faker.internet.email()',
|
|
133
|
+
firstName: () => 'faker.person.firstName()',
|
|
134
|
+
lastName: () => 'faker.person.lastName()',
|
|
135
|
+
password: () => 'faker.internet.password()',
|
|
136
|
+
phone: () => 'faker.phone.number()',
|
|
137
|
+
blob: () => 'faker.image.url() as unknown as Blob',
|
|
138
|
+
default: undefined,
|
|
139
|
+
describe: undefined,
|
|
140
|
+
const: (value?: string | number) => (value as string) ?? '',
|
|
141
|
+
max: undefined,
|
|
142
|
+
min: undefined,
|
|
143
|
+
nullable: undefined,
|
|
144
|
+
nullish: undefined,
|
|
145
|
+
optional: undefined,
|
|
146
|
+
readOnly: undefined,
|
|
147
|
+
writeOnly: undefined,
|
|
148
|
+
strict: undefined,
|
|
149
|
+
deprecated: undefined,
|
|
150
|
+
example: undefined,
|
|
151
|
+
schema: undefined,
|
|
152
|
+
catchall: undefined,
|
|
153
|
+
name: undefined,
|
|
154
|
+
} satisfies SchemaMapper<string | null | undefined>
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @link based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398
|
|
158
|
+
*/
|
|
159
|
+
|
|
160
|
+
function schemaKeywordsorter(a: Schema, b: Schema) {
|
|
161
|
+
if (b.keyword === 'null') {
|
|
162
|
+
return -1
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return 0
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function joinItems(items: string[]): string {
|
|
169
|
+
switch (items.length) {
|
|
170
|
+
case 0:
|
|
171
|
+
return 'undefined'
|
|
172
|
+
case 1:
|
|
173
|
+
return items[0]!
|
|
174
|
+
default:
|
|
175
|
+
return fakerKeywordMapper.union(items)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
type ParserOptions = {
|
|
180
|
+
name: string
|
|
181
|
+
typeName?: string
|
|
182
|
+
description?: string
|
|
183
|
+
|
|
184
|
+
seed?: number | number[]
|
|
185
|
+
regexGenerator?: 'faker' | 'randexp'
|
|
186
|
+
canOverride?: boolean
|
|
187
|
+
dateParser?: Options['dateParser']
|
|
188
|
+
mapper?: Record<string, string>
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function parse({ parent, current, siblings }: SchemaTree, options: ParserOptions): string | null | undefined {
|
|
192
|
+
const value = fakerKeywordMapper[current.keyword as keyof typeof fakerKeywordMapper]
|
|
193
|
+
|
|
194
|
+
if (!value) {
|
|
195
|
+
return undefined
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (isKeyword(current, schemaKeywords.union)) {
|
|
199
|
+
return fakerKeywordMapper.union(
|
|
200
|
+
current.args.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (isKeyword(current, schemaKeywords.and)) {
|
|
205
|
+
return fakerKeywordMapper.and(
|
|
206
|
+
current.args.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (isKeyword(current, schemaKeywords.array)) {
|
|
211
|
+
return fakerKeywordMapper.array(
|
|
212
|
+
current.args.items.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),
|
|
213
|
+
current.args.min,
|
|
214
|
+
current.args.max,
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (isKeyword(current, schemaKeywords.enum)) {
|
|
219
|
+
return fakerKeywordMapper.enum(
|
|
220
|
+
current.args.items.map((schema) => {
|
|
221
|
+
if (schema.format === 'number') {
|
|
222
|
+
return schema.name
|
|
223
|
+
}
|
|
224
|
+
return transformers.stringify(schema.name)
|
|
225
|
+
}),
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (isKeyword(current, schemaKeywords.ref)) {
|
|
230
|
+
if (!current.args?.name) {
|
|
231
|
+
throw new Error(`Name not defined for keyword ${current.keyword}`)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (options.canOverride) {
|
|
235
|
+
return `${current.args.name}(data)`
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return `${current.args.name}()`
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (isKeyword(current, schemaKeywords.object)) {
|
|
242
|
+
const argsObject = Object.entries(current.args?.properties || {})
|
|
243
|
+
.filter((item) => {
|
|
244
|
+
const schema = item[1]
|
|
245
|
+
return schema && typeof schema.map === 'function'
|
|
246
|
+
})
|
|
247
|
+
.map(([name, schemas]) => {
|
|
248
|
+
const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
|
|
249
|
+
const mappedName = nameSchema?.args || name
|
|
250
|
+
|
|
251
|
+
// custom mapper(pluginOptions)
|
|
252
|
+
if (options.mapper?.[mappedName]) {
|
|
253
|
+
return `"${name}": ${options.mapper?.[mappedName]}`
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return `"${name}": ${joinItems(
|
|
257
|
+
schemas
|
|
258
|
+
.sort(schemaKeywordsorter)
|
|
259
|
+
.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false }))
|
|
260
|
+
.filter(Boolean),
|
|
261
|
+
)}`
|
|
262
|
+
})
|
|
263
|
+
.join(',')
|
|
264
|
+
|
|
265
|
+
return `{${argsObject}}`
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
269
|
+
if (Array.isArray(current.args.items)) {
|
|
270
|
+
return fakerKeywordMapper.tuple(
|
|
271
|
+
current.args.items.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return parse({ parent: current, current: current.args.items, siblings }, { ...options, canOverride: false })
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (isKeyword(current, schemaKeywords.const)) {
|
|
279
|
+
if (current.args.format === 'number' && current.args.name !== undefined) {
|
|
280
|
+
return fakerKeywordMapper.const(current.args.name?.toString())
|
|
281
|
+
}
|
|
282
|
+
return fakerKeywordMapper.const(transformers.stringify(current.args.value))
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (isKeyword(current, schemaKeywords.matches) && current.args) {
|
|
286
|
+
return fakerKeywordMapper.matches(current.args, options.regexGenerator)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (isKeyword(current, schemaKeywords.null) || isKeyword(current, schemaKeywords.undefined) || isKeyword(current, schemaKeywords.any)) {
|
|
290
|
+
return value() || ''
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (isKeyword(current, schemaKeywords.string)) {
|
|
294
|
+
if (parent) {
|
|
295
|
+
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min)
|
|
296
|
+
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max)
|
|
297
|
+
|
|
298
|
+
return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args)
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return fakerKeywordMapper.string()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (isKeyword(current, schemaKeywords.number)) {
|
|
305
|
+
if (parent) {
|
|
306
|
+
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min)
|
|
307
|
+
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max)
|
|
308
|
+
|
|
309
|
+
return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return fakerKeywordMapper.number()
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (isKeyword(current, schemaKeywords.integer)) {
|
|
316
|
+
if (parent) {
|
|
317
|
+
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min)
|
|
318
|
+
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max)
|
|
319
|
+
|
|
320
|
+
return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return fakerKeywordMapper.integer()
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (isKeyword(current, schemaKeywords.datetime)) {
|
|
327
|
+
return fakerKeywordMapper.datetime()
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (isKeyword(current, schemaKeywords.date)) {
|
|
331
|
+
return fakerKeywordMapper.date(current.args.type, options.dateParser)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (isKeyword(current, schemaKeywords.time)) {
|
|
335
|
+
return fakerKeywordMapper.time(current.args.type, options.dateParser)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (current.keyword in fakerKeywordMapper && 'args' in current) {
|
|
339
|
+
const value = fakerKeywordMapper[current.keyword as keyof typeof fakerKeywordMapper] as (typeof fakerKeywordMapper)['const']
|
|
340
|
+
|
|
341
|
+
const options = JSON.stringify((current as SchemaKeywordBase<unknown>).args)
|
|
342
|
+
|
|
343
|
+
return value(options)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (current.keyword in fakerKeywordMapper) {
|
|
347
|
+
return value()
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
return undefined
|
|
351
|
+
}
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
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, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
6
|
+
|
|
7
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
8
|
+
|
|
9
|
+
import type { Plugin } from '@kubb/core'
|
|
10
|
+
import type { PluginOas } from '@kubb/plugin-oas'
|
|
11
|
+
import { fakerGenerator } from './generators/fakerGenerator.tsx'
|
|
12
|
+
import type { PluginFaker } from './types.ts'
|
|
13
|
+
|
|
14
|
+
export const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']
|
|
15
|
+
|
|
16
|
+
export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
17
|
+
const {
|
|
18
|
+
output = { path: 'mocks', barrelType: 'named' },
|
|
19
|
+
seed,
|
|
20
|
+
group,
|
|
21
|
+
exclude = [],
|
|
22
|
+
include,
|
|
23
|
+
override = [],
|
|
24
|
+
transformers = {},
|
|
25
|
+
mapper = {},
|
|
26
|
+
unknownType = 'any',
|
|
27
|
+
dateType = 'string',
|
|
28
|
+
dateParser = 'faker',
|
|
29
|
+
generators = [fakerGenerator].filter(Boolean),
|
|
30
|
+
regexGenerator = 'faker',
|
|
31
|
+
} = options
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
name: pluginFakerName,
|
|
35
|
+
options: {
|
|
36
|
+
output,
|
|
37
|
+
transformers,
|
|
38
|
+
seed,
|
|
39
|
+
dateType,
|
|
40
|
+
unknownType,
|
|
41
|
+
dateParser,
|
|
42
|
+
mapper,
|
|
43
|
+
override,
|
|
44
|
+
regexGenerator,
|
|
45
|
+
},
|
|
46
|
+
pre: [pluginOasName, pluginTsName],
|
|
47
|
+
resolvePath(baseName, pathMode, options) {
|
|
48
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
49
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
50
|
+
|
|
51
|
+
if (options?.tag && group?.type === 'tag') {
|
|
52
|
+
const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
|
|
53
|
+
|
|
54
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (mode === 'single') {
|
|
58
|
+
/**
|
|
59
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
60
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
61
|
+
*/
|
|
62
|
+
return path.resolve(root, output.path)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return path.resolve(root, output.path, baseName)
|
|
66
|
+
},
|
|
67
|
+
resolveName(name, type) {
|
|
68
|
+
const resolvedName = camelCase(name, {
|
|
69
|
+
prefix: type ? 'create' : undefined,
|
|
70
|
+
isFile: type === 'file',
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
if (type) {
|
|
74
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return resolvedName
|
|
78
|
+
},
|
|
79
|
+
async buildStart() {
|
|
80
|
+
const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
|
|
81
|
+
|
|
82
|
+
const oas = await swaggerPlugin.context.getOas()
|
|
83
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
84
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
85
|
+
|
|
86
|
+
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
87
|
+
oas,
|
|
88
|
+
pluginManager: this.pluginManager,
|
|
89
|
+
plugin: this.plugin,
|
|
90
|
+
contentType: swaggerPlugin.context.contentType,
|
|
91
|
+
include: undefined,
|
|
92
|
+
override,
|
|
93
|
+
mode,
|
|
94
|
+
output: output.path,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
98
|
+
await this.addFile(...schemaFiles)
|
|
99
|
+
|
|
100
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
101
|
+
oas,
|
|
102
|
+
pluginManager: this.pluginManager,
|
|
103
|
+
plugin: this.plugin,
|
|
104
|
+
contentType: swaggerPlugin.context.contentType,
|
|
105
|
+
exclude,
|
|
106
|
+
include,
|
|
107
|
+
override,
|
|
108
|
+
mode,
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
112
|
+
await this.addFile(...operationFiles)
|
|
113
|
+
|
|
114
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
115
|
+
type: output.barrelType ?? 'named',
|
|
116
|
+
root,
|
|
117
|
+
output,
|
|
118
|
+
files: this.fileManager.files,
|
|
119
|
+
meta: {
|
|
120
|
+
pluginKey: this.plugin.key,
|
|
121
|
+
},
|
|
122
|
+
logger: this.logger,
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
await this.addFile(...barrelFiles)
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
|
|
3
|
+
import type { SchemaObject } from '@kubb/oas'
|
|
4
|
+
import type { Exclude, Generator, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
|
|
5
|
+
|
|
6
|
+
export type Options = {
|
|
7
|
+
/**
|
|
8
|
+
* Specify the export location for the files and define the behavior of the output
|
|
9
|
+
* @default { path: 'handlers', barrelType: 'named' }
|
|
10
|
+
*/
|
|
11
|
+
output?: Output
|
|
12
|
+
/**
|
|
13
|
+
* Group the Faker mocks based on the provided name.
|
|
14
|
+
*/
|
|
15
|
+
group?: Group
|
|
16
|
+
/**
|
|
17
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
18
|
+
*/
|
|
19
|
+
exclude?: Array<Exclude>
|
|
20
|
+
/**
|
|
21
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
22
|
+
*/
|
|
23
|
+
include?: Array<Include>
|
|
24
|
+
/**
|
|
25
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
26
|
+
*/
|
|
27
|
+
override?: Array<Override<ResolvedOptions>>
|
|
28
|
+
/**
|
|
29
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
|
30
|
+
* @default 'string'
|
|
31
|
+
*/
|
|
32
|
+
dateType?: 'string' | 'date'
|
|
33
|
+
/**
|
|
34
|
+
* Which parser should be used when dateType is set to 'string'.
|
|
35
|
+
* - Schema with format 'date' will use ISO date format (YYYY-MM-DD)
|
|
36
|
+
* - `'dayjs'` will use `dayjs(faker.date.anytime()).format("YYYY-MM-DD")`.
|
|
37
|
+
* - `undefined` will use `faker.date.anytime().toString()`
|
|
38
|
+
* - Schema with format 'time' will use ISO time format (HH:mm:ss[.SSSSSS])
|
|
39
|
+
* - `'dayjs'` will use `dayjs(faker.date.anytime()).format("HH:mm:ss")`.
|
|
40
|
+
* - `undefined` will use `faker.date.anytime().toString()`
|
|
41
|
+
* * @default 'faker'
|
|
42
|
+
*/
|
|
43
|
+
dateParser?: 'faker' | 'dayjs' | 'moment' | (string & {})
|
|
44
|
+
/**
|
|
45
|
+
* Which type to use when the Swagger/OpenAPI file is not providing more information
|
|
46
|
+
* @default 'any'
|
|
47
|
+
*/
|
|
48
|
+
unknownType?: 'any' | 'unknown'
|
|
49
|
+
/**
|
|
50
|
+
* Choose which generator to use when using Regexp.
|
|
51
|
+
*
|
|
52
|
+
* `'faker'` will use `faker.helpers.fromRegExp(new RegExp(/test/))`
|
|
53
|
+
* `'randexp'` will use `new RandExp(/test/).gen()`
|
|
54
|
+
* @default 'faker'
|
|
55
|
+
*/
|
|
56
|
+
regexGenerator?: 'faker' | 'randexp'
|
|
57
|
+
|
|
58
|
+
mapper?: Record<string, string>
|
|
59
|
+
/**
|
|
60
|
+
* The use of Seed is intended to allow for consistent values in a test.
|
|
61
|
+
*/
|
|
62
|
+
seed?: number | number[]
|
|
63
|
+
transformers?: {
|
|
64
|
+
/**
|
|
65
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
66
|
+
*/
|
|
67
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
68
|
+
/**
|
|
69
|
+
* Receive schema and baseName(propertName) and return FakerMeta array
|
|
70
|
+
* TODO TODO add docs
|
|
71
|
+
* @beta
|
|
72
|
+
*/
|
|
73
|
+
schema?: (props: { schema?: SchemaObject; name?: string; parentName?: string }, defaultSchemas: Schema[]) => Schema[] | undefined
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Define some generators next to the faker generators
|
|
77
|
+
*/
|
|
78
|
+
generators?: Array<Generator<PluginFaker>>
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
type ResolvedOptions = {
|
|
82
|
+
output: Output
|
|
83
|
+
override: NonNullable<Options['override']>
|
|
84
|
+
dateType: NonNullable<Options['dateType']>
|
|
85
|
+
dateParser: NonNullable<Options['dateParser']>
|
|
86
|
+
unknownType: NonNullable<Options['unknownType']>
|
|
87
|
+
transformers: NonNullable<Options['transformers']>
|
|
88
|
+
seed: NonNullable<Options['seed']> | undefined
|
|
89
|
+
mapper: NonNullable<Options['mapper']>
|
|
90
|
+
regexGenerator: NonNullable<Options['regexGenerator']>
|
|
91
|
+
}
|
|
92
|
+
export type PluginFaker = PluginFactoryOptions<'plugin-faker', Options, ResolvedOptions, never, ResolvePathOptions>
|