@kubb/plugin-faker 5.0.0-alpha.9 → 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.
- package/LICENSE +17 -10
- package/README.md +1 -4
- package/dist/Faker-BgleOzVN.cjs +486 -0
- package/dist/Faker-BgleOzVN.cjs.map +1 -0
- package/dist/Faker-CdyPfOPg.d.ts +27 -0
- package/dist/Faker-fcQEB9i5.js +384 -0
- package/dist/Faker-fcQEB9i5.js.map +1 -0
- package/dist/components.cjs +2 -2
- package/dist/components.d.ts +2 -31
- package/dist/components.js +1 -1
- package/dist/fakerGenerator-C3Ho3BaI.d.ts +9 -0
- package/dist/fakerGenerator-D7daHCh6.js +516 -0
- package/dist/fakerGenerator-D7daHCh6.js.map +1 -0
- package/dist/fakerGenerator-VJEVzLjc.cjs +526 -0
- package/dist/fakerGenerator-VJEVzLjc.cjs.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +2 -505
- package/dist/generators.js +1 -1
- package/dist/index.cjs +136 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -4
- package/dist/index.js +128 -83
- package/dist/index.js.map +1 -1
- package/dist/printerFaker-CJiwzoto.d.ts +206 -0
- package/package.json +52 -50
- package/src/components/Faker.tsx +124 -78
- package/src/generators/fakerGenerator.tsx +235 -134
- package/src/index.ts +7 -2
- package/src/plugin.ts +60 -121
- package/src/printers/printerFaker.ts +341 -0
- package/src/resolvers/resolverFaker.ts +92 -0
- package/src/types.ts +127 -81
- package/src/utils.ts +356 -0
- 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/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/types-r7BubMLO.d.ts +0 -132
- 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/package.json
CHANGED
|
@@ -1,35 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-faker",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-beta.3",
|
|
4
4
|
"description": "Faker.js data generator plugin for Kubb, creating realistic mock data from OpenAPI specifications for development and testing.",
|
|
5
5
|
"keywords": [
|
|
6
|
+
"code-generator",
|
|
7
|
+
"codegen",
|
|
8
|
+
"data-generation",
|
|
9
|
+
"development",
|
|
6
10
|
"faker",
|
|
7
11
|
"faker.js",
|
|
8
12
|
"fakerjs",
|
|
13
|
+
"fixtures",
|
|
14
|
+
"kubb",
|
|
9
15
|
"mock-data",
|
|
10
16
|
"mocks",
|
|
11
|
-
"data-generation",
|
|
12
|
-
"fixtures",
|
|
13
|
-
"testing",
|
|
14
|
-
"development",
|
|
15
|
-
"typescript",
|
|
16
|
-
"openapi",
|
|
17
|
-
"swagger",
|
|
18
17
|
"oas",
|
|
19
|
-
"
|
|
20
|
-
"codegen",
|
|
18
|
+
"openapi",
|
|
21
19
|
"plugins",
|
|
22
|
-
"
|
|
20
|
+
"swagger",
|
|
21
|
+
"testing",
|
|
22
|
+
"typescript"
|
|
23
23
|
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "stijnvanhulle",
|
|
24
26
|
"repository": {
|
|
25
27
|
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/kubb-labs/
|
|
28
|
+
"url": "git+https://github.com/kubb-labs/plugins.git",
|
|
27
29
|
"directory": "packages/plugin-faker"
|
|
28
30
|
},
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
"files": [
|
|
32
|
+
"src",
|
|
33
|
+
"dist",
|
|
34
|
+
"plugin.json",
|
|
35
|
+
"!/**/**.test.**",
|
|
36
|
+
"!/**/__tests__/**",
|
|
37
|
+
"!/**/__snapshots__/**"
|
|
38
|
+
],
|
|
32
39
|
"type": "module",
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"main": "./dist/index.cjs",
|
|
42
|
+
"module": "./dist/index.js",
|
|
43
|
+
"types": "./dist/index.d.ts",
|
|
44
|
+
"typesVersions": {
|
|
45
|
+
"*": {
|
|
46
|
+
"components": [
|
|
47
|
+
"./dist/components.d.ts"
|
|
48
|
+
],
|
|
49
|
+
"generators": [
|
|
50
|
+
"./dist/generators.d.ts"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
},
|
|
33
54
|
"exports": {
|
|
34
55
|
".": {
|
|
35
56
|
"import": "./dist/index.js",
|
|
@@ -45,24 +66,21 @@
|
|
|
45
66
|
},
|
|
46
67
|
"./package.json": "./package.json"
|
|
47
68
|
},
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
69
|
+
"publishConfig": {
|
|
70
|
+
"access": "public",
|
|
71
|
+
"registry": "https://registry.npmjs.org/"
|
|
72
|
+
},
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"@kubb/core": "5.0.0-beta.3",
|
|
75
|
+
"@kubb/renderer-jsx": "5.0.0-beta.3",
|
|
76
|
+
"@kubb/plugin-ts": "5.0.0-beta.3"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@internals/utils": "0.0.0"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"@kubb/renderer-jsx": "5.0.0-beta.3"
|
|
58
83
|
},
|
|
59
|
-
"files": [
|
|
60
|
-
"src",
|
|
61
|
-
"dist",
|
|
62
|
-
"!/**/**.test.**",
|
|
63
|
-
"!/**/__tests__/**",
|
|
64
|
-
"!/**/__snapshots__/**"
|
|
65
|
-
],
|
|
66
84
|
"size-limit": [
|
|
67
85
|
{
|
|
68
86
|
"path": "./dist/*.js",
|
|
@@ -70,30 +88,14 @@
|
|
|
70
88
|
"gzip": true
|
|
71
89
|
}
|
|
72
90
|
],
|
|
73
|
-
"dependencies": {
|
|
74
|
-
"@kubb/react-fabric": "0.14.0",
|
|
75
|
-
"@kubb/oas": "5.0.0-alpha.9",
|
|
76
|
-
"@kubb/core": "5.0.0-alpha.9",
|
|
77
|
-
"@kubb/plugin-oas": "5.0.0-alpha.9",
|
|
78
|
-
"@kubb/plugin-ts": "5.0.0-alpha.9"
|
|
79
|
-
},
|
|
80
91
|
"engines": {
|
|
81
92
|
"node": ">=22"
|
|
82
93
|
},
|
|
83
|
-
"publishConfig": {
|
|
84
|
-
"access": "public",
|
|
85
|
-
"registry": "https://registry.npmjs.org/"
|
|
86
|
-
},
|
|
87
|
-
"main": "./dist/index.cjs",
|
|
88
|
-
"module": "./dist/index.js",
|
|
89
|
-
"devDependencies": {
|
|
90
|
-
"@internals/utils": "0.0.0"
|
|
91
|
-
},
|
|
92
94
|
"scripts": {
|
|
93
95
|
"build": "tsdown && size-limit",
|
|
94
96
|
"clean": "npx rimraf ./dist",
|
|
95
|
-
"lint": "
|
|
96
|
-
"lint:fix": "
|
|
97
|
+
"lint": "oxlint .",
|
|
98
|
+
"lint:fix": "oxlint --fix .",
|
|
97
99
|
"release": "pnpm publish --no-git-check",
|
|
98
100
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
99
101
|
"start": "tsdown --watch",
|
package/src/components/Faker.tsx
CHANGED
|
@@ -1,107 +1,153 @@
|
|
|
1
1
|
import { jsStringEscape } from '@internals/utils'
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { File, Function
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
2
|
+
import { ast } from '@kubb/core'
|
|
3
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
+
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
+
import type { PrinterFakerFactory } from '../printers/printerFaker.ts'
|
|
7
7
|
import type { PluginFaker } from '../types.ts'
|
|
8
|
+
import { resolveFakerTypeUsage } from '../utils.ts'
|
|
8
9
|
|
|
9
10
|
type Props = {
|
|
10
11
|
name: string
|
|
11
12
|
typeName: string
|
|
12
|
-
|
|
13
|
+
node: ast.SchemaNode
|
|
14
|
+
printer: ast.Printer<PrinterFakerFactory>
|
|
13
15
|
seed?: PluginFaker['options']['seed']
|
|
14
16
|
description?: string
|
|
15
|
-
regexGenerator?: PluginFaker['options']['regexGenerator']
|
|
16
|
-
mapper?: PluginFaker['options']['mapper']
|
|
17
|
-
dateParser?: PluginFaker['options']['dateParser']
|
|
18
17
|
canOverride: boolean
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
const isSimpleFloat = name === 'float'
|
|
20
|
+
const OBJECT_TYPES = new Set<ast.SchemaNode['type']>(['object', 'intersection'])
|
|
21
|
+
const ARRAY_TYPES = new Set<ast.SchemaNode['type']>(['array'])
|
|
22
|
+
const SCALAR_TYPES = new Set<ast.SchemaNode['type']>([
|
|
23
|
+
'string',
|
|
24
|
+
'email',
|
|
25
|
+
'url',
|
|
26
|
+
'uuid',
|
|
27
|
+
'number',
|
|
28
|
+
'integer',
|
|
29
|
+
'bigint',
|
|
30
|
+
'boolean',
|
|
31
|
+
'date',
|
|
32
|
+
'time',
|
|
33
|
+
'datetime',
|
|
34
|
+
'blob',
|
|
35
|
+
'enum',
|
|
36
|
+
])
|
|
37
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
38
|
+
|
|
39
|
+
export function Faker({ node, description, name, typeName, printer, seed, canOverride }: Props): KubbReactNode {
|
|
40
|
+
const fakerText = printer.print(node) ?? 'undefined'
|
|
41
|
+
|
|
42
|
+
const isArray = ARRAY_TYPES.has(node.type)
|
|
43
|
+
const isObject = OBJECT_TYPES.has(node.type)
|
|
44
|
+
const isTuple = node.type === 'tuple'
|
|
45
|
+
const isScalar = SCALAR_TYPES.has(node.type)
|
|
48
46
|
|
|
49
47
|
let fakerTextWithOverride = fakerText
|
|
48
|
+
let useGenericOverride = false
|
|
50
49
|
|
|
51
50
|
if (canOverride && isObject) {
|
|
52
|
-
|
|
53
|
-
...${fakerText},
|
|
54
|
-
...data || {}
|
|
55
|
-
}`
|
|
51
|
+
useGenericOverride = true
|
|
56
52
|
}
|
|
57
53
|
|
|
58
|
-
if (canOverride && isTuple)
|
|
54
|
+
if (canOverride && isTuple) {
|
|
55
|
+
fakerTextWithOverride = `data || ${fakerText}`
|
|
56
|
+
}
|
|
59
57
|
|
|
60
58
|
if (canOverride && isArray) {
|
|
61
59
|
fakerTextWithOverride = `[
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
...${fakerText},
|
|
61
|
+
...(data || [])
|
|
62
|
+
]`
|
|
65
63
|
}
|
|
66
64
|
|
|
67
|
-
if (canOverride &&
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (canOverride && isSimpleFloat) fakerTextWithOverride = 'data ?? faker.number.float()'
|
|
72
|
-
|
|
73
|
-
let type = `Partial<${typeName}>`
|
|
74
|
-
|
|
75
|
-
if (isArray) type = typeName
|
|
76
|
-
if (isRefToArray) type = typeName
|
|
77
|
-
if (isSimpleString) type = name
|
|
78
|
-
if (isSimpleInt || isSimpleFloat) type = 'number'
|
|
79
|
-
|
|
80
|
-
const params = FunctionParams.factory({
|
|
81
|
-
data: {
|
|
82
|
-
// making a partial out of an array does not make sense
|
|
83
|
-
type,
|
|
84
|
-
optional: true,
|
|
85
|
-
},
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
let returnType = canOverride ? typeName : undefined
|
|
65
|
+
if (canOverride && isScalar) {
|
|
66
|
+
fakerTextWithOverride = `data ?? ${fakerText}`
|
|
67
|
+
}
|
|
89
68
|
|
|
90
|
-
|
|
69
|
+
const { dataType, returnType: resolvedReturnType } = resolveFakerTypeUsage(node, typeName, canOverride)
|
|
70
|
+
|
|
71
|
+
let functionSignature = ''
|
|
72
|
+
let functionBody = ''
|
|
73
|
+
|
|
74
|
+
if (useGenericOverride) {
|
|
75
|
+
// Generate function with defaultFakeData structure
|
|
76
|
+
const jsdoc = description ? `/**\n * @description ${jsStringEscape(description)}\n */\n ` : ''
|
|
77
|
+
functionSignature = `${jsdoc}export function ${name}(data?: Partial<${typeName}>): Required<${typeName}>`
|
|
78
|
+
|
|
79
|
+
const seedCode = seed ? `faker.seed(${JSON.stringify(seed)})\n ` : ''
|
|
80
|
+
|
|
81
|
+
// When the object node has properties that transitively reference a cyclic schema,
|
|
82
|
+
// the printer emits memoizing getters for those properties. Spreading the object
|
|
83
|
+
// literal would immediately invoke those getters, triggering recursive faker calls
|
|
84
|
+
// and causing a stack overflow. Detect this upfront via ast helpers so we can
|
|
85
|
+
// use Object.defineProperty-based merging instead of spread.
|
|
86
|
+
const { cyclicSchemas, schemaName } = printer.options
|
|
87
|
+
const hasGetters =
|
|
88
|
+
node.type === 'object' &&
|
|
89
|
+
!!cyclicSchemas &&
|
|
90
|
+
(node.properties ?? []).some((p) => ast.containsCircularRef(p.schema, { circularSchemas: cyclicSchemas, excludeName: schemaName }))
|
|
91
|
+
|
|
92
|
+
if (hasGetters) {
|
|
93
|
+
functionBody = `{
|
|
94
|
+
${seedCode}const defaultFakeData = ${fakerText}
|
|
95
|
+
if (data) {
|
|
96
|
+
for (const [key, value] of Object.entries(data)) {
|
|
97
|
+
Object.defineProperty(defaultFakeData, key, { value, configurable: true, writable: true, enumerable: true })
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return defaultFakeData as Required<${typeName}>
|
|
101
|
+
}`
|
|
102
|
+
} else {
|
|
103
|
+
functionBody = `{
|
|
104
|
+
${seedCode}const defaultFakeData = ${fakerText}
|
|
105
|
+
return {
|
|
106
|
+
...defaultFakeData,
|
|
107
|
+
...(data || {}),
|
|
108
|
+
} as Required<${typeName}>
|
|
109
|
+
}`
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
const usesData = /\bdata\b/.test(fakerTextWithOverride)
|
|
113
|
+
const dataParamName = usesData ? 'data' : '_data'
|
|
114
|
+
const params = ast.createFunctionParameters({
|
|
115
|
+
params: [
|
|
116
|
+
ast.createFunctionParameter({
|
|
117
|
+
name: dataParamName,
|
|
118
|
+
type: ast.createParamsType({ variant: 'reference', name: dataType }),
|
|
119
|
+
optional: true,
|
|
120
|
+
}),
|
|
121
|
+
],
|
|
122
|
+
})
|
|
123
|
+
const paramsSignature = declarationPrinter.print(params) ?? ''
|
|
124
|
+
const returnType = resolvedReturnType
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<File.Source name={name} isExportable isIndexable>
|
|
128
|
+
<Function
|
|
129
|
+
export
|
|
130
|
+
name={name}
|
|
131
|
+
JSDoc={{ comments: description ? [`@description ${jsStringEscape(description)}`] : [] }}
|
|
132
|
+
params={canOverride ? paramsSignature : undefined}
|
|
133
|
+
returnType={returnType}
|
|
134
|
+
>
|
|
135
|
+
{seed ? (
|
|
136
|
+
<>
|
|
137
|
+
{`faker.seed(${JSON.stringify(seed)})`}
|
|
138
|
+
<br />
|
|
139
|
+
</>
|
|
140
|
+
) : undefined}
|
|
141
|
+
{`return ${fakerTextWithOverride}`}
|
|
142
|
+
</Function>
|
|
143
|
+
</File.Source>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
91
146
|
|
|
92
147
|
return (
|
|
93
148
|
<File.Source name={name} isExportable isIndexable>
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
name={name}
|
|
97
|
-
JSDoc={{ comments: [description ? `@description ${jsStringEscape(description)}` : undefined].filter(Boolean) }}
|
|
98
|
-
params={canOverride ? params.toConstructor() : undefined}
|
|
99
|
-
returnType={returnType}
|
|
100
|
-
>
|
|
101
|
-
{seed ? `faker.seed(${JSON.stringify(seed)})` : undefined}
|
|
102
|
-
<br />
|
|
103
|
-
{`return ${fakerTextWithOverride}`}
|
|
104
|
-
</Function>
|
|
149
|
+
{functionSignature}
|
|
150
|
+
{functionBody}
|
|
105
151
|
</File.Source>
|
|
106
152
|
)
|
|
107
153
|
}
|