@kubb/plugin-faker 5.0.0-alpha.9 → 5.0.0-beta.100
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 +17 -10
- package/README.md +37 -23
- package/dist/index.cjs +1443 -103
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +291 -6
- package/dist/index.js +1437 -104
- package/dist/index.js.map +1 -1
- package/package.json +41 -64
- package/dist/components-BkBIov4R.js +0 -419
- package/dist/components-BkBIov4R.js.map +0 -1
- package/dist/components-IdP8GXXX.cjs +0 -461
- package/dist/components-IdP8GXXX.cjs.map +0 -1
- package/dist/components.cjs +0 -3
- package/dist/components.d.ts +0 -31
- package/dist/components.js +0 -2
- package/dist/fakerGenerator-CYUCNH3Q.cjs +0 -204
- package/dist/fakerGenerator-CYUCNH3Q.cjs.map +0 -1
- package/dist/fakerGenerator-M5oCrPmy.js +0 -200
- package/dist/fakerGenerator-M5oCrPmy.js.map +0 -1
- package/dist/generators.cjs +0 -3
- package/dist/generators.d.ts +0 -505
- package/dist/generators.js +0 -2
- package/dist/types-r7BubMLO.d.ts +0 -132
- package/src/components/Faker.tsx +0 -107
- package/src/components/index.ts +0 -1
- package/src/generators/fakerGenerator.tsx +0 -170
- package/src/generators/index.ts +0 -1
- package/src/index.ts +0 -2
- package/src/parser.ts +0 -453
- package/src/plugin.ts +0 -149
- package/src/types.ts +0 -126
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/src/parser.ts
DELETED
|
@@ -1,453 +0,0 @@
|
|
|
1
|
-
import { stringify, toRegExpString } from '@internals/utils'
|
|
2
|
-
import type { Schema, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'
|
|
3
|
-
import { createParser, findSchemaKeyword, isKeyword, schemaKeywords } from '@kubb/plugin-oas'
|
|
4
|
-
import type { Options } from './types.ts'
|
|
5
|
-
|
|
6
|
-
const fakerKeywordMapper = {
|
|
7
|
-
any: () => 'undefined',
|
|
8
|
-
unknown: () => 'undefined',
|
|
9
|
-
void: () => 'undefined',
|
|
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 (max !== undefined) {
|
|
16
|
-
return `faker.number.float({ max: ${max} })`
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (min !== undefined) {
|
|
20
|
-
return `faker.number.float({ min: ${min} })`
|
|
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 (max !== undefined) {
|
|
31
|
-
return `faker.number.int({ max: ${max} })`
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (min !== undefined) {
|
|
35
|
-
return `faker.number.int({ min: ${min} })`
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return 'faker.number.int()'
|
|
39
|
-
},
|
|
40
|
-
bigint: () => 'faker.number.bigInt()',
|
|
41
|
-
string: (min?: number, max?: number) => {
|
|
42
|
-
if (max !== undefined && min !== undefined) {
|
|
43
|
-
return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (max !== undefined) {
|
|
47
|
-
return `faker.string.alpha({ length: ${max} })`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (min !== undefined) {
|
|
51
|
-
return `faker.string.alpha({ length: ${min} })`
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return 'faker.string.alpha()'
|
|
55
|
-
},
|
|
56
|
-
boolean: () => 'faker.datatype.boolean()',
|
|
57
|
-
undefined: () => 'undefined',
|
|
58
|
-
null: () => 'null',
|
|
59
|
-
array: (items: string[] = [], min?: number, max?: number) => {
|
|
60
|
-
if (items.length > 1) {
|
|
61
|
-
return `faker.helpers.arrayElements([${items.join(', ')}])`
|
|
62
|
-
}
|
|
63
|
-
const item = items.at(0)
|
|
64
|
-
|
|
65
|
-
if (min !== undefined && max !== undefined) {
|
|
66
|
-
return `faker.helpers.multiple(() => (${item}), { count: { min: ${min}, max: ${max} }})`
|
|
67
|
-
}
|
|
68
|
-
if (min !== undefined) {
|
|
69
|
-
return `faker.helpers.multiple(() => (${item}), { count: ${min} })`
|
|
70
|
-
}
|
|
71
|
-
if (max !== undefined) {
|
|
72
|
-
return `faker.helpers.multiple(() => (${item}), { count: { min: 0, max: ${max} }})`
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return `faker.helpers.multiple(() => (${item}))`
|
|
76
|
-
},
|
|
77
|
-
tuple: (items: string[] = []) => `[${items.join(', ')}]`,
|
|
78
|
-
enum: (items: Array<string | number | boolean | undefined> = [], type = 'any') => `faker.helpers.arrayElement<${type}>([${items.join(', ')}])`,
|
|
79
|
-
union: (items: string[] = []) => `faker.helpers.arrayElement<any>([${items.join(', ')}])`,
|
|
80
|
-
/**
|
|
81
|
-
* ISO 8601
|
|
82
|
-
*/
|
|
83
|
-
datetime: () => 'faker.date.anytime().toISOString()',
|
|
84
|
-
/**
|
|
85
|
-
* Type `'date'` Date
|
|
86
|
-
* Type `'string'` ISO date format (YYYY-MM-DD)
|
|
87
|
-
* @default ISO date format (YYYY-MM-DD)
|
|
88
|
-
*/
|
|
89
|
-
date: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {
|
|
90
|
-
if (type === 'string') {
|
|
91
|
-
if (parser !== 'faker') {
|
|
92
|
-
return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`
|
|
93
|
-
}
|
|
94
|
-
return 'faker.date.anytime().toISOString().substring(0, 10)'
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (parser !== 'faker') {
|
|
98
|
-
throw new Error(`type '${type}' and parser '${parser}' can not work together`)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
return 'faker.date.anytime()'
|
|
102
|
-
},
|
|
103
|
-
/**
|
|
104
|
-
* Type `'date'` Date
|
|
105
|
-
* Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
|
|
106
|
-
* @default ISO time format (HH:mm:ss[.SSSSSS])
|
|
107
|
-
*/
|
|
108
|
-
time: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {
|
|
109
|
-
if (type === 'string') {
|
|
110
|
-
if (parser !== 'faker') {
|
|
111
|
-
return `${parser}(faker.date.anytime()).format("HH:mm:ss")`
|
|
112
|
-
}
|
|
113
|
-
return 'faker.date.anytime().toISOString().substring(11, 19)'
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (parser !== 'faker') {
|
|
117
|
-
throw new Error(`type '${type}' and parser '${parser}' can not work together`)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return 'faker.date.anytime()'
|
|
121
|
-
},
|
|
122
|
-
uuid: () => 'faker.string.uuid()',
|
|
123
|
-
url: () => 'faker.internet.url()',
|
|
124
|
-
and: (items: string[] = []) => {
|
|
125
|
-
// Handle empty array case
|
|
126
|
-
if (items.length === 0) {
|
|
127
|
-
return '{}'
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// If only one item, return it as-is (no need to spread)
|
|
131
|
-
// This fixes the issue with single refs to primitives like enums
|
|
132
|
-
if (items.length === 1) {
|
|
133
|
-
return items[0] ?? '{}'
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// If multiple items, spread them together
|
|
137
|
-
// This handles both object literals and multiple refs to objects
|
|
138
|
-
return `{...${items.join(', ...')}}`
|
|
139
|
-
},
|
|
140
|
-
object: () => 'object',
|
|
141
|
-
ref: () => 'ref',
|
|
142
|
-
matches: (value = '', regexGenerator: 'faker' | 'randexp' = 'faker') => {
|
|
143
|
-
if (regexGenerator === 'randexp') {
|
|
144
|
-
return `${toRegExpString(value, 'RandExp')}.gen()`
|
|
145
|
-
}
|
|
146
|
-
return `faker.helpers.fromRegExp("${value}")`
|
|
147
|
-
},
|
|
148
|
-
email: () => 'faker.internet.email()',
|
|
149
|
-
firstName: () => 'faker.person.firstName()',
|
|
150
|
-
lastName: () => 'faker.person.lastName()',
|
|
151
|
-
password: () => 'faker.internet.password()',
|
|
152
|
-
phone: () => 'faker.phone.number()',
|
|
153
|
-
blob: () => 'faker.image.url() as unknown as Blob',
|
|
154
|
-
default: undefined,
|
|
155
|
-
describe: undefined,
|
|
156
|
-
const: (value?: string | number) => (value as string) ?? '',
|
|
157
|
-
max: undefined,
|
|
158
|
-
min: undefined,
|
|
159
|
-
nullable: undefined,
|
|
160
|
-
nullish: undefined,
|
|
161
|
-
optional: undefined,
|
|
162
|
-
readOnly: undefined,
|
|
163
|
-
writeOnly: undefined,
|
|
164
|
-
deprecated: undefined,
|
|
165
|
-
example: undefined,
|
|
166
|
-
schema: undefined,
|
|
167
|
-
catchall: undefined,
|
|
168
|
-
name: undefined,
|
|
169
|
-
interface: undefined,
|
|
170
|
-
exclusiveMaximum: undefined,
|
|
171
|
-
exclusiveMinimum: undefined,
|
|
172
|
-
} satisfies SchemaMapper<string | null | undefined>
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @link based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398
|
|
176
|
-
*/
|
|
177
|
-
|
|
178
|
-
function schemaKeywordSorter(_a: Schema, b: Schema) {
|
|
179
|
-
if (b.keyword === 'null') {
|
|
180
|
-
return -1
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return 0
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
export function joinItems(items: string[]): string {
|
|
187
|
-
switch (items.length) {
|
|
188
|
-
case 0:
|
|
189
|
-
return 'undefined'
|
|
190
|
-
case 1:
|
|
191
|
-
return items[0]!
|
|
192
|
-
default:
|
|
193
|
-
return fakerKeywordMapper.union(items)
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
type ParserOptions = {
|
|
198
|
-
typeName?: string
|
|
199
|
-
rootTypeName?: string
|
|
200
|
-
regexGenerator?: 'faker' | 'randexp'
|
|
201
|
-
canOverride?: boolean
|
|
202
|
-
dateParser?: Options['dateParser']
|
|
203
|
-
mapper?: Record<string, string>
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export const parse = createParser<string, ParserOptions>({
|
|
207
|
-
mapper: fakerKeywordMapper,
|
|
208
|
-
handlers: {
|
|
209
|
-
union(tree, options) {
|
|
210
|
-
const { current, schema, name, siblings } = tree
|
|
211
|
-
|
|
212
|
-
if (Array.isArray(current.args) && !current.args.length) {
|
|
213
|
-
return ''
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
return fakerKeywordMapper.union(
|
|
217
|
-
current.args
|
|
218
|
-
.map((it) => this.parse({ schema, parent: current, name, current: it, siblings }, { ...options, canOverride: false }))
|
|
219
|
-
.filter((x): x is string => Boolean(x)),
|
|
220
|
-
)
|
|
221
|
-
},
|
|
222
|
-
and(tree, options) {
|
|
223
|
-
const { current, schema, siblings } = tree
|
|
224
|
-
|
|
225
|
-
return fakerKeywordMapper.and(
|
|
226
|
-
current.args
|
|
227
|
-
.map((it) => this.parse({ schema, parent: current, current: it, siblings }, { ...options, canOverride: false }))
|
|
228
|
-
.filter((x): x is string => Boolean(x)),
|
|
229
|
-
)
|
|
230
|
-
},
|
|
231
|
-
array(tree, options) {
|
|
232
|
-
const { current, schema } = tree
|
|
233
|
-
|
|
234
|
-
return fakerKeywordMapper.array(
|
|
235
|
-
current.args.items
|
|
236
|
-
.map((it) =>
|
|
237
|
-
this.parse(
|
|
238
|
-
{
|
|
239
|
-
schema,
|
|
240
|
-
parent: current,
|
|
241
|
-
current: it,
|
|
242
|
-
siblings: current.args.items,
|
|
243
|
-
},
|
|
244
|
-
{
|
|
245
|
-
...options,
|
|
246
|
-
typeName: `NonNullable<${options.typeName}>[number]`,
|
|
247
|
-
canOverride: false,
|
|
248
|
-
},
|
|
249
|
-
),
|
|
250
|
-
)
|
|
251
|
-
.filter((x): x is string => Boolean(x)),
|
|
252
|
-
current.args.min,
|
|
253
|
-
current.args.max,
|
|
254
|
-
)
|
|
255
|
-
},
|
|
256
|
-
enum(tree, options) {
|
|
257
|
-
const { current, parent, name } = tree
|
|
258
|
-
|
|
259
|
-
const isParentTuple = parent ? isKeyword(parent, schemaKeywords.tuple) : false
|
|
260
|
-
|
|
261
|
-
if (isParentTuple) {
|
|
262
|
-
return fakerKeywordMapper.enum(
|
|
263
|
-
current.args.items.map((schema) => {
|
|
264
|
-
if (schema.format === 'number') {
|
|
265
|
-
return schema.value
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (schema.format === 'boolean') {
|
|
269
|
-
return schema.value
|
|
270
|
-
}
|
|
271
|
-
return stringify(schema.value)
|
|
272
|
-
}),
|
|
273
|
-
)
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
return fakerKeywordMapper.enum(
|
|
277
|
-
current.args.items.map((schema) => {
|
|
278
|
-
if (schema.format === 'number') {
|
|
279
|
-
return schema.value
|
|
280
|
-
}
|
|
281
|
-
if (schema.format === 'boolean') {
|
|
282
|
-
return schema.value
|
|
283
|
-
}
|
|
284
|
-
return stringify(schema.value)
|
|
285
|
-
}),
|
|
286
|
-
// TODO replace this with getEnumNameFromSchema
|
|
287
|
-
name ? options.typeName : undefined,
|
|
288
|
-
)
|
|
289
|
-
},
|
|
290
|
-
ref(tree, options) {
|
|
291
|
-
const { current, parent } = tree
|
|
292
|
-
|
|
293
|
-
if (!current.args?.name) {
|
|
294
|
-
throw new Error(`Name not defined for keyword ${current.keyword}`)
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Check if this is a self-referencing type (prevents infinite recursion)
|
|
298
|
-
// The rootTypeName is the function name being generated (e.g., "createNode")
|
|
299
|
-
// The current.args.name is the ref function name (e.g., "createNode")
|
|
300
|
-
const isSelfReferencing = options.rootTypeName && current.args.name === options.rootTypeName
|
|
301
|
-
|
|
302
|
-
if (isSelfReferencing) {
|
|
303
|
-
// For self-referencing types, return undefined to prevent infinite recursion
|
|
304
|
-
// This will result in empty arrays/objects by default
|
|
305
|
-
return 'undefined as any'
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// Check if the parent is an object or and keyword - in these cases, we don't want to pass data
|
|
309
|
-
// because it would incorrectly forward the parent's data to a nested ref
|
|
310
|
-
const isNestedInObjectOrAnd = parent && (isKeyword(parent, schemaKeywords.object) || isKeyword(parent, schemaKeywords.and))
|
|
311
|
-
|
|
312
|
-
if (options.canOverride && !isNestedInObjectOrAnd) {
|
|
313
|
-
return `${current.args.name}(data)`
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return `${current.args.name}()`
|
|
317
|
-
},
|
|
318
|
-
object(tree, options) {
|
|
319
|
-
const { current, schema } = tree
|
|
320
|
-
|
|
321
|
-
const argsObject = Object.entries(current.args?.properties || {})
|
|
322
|
-
.filter((item) => {
|
|
323
|
-
const schema = item[1]
|
|
324
|
-
return schema && typeof schema.map === 'function'
|
|
325
|
-
})
|
|
326
|
-
.map(([name, schemas]) => {
|
|
327
|
-
const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
|
|
328
|
-
const mappedName = nameSchema?.args || name
|
|
329
|
-
|
|
330
|
-
// custom mapper(pluginOptions)
|
|
331
|
-
// Use Object.hasOwn to avoid matching inherited properties like 'toString', 'valueOf', etc.
|
|
332
|
-
if (options.mapper && Object.hasOwn(options.mapper, mappedName)) {
|
|
333
|
-
return `"${name}": ${options.mapper?.[mappedName]}`
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
return `"${name}": ${joinItems(
|
|
337
|
-
schemas
|
|
338
|
-
.sort(schemaKeywordSorter)
|
|
339
|
-
.map((it) =>
|
|
340
|
-
this.parse(
|
|
341
|
-
{
|
|
342
|
-
schema,
|
|
343
|
-
name,
|
|
344
|
-
parent: current,
|
|
345
|
-
current: it,
|
|
346
|
-
siblings: schemas,
|
|
347
|
-
},
|
|
348
|
-
{
|
|
349
|
-
...options,
|
|
350
|
-
typeName: `NonNullable<${options.typeName}>[${JSON.stringify(name)}]`,
|
|
351
|
-
canOverride: false,
|
|
352
|
-
},
|
|
353
|
-
),
|
|
354
|
-
)
|
|
355
|
-
.filter((x): x is string => Boolean(x)),
|
|
356
|
-
)}`
|
|
357
|
-
})
|
|
358
|
-
.join(',')
|
|
359
|
-
|
|
360
|
-
return `{${argsObject}}`
|
|
361
|
-
},
|
|
362
|
-
tuple(tree, options) {
|
|
363
|
-
const { current, schema, siblings } = tree
|
|
364
|
-
|
|
365
|
-
if (Array.isArray(current.args.items)) {
|
|
366
|
-
return fakerKeywordMapper.tuple(
|
|
367
|
-
current.args.items
|
|
368
|
-
.map((it) => this.parse({ schema, parent: current, current: it, siblings }, { ...options, canOverride: false }))
|
|
369
|
-
.filter((x): x is string => Boolean(x)),
|
|
370
|
-
)
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
return this.parse({ schema, parent: current, current: current.args.items, siblings }, { ...options, canOverride: false })
|
|
374
|
-
},
|
|
375
|
-
const(tree, _options) {
|
|
376
|
-
const { current } = tree
|
|
377
|
-
|
|
378
|
-
if (current.args.format === 'number' && current.args.name !== undefined) {
|
|
379
|
-
return fakerKeywordMapper.const(current.args.name?.toString())
|
|
380
|
-
}
|
|
381
|
-
return fakerKeywordMapper.const(stringify(current.args.value))
|
|
382
|
-
},
|
|
383
|
-
matches(tree, options) {
|
|
384
|
-
const { current } = tree
|
|
385
|
-
|
|
386
|
-
if (current.args) {
|
|
387
|
-
return fakerKeywordMapper.matches(current.args, options.regexGenerator)
|
|
388
|
-
}
|
|
389
|
-
return undefined
|
|
390
|
-
},
|
|
391
|
-
null() {
|
|
392
|
-
return fakerKeywordMapper.null()
|
|
393
|
-
},
|
|
394
|
-
undefined() {
|
|
395
|
-
return fakerKeywordMapper.undefined()
|
|
396
|
-
},
|
|
397
|
-
any() {
|
|
398
|
-
return fakerKeywordMapper.any()
|
|
399
|
-
},
|
|
400
|
-
string(tree, _options) {
|
|
401
|
-
const { siblings } = tree
|
|
402
|
-
|
|
403
|
-
if (siblings) {
|
|
404
|
-
const minSchema = findSchemaKeyword(siblings, 'min')
|
|
405
|
-
const maxSchema = findSchemaKeyword(siblings, 'max')
|
|
406
|
-
|
|
407
|
-
return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args)
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
return fakerKeywordMapper.string()
|
|
411
|
-
},
|
|
412
|
-
number(tree, _options) {
|
|
413
|
-
const { siblings } = tree
|
|
414
|
-
|
|
415
|
-
if (siblings) {
|
|
416
|
-
const minSchema = findSchemaKeyword(siblings, 'min')
|
|
417
|
-
const maxSchema = findSchemaKeyword(siblings, 'max')
|
|
418
|
-
|
|
419
|
-
return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args)
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return fakerKeywordMapper.number()
|
|
423
|
-
},
|
|
424
|
-
integer(tree, _options) {
|
|
425
|
-
const { siblings } = tree
|
|
426
|
-
|
|
427
|
-
if (siblings) {
|
|
428
|
-
const minSchema = findSchemaKeyword(siblings, 'min')
|
|
429
|
-
const maxSchema = findSchemaKeyword(siblings, 'max')
|
|
430
|
-
|
|
431
|
-
return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args)
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
return fakerKeywordMapper.integer()
|
|
435
|
-
},
|
|
436
|
-
bigint(_tree, _options) {
|
|
437
|
-
return fakerKeywordMapper.bigint()
|
|
438
|
-
},
|
|
439
|
-
datetime() {
|
|
440
|
-
return fakerKeywordMapper.datetime()
|
|
441
|
-
},
|
|
442
|
-
date(tree, options) {
|
|
443
|
-
const { current } = tree
|
|
444
|
-
|
|
445
|
-
return fakerKeywordMapper.date(current.args.type, options.dateParser)
|
|
446
|
-
},
|
|
447
|
-
time(tree, options) {
|
|
448
|
-
const { current } = tree
|
|
449
|
-
|
|
450
|
-
return fakerKeywordMapper.time(current.args.type, options.dateParser)
|
|
451
|
-
},
|
|
452
|
-
},
|
|
453
|
-
})
|
package/src/plugin.ts
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import { camelCase } from '@internals/utils'
|
|
3
|
-
import { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'
|
|
4
|
-
import { OperationGenerator, pluginOasName, SchemaGenerator } from '@kubb/plugin-oas'
|
|
5
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
|
-
import { fakerGenerator } from './generators/fakerGenerator.tsx'
|
|
7
|
-
import type { PluginFaker } from './types.ts'
|
|
8
|
-
|
|
9
|
-
export const pluginFakerName = 'plugin-faker' satisfies PluginFaker['name']
|
|
10
|
-
|
|
11
|
-
export const pluginFaker = createPlugin<PluginFaker>((options) => {
|
|
12
|
-
const {
|
|
13
|
-
output = { path: 'mocks', barrelType: 'named' },
|
|
14
|
-
seed,
|
|
15
|
-
group,
|
|
16
|
-
exclude = [],
|
|
17
|
-
include,
|
|
18
|
-
override = [],
|
|
19
|
-
transformers = {},
|
|
20
|
-
mapper = {},
|
|
21
|
-
unknownType = 'any',
|
|
22
|
-
emptySchemaType = unknownType,
|
|
23
|
-
dateType = 'string',
|
|
24
|
-
integerType = 'number',
|
|
25
|
-
dateParser = 'faker',
|
|
26
|
-
generators = [fakerGenerator].filter(Boolean),
|
|
27
|
-
regexGenerator = 'faker',
|
|
28
|
-
paramsCasing,
|
|
29
|
-
contentType,
|
|
30
|
-
} = options
|
|
31
|
-
|
|
32
|
-
// @deprecated Will be removed in v5 when collisionDetection defaults to true
|
|
33
|
-
const usedEnumNames = {}
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
name: pluginFakerName,
|
|
37
|
-
options: {
|
|
38
|
-
output,
|
|
39
|
-
transformers,
|
|
40
|
-
seed,
|
|
41
|
-
dateType,
|
|
42
|
-
integerType,
|
|
43
|
-
unknownType,
|
|
44
|
-
emptySchemaType,
|
|
45
|
-
dateParser,
|
|
46
|
-
mapper,
|
|
47
|
-
override,
|
|
48
|
-
regexGenerator,
|
|
49
|
-
paramsCasing,
|
|
50
|
-
group,
|
|
51
|
-
usedEnumNames,
|
|
52
|
-
},
|
|
53
|
-
pre: [pluginOasName, pluginTsName],
|
|
54
|
-
resolvePath(baseName, pathMode, options) {
|
|
55
|
-
const root = path.resolve(this.config.root, this.config.output.path)
|
|
56
|
-
const mode = pathMode ?? getMode(path.resolve(root, output.path))
|
|
57
|
-
|
|
58
|
-
if (mode === 'single') {
|
|
59
|
-
/**
|
|
60
|
-
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
61
|
-
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
62
|
-
*/
|
|
63
|
-
return path.resolve(root, output.path)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (group && (options?.group?.path || options?.group?.tag)) {
|
|
67
|
-
const groupName: Group['name'] = group?.name
|
|
68
|
-
? group.name
|
|
69
|
-
: (ctx) => {
|
|
70
|
-
if (group?.type === 'path') {
|
|
71
|
-
return `${ctx.group.split('/')[1]}`
|
|
72
|
-
}
|
|
73
|
-
return `${camelCase(ctx.group)}Controller`
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
return path.resolve(
|
|
77
|
-
root,
|
|
78
|
-
output.path,
|
|
79
|
-
groupName({
|
|
80
|
-
group: group.type === 'path' ? options.group.path! : options.group.tag!,
|
|
81
|
-
}),
|
|
82
|
-
baseName,
|
|
83
|
-
)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return path.resolve(root, output.path, baseName)
|
|
87
|
-
},
|
|
88
|
-
resolveName(name, type) {
|
|
89
|
-
const resolvedName = camelCase(name, {
|
|
90
|
-
prefix: type ? 'create' : undefined,
|
|
91
|
-
isFile: type === 'file',
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
if (type) {
|
|
95
|
-
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return resolvedName
|
|
99
|
-
},
|
|
100
|
-
async install() {
|
|
101
|
-
const root = path.resolve(this.config.root, this.config.output.path)
|
|
102
|
-
const mode = getMode(path.resolve(root, output.path))
|
|
103
|
-
const oas = await this.getOas()
|
|
104
|
-
|
|
105
|
-
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
106
|
-
fabric: this.fabric,
|
|
107
|
-
oas,
|
|
108
|
-
driver: this.driver,
|
|
109
|
-
events: this.events,
|
|
110
|
-
plugin: this.plugin,
|
|
111
|
-
contentType,
|
|
112
|
-
include: undefined,
|
|
113
|
-
override,
|
|
114
|
-
mode,
|
|
115
|
-
output: output.path,
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
const schemaFiles = await schemaGenerator.build(...generators)
|
|
119
|
-
await this.upsertFile(...schemaFiles)
|
|
120
|
-
|
|
121
|
-
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
122
|
-
fabric: this.fabric,
|
|
123
|
-
oas,
|
|
124
|
-
driver: this.driver,
|
|
125
|
-
events: this.events,
|
|
126
|
-
plugin: this.plugin,
|
|
127
|
-
contentType,
|
|
128
|
-
exclude,
|
|
129
|
-
include,
|
|
130
|
-
override,
|
|
131
|
-
mode,
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
const operationFiles = await operationGenerator.build(...generators)
|
|
135
|
-
await this.upsertFile(...operationFiles)
|
|
136
|
-
|
|
137
|
-
const barrelFiles = await getBarrelFiles(this.fabric.files, {
|
|
138
|
-
type: output.barrelType ?? 'named',
|
|
139
|
-
root,
|
|
140
|
-
output,
|
|
141
|
-
meta: {
|
|
142
|
-
pluginName: this.plugin.name,
|
|
143
|
-
},
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
await this.upsertFile(...barrelFiles)
|
|
147
|
-
},
|
|
148
|
-
}
|
|
149
|
-
})
|