@kubb/plugin-faker 5.1.3 → 5.1.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.
- package/README.md +2 -2
- package/dist/index.cjs +4 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/docs/options.md +205 -0
- package/{OVERVIEW.md → docs/overview.md} +24 -1
- package/kubb-extension.json +9 -2
- package/package.json +6 -6
package/docs/options.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# Options
|
|
2
|
+
|
|
3
|
+
Pass these options to `pluginFaker()`, all of them optional. Shared options link to [Shared plugin options](https://kubb.dev/docs/5.x/reference/plugin-options).
|
|
4
|
+
|
|
5
|
+
## Options overview
|
|
6
|
+
|
|
7
|
+
| Option | Purpose | Default |
|
|
8
|
+
| ------------------------------------------------------------------------- | --------------------------------------------------------------------------- | ---------------------------------------------- |
|
|
9
|
+
| [`output`](https://kubb.dev/docs/5.x/reference/plugin-options#output) | Where files are written: `path`, `mode`, `barrel`, `banner`, `footer`. | `{ path: 'mocks', barrel: { type: 'named' } }` |
|
|
10
|
+
| [`group`](https://kubb.dev/docs/5.x/reference/plugin-options#group) | Split output by tag or path: `type`, `name`. | None |
|
|
11
|
+
| [`typeMode`](#typemode) | Whether factory types come from the generated values or the declared model. | `'inferred'` |
|
|
12
|
+
| [`dateParser`](#dateparser) | Library that formats string date and time fields. | `'faker'` |
|
|
13
|
+
| [`regexGenerator`](#regexgenerator) | Library that turns a regex `pattern` into a string. | `'faker'` |
|
|
14
|
+
| [`locale`](#locale) | Faker locale code for the generated values. | `'en'` |
|
|
15
|
+
| [`seed`](#seed) | Value passed to `faker.seed(...)` for deterministic output. | None |
|
|
16
|
+
| [`include`](https://kubb.dev/docs/5.x/reference/plugin-options#include) | Keep only operations and schemas that match. | None |
|
|
17
|
+
| [`exclude`](https://kubb.dev/docs/5.x/reference/plugin-options#exclude) | Skip operations and schemas that match. | `[]` |
|
|
18
|
+
| [`override`](https://kubb.dev/docs/5.x/reference/plugin-options#override) | Apply different options per pattern. | `[]` |
|
|
19
|
+
| [`resolver`](#resolver) | Customize generated names and file paths. | `resolverFaker` |
|
|
20
|
+
| [`macros`](https://kubb.dev/docs/5.x/reference/plugin-options#macros) | Rewrite AST nodes before printing. | `[]` |
|
|
21
|
+
| [`printer`](#printer) | Replace the handler for a schema type. | None |
|
|
22
|
+
|
|
23
|
+
## Option details
|
|
24
|
+
|
|
25
|
+
### typeMode
|
|
26
|
+
|
|
27
|
+
Controls the input and return types of object and intersection factories. Both modes generate the same values and apply overrides with a shallow merge.
|
|
28
|
+
|
|
29
|
+
| | |
|
|
30
|
+
| -------- | ------------------------ |
|
|
31
|
+
| Type | `'inferred' \| 'schema'` |
|
|
32
|
+
| Required | `false` |
|
|
33
|
+
| Default | `'inferred'` |
|
|
34
|
+
|
|
35
|
+
::field-group
|
|
36
|
+
|
|
37
|
+
:::field{name="'inferred'"}
|
|
38
|
+
Default value. Derives return types from generated values and supplied overrides. Optional and nullable fields retain their concrete value types, and overrides preserve literal types.
|
|
39
|
+
:::
|
|
40
|
+
|
|
41
|
+
:::field{name="'schema'"}
|
|
42
|
+
Accepts `Partial<Model>` and returns the declared model type, including optional and nullable fields. Use it for fixtures you modify after creation.
|
|
43
|
+
:::
|
|
44
|
+
|
|
45
|
+
::
|
|
46
|
+
|
|
47
|
+
::code-group
|
|
48
|
+
|
|
49
|
+
```typescript ['inferred' (default)]
|
|
50
|
+
// export function createOrder<TData extends Partial<Order> = object>(data?: TData)
|
|
51
|
+
const order = createOrder({ quantity: 2, complete: true })
|
|
52
|
+
const complete: true = order.complete
|
|
53
|
+
|
|
54
|
+
// TypeScript error: false is not assignable to true.
|
|
55
|
+
order.complete = false
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
```typescript ['schema']
|
|
59
|
+
// export function createOrder(data?: Partial<Order>): Order
|
|
60
|
+
const order = createOrder({ quantity: 2, complete: true })
|
|
61
|
+
order.complete = false
|
|
62
|
+
|
|
63
|
+
// TypeScript error: foo does not exist in Partial<Order>.
|
|
64
|
+
createOrder({ quantity: 2, foo: '' })
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
::
|
|
68
|
+
|
|
69
|
+
Use [`override`](https://kubb.dev/docs/5.x/reference/plugin-options#override) to select a mode for matching schemas. String patterns are regular expressions, so `'^Order$'` matches only the `Order` schema:
|
|
70
|
+
|
|
71
|
+
```typescript [kubb.config.ts]
|
|
72
|
+
pluginFaker({
|
|
73
|
+
override: [{ type: 'schemaName', pattern: '^Order$', options: { typeMode: 'schema' } }],
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### dateParser
|
|
78
|
+
|
|
79
|
+
Library used to format `date` and `time` fields represented as strings. Pick a value other than `'faker'` when your project already uses a date library, and install that library in the consuming app. Generated `date` and `time` strings use `YYYY-MM-DD` and `HH:mm:ss`, and `date-time` values keep the ISO string.
|
|
80
|
+
|
|
81
|
+
| | |
|
|
82
|
+
| -------- | ------------------------------------------ |
|
|
83
|
+
| Type | `'faker' \| 'dayjs' \| 'moment' \| string` |
|
|
84
|
+
| Required | `false` |
|
|
85
|
+
| Default | `'faker'` |
|
|
86
|
+
|
|
87
|
+
::field-group
|
|
88
|
+
|
|
89
|
+
:::field{name="'faker'"}
|
|
90
|
+
Default value. Renders a `date` field as `faker.date.anytime().toISOString().substring(0, 10)`.
|
|
91
|
+
:::
|
|
92
|
+
|
|
93
|
+
:::field{name="'dayjs'"}
|
|
94
|
+
Renders it as `dayjs(faker.date.anytime()).format('YYYY-MM-DD')`.
|
|
95
|
+
:::
|
|
96
|
+
|
|
97
|
+
:::field{name="'moment'"}
|
|
98
|
+
Renders it as `moment(faker.date.anytime()).format('YYYY-MM-DD')`.
|
|
99
|
+
:::
|
|
100
|
+
|
|
101
|
+
:::field{name="Custom module"}
|
|
102
|
+
Pass a module name that exports a default formatting function. Kubb adds the import to generated files.
|
|
103
|
+
:::
|
|
104
|
+
|
|
105
|
+
::
|
|
106
|
+
|
|
107
|
+
### regexGenerator
|
|
108
|
+
|
|
109
|
+
Library used to generate strings that satisfy a regex `pattern` in the spec.
|
|
110
|
+
|
|
111
|
+
| | |
|
|
112
|
+
| -------- | ---------------------- |
|
|
113
|
+
| Type | `'faker' \| 'randexp'` |
|
|
114
|
+
| Required | `false` |
|
|
115
|
+
| Default | `'faker'` |
|
|
116
|
+
|
|
117
|
+
::field-group
|
|
118
|
+
|
|
119
|
+
:::field{name="'faker'"}
|
|
120
|
+
Default value. Generates `faker.helpers.fromRegExp(pattern)` without an extra dependency.
|
|
121
|
+
:::
|
|
122
|
+
|
|
123
|
+
:::field{name="'randexp'"}
|
|
124
|
+
Generates `new RandExp(pattern).gen()`. Supports a wider regex grammar and requires the `randexp` runtime dependency.
|
|
125
|
+
:::
|
|
126
|
+
|
|
127
|
+
::
|
|
128
|
+
|
|
129
|
+
### locale
|
|
130
|
+
|
|
131
|
+
Faker locale code. It switches the named import to `fakerXX` from `@faker-js/faker`, so generated values reflect the target region. The default `'en'` imports `fakerEN`, `'de'` imports `fakerDE`, and `'de_AT'` imports `fakerDE_AT`. See [Faker.js localization](https://fakerjs.dev/api/localization.html) for all locale codes.
|
|
132
|
+
|
|
133
|
+
| | |
|
|
134
|
+
| -------- | -------- |
|
|
135
|
+
| Type | `string` |
|
|
136
|
+
| Required | `false` |
|
|
137
|
+
| Default | `'en'` |
|
|
138
|
+
|
|
139
|
+
### seed
|
|
140
|
+
|
|
141
|
+
Kubb passes this value to `faker.seed(...)` at the top of each factory, so every run returns the same data. Use it for snapshot tests. Pass a single number or an array of numbers. Factories still accept overrides, such as `createPet({ name: 'Rex' })`.
|
|
142
|
+
|
|
143
|
+
| | |
|
|
144
|
+
| -------- | -------------------- |
|
|
145
|
+
| Type | `number \| number[]` |
|
|
146
|
+
| Required | `false` |
|
|
147
|
+
|
|
148
|
+
### resolver
|
|
149
|
+
|
|
150
|
+
Overrides generated file and symbol names. Omitted members keep `resolverFaker`. The shared members (`name`, `file`, `imports`) and the `this` context are described under [`resolver`](https://kubb.dev/docs/5.x/reference/plugin-options#resolver).
|
|
151
|
+
|
|
152
|
+
| | |
|
|
153
|
+
| -------- | ------------------------------ |
|
|
154
|
+
| Type | `ResolverPatch<ResolverFaker>` |
|
|
155
|
+
| Required | `false` |
|
|
156
|
+
|
|
157
|
+
::code-collapse{name="ResolverFaker patch members"}
|
|
158
|
+
|
|
159
|
+
```typescript [Partial override]
|
|
160
|
+
type ResolverFakerPatch = {
|
|
161
|
+
name?(name: string): string
|
|
162
|
+
file?: {
|
|
163
|
+
baseName?(params: { name: string; extname: string }): string
|
|
164
|
+
path?(params: { baseName: string; output: Output }): string
|
|
165
|
+
}
|
|
166
|
+
imports?(options: ResolveImportsOptions): Array<ImportNode>
|
|
167
|
+
param?: {
|
|
168
|
+
name?(node: OperationNode, param: ParameterNode): string // → 'showPetByIdPathPetId'
|
|
169
|
+
path?(node: OperationNode, param: ParameterNode): string // → 'createShowPetByIdPath'
|
|
170
|
+
query?(node: OperationNode, param: ParameterNode): string // → 'createListPetsQuery'
|
|
171
|
+
headers?(node: OperationNode, param: ParameterNode): string // → 'createDeletePetHeaders'
|
|
172
|
+
}
|
|
173
|
+
response?: {
|
|
174
|
+
status?(node: OperationNode, statusCode: StatusCode): string // → 'listPetsStatus200'
|
|
175
|
+
body?(node: OperationNode): string // → 'createPetsBody'
|
|
176
|
+
response?(node: OperationNode): string // → 'listPetsResponse'
|
|
177
|
+
responses?(node: OperationNode): string // → 'listPetsResponses'
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
::
|
|
183
|
+
|
|
184
|
+
### printer
|
|
185
|
+
|
|
186
|
+
Replaces the Faker node handler for a specific schema type, such as `'integer'`, `'date'`, or `'string'`. Each handler returns the Faker expression as a string. Use `this.transform` to recurse into nested nodes and `this.options` to read printer options. The [printer guide](https://kubb.dev/docs/5.x/how-to/printers) covers how overrides compose with macros.
|
|
187
|
+
|
|
188
|
+
| | |
|
|
189
|
+
| -------- | ------------------------------- |
|
|
190
|
+
| Type | `{ nodes?: PrinterFakerNodes }` |
|
|
191
|
+
| Required | `false` |
|
|
192
|
+
|
|
193
|
+
```typescript [Map integer schemas to a float]
|
|
194
|
+
import { pluginFaker } from '@kubb/plugin-faker'
|
|
195
|
+
|
|
196
|
+
pluginFaker({
|
|
197
|
+
printer: {
|
|
198
|
+
nodes: {
|
|
199
|
+
integer() {
|
|
200
|
+
return 'faker.number.float()'
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
})
|
|
205
|
+
```
|
|
@@ -15,4 +15,27 @@ Generates a Faker.js mock-data factory for every schema in your OpenAPI spec. Us
|
|
|
15
15
|
- `locale` sets the Faker locale. The default is `en`.
|
|
16
16
|
- `dateParser` formats date and time strings with `faker` (default), `dayjs`, or `moment`. Install the library you pick.
|
|
17
17
|
- `regexGenerator` turns a `pattern` into a string with `faker` (default) or `randexp`.
|
|
18
|
-
- `typeMode` sets the factory types: `inferred` (default) derives them from the generated values
|
|
18
|
+
- `typeMode` sets the factory types: `inferred` (default) derives them from the generated values. `schema` uses the declared model type.
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```typescript [kubb.config.ts]
|
|
23
|
+
import { defineConfig } from 'kubb/config'
|
|
24
|
+
import { pluginTs } from '@kubb/plugin-ts'
|
|
25
|
+
import { pluginFaker } from '@kubb/plugin-faker'
|
|
26
|
+
|
|
27
|
+
export default defineConfig({
|
|
28
|
+
input: './petStore.yaml',
|
|
29
|
+
output: { path: './src/gen' },
|
|
30
|
+
plugins: [
|
|
31
|
+
pluginTs(),
|
|
32
|
+
pluginFaker({ seed: [100] }),
|
|
33
|
+
],
|
|
34
|
+
})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## See also
|
|
38
|
+
|
|
39
|
+
- [Faker.js](https://fakerjs.dev/)
|
|
40
|
+
- [@kubb/plugin-ts](https://kubb.dev/extensions/kubb-labs/plugin-ts)
|
|
41
|
+
- [@kubb/plugin-msw](https://kubb.dev/extensions/kubb-labs/plugin-msw)
|
package/kubb-extension.json
CHANGED
|
@@ -10,9 +10,16 @@
|
|
|
10
10
|
"light": "https://kubb.dev/feature/faker.svg"
|
|
11
11
|
},
|
|
12
12
|
"tags": ["faker", "mock-data", "mocks", "fixtures", "testing", "codegen", "openapi"],
|
|
13
|
-
"docs": "https://kubb.dev/
|
|
13
|
+
"docs": "https://kubb.dev/extensions/kubb-labs/plugin-faker",
|
|
14
|
+
"maintainers": [
|
|
15
|
+
{
|
|
16
|
+
"name": "Stijn Van Hulle",
|
|
17
|
+
"github": "stijnvanhulle"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"dependencies": ["plugin-ts"],
|
|
14
21
|
"compatibility": {
|
|
15
|
-
"kubb": ">=5.8.
|
|
22
|
+
"kubb": ">=5.8.3",
|
|
16
23
|
"node": ">=22"
|
|
17
24
|
},
|
|
18
25
|
"options": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-faker",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.4",
|
|
4
4
|
"description": "Generate Faker.js mock data factories with Kubb. Produces realistic seed data and fixtures for development and testing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generation",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"mocks",
|
|
16
16
|
"typescript"
|
|
17
17
|
],
|
|
18
|
-
"homepage": "https://kubb.dev/
|
|
18
|
+
"homepage": "https://kubb.dev/extensions/kubb-labs/plugin-faker",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"author": "stijnvanhulle",
|
|
21
21
|
"repository": {
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"files": [
|
|
41
41
|
"dist",
|
|
42
42
|
"kubb-extension.json",
|
|
43
|
-
"
|
|
43
|
+
"docs",
|
|
44
44
|
"!/**/**.test.**",
|
|
45
45
|
"!/**/__tests__/**",
|
|
46
46
|
"!/**/__snapshots__/**"
|
|
@@ -74,17 +74,17 @@
|
|
|
74
74
|
"registry": "https://registry.npmjs.org/"
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
-
"@kubb/plugin-ts": "^5.0.
|
|
77
|
+
"@kubb/plugin-ts": "^5.0.9"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
80
|
"@faker-js/faker": "^10.6.0",
|
|
81
81
|
"@internals/shared": "0.0.0",
|
|
82
82
|
"@internals/utils": "0.0.0",
|
|
83
|
-
"kubb": "^5.8.
|
|
83
|
+
"kubb": "^5.8.3"
|
|
84
84
|
},
|
|
85
85
|
"peerDependencies": {
|
|
86
86
|
"@faker-js/faker": "^10.0.0",
|
|
87
|
-
"kubb": "^5.8.
|
|
87
|
+
"kubb": "^5.8.3"
|
|
88
88
|
},
|
|
89
89
|
"peerDependenciesMeta": {
|
|
90
90
|
"@faker-js/faker": {
|