@kubb/plugin-faker 5.0.0-alpha.8 → 5.0.0-beta.3

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.
Files changed (43) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +1 -4
  3. package/dist/Faker-BgleOzVN.cjs +486 -0
  4. package/dist/Faker-BgleOzVN.cjs.map +1 -0
  5. package/dist/Faker-CdyPfOPg.d.ts +27 -0
  6. package/dist/Faker-fcQEB9i5.js +384 -0
  7. package/dist/Faker-fcQEB9i5.js.map +1 -0
  8. package/dist/components.cjs +2 -2
  9. package/dist/components.d.ts +2 -31
  10. package/dist/components.js +1 -1
  11. package/dist/fakerGenerator-C3Ho3BaI.d.ts +9 -0
  12. package/dist/fakerGenerator-D7daHCh6.js +516 -0
  13. package/dist/fakerGenerator-D7daHCh6.js.map +1 -0
  14. package/dist/fakerGenerator-VJEVzLjc.cjs +526 -0
  15. package/dist/fakerGenerator-VJEVzLjc.cjs.map +1 -0
  16. package/dist/generators.cjs +1 -1
  17. package/dist/generators.d.ts +2 -476
  18. package/dist/generators.js +1 -1
  19. package/dist/index.cjs +136 -84
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.ts +28 -4
  22. package/dist/index.js +128 -83
  23. package/dist/index.js.map +1 -1
  24. package/dist/printerFaker-CJiwzoto.d.ts +206 -0
  25. package/package.json +52 -50
  26. package/src/components/Faker.tsx +124 -78
  27. package/src/generators/fakerGenerator.tsx +235 -134
  28. package/src/index.ts +7 -2
  29. package/src/plugin.ts +60 -121
  30. package/src/printers/printerFaker.ts +341 -0
  31. package/src/resolvers/resolverFaker.ts +92 -0
  32. package/src/types.ts +127 -81
  33. package/src/utils.ts +356 -0
  34. package/dist/components-BkBIov4R.js +0 -419
  35. package/dist/components-BkBIov4R.js.map +0 -1
  36. package/dist/components-IdP8GXXX.cjs +0 -461
  37. package/dist/components-IdP8GXXX.cjs.map +0 -1
  38. package/dist/fakerGenerator-CYUCNH3Q.cjs +0 -204
  39. package/dist/fakerGenerator-CYUCNH3Q.cjs.map +0 -1
  40. package/dist/fakerGenerator-M5oCrPmy.js +0 -200
  41. package/dist/fakerGenerator-M5oCrPmy.js.map +0 -1
  42. package/dist/types-r7BubMLO.d.ts +0 -132
  43. package/src/parser.ts +0 -453
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
- })