@kubb/plugin-faker 5.0.0-beta.10 → 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.
@@ -1,213 +0,0 @@
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 the response collection.
46
- *
47
- * @example Responses collection names
48
- * `resolver.resolveResponsesName(node) // -> 'listPetsResponses'`
49
- */
50
- resolveResponsesName(this: ResolverFaker, node: ast.OperationNode): string;
51
- /**
52
- * Resolves the faker function name for path parameters.
53
- *
54
- * @example Path parameters names
55
- * `resolver.resolvePathParamsName(node, param) // -> 'showPetByIdPathPetId'`
56
- */
57
- resolvePathParamsName(this: ResolverFaker, node: ast.OperationNode, param: ast.ParameterNode): string;
58
- /**
59
- * Resolves the faker function name for query parameters.
60
- *
61
- * @example Query parameters names
62
- * `resolver.resolveQueryParamsName(node, param) // -> 'listPetsQueryLimit'`
63
- */
64
- resolveQueryParamsName(this: ResolverFaker, node: ast.OperationNode, param: ast.ParameterNode): string;
65
- /**
66
- * Resolves the faker function name for header parameters.
67
- *
68
- * @example Header parameters names
69
- * `resolver.resolveHeaderParamsName(node, param) // -> 'deletePetHeaderApiKey'`
70
- */
71
- resolveHeaderParamsName(this: ResolverFaker, node: ast.OperationNode, param: ast.ParameterNode): string;
72
- };
73
- type Options = {
74
- /**
75
- * Specify the export location for the files and define the behavior of the output.
76
- * @default { path: 'mocks', barrelType: 'named' }
77
- */
78
- output?: Output;
79
- /**
80
- * Group the Faker mocks based on the provided name.
81
- */
82
- group?: Group;
83
- /**
84
- * Tags, operations, or paths to exclude from generation.
85
- */
86
- exclude?: Array<Exclude>;
87
- /**
88
- * Tags, operations, or paths to include in generation.
89
- */
90
- include?: Array<Include>;
91
- /**
92
- * Override options for specific tags, operations, or paths.
93
- */
94
- override?: Array<Override<ResolvedOptions>>;
95
- /**
96
- * Parser to use when formatting date/time values as strings.
97
- *
98
- * @default 'faker'
99
- */
100
- dateParser?: 'faker' | 'dayjs' | 'moment' | (string & {});
101
- /**
102
- * Generator to use for RegExp patterns.
103
- *
104
- * @default 'faker'
105
- */
106
- regexGenerator?: 'faker' | 'randexp';
107
- /**
108
- * Provide per-property faker expressions keyed by property name.
109
- */
110
- mapper?: Record<string, string>;
111
- /**
112
- * Locale for generating mock data.
113
- * Imports the matching localized `@faker-js/faker` instance so names, addresses,
114
- * and phone numbers reflect the target region.
115
- *
116
- * @default 'en'
117
- *
118
- * @example German
119
- * `locale: 'de'`
120
- *
121
- * @example Austrian German
122
- * `locale: 'de_AT'`
123
- *
124
- * @see https://fakerjs.dev/api/localization.html
125
- */
126
- locale?: string;
127
- /**
128
- * Seed faker for deterministic output.
129
- */
130
- seed?: number | number[];
131
- /**
132
- * Apply casing to parameter names to match your configuration.
133
- */
134
- paramsCasing?: 'camelcase';
135
- /**
136
- * Additional generators alongside the default generators.
137
- */
138
- generators?: Array<Generator<PluginFaker>>;
139
- /**
140
- * Override naming conventions for function names and types.
141
- */
142
- resolver?: Partial<ResolverFaker> & ThisType<ResolverFaker>;
143
- /**
144
- * AST visitor to transform generated nodes.
145
- */
146
- transformer?: ast.Visitor;
147
- /**
148
- * Override individual faker printer node handlers.
149
- */
150
- printer?: {
151
- nodes?: PrinterFakerNodes;
152
- };
153
- };
154
- type ResolvedOptions = {
155
- output: Output;
156
- group: Group | undefined;
157
- exclude: NonNullable<Options['exclude']>;
158
- include: Options['include'];
159
- override: NonNullable<Options['override']>;
160
- dateParser: NonNullable<Options['dateParser']>;
161
- regexGenerator: NonNullable<Options['regexGenerator']>;
162
- mapper: NonNullable<Options['mapper']>;
163
- seed: NonNullable<Options['seed']> | undefined;
164
- locale: Options['locale'];
165
- paramsCasing: Options['paramsCasing'];
166
- printer: Options['printer'];
167
- };
168
- type PluginFaker = PluginFactoryOptions<'plugin-faker', Options, ResolvedOptions, ResolverFaker>;
169
- declare global {
170
- namespace Kubb {
171
- interface PluginRegistry {
172
- 'plugin-faker': PluginFaker;
173
- }
174
- }
175
- }
176
- //#endregion
177
- //#region src/printers/printerFaker.d.ts
178
- /**
179
- * Partial printer nodes for Faker generation, mapping schema types to output strings.
180
- */
181
- type PrinterFakerNodes = ast.PrinterPartial<string, PrinterFakerOptions>;
182
- /**
183
- * Configuration options for the Faker printer, including resolvers, mappers, and cyclic schema tracking.
184
- */
185
- type PrinterFakerOptions = {
186
- dateParser?: PluginFaker['resolvedOptions']['dateParser'];
187
- regexGenerator?: PluginFaker['resolvedOptions']['regexGenerator'];
188
- mapper?: PluginFaker['resolvedOptions']['mapper'];
189
- resolver: ResolverFaker;
190
- typeName?: string;
191
- schemaName?: string;
192
- nestedInObject?: boolean;
193
- nodes?: PrinterFakerNodes;
194
- /**
195
- * Names of schemas that participate in a circular dependency chain.
196
- * Properties whose schema transitively references one of these are emitted
197
- * as lazy getters so that user overrides via the `data` parameter prevent
198
- * the recursive faker call from ever executing (avoiding stack overflow).
199
- */
200
- cyclicSchemas?: ReadonlySet<string>;
201
- };
202
- /**
203
- * Factory options for the Faker printer, defining input/output types and configuration.
204
- */
205
- type PrinterFakerFactory = ast.PrinterFactoryOptions<'faker', PrinterFakerOptions, string, string>;
206
- /**
207
- * Creates a Faker printer that generates mock data generation code from schema nodes.
208
- * Handles circular references gracefully by emitting memoizing getters for cyclic properties.
209
- */
210
- declare const printerFaker: (options: PrinterFakerOptions) => ast.Printer<PrinterFakerFactory>;
211
- //#endregion
212
- export { Options as a, printerFaker as i, PrinterFakerNodes as n, PluginFaker as o, PrinterFakerOptions as r, ResolverFaker as s, PrinterFakerFactory as t };
213
- //# sourceMappingURL=printerFaker-W0pLunAj.d.ts.map
package/extension.yaml DELETED
@@ -1,357 +0,0 @@
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', barrel: { type: '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: barrel
55
- type: "{ type: 'named' | 'all', nested?: boolean } | false"
56
- required: false
57
- default: "{ type: 'named' }"
58
- description: "Configure barrel file export strategy. Use `type` to control named vs. wildcard exports; set `nested: true` to generate hierarchical barrels in subdirectories."
59
- examples:
60
- - name: all
61
- files:
62
- - lang: typescript
63
- code: |
64
- export * from './gen/petService.ts'
65
- twoslash: false
66
- - name: named
67
- files:
68
- - lang: typescript
69
- code: |
70
- export { PetService } from './gen/petService.ts'
71
- twoslash: false
72
- - name: 'false'
73
- files:
74
- - lang: typescript
75
- code: ''
76
- twoslash: false
77
- - name: banner
78
- type: 'string | ((node: RootNode) => string)'
79
- required: false
80
- 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.
81
- - name: footer
82
- type: 'string | ((node: RootNode) => string)'
83
- required: false
84
- 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.
85
- - name: override
86
- type: boolean
87
- required: false
88
- default: 'false'
89
- description: Whether Kubb overrides existing external files that can be generated if they already exist.
90
- - name: group
91
- type: Group
92
- required: false
93
- description: |
94
- Grouping combines files in a folder based on a specific `type`.
95
- examples:
96
- - name: kubb.config.ts
97
- files:
98
- - lang: typescript
99
- code: |
100
- group: {
101
- type: 'tag',
102
- name({ group }) {
103
- return `${group}Controller`
104
- }
105
- }
106
- twoslash: false
107
- body: |
108
- With the configuration above, the generator emits:
109
-
110
- ```text
111
- .
112
- ├── src/
113
- │ └── petController/
114
- │ │ ├── addPet.ts
115
- │ │ └── getPet.ts
116
- │ └── storeController/
117
- │ ├── createStore.ts
118
- │ └── getStoreById.ts
119
- ├── petStore.yaml
120
- ├── kubb.config.ts
121
- └── package.json
122
- ```
123
- properties:
124
- - name: type
125
- type: "'tag'"
126
- required: true
127
- description: Specify the property to group files by. Required when `group` is defined.
128
- note: |
129
- `Required: true*` means this is required only when the `group` option is used. The `group` option itself is optional.
130
- body: |
131
- - `'tag'`: Uses the first tag from `operation.getTags().at(0)?.name`
132
- - name: name
133
- type: '(context: GroupContext) => string'
134
- required: false
135
- default: (ctx) => `${ctx.group}Controller`
136
- description: Return the name of a group based on the group name. This is used for file and helper name generation.
137
- - name: dateParser
138
- type: "'faker' | 'dayjs' | 'moment' | string"
139
- required: false
140
- default: "'faker'"
141
- description: Choose which formatter to use for `date`, `time`, or `datetime` fields represented as strings.
142
- tip: |
143
- You can use another library that exposes a default export. Kubb adds the import automatically.
144
- examples:
145
- - name: "'faker'"
146
- files:
147
- - lang: ts
148
- code: |
149
- faker.date.anytime().toISOString().substring(0, 10)
150
- - name: "'dayjs'"
151
- files:
152
- - lang: ts
153
- code: |
154
- dayjs(faker.date.anytime()).format('YYYY-MM-DD')
155
- - name: "'moment'"
156
- files:
157
- - lang: ts
158
- code: |
159
- moment(faker.date.anytime()).format('YYYY-MM-DD')
160
- - name: mapper
161
- type: Record<string, string>
162
- required: false
163
- description: Override individual properties with custom Faker expressions.
164
- - name: paramsCasing
165
- type: "'camelcase'"
166
- required: false
167
- description: Transform parameter property names in generated mocks for path, query, and headers.
168
- important: |
169
- Use the same `paramsCasing` value in `@kubb/plugin-ts` so the generated mock objects stay compatible with the generated types.
170
- - name: regexGenerator
171
- type: "'faker' | 'randexp'"
172
- required: false
173
- default: "'faker'"
174
- description: Generate strings from regex patterns using Faker or randexp.
175
- examples:
176
- - name: "'faker'"
177
- files:
178
- - lang: ts
179
- code: |
180
- faker.helpers.fromRegExp('^[A-Z]+$')
181
- - name: "'randexp'"
182
- files:
183
- - lang: ts
184
- code: |
185
- new RandExp(/^[A-Z]+$/).gen()
186
- - name: seed
187
- type: number | number[]
188
- required: false
189
- description: Seed faker for deterministic output.
190
- - name: locale
191
- type: string
192
- required: false
193
- default: "'en'"
194
- description: Generate mock data using a locale-specific Faker instance.
195
- tip: |
196
- See the [Faker.js localization docs](https://fakerjs.dev/api/localization.html) for the full list of supported locales.
197
- examples:
198
- - name: "'de'"
199
- files:
200
- - lang: ts
201
- code: |
202
- import { fakerDE as faker } from '@faker-js/faker'
203
- - name: "'de_AT'"
204
- files:
205
- - lang: ts
206
- code: |
207
- import { fakerDE_AT as faker } from '@faker-js/faker'
208
- - name: include
209
- type: Array<Include>
210
- required: false
211
- description: Array containing include parameters to include tags, operations, methods, paths, or content types.
212
- codeBlock:
213
- lang: typescript
214
- title: Include
215
- code: |
216
- export type Include = {
217
- type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
218
- pattern: string | RegExp
219
- }
220
- - name: exclude
221
- type: Array<Exclude>
222
- required: false
223
- description: Array containing exclude parameters to exclude or skip tags, operations, methods, paths, or content types.
224
- codeBlock:
225
- lang: typescript
226
- title: Exclude
227
- code: |
228
- export type Exclude = {
229
- type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
230
- pattern: string | RegExp
231
- }
232
- - name: override
233
- type: Array<Override>
234
- required: false
235
- description: Array containing override parameters to override `options` based on tags, operations, methods, paths, or content types.
236
- codeBlock:
237
- lang: typescript
238
- title: Override
239
- code: |
240
- export type Override = {
241
- type: 'tag' | 'operationId' | 'path' | 'method' | 'contentType'
242
- pattern: string | RegExp
243
- options: PluginOptions
244
- }
245
- - name: generators
246
- type: Array<Generator<PluginFaker>>
247
- required: false
248
- experimental: true
249
- description: |
250
- Define additional generators next to the built-in generators.
251
-
252
- See [Generators](https://kubb.dev/docs/5.x/guides/creating-plugins) for more information on how to use generators.
253
- - name: resolver
254
- type: Partial<ResolverFaker>
255
- required: false
256
- description: Customize helper naming on top of the active compatibility preset.
257
- codeBlock:
258
- lang: typescript
259
- code: |
260
- pluginFaker({
261
- resolver: {
262
- resolveName(name) {
263
- return `${this.default(name)}Mock`
264
- },
265
- },
266
- })
267
- - name: transformer
268
- type: ast.Visitor
269
- required: false
270
- description: Apply a single AST visitor before the faker helpers are rendered.
271
- codeBlock:
272
- lang: typescript
273
- code: |
274
- pluginFaker({
275
- transformer: {
276
- schema(node) {
277
- return { ...node, description: undefined }
278
- },
279
- },
280
- })
281
- - name: printer
282
- type: '{ nodes?: PrinterFakerNodes }'
283
- required: false
284
- description: |
285
- Override individual printer node handlers to customize how specific schema types are rendered.
286
-
287
- 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.
288
- examples:
289
- - name: Override integer to float()
290
- files:
291
- - lang: typescript
292
- code: |
293
- import { pluginFaker } from '@kubb/plugin-faker'
294
-
295
- pluginFaker({
296
- printer: {
297
- nodes: {
298
- integer() {
299
- return 'faker.number.float()'
300
- },
301
- },
302
- },
303
- })
304
- twoslash: false
305
- - name: Override date strings
306
- files:
307
- - lang: typescript
308
- code: |
309
- import { pluginFaker } from '@kubb/plugin-faker'
310
-
311
- pluginFaker({
312
- printer: {
313
- nodes: {
314
- date(node) {
315
- if (node.representation === 'string') {
316
- return 'new Date().toISOString().substring(0, 10)'
317
- }
318
-
319
- return 'new Date()'
320
- },
321
- },
322
- },
323
- })
324
- twoslash: false
325
- examples:
326
- - name: kubb.config.ts
327
- files:
328
- - lang: typescript
329
- code: |
330
- import { defineConfig } from 'kubb'
331
- import { pluginFaker } from '@kubb/plugin-faker'
332
- import { pluginTs } from '@kubb/plugin-ts'
333
-
334
- export default defineConfig({
335
- input: {
336
- path: './petStore.yaml',
337
- },
338
- output: {
339
- path: './src/gen',
340
- },
341
- plugins: [
342
- pluginTs({
343
- output: {
344
- path: './types',
345
- },
346
- }),
347
- pluginFaker({
348
- output: {
349
- path: './mocks',
350
- barrel: { type: 'named' },
351
- },
352
- seed: [100],
353
- paramsCasing: 'camelcase',
354
- }),
355
- ],
356
- })
357
- twoslash: false