@kubb/plugin-faker 5.0.0-alpha.9 → 5.0.0-beta.4

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 (44) 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 -505
  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/extension.yaml +364 -0
  26. package/package.json +52 -50
  27. package/src/components/Faker.tsx +124 -78
  28. package/src/generators/fakerGenerator.tsx +235 -134
  29. package/src/index.ts +7 -2
  30. package/src/plugin.ts +60 -121
  31. package/src/printers/printerFaker.ts +341 -0
  32. package/src/resolvers/resolverFaker.ts +92 -0
  33. package/src/types.ts +127 -81
  34. package/src/utils.ts +356 -0
  35. package/dist/components-BkBIov4R.js +0 -419
  36. package/dist/components-BkBIov4R.js.map +0 -1
  37. package/dist/components-IdP8GXXX.cjs +0 -461
  38. package/dist/components-IdP8GXXX.cjs.map +0 -1
  39. package/dist/fakerGenerator-CYUCNH3Q.cjs +0 -204
  40. package/dist/fakerGenerator-CYUCNH3Q.cjs.map +0 -1
  41. package/dist/fakerGenerator-M5oCrPmy.js +0 -200
  42. package/dist/fakerGenerator-M5oCrPmy.js.map +0 -1
  43. package/dist/types-r7BubMLO.d.ts +0 -132
  44. package/src/parser.ts +0 -453
@@ -0,0 +1,206 @@
1
+ import { t as __name } from "./chunk--u3MIqq1.js";
2
+ import { Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
3
+
4
+ //#region src/types.d.ts
5
+ /**
6
+ * Resolver for Faker that provides naming methods for mock functions.
7
+ */
8
+ type ResolverFaker = Resolver & ast.OperationParamsResolver & {
9
+ /**
10
+ * Resolves the faker function name for a schema.
11
+ *
12
+ * @example Resolving faker function names
13
+ * `resolver.resolveName('show pet by id') // -> 'showPetById'`
14
+ */
15
+ resolveName(this: ResolverFaker, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
16
+ /**
17
+ * Resolves the output file name for a faker module.
18
+ *
19
+ * @example Resolving faker file names
20
+ * `resolver.resolvePathName('show pet by id', 'file') // -> 'showPetById'`
21
+ */
22
+ resolvePathName(this: ResolverFaker, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
23
+ /**
24
+ * Resolves the faker function name for a request body.
25
+ *
26
+ * @example Resolving data function names
27
+ * `resolver.resolveDataName(node) // -> 'createPetsData'`
28
+ */
29
+ resolveDataName(this: ResolverFaker, node: ast.OperationNode): string;
30
+ /**
31
+ * Resolves the faker function name for a response by status code.
32
+ *
33
+ * @example Response status names
34
+ * `resolver.resolveResponseStatusName(node, 200) // -> 'listPetsStatus200'`
35
+ */
36
+ resolveResponseStatusName(this: ResolverFaker, node: ast.OperationNode, statusCode: ast.StatusCode): string;
37
+ /**
38
+ * Resolves the faker function name for the response union.
39
+ *
40
+ * @example Response union names
41
+ * `resolver.resolveResponseName(node) // -> 'listPetsResponse'`
42
+ */
43
+ resolveResponseName(this: ResolverFaker, node: ast.OperationNode): string;
44
+ /**
45
+ * Resolves the faker function name for path parameters.
46
+ *
47
+ * @example Path parameters names
48
+ * `resolver.resolvePathParamsName(node, param) // -> 'showPetByIdPathPetId'`
49
+ */
50
+ resolvePathParamsName(this: ResolverFaker, node: ast.OperationNode, param: ast.ParameterNode): string;
51
+ /**
52
+ * Resolves the faker function name for query parameters.
53
+ *
54
+ * @example Query parameters names
55
+ * `resolver.resolveQueryParamsName(node, param) // -> 'listPetsQueryLimit'`
56
+ */
57
+ resolveQueryParamsName(this: ResolverFaker, node: ast.OperationNode, param: ast.ParameterNode): string;
58
+ /**
59
+ * Resolves the faker function name for header parameters.
60
+ *
61
+ * @example Header parameters names
62
+ * `resolver.resolveHeaderParamsName(node, param) // -> 'deletePetHeaderApiKey'`
63
+ */
64
+ resolveHeaderParamsName(this: ResolverFaker, node: ast.OperationNode, param: ast.ParameterNode): string;
65
+ };
66
+ type Options = {
67
+ /**
68
+ * Specify the export location for the files and define the behavior of the output.
69
+ * @default { path: 'mocks', barrelType: 'named' }
70
+ */
71
+ output?: Output;
72
+ /**
73
+ * Group the Faker mocks based on the provided name.
74
+ */
75
+ group?: Group;
76
+ /**
77
+ * Tags, operations, or paths to exclude from generation.
78
+ */
79
+ exclude?: Array<Exclude>;
80
+ /**
81
+ * Tags, operations, or paths to include in generation.
82
+ */
83
+ include?: Array<Include>;
84
+ /**
85
+ * Override options for specific tags, operations, or paths.
86
+ */
87
+ override?: Array<Override<ResolvedOptions>>;
88
+ /**
89
+ * Parser to use when formatting date/time values as strings.
90
+ *
91
+ * @default 'faker'
92
+ */
93
+ dateParser?: 'faker' | 'dayjs' | 'moment' | (string & {});
94
+ /**
95
+ * Generator to use for RegExp patterns.
96
+ *
97
+ * @default 'faker'
98
+ */
99
+ regexGenerator?: 'faker' | 'randexp';
100
+ /**
101
+ * Provide per-property faker expressions keyed by property name.
102
+ */
103
+ mapper?: Record<string, string>;
104
+ /**
105
+ * Locale for generating mock data.
106
+ * Imports the matching localized `@faker-js/faker` instance so names, addresses,
107
+ * and phone numbers reflect the target region.
108
+ *
109
+ * @default 'en'
110
+ *
111
+ * @example German
112
+ * `locale: 'de'`
113
+ *
114
+ * @example Austrian German
115
+ * `locale: 'de_AT'`
116
+ *
117
+ * @see https://fakerjs.dev/api/localization.html
118
+ */
119
+ locale?: string;
120
+ /**
121
+ * Seed faker for deterministic output.
122
+ */
123
+ seed?: number | number[];
124
+ /**
125
+ * Apply casing to parameter names to match your configuration.
126
+ */
127
+ paramsCasing?: 'camelcase';
128
+ /**
129
+ * Additional generators alongside the default generators.
130
+ */
131
+ generators?: Array<Generator<PluginFaker>>;
132
+ /**
133
+ * Override naming conventions for function names and types.
134
+ */
135
+ resolver?: Partial<ResolverFaker> & ThisType<ResolverFaker>;
136
+ /**
137
+ * AST visitor to transform generated nodes.
138
+ */
139
+ transformer?: ast.Visitor;
140
+ /**
141
+ * Override individual faker printer node handlers.
142
+ */
143
+ printer?: {
144
+ nodes?: PrinterFakerNodes;
145
+ };
146
+ };
147
+ type ResolvedOptions = {
148
+ output: Output;
149
+ group: Group | undefined;
150
+ exclude: NonNullable<Options['exclude']>;
151
+ include: Options['include'];
152
+ override: NonNullable<Options['override']>;
153
+ dateParser: NonNullable<Options['dateParser']>;
154
+ regexGenerator: NonNullable<Options['regexGenerator']>;
155
+ mapper: NonNullable<Options['mapper']>;
156
+ seed: NonNullable<Options['seed']> | undefined;
157
+ locale: Options['locale'];
158
+ paramsCasing: Options['paramsCasing'];
159
+ printer: Options['printer'];
160
+ };
161
+ type PluginFaker = PluginFactoryOptions<'plugin-faker', Options, ResolvedOptions, ResolverFaker>;
162
+ declare global {
163
+ namespace Kubb {
164
+ interface PluginRegistry {
165
+ 'plugin-faker': PluginFaker;
166
+ }
167
+ }
168
+ }
169
+ //#endregion
170
+ //#region src/printers/printerFaker.d.ts
171
+ /**
172
+ * Partial printer nodes for Faker generation, mapping schema types to output strings.
173
+ */
174
+ type PrinterFakerNodes = ast.PrinterPartial<string, PrinterFakerOptions>;
175
+ /**
176
+ * Configuration options for the Faker printer, including resolvers, mappers, and cyclic schema tracking.
177
+ */
178
+ type PrinterFakerOptions = {
179
+ dateParser?: PluginFaker['resolvedOptions']['dateParser'];
180
+ regexGenerator?: PluginFaker['resolvedOptions']['regexGenerator'];
181
+ mapper?: PluginFaker['resolvedOptions']['mapper'];
182
+ resolver: ResolverFaker;
183
+ typeName?: string;
184
+ schemaName?: string;
185
+ nestedInObject?: boolean;
186
+ nodes?: PrinterFakerNodes;
187
+ /**
188
+ * Names of schemas that participate in a circular dependency chain.
189
+ * Properties whose schema transitively references one of these are emitted
190
+ * as lazy getters so that user overrides via the `data` parameter prevent
191
+ * the recursive faker call from ever executing (avoiding stack overflow).
192
+ */
193
+ cyclicSchemas?: ReadonlySet<string>;
194
+ };
195
+ /**
196
+ * Factory options for the Faker printer, defining input/output types and configuration.
197
+ */
198
+ type PrinterFakerFactory = ast.PrinterFactoryOptions<'faker', PrinterFakerOptions, string, string>;
199
+ /**
200
+ * Creates a Faker printer that generates mock data generation code from schema nodes.
201
+ * Handles circular references gracefully by emitting memoizing getters for cyclic properties.
202
+ */
203
+ declare const printerFaker: (options: PrinterFakerOptions) => ast.Printer<PrinterFakerFactory>;
204
+ //#endregion
205
+ export { Options as a, printerFaker as i, PrinterFakerNodes as n, PluginFaker as o, PrinterFakerOptions as r, ResolverFaker as s, PrinterFakerFactory as t };
206
+ //# sourceMappingURL=printerFaker-CJiwzoto.d.ts.map
package/extension.yaml ADDED
@@ -0,0 +1,364 @@
1
+ $schema: https://kubb.dev/schemas/extension.json
2
+ kind: plugin
3
+ id: plugin-faker
4
+ name: Faker
5
+ description: Generate realistic mock data using Faker.js from OpenAPI specifications.
6
+ category: mocks
7
+ type: official
8
+ npmPackage: "@kubb/plugin-faker"
9
+ docsPath: /plugins/plugin-faker
10
+ repo: https://github.com/kubb-labs/plugins
11
+ maintainers:
12
+ - name: Stijn Van Hulle
13
+ github: stijnvanhulle
14
+ compatibility:
15
+ kubb: ">=5.0.0"
16
+ node: ">=22"
17
+ tags:
18
+ - faker
19
+ - mock-data
20
+ - mocks
21
+ - fixtures
22
+ - testing
23
+ - codegen
24
+ - openapi
25
+ dependencies:
26
+ - plugin-ts
27
+ resources:
28
+ documentation: https://kubb.dev/plugins/plugin-faker
29
+ repository: https://github.com/kubb-labs/plugins
30
+ issues: https://github.com/kubb-labs/plugins/issues
31
+ changelog: https://github.com/kubb-labs/plugins/blob/main/packages/plugin-faker/CHANGELOG.md
32
+ codesandbox: https://codesandbox.io/p/github/kubb-labs/plugins/main/examples/faker
33
+ featured: false
34
+ icon:
35
+ light: https://kubb.dev/feature/faker.svg
36
+ intro: |
37
+ # @kubb/plugin-faker
38
+
39
+ Generate mock data factories from your OpenAPI schema using [Faker.js](https://fakerjs.dev/). Create realistic test data that matches your API's types.
40
+ options:
41
+ - name: output
42
+ type: Output
43
+ required: false
44
+ default: "{ path: 'mocks', barrelType: 'named' }"
45
+ description: Specify the export location for the files and define the behavior of the output.
46
+ properties:
47
+ - name: path
48
+ type: string
49
+ required: true
50
+ description: Output directory or file for the generated code, relative to the global `output.path`.
51
+ tip: |
52
+ if `output.path` is a file, `group` cannot be used.
53
+ default: "'mocks'"
54
+ - name: barrelType
55
+ type: "'all' | 'named' | 'propagate' | false"
56
+ required: false
57
+ default: "'named'"
58
+ description: Specify what to export and optionally disable barrel-file generation.
59
+ tip: |
60
+ Using `propagate` will prevent a plugin from creating a barrel file, but it will still propagate, allowing [`output.barrelType`](https://kubb.dev/docs/5.x/configuration#output-barreltype) to export the specific function or type.
61
+ examples:
62
+ - name: all
63
+ files:
64
+ - lang: typescript
65
+ code: |
66
+ export * from './gen/petService.ts'
67
+ twoslash: false
68
+ - name: named
69
+ files:
70
+ - lang: typescript
71
+ code: |
72
+ export { PetService } from './gen/petService.ts'
73
+ twoslash: false
74
+ - name: propagate
75
+ files:
76
+ - lang: typescript
77
+ code: ""
78
+ twoslash: false
79
+ - name: "false"
80
+ files:
81
+ - lang: typescript
82
+ code: ""
83
+ twoslash: false
84
+ - name: banner
85
+ type: "string | ((node: RootNode) => string)"
86
+ required: false
87
+ description: Add a banner comment at the top of every generated file. Accepts a static string or a function that receives the `RootNode` and returns a string.
88
+ - name: footer
89
+ type: "string | ((node: RootNode) => string)"
90
+ required: false
91
+ description: Add a footer comment at the end of every generated file. Accepts a static string or a function that receives the `RootNode` and returns a string.
92
+ - name: override
93
+ type: boolean
94
+ required: false
95
+ default: "false"
96
+ description: Whether Kubb overrides existing external files that can be generated if they already exist.
97
+ - name: group
98
+ type: Group
99
+ required: false
100
+ description: |
101
+ Grouping combines files in a folder based on a specific `type`.
102
+ examples:
103
+ - name: kubb.config.ts
104
+ files:
105
+ - lang: typescript
106
+ code: |
107
+ group: {
108
+ type: 'tag',
109
+ name({ group }) {
110
+ return `${group}Controller`
111
+ }
112
+ }
113
+ twoslash: false
114
+ body: |
115
+ With the configuration above, the generator emits:
116
+
117
+ ```text
118
+ .
119
+ ├── src/
120
+ │ └── petController/
121
+ │ │ ├── addPet.ts
122
+ │ │ └── getPet.ts
123
+ │ └── storeController/
124
+ │ ├── createStore.ts
125
+ │ └── getStoreById.ts
126
+ ├── petStore.yaml
127
+ ├── kubb.config.ts
128
+ └── package.json
129
+ ```
130
+ properties:
131
+ - name: type
132
+ type: "'tag'"
133
+ required: true
134
+ description: Specify the property to group files by. Required when `group` is defined.
135
+ note: |
136
+ `Required: true*` means this is required only when the `group` option is used. The `group` option itself is optional.
137
+ body: |
138
+ - `'tag'`: Uses the first tag from `operation.getTags().at(0)?.name`
139
+ - name: name
140
+ type: "(context: GroupContext) => string"
141
+ required: false
142
+ default: (ctx) => `${ctx.group}Controller`
143
+ description: Return the name of a group based on the group name. This is used for file and helper name generation.
144
+ - name: dateParser
145
+ type: "'faker' | 'dayjs' | 'moment' | string"
146
+ required: false
147
+ default: "'faker'"
148
+ description: Choose which formatter to use for `date`, `time`, or `datetime` fields represented as strings.
149
+ tip: |
150
+ You can use another library that exposes a default export. Kubb adds the import automatically.
151
+ examples:
152
+ - name: "'faker'"
153
+ files:
154
+ - lang: ts
155
+ code: |
156
+ faker.date.anytime().toISOString().substring(0, 10)
157
+ - name: "'dayjs'"
158
+ files:
159
+ - lang: ts
160
+ code: |
161
+ dayjs(faker.date.anytime()).format('YYYY-MM-DD')
162
+ - name: "'moment'"
163
+ files:
164
+ - lang: ts
165
+ code: |
166
+ moment(faker.date.anytime()).format('YYYY-MM-DD')
167
+ - name: mapper
168
+ type: Record<string, string>
169
+ required: false
170
+ description: Override individual properties with custom Faker expressions.
171
+ - name: paramsCasing
172
+ type: "'camelcase'"
173
+ required: false
174
+ description: Transform parameter property names in generated mocks for path, query, and headers.
175
+ important: |
176
+ Use the same `paramsCasing` value in `@kubb/plugin-ts` so the generated mock objects stay compatible with the generated types.
177
+ - name: regexGenerator
178
+ type: "'faker' | 'randexp'"
179
+ required: false
180
+ default: "'faker'"
181
+ description: Generate strings from regex patterns using Faker or randexp.
182
+ examples:
183
+ - name: "'faker'"
184
+ files:
185
+ - lang: ts
186
+ code: |
187
+ faker.helpers.fromRegExp('^[A-Z]+$')
188
+ - name: "'randexp'"
189
+ files:
190
+ - lang: ts
191
+ code: |
192
+ new RandExp(/^[A-Z]+$/).gen()
193
+ - name: seed
194
+ type: number | number[]
195
+ required: false
196
+ description: Seed faker for deterministic output.
197
+ - name: locale
198
+ type: string
199
+ required: false
200
+ default: "'en'"
201
+ description: Generate mock data using a locale-specific Faker instance.
202
+ tip: |
203
+ See the [Faker.js localization docs](https://fakerjs.dev/api/localization.html) for the full list of supported locales.
204
+ examples:
205
+ - name: "'de'"
206
+ files:
207
+ - lang: ts
208
+ code: |
209
+ import { fakerDE as faker } from '@faker-js/faker'
210
+ - name: "'de_AT'"
211
+ files:
212
+ - lang: ts
213
+ code: |
214
+ import { fakerDE_AT as faker } from '@faker-js/faker'
215
+ - name: include
216
+ type: Array<Include>
217
+ required: false
218
+ description: Array containing include parameters to include tags, operations, methods, paths, or content types.
219
+ codeBlock:
220
+ lang: typescript
221
+ title: Include
222
+ code: |
223
+ export type Include = {
224
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
225
+ pattern: string | RegExp
226
+ }
227
+ - name: exclude
228
+ type: Array<Exclude>
229
+ required: false
230
+ description: Array containing exclude parameters to exclude or skip tags, operations, methods, paths, or content types.
231
+ codeBlock:
232
+ lang: typescript
233
+ title: Exclude
234
+ code: |
235
+ export type Exclude = {
236
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
237
+ pattern: string | RegExp
238
+ }
239
+ - name: override
240
+ type: Array<Override>
241
+ required: false
242
+ description: Array containing override parameters to override `options` based on tags, operations, methods, paths, or content types.
243
+ codeBlock:
244
+ lang: typescript
245
+ title: Override
246
+ code: |
247
+ export type Override = {
248
+ type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
249
+ pattern: string | RegExp
250
+ options: PluginOptions
251
+ }
252
+ - name: generators
253
+ type: Array<Generator<PluginFaker>>
254
+ required: false
255
+ experimental: true
256
+ description: |
257
+ Define additional generators next to the built-in generators.
258
+
259
+ See [Generators](https://kubb.dev/docs/5.x/guides/creating-plugins) for more information on how to use generators.
260
+ - name: resolver
261
+ type: Partial<ResolverFaker>
262
+ required: false
263
+ description: Customize helper naming on top of the active compatibility preset.
264
+ codeBlock:
265
+ lang: typescript
266
+ code: |
267
+ pluginFaker({
268
+ resolver: {
269
+ resolveName(name) {
270
+ return `${this.default(name)}Mock`
271
+ },
272
+ },
273
+ })
274
+ - name: transformer
275
+ type: ast.Visitor
276
+ required: false
277
+ description: Apply a single AST visitor before the faker helpers are rendered.
278
+ codeBlock:
279
+ lang: typescript
280
+ code: |
281
+ pluginFaker({
282
+ transformer: {
283
+ schema(node) {
284
+ return { ...node, description: undefined }
285
+ },
286
+ },
287
+ })
288
+ - name: printer
289
+ type: "{ nodes?: PrinterFakerNodes }"
290
+ required: false
291
+ description: |
292
+ Override individual printer node handlers to customize how specific schema types are rendered.
293
+
294
+ Each key is a `SchemaType` (for example `'integer'`, `'date'`, or `'ref'`). The function you provide replaces the built-in handler for that type. Use `this.transform` to recurse into nested schema nodes and `this.options` to read printer options.
295
+ examples:
296
+ - name: Override integer to float()
297
+ files:
298
+ - lang: typescript
299
+ code: |
300
+ import { pluginFaker } from '@kubb/plugin-faker'
301
+
302
+ pluginFaker({
303
+ printer: {
304
+ nodes: {
305
+ integer() {
306
+ return 'faker.number.float()'
307
+ },
308
+ },
309
+ },
310
+ })
311
+ twoslash: false
312
+ - name: Override date strings
313
+ files:
314
+ - lang: typescript
315
+ code: |
316
+ import { pluginFaker } from '@kubb/plugin-faker'
317
+
318
+ pluginFaker({
319
+ printer: {
320
+ nodes: {
321
+ date(node) {
322
+ if (node.representation === 'string') {
323
+ return 'new Date().toISOString().substring(0, 10)'
324
+ }
325
+
326
+ return 'new Date()'
327
+ },
328
+ },
329
+ },
330
+ })
331
+ twoslash: false
332
+ examples:
333
+ - name: kubb.config.ts
334
+ files:
335
+ - lang: typescript
336
+ code: |
337
+ import { defineConfig } from 'kubb'
338
+ import { pluginFaker } from '@kubb/plugin-faker'
339
+ import { pluginTs } from '@kubb/plugin-ts'
340
+
341
+ export default defineConfig({
342
+ input: {
343
+ path: './petStore.yaml',
344
+ },
345
+ output: {
346
+ path: './src/gen',
347
+ },
348
+ plugins: [
349
+ pluginTs({
350
+ output: {
351
+ path: './types',
352
+ },
353
+ }),
354
+ pluginFaker({
355
+ output: {
356
+ path: './mocks',
357
+ barrelType: 'named',
358
+ },
359
+ seed: [100],
360
+ paramsCasing: 'camelcase',
361
+ }),
362
+ ],
363
+ })
364
+ twoslash: false