@kubb/plugin-faker 3.16.1 → 3.16.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/dist/components-BpyQzZAY.cjs +324 -0
- package/dist/components-BpyQzZAY.cjs.map +1 -0
- package/dist/components-BwSq4Ikw.js +290 -0
- package/dist/components-BwSq4Ikw.js.map +1 -0
- package/dist/components.cjs +2 -11
- package/dist/components.d.cts +24 -15
- package/dist/components.d.ts +24 -15
- package/dist/components.js +3 -3
- package/dist/fakerGenerator-CXR9ksYA.js +199 -0
- package/dist/fakerGenerator-CXR9ksYA.js.map +1 -0
- package/dist/fakerGenerator-S8sgrtl5.cjs +204 -0
- package/dist/fakerGenerator-S8sgrtl5.cjs.map +1 -0
- package/dist/generators.cjs +3 -12
- package/dist/generators.d.cts +5 -6
- package/dist/generators.d.ts +5 -6
- package/dist/generators.js +4 -4
- package/dist/index.cjs +96 -124
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -7
- package/dist/index.d.ts +6 -7
- package/dist/index.js +96 -118
- package/dist/index.js.map +1 -1
- package/dist/types-Bh_6SttY.d.cts +1214 -0
- package/dist/types-DYUKPbgH.d.ts +1214 -0
- package/package.json +23 -28
- package/src/generators/__snapshots__/pet.ts +1 -0
- package/src/generators/__snapshots__/petWithDateString.ts +1 -0
- package/src/generators/__snapshots__/petWithDayjs.ts +1 -0
- package/src/generators/__snapshots__/petWithMapper.ts +1 -0
- package/src/generators/__snapshots__/petWithRandExp.ts +1 -0
- package/src/parser.ts +12 -3
- package/dist/chunk-BQQKSGJ4.js +0 -370
- package/dist/chunk-BQQKSGJ4.js.map +0 -1
- package/dist/chunk-DP3UK3GA.js +0 -137
- package/dist/chunk-DP3UK3GA.js.map +0 -1
- package/dist/chunk-DUV3ZLQX.cjs +0 -139
- package/dist/chunk-DUV3ZLQX.cjs.map +0 -1
- package/dist/chunk-ZA3QHI3O.cjs +0 -376
- package/dist/chunk-ZA3QHI3O.cjs.map +0 -1
- package/dist/components.cjs.map +0 -1
- package/dist/components.js.map +0 -1
- package/dist/generators.cjs.map +0 -1
- package/dist/generators.js.map +0 -1
- package/dist/types-kyHdJ8oH.d.cts +0 -107
- package/dist/types-kyHdJ8oH.d.ts +0 -107
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const __kubb_core_transformers = __toESM(require("@kubb/core/transformers"));
|
|
25
|
+
const __kubb_plugin_oas = __toESM(require("@kubb/plugin-oas"));
|
|
26
|
+
const __kubb_react = __toESM(require("@kubb/react"));
|
|
27
|
+
const __kubb_react_jsx_runtime = __toESM(require("@kubb/react/jsx-runtime"));
|
|
28
|
+
|
|
29
|
+
//#region src/parser.ts
|
|
30
|
+
const fakerKeywordMapper = {
|
|
31
|
+
any: () => "undefined",
|
|
32
|
+
unknown: () => "undefined",
|
|
33
|
+
void: () => "undefined",
|
|
34
|
+
number: (min, max) => {
|
|
35
|
+
if (max !== void 0 && min !== void 0) return `faker.number.float({ min: ${min}, max: ${max} })`;
|
|
36
|
+
if (max !== void 0) return `faker.number.float({ max: ${max} })`;
|
|
37
|
+
if (min !== void 0) return `faker.number.float({ min: ${min} })`;
|
|
38
|
+
return "faker.number.float()";
|
|
39
|
+
},
|
|
40
|
+
integer: (min, max) => {
|
|
41
|
+
if (max !== void 0 && min !== void 0) return `faker.number.int({ min: ${min}, max: ${max} })`;
|
|
42
|
+
if (max !== void 0) return `faker.number.int({ max: ${max} })`;
|
|
43
|
+
if (min !== void 0) return `faker.number.int({ min: ${min} })`;
|
|
44
|
+
return "faker.number.int()";
|
|
45
|
+
},
|
|
46
|
+
string: (min, max) => {
|
|
47
|
+
if (max !== void 0 && min !== void 0) return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`;
|
|
48
|
+
if (max !== void 0) return `faker.string.alpha({ length: ${max} })`;
|
|
49
|
+
if (min !== void 0) return `faker.string.alpha({ length: ${min} })`;
|
|
50
|
+
return "faker.string.alpha()";
|
|
51
|
+
},
|
|
52
|
+
boolean: () => "faker.datatype.boolean()",
|
|
53
|
+
undefined: () => "undefined",
|
|
54
|
+
null: () => "null",
|
|
55
|
+
array: (items = [], min, max) => {
|
|
56
|
+
if (items.length > 1) return `faker.helpers.arrayElements([${items.join(", ")}])`;
|
|
57
|
+
const item = items.at(0);
|
|
58
|
+
if (min !== void 0 && max !== void 0) return `faker.helpers.multiple(() => (${item}), { count: { min: ${min}, max: ${max} }})`;
|
|
59
|
+
if (min !== void 0) return `faker.helpers.multiple(() => (${item}), { count: ${min} })`;
|
|
60
|
+
if (max !== void 0) return `faker.helpers.multiple(() => (${item}), { count: { min: 0, max: ${max} }})`;
|
|
61
|
+
return `faker.helpers.multiple(() => (${item}))`;
|
|
62
|
+
},
|
|
63
|
+
tuple: (items = []) => `[${items.join(", ")}]`,
|
|
64
|
+
enum: (items = [], type = "any") => `faker.helpers.arrayElement<${type}>([${items.join(", ")}])`,
|
|
65
|
+
union: (items = []) => `faker.helpers.arrayElement<any>([${items.join(", ")}])`,
|
|
66
|
+
datetime: () => "faker.date.anytime().toISOString()",
|
|
67
|
+
date: (type = "string", parser = "faker") => {
|
|
68
|
+
if (type === "string") {
|
|
69
|
+
if (parser !== "faker") return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`;
|
|
70
|
+
return "faker.date.anytime().toISOString().substring(0, 10)";
|
|
71
|
+
}
|
|
72
|
+
if (parser !== "faker") throw new Error(`type '${type}' and parser '${parser}' can not work together`);
|
|
73
|
+
return "faker.date.anytime()";
|
|
74
|
+
},
|
|
75
|
+
time: (type = "string", parser = "faker") => {
|
|
76
|
+
if (type === "string") {
|
|
77
|
+
if (parser !== "faker") return `${parser}(faker.date.anytime()).format("HH:mm:ss")`;
|
|
78
|
+
return "faker.date.anytime().toISOString().substring(11, 19)";
|
|
79
|
+
}
|
|
80
|
+
if (parser !== "faker") throw new Error(`type '${type}' and parser '${parser}' can not work together`);
|
|
81
|
+
return "faker.date.anytime()";
|
|
82
|
+
},
|
|
83
|
+
uuid: () => "faker.string.uuid()",
|
|
84
|
+
url: () => "faker.internet.url()",
|
|
85
|
+
and: (items = []) => `Object.assign({}, ${items.join(", ")})`,
|
|
86
|
+
object: () => "object",
|
|
87
|
+
ref: () => "ref",
|
|
88
|
+
matches: (value = "", regexGenerator = "faker") => {
|
|
89
|
+
if (regexGenerator === "randexp") return `${__kubb_core_transformers.default.toRegExpString(value, "RandExp")}.gen()`;
|
|
90
|
+
return `faker.helpers.fromRegExp(${__kubb_core_transformers.default.toRegExpString(value)})`;
|
|
91
|
+
},
|
|
92
|
+
email: () => "faker.internet.email()",
|
|
93
|
+
firstName: () => "faker.person.firstName()",
|
|
94
|
+
lastName: () => "faker.person.lastName()",
|
|
95
|
+
password: () => "faker.internet.password()",
|
|
96
|
+
phone: () => "faker.phone.number()",
|
|
97
|
+
blob: () => "faker.image.url() as unknown as Blob",
|
|
98
|
+
default: void 0,
|
|
99
|
+
describe: void 0,
|
|
100
|
+
const: (value) => value ?? "",
|
|
101
|
+
max: void 0,
|
|
102
|
+
min: void 0,
|
|
103
|
+
nullable: void 0,
|
|
104
|
+
nullish: void 0,
|
|
105
|
+
optional: void 0,
|
|
106
|
+
readOnly: void 0,
|
|
107
|
+
writeOnly: void 0,
|
|
108
|
+
deprecated: void 0,
|
|
109
|
+
example: void 0,
|
|
110
|
+
schema: void 0,
|
|
111
|
+
catchall: void 0,
|
|
112
|
+
name: void 0,
|
|
113
|
+
interface: void 0
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* @link based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398
|
|
117
|
+
*/
|
|
118
|
+
function schemaKeywordSorter(_a, b) {
|
|
119
|
+
if (b.keyword === "null") return -1;
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
function joinItems(items) {
|
|
123
|
+
switch (items.length) {
|
|
124
|
+
case 0: return "undefined";
|
|
125
|
+
case 1: return items[0];
|
|
126
|
+
default: return fakerKeywordMapper.union(items);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function parse({ current, parent, name, siblings }, options) {
|
|
130
|
+
const value = fakerKeywordMapper[current.keyword];
|
|
131
|
+
if (!value) return void 0;
|
|
132
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.union)) {
|
|
133
|
+
if (Array.isArray(current.args) && !current.args.length) return "";
|
|
134
|
+
return fakerKeywordMapper.union(current.args.map((schema) => parse({
|
|
135
|
+
parent: current,
|
|
136
|
+
current: schema,
|
|
137
|
+
siblings
|
|
138
|
+
}, {
|
|
139
|
+
...options,
|
|
140
|
+
canOverride: false
|
|
141
|
+
})).filter(Boolean));
|
|
142
|
+
}
|
|
143
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.and)) return fakerKeywordMapper.and(current.args.map((schema) => parse({
|
|
144
|
+
parent: current,
|
|
145
|
+
current: schema,
|
|
146
|
+
siblings
|
|
147
|
+
}, {
|
|
148
|
+
...options,
|
|
149
|
+
canOverride: false
|
|
150
|
+
})).filter(Boolean));
|
|
151
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.array)) return fakerKeywordMapper.array(current.args.items.map((schema) => parse({
|
|
152
|
+
parent: current,
|
|
153
|
+
current: schema,
|
|
154
|
+
siblings
|
|
155
|
+
}, {
|
|
156
|
+
...options,
|
|
157
|
+
typeName: `NonNullable<${options.typeName}>[number]`,
|
|
158
|
+
canOverride: false
|
|
159
|
+
})).filter(Boolean), current.args.min, current.args.max);
|
|
160
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.enum)) {
|
|
161
|
+
const isParentTuple = parent ? (0, __kubb_plugin_oas.isKeyword)(parent, __kubb_plugin_oas.schemaKeywords.tuple) : false;
|
|
162
|
+
if (isParentTuple) return fakerKeywordMapper.enum(current.args.items.map((schema) => {
|
|
163
|
+
if (schema.format === "number") return schema.value;
|
|
164
|
+
if (schema.format === "boolean") return schema.value;
|
|
165
|
+
return __kubb_core_transformers.default.stringify(schema.value);
|
|
166
|
+
}));
|
|
167
|
+
return fakerKeywordMapper.enum(current.args.items.map((schema) => {
|
|
168
|
+
if (schema.format === "number") return schema.value;
|
|
169
|
+
if (schema.format === "boolean") return schema.value;
|
|
170
|
+
return __kubb_core_transformers.default.stringify(schema.value);
|
|
171
|
+
}), name ? options.typeName : void 0);
|
|
172
|
+
}
|
|
173
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.ref)) {
|
|
174
|
+
if (!current.args?.name) throw new Error(`Name not defined for keyword ${current.keyword}`);
|
|
175
|
+
if (options.canOverride) return `${current.args.name}(data)`;
|
|
176
|
+
return `${current.args.name}()`;
|
|
177
|
+
}
|
|
178
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.object)) {
|
|
179
|
+
const argsObject = Object.entries(current.args?.properties || {}).filter((item) => {
|
|
180
|
+
const schema = item[1];
|
|
181
|
+
return schema && typeof schema.map === "function";
|
|
182
|
+
}).map(([name$1, schemas]) => {
|
|
183
|
+
const nameSchema = schemas.find((schema) => schema.keyword === __kubb_plugin_oas.schemaKeywords.name);
|
|
184
|
+
const mappedName = nameSchema?.args || name$1;
|
|
185
|
+
if (options.mapper?.[mappedName]) return `"${name$1}": ${options.mapper?.[mappedName]}`;
|
|
186
|
+
return `"${name$1}": ${joinItems(schemas.sort(schemaKeywordSorter).map((schema) => parse({
|
|
187
|
+
name: name$1,
|
|
188
|
+
parent: current,
|
|
189
|
+
current: schema,
|
|
190
|
+
siblings: schemas
|
|
191
|
+
}, {
|
|
192
|
+
...options,
|
|
193
|
+
typeName: `NonNullable<${options.typeName}>[${JSON.stringify(name$1)}]`,
|
|
194
|
+
canOverride: false
|
|
195
|
+
})).filter(Boolean))}`;
|
|
196
|
+
}).join(",");
|
|
197
|
+
return `{${argsObject}}`;
|
|
198
|
+
}
|
|
199
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.tuple)) {
|
|
200
|
+
if (Array.isArray(current.args.items)) return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse({
|
|
201
|
+
parent: current,
|
|
202
|
+
current: schema,
|
|
203
|
+
siblings
|
|
204
|
+
}, {
|
|
205
|
+
...options,
|
|
206
|
+
canOverride: false
|
|
207
|
+
})).filter(Boolean));
|
|
208
|
+
return parse({
|
|
209
|
+
parent: current,
|
|
210
|
+
current: current.args.items,
|
|
211
|
+
siblings
|
|
212
|
+
}, {
|
|
213
|
+
...options,
|
|
214
|
+
canOverride: false
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.const)) {
|
|
218
|
+
if (current.args.format === "number" && current.args.name !== void 0) return fakerKeywordMapper.const(current.args.name?.toString());
|
|
219
|
+
return fakerKeywordMapper.const(__kubb_core_transformers.default.stringify(current.args.value));
|
|
220
|
+
}
|
|
221
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.matches) && current.args) return fakerKeywordMapper.matches(current.args, options.regexGenerator);
|
|
222
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.null) || (0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.undefined) || (0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.any)) return value() || "";
|
|
223
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.string)) {
|
|
224
|
+
if (siblings) {
|
|
225
|
+
const minSchema = __kubb_plugin_oas.SchemaGenerator.find(siblings, __kubb_plugin_oas.schemaKeywords.min);
|
|
226
|
+
const maxSchema = __kubb_plugin_oas.SchemaGenerator.find(siblings, __kubb_plugin_oas.schemaKeywords.max);
|
|
227
|
+
return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args);
|
|
228
|
+
}
|
|
229
|
+
return fakerKeywordMapper.string();
|
|
230
|
+
}
|
|
231
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.number)) {
|
|
232
|
+
if (siblings) {
|
|
233
|
+
const minSchema = __kubb_plugin_oas.SchemaGenerator.find(siblings, __kubb_plugin_oas.schemaKeywords.min);
|
|
234
|
+
const maxSchema = __kubb_plugin_oas.SchemaGenerator.find(siblings, __kubb_plugin_oas.schemaKeywords.max);
|
|
235
|
+
return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args);
|
|
236
|
+
}
|
|
237
|
+
return fakerKeywordMapper.number();
|
|
238
|
+
}
|
|
239
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.integer)) {
|
|
240
|
+
if (siblings) {
|
|
241
|
+
const minSchema = __kubb_plugin_oas.SchemaGenerator.find(siblings, __kubb_plugin_oas.schemaKeywords.min);
|
|
242
|
+
const maxSchema = __kubb_plugin_oas.SchemaGenerator.find(siblings, __kubb_plugin_oas.schemaKeywords.max);
|
|
243
|
+
return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args);
|
|
244
|
+
}
|
|
245
|
+
return fakerKeywordMapper.integer();
|
|
246
|
+
}
|
|
247
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.datetime)) return fakerKeywordMapper.datetime();
|
|
248
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.date)) return fakerKeywordMapper.date(current.args.type, options.dateParser);
|
|
249
|
+
if ((0, __kubb_plugin_oas.isKeyword)(current, __kubb_plugin_oas.schemaKeywords.time)) return fakerKeywordMapper.time(current.args.type, options.dateParser);
|
|
250
|
+
if (current.keyword in fakerKeywordMapper && "args" in current) {
|
|
251
|
+
const value$1 = fakerKeywordMapper[current.keyword];
|
|
252
|
+
const options$1 = JSON.stringify(current.args);
|
|
253
|
+
return value$1(options$1);
|
|
254
|
+
}
|
|
255
|
+
if (current.keyword in fakerKeywordMapper) return value();
|
|
256
|
+
return void 0;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/components/Faker.tsx
|
|
261
|
+
function Faker({ tree, description, name, typeName, seed, regexGenerator, canOverride, mapper, dateParser }) {
|
|
262
|
+
const fakerText = joinItems(tree.map((schema, _index, siblings) => parse({
|
|
263
|
+
parent: void 0,
|
|
264
|
+
current: schema,
|
|
265
|
+
siblings
|
|
266
|
+
}, {
|
|
267
|
+
name,
|
|
268
|
+
typeName,
|
|
269
|
+
seed,
|
|
270
|
+
regexGenerator,
|
|
271
|
+
mapper,
|
|
272
|
+
canOverride,
|
|
273
|
+
dateParser
|
|
274
|
+
})).filter(Boolean));
|
|
275
|
+
const isArray = fakerText.startsWith("faker.helpers.arrayElements") || fakerText.startsWith("faker.helpers.multiple");
|
|
276
|
+
const isObject = fakerText.startsWith("{");
|
|
277
|
+
const isTuple = fakerText.startsWith("faker.helpers.arrayElement");
|
|
278
|
+
let fakerTextWithOverride = fakerText;
|
|
279
|
+
if (canOverride && isObject) fakerTextWithOverride = `{
|
|
280
|
+
...${fakerText},
|
|
281
|
+
...data || {}
|
|
282
|
+
}`;
|
|
283
|
+
if (canOverride && isTuple) fakerTextWithOverride = `data || ${fakerText}`;
|
|
284
|
+
if (canOverride && isArray) fakerTextWithOverride = `[
|
|
285
|
+
...${fakerText},
|
|
286
|
+
...data || []
|
|
287
|
+
]`;
|
|
288
|
+
const params = __kubb_react.FunctionParams.factory({ data: {
|
|
289
|
+
type: isArray ? typeName : `Partial<${typeName}>`,
|
|
290
|
+
optional: true
|
|
291
|
+
} });
|
|
292
|
+
return /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)(__kubb_react.File.Source, {
|
|
293
|
+
name,
|
|
294
|
+
isExportable: true,
|
|
295
|
+
isIndexable: true,
|
|
296
|
+
children: /* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsxs)(__kubb_react.Function, {
|
|
297
|
+
export: true,
|
|
298
|
+
name,
|
|
299
|
+
JSDoc: { comments: [description ? `@description ${__kubb_core_transformers.default.jsStringEscape(description)}` : void 0].filter(Boolean) },
|
|
300
|
+
params: canOverride ? params.toConstructor() : void 0,
|
|
301
|
+
returnType: canOverride ? typeName : void 0,
|
|
302
|
+
children: [
|
|
303
|
+
seed ? `faker.seed(${JSON.stringify(seed)})` : void 0,
|
|
304
|
+
/* @__PURE__ */ (0, __kubb_react_jsx_runtime.jsx)("br", {}),
|
|
305
|
+
`return ${fakerTextWithOverride}`
|
|
306
|
+
]
|
|
307
|
+
})
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
//#endregion
|
|
312
|
+
Object.defineProperty(exports, 'Faker', {
|
|
313
|
+
enumerable: true,
|
|
314
|
+
get: function () {
|
|
315
|
+
return Faker;
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
Object.defineProperty(exports, '__toESM', {
|
|
319
|
+
enumerable: true,
|
|
320
|
+
get: function () {
|
|
321
|
+
return __toESM;
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
//# sourceMappingURL=components-BpyQzZAY.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components-BpyQzZAY.cjs","names":["min?: number","max?: number","items: string[]","items: Array<string | number | boolean | undefined>","type: 'date' | 'string'","parser: Options['dateParser']","regexGenerator: 'faker' | 'randexp'","transformers","value?: string | number","_a: Schema","b: Schema","options: ParserOptions","schemaKeywords","name","SchemaGenerator","value","options","FunctionParams","File","Function","transformers"],"sources":["../src/parser.ts","../src/components/Faker.tsx"],"sourcesContent":["import transformers from '@kubb/core/transformers'\nimport type { Schema, SchemaKeywordBase, SchemaKeywordMapper, SchemaMapper } from '@kubb/plugin-oas'\nimport { isKeyword, SchemaGenerator, type SchemaTree, schemaKeywords } from '@kubb/plugin-oas'\nimport type { Options } from './types.ts'\n\nconst fakerKeywordMapper = {\n any: () => 'undefined',\n unknown: () => 'undefined',\n void: () => 'undefined',\n number: (min?: number, max?: number) => {\n if (max !== undefined && min !== undefined) {\n return `faker.number.float({ min: ${min}, max: ${max} })`\n }\n\n if (max !== undefined) {\n return `faker.number.float({ max: ${max} })`\n }\n\n if (min !== undefined) {\n return `faker.number.float({ min: ${min} })`\n }\n\n return 'faker.number.float()'\n },\n integer: (min?: number, max?: number) => {\n if (max !== undefined && min !== undefined) {\n return `faker.number.int({ min: ${min}, max: ${max} })`\n }\n\n if (max !== undefined) {\n return `faker.number.int({ max: ${max} })`\n }\n\n if (min !== undefined) {\n return `faker.number.int({ min: ${min} })`\n }\n\n return 'faker.number.int()'\n },\n string: (min?: number, max?: number) => {\n if (max !== undefined && min !== undefined) {\n return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`\n }\n\n if (max !== undefined) {\n return `faker.string.alpha({ length: ${max} })`\n }\n\n if (min !== undefined) {\n return `faker.string.alpha({ length: ${min} })`\n }\n\n return 'faker.string.alpha()'\n },\n boolean: () => 'faker.datatype.boolean()',\n undefined: () => 'undefined',\n null: () => 'null',\n array: (items: string[] = [], min?: number, max?: number) => {\n if (items.length > 1) {\n return `faker.helpers.arrayElements([${items.join(', ')}])`\n }\n const item = items.at(0)\n\n if (min !== undefined && max !== undefined) {\n return `faker.helpers.multiple(() => (${item}), { count: { min: ${min}, max: ${max} }})`\n }\n if (min !== undefined) {\n return `faker.helpers.multiple(() => (${item}), { count: ${min} })`\n }\n if (max !== undefined) {\n return `faker.helpers.multiple(() => (${item}), { count: { min: 0, max: ${max} }})`\n }\n\n return `faker.helpers.multiple(() => (${item}))`\n },\n tuple: (items: string[] = []) => `[${items.join(', ')}]`,\n enum: (items: Array<string | number | boolean | undefined> = [], type = 'any') => `faker.helpers.arrayElement<${type}>([${items.join(', ')}])`,\n union: (items: string[] = []) => `faker.helpers.arrayElement<any>([${items.join(', ')}])`,\n /**\n * ISO 8601\n */\n datetime: () => 'faker.date.anytime().toISOString()',\n /**\n * Type `'date'` Date\n * Type `'string'` ISO date format (YYYY-MM-DD)\n * @default ISO date format (YYYY-MM-DD)\n */\n date: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {\n if (type === 'string') {\n if (parser !== 'faker') {\n return `${parser}(faker.date.anytime()).format(\"YYYY-MM-DD\")`\n }\n return 'faker.date.anytime().toISOString().substring(0, 10)'\n }\n\n if (parser !== 'faker') {\n throw new Error(`type '${type}' and parser '${parser}' can not work together`)\n }\n\n return 'faker.date.anytime()'\n },\n /**\n * Type `'date'` Date\n * Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])\n * @default ISO time format (HH:mm:ss[.SSSSSS])\n */\n time: (type: 'date' | 'string' = 'string', parser: Options['dateParser'] = 'faker') => {\n if (type === 'string') {\n if (parser !== 'faker') {\n return `${parser}(faker.date.anytime()).format(\"HH:mm:ss\")`\n }\n return 'faker.date.anytime().toISOString().substring(11, 19)'\n }\n\n if (parser !== 'faker') {\n throw new Error(`type '${type}' and parser '${parser}' can not work together`)\n }\n\n return 'faker.date.anytime()'\n },\n uuid: () => 'faker.string.uuid()',\n url: () => 'faker.internet.url()',\n and: (items: string[] = []) => `Object.assign({}, ${items.join(', ')})`,\n object: () => 'object',\n ref: () => 'ref',\n matches: (value = '', regexGenerator: 'faker' | 'randexp' = 'faker') => {\n if (regexGenerator === 'randexp') {\n return `${transformers.toRegExpString(value, 'RandExp')}.gen()`\n }\n return `faker.helpers.fromRegExp(${transformers.toRegExpString(value)})`\n },\n email: () => 'faker.internet.email()',\n firstName: () => 'faker.person.firstName()',\n lastName: () => 'faker.person.lastName()',\n password: () => 'faker.internet.password()',\n phone: () => 'faker.phone.number()',\n blob: () => 'faker.image.url() as unknown as Blob',\n default: undefined,\n describe: undefined,\n const: (value?: string | number) => (value as string) ?? '',\n max: undefined,\n min: undefined,\n nullable: undefined,\n nullish: undefined,\n optional: undefined,\n readOnly: undefined,\n writeOnly: undefined,\n deprecated: undefined,\n example: undefined,\n schema: undefined,\n catchall: undefined,\n name: undefined,\n interface: undefined,\n} satisfies SchemaMapper<string | null | undefined>\n\n/**\n * @link based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398\n */\n\nfunction schemaKeywordSorter(_a: Schema, b: Schema) {\n if (b.keyword === 'null') {\n return -1\n }\n\n return 0\n}\n\nexport function joinItems(items: string[]): string {\n switch (items.length) {\n case 0:\n return 'undefined'\n case 1:\n return items[0]!\n default:\n return fakerKeywordMapper.union(items)\n }\n}\n\ntype ParserOptions = {\n name: string\n typeName?: string\n description?: string\n\n seed?: number | number[]\n regexGenerator?: 'faker' | 'randexp'\n canOverride?: boolean\n dateParser?: Options['dateParser']\n mapper?: Record<string, string>\n}\n\nexport function parse({ current, parent, name, siblings }: SchemaTree, options: ParserOptions): string | null | undefined {\n const value = fakerKeywordMapper[current.keyword as keyof typeof fakerKeywordMapper]\n\n if (!value) {\n return undefined\n }\n\n if (isKeyword(current, schemaKeywords.union)) {\n if (Array.isArray(current.args) && !current.args.length) {\n return ''\n }\n\n return fakerKeywordMapper.union(\n current.args.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),\n )\n }\n\n if (isKeyword(current, schemaKeywords.and)) {\n return fakerKeywordMapper.and(\n current.args.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),\n )\n }\n\n if (isKeyword(current, schemaKeywords.array)) {\n return fakerKeywordMapper.array(\n current.args.items\n .map((schema) =>\n parse(\n { parent: current, current: schema, siblings },\n {\n ...options,\n typeName: `NonNullable<${options.typeName}>[number]`,\n canOverride: false,\n },\n ),\n )\n .filter(Boolean),\n current.args.min,\n current.args.max,\n )\n }\n\n if (isKeyword(current, schemaKeywords.enum)) {\n const isParentTuple = parent ? isKeyword(parent, schemaKeywords.tuple) : false\n\n if (isParentTuple) {\n return fakerKeywordMapper.enum(\n current.args.items.map((schema) => {\n if (schema.format === 'number') {\n return schema.value\n }\n\n if (schema.format === 'boolean') {\n return schema.value\n }\n return transformers.stringify(schema.value)\n }),\n )\n }\n\n return fakerKeywordMapper.enum(\n current.args.items.map((schema) => {\n if (schema.format === 'number') {\n return schema.value\n }\n if (schema.format === 'boolean') {\n return schema.value\n }\n return transformers.stringify(schema.value)\n }),\n // TODO replace this with getEnumNameFromSchema\n name ? options.typeName : undefined,\n )\n }\n\n if (isKeyword(current, schemaKeywords.ref)) {\n if (!current.args?.name) {\n throw new Error(`Name not defined for keyword ${current.keyword}`)\n }\n\n if (options.canOverride) {\n return `${current.args.name}(data)`\n }\n\n return `${current.args.name}()`\n }\n\n if (isKeyword(current, schemaKeywords.object)) {\n const argsObject = Object.entries(current.args?.properties || {})\n .filter((item) => {\n const schema = item[1]\n return schema && typeof schema.map === 'function'\n })\n .map(([name, schemas]) => {\n const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']\n const mappedName = nameSchema?.args || name\n\n // custom mapper(pluginOptions)\n if (options.mapper?.[mappedName]) {\n return `\"${name}\": ${options.mapper?.[mappedName]}`\n }\n\n return `\"${name}\": ${joinItems(\n schemas\n .sort(schemaKeywordSorter)\n .map((schema) =>\n parse(\n { name, parent: current, current: schema, siblings: schemas },\n {\n ...options,\n typeName: `NonNullable<${options.typeName}>[${JSON.stringify(name)}]`,\n canOverride: false,\n },\n ),\n )\n .filter(Boolean),\n )}`\n })\n .join(',')\n\n return `{${argsObject}}`\n }\n\n if (isKeyword(current, schemaKeywords.tuple)) {\n if (Array.isArray(current.args.items)) {\n return fakerKeywordMapper.tuple(\n current.args.items.map((schema) => parse({ parent: current, current: schema, siblings }, { ...options, canOverride: false })).filter(Boolean),\n )\n }\n\n return parse({ parent: current, current: current.args.items, siblings }, { ...options, canOverride: false })\n }\n\n if (isKeyword(current, schemaKeywords.const)) {\n if (current.args.format === 'number' && current.args.name !== undefined) {\n return fakerKeywordMapper.const(current.args.name?.toString())\n }\n return fakerKeywordMapper.const(transformers.stringify(current.args.value))\n }\n\n if (isKeyword(current, schemaKeywords.matches) && current.args) {\n return fakerKeywordMapper.matches(current.args, options.regexGenerator)\n }\n\n if (isKeyword(current, schemaKeywords.null) || isKeyword(current, schemaKeywords.undefined) || isKeyword(current, schemaKeywords.any)) {\n return value() || ''\n }\n\n if (isKeyword(current, schemaKeywords.string)) {\n if (siblings) {\n const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)\n const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)\n\n return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args)\n }\n\n return fakerKeywordMapper.string()\n }\n\n if (isKeyword(current, schemaKeywords.number)) {\n if (siblings) {\n const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)\n const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)\n\n return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args)\n }\n\n return fakerKeywordMapper.number()\n }\n\n if (isKeyword(current, schemaKeywords.integer)) {\n if (siblings) {\n const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)\n const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)\n\n return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args)\n }\n\n return fakerKeywordMapper.integer()\n }\n\n if (isKeyword(current, schemaKeywords.datetime)) {\n return fakerKeywordMapper.datetime()\n }\n\n if (isKeyword(current, schemaKeywords.date)) {\n return fakerKeywordMapper.date(current.args.type, options.dateParser)\n }\n\n if (isKeyword(current, schemaKeywords.time)) {\n return fakerKeywordMapper.time(current.args.type, options.dateParser)\n }\n\n if (current.keyword in fakerKeywordMapper && 'args' in current) {\n const value = fakerKeywordMapper[current.keyword as keyof typeof fakerKeywordMapper] as (typeof fakerKeywordMapper)['const']\n\n const options = JSON.stringify((current as SchemaKeywordBase<unknown>).args)\n\n return value(options)\n }\n\n if (current.keyword in fakerKeywordMapper) {\n return value()\n }\n\n return undefined\n}\n","import { File, Function, FunctionParams } from '@kubb/react'\n\nimport transformers from '@kubb/core/transformers'\nimport type { Schema } from '@kubb/plugin-oas'\nimport * as parserFaker from '../parser.ts'\nimport type { PluginFaker } from '../types.ts'\n\ntype Props = {\n name: string\n typeName: string\n tree: Array<Schema>\n seed?: PluginFaker['options']['seed']\n description?: string\n regexGenerator?: PluginFaker['options']['regexGenerator']\n mapper?: PluginFaker['options']['mapper']\n dateParser?: PluginFaker['options']['dateParser']\n canOverride: boolean\n}\n\nexport function Faker({ tree, description, name, typeName, seed, regexGenerator, canOverride, mapper, dateParser }: Props) {\n const fakerText = parserFaker.joinItems(\n tree\n .map((schema, _index, siblings) =>\n parserFaker.parse(\n { parent: undefined, current: schema, siblings },\n {\n name,\n typeName,\n seed,\n regexGenerator,\n mapper,\n canOverride,\n dateParser,\n },\n ),\n )\n .filter(Boolean),\n )\n\n const isArray = fakerText.startsWith('faker.helpers.arrayElements') || fakerText.startsWith('faker.helpers.multiple')\n const isObject = fakerText.startsWith('{')\n const isTuple = fakerText.startsWith('faker.helpers.arrayElement')\n\n let fakerTextWithOverride = fakerText\n\n if (canOverride && isObject) {\n fakerTextWithOverride = `{\n ...${fakerText},\n ...data || {}\n}`\n }\n\n if (canOverride && isTuple) {\n fakerTextWithOverride = `data || ${fakerText}`\n }\n\n if (canOverride && isArray) {\n fakerTextWithOverride = `[\n ...${fakerText},\n ...data || []\n ]`\n }\n\n const params = FunctionParams.factory({\n data: {\n // making a partial out of an array does not make sense\n type: isArray ? typeName : `Partial<${typeName}>`,\n optional: true,\n },\n })\n\n return (\n <File.Source name={name} isExportable isIndexable>\n <Function\n export\n name={name}\n JSDoc={{ comments: [description ? `@description ${transformers.jsStringEscape(description)}` : undefined].filter(Boolean) }}\n params={canOverride ? params.toConstructor() : undefined}\n returnType={canOverride ? typeName : undefined}\n >\n {seed ? `faker.seed(${JSON.stringify(seed)})` : undefined}\n <br />\n {`return ${fakerTextWithOverride}`}\n </Function>\n </File.Source>\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,MAAM,qBAAqB;CACzB,KAAK,MAAM;CACX,SAAS,MAAM;CACf,MAAM,MAAM;CACZ,QAAQ,CAACA,KAAcC,QAAiB;AACtC,MAAI,QAAQ,UAAa,QAAQ,OAC/B,QAAO,CAAC,0BAA0B,EAAE,IAAI,OAAO,EAAE,IAAI,GAAG,CAAC;AAG3D,MAAI,QAAQ,OACV,QAAO,CAAC,0BAA0B,EAAE,IAAI,GAAG,CAAC;AAG9C,MAAI,QAAQ,OACV,QAAO,CAAC,0BAA0B,EAAE,IA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EpC,OAAO,CAAC,WAAW,EAAE,KAAK,UAAU,KAAK,CAAC,CAAC,CAAC,GAAG;sDAC/C,SAAK;IACL,CAAC,OAAO,EAAE,uBAAuB;;IACzB;GACC;AAEjB"}
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import transformers from "@kubb/core/transformers";
|
|
2
|
+
import { SchemaGenerator, isKeyword, schemaKeywords } from "@kubb/plugin-oas";
|
|
3
|
+
import { File, Function, FunctionParams } from "@kubb/react";
|
|
4
|
+
import { jsx, jsxs } from "@kubb/react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/parser.ts
|
|
7
|
+
const fakerKeywordMapper = {
|
|
8
|
+
any: () => "undefined",
|
|
9
|
+
unknown: () => "undefined",
|
|
10
|
+
void: () => "undefined",
|
|
11
|
+
number: (min, max) => {
|
|
12
|
+
if (max !== void 0 && min !== void 0) return `faker.number.float({ min: ${min}, max: ${max} })`;
|
|
13
|
+
if (max !== void 0) return `faker.number.float({ max: ${max} })`;
|
|
14
|
+
if (min !== void 0) return `faker.number.float({ min: ${min} })`;
|
|
15
|
+
return "faker.number.float()";
|
|
16
|
+
},
|
|
17
|
+
integer: (min, max) => {
|
|
18
|
+
if (max !== void 0 && min !== void 0) return `faker.number.int({ min: ${min}, max: ${max} })`;
|
|
19
|
+
if (max !== void 0) return `faker.number.int({ max: ${max} })`;
|
|
20
|
+
if (min !== void 0) return `faker.number.int({ min: ${min} })`;
|
|
21
|
+
return "faker.number.int()";
|
|
22
|
+
},
|
|
23
|
+
string: (min, max) => {
|
|
24
|
+
if (max !== void 0 && min !== void 0) return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`;
|
|
25
|
+
if (max !== void 0) return `faker.string.alpha({ length: ${max} })`;
|
|
26
|
+
if (min !== void 0) return `faker.string.alpha({ length: ${min} })`;
|
|
27
|
+
return "faker.string.alpha()";
|
|
28
|
+
},
|
|
29
|
+
boolean: () => "faker.datatype.boolean()",
|
|
30
|
+
undefined: () => "undefined",
|
|
31
|
+
null: () => "null",
|
|
32
|
+
array: (items = [], min, max) => {
|
|
33
|
+
if (items.length > 1) return `faker.helpers.arrayElements([${items.join(", ")}])`;
|
|
34
|
+
const item = items.at(0);
|
|
35
|
+
if (min !== void 0 && max !== void 0) return `faker.helpers.multiple(() => (${item}), { count: { min: ${min}, max: ${max} }})`;
|
|
36
|
+
if (min !== void 0) return `faker.helpers.multiple(() => (${item}), { count: ${min} })`;
|
|
37
|
+
if (max !== void 0) return `faker.helpers.multiple(() => (${item}), { count: { min: 0, max: ${max} }})`;
|
|
38
|
+
return `faker.helpers.multiple(() => (${item}))`;
|
|
39
|
+
},
|
|
40
|
+
tuple: (items = []) => `[${items.join(", ")}]`,
|
|
41
|
+
enum: (items = [], type = "any") => `faker.helpers.arrayElement<${type}>([${items.join(", ")}])`,
|
|
42
|
+
union: (items = []) => `faker.helpers.arrayElement<any>([${items.join(", ")}])`,
|
|
43
|
+
datetime: () => "faker.date.anytime().toISOString()",
|
|
44
|
+
date: (type = "string", parser = "faker") => {
|
|
45
|
+
if (type === "string") {
|
|
46
|
+
if (parser !== "faker") return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`;
|
|
47
|
+
return "faker.date.anytime().toISOString().substring(0, 10)";
|
|
48
|
+
}
|
|
49
|
+
if (parser !== "faker") throw new Error(`type '${type}' and parser '${parser}' can not work together`);
|
|
50
|
+
return "faker.date.anytime()";
|
|
51
|
+
},
|
|
52
|
+
time: (type = "string", parser = "faker") => {
|
|
53
|
+
if (type === "string") {
|
|
54
|
+
if (parser !== "faker") return `${parser}(faker.date.anytime()).format("HH:mm:ss")`;
|
|
55
|
+
return "faker.date.anytime().toISOString().substring(11, 19)";
|
|
56
|
+
}
|
|
57
|
+
if (parser !== "faker") throw new Error(`type '${type}' and parser '${parser}' can not work together`);
|
|
58
|
+
return "faker.date.anytime()";
|
|
59
|
+
},
|
|
60
|
+
uuid: () => "faker.string.uuid()",
|
|
61
|
+
url: () => "faker.internet.url()",
|
|
62
|
+
and: (items = []) => `Object.assign({}, ${items.join(", ")})`,
|
|
63
|
+
object: () => "object",
|
|
64
|
+
ref: () => "ref",
|
|
65
|
+
matches: (value = "", regexGenerator = "faker") => {
|
|
66
|
+
if (regexGenerator === "randexp") return `${transformers.toRegExpString(value, "RandExp")}.gen()`;
|
|
67
|
+
return `faker.helpers.fromRegExp(${transformers.toRegExpString(value)})`;
|
|
68
|
+
},
|
|
69
|
+
email: () => "faker.internet.email()",
|
|
70
|
+
firstName: () => "faker.person.firstName()",
|
|
71
|
+
lastName: () => "faker.person.lastName()",
|
|
72
|
+
password: () => "faker.internet.password()",
|
|
73
|
+
phone: () => "faker.phone.number()",
|
|
74
|
+
blob: () => "faker.image.url() as unknown as Blob",
|
|
75
|
+
default: void 0,
|
|
76
|
+
describe: void 0,
|
|
77
|
+
const: (value) => value ?? "",
|
|
78
|
+
max: void 0,
|
|
79
|
+
min: void 0,
|
|
80
|
+
nullable: void 0,
|
|
81
|
+
nullish: void 0,
|
|
82
|
+
optional: void 0,
|
|
83
|
+
readOnly: void 0,
|
|
84
|
+
writeOnly: void 0,
|
|
85
|
+
deprecated: void 0,
|
|
86
|
+
example: void 0,
|
|
87
|
+
schema: void 0,
|
|
88
|
+
catchall: void 0,
|
|
89
|
+
name: void 0,
|
|
90
|
+
interface: void 0
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* @link based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398
|
|
94
|
+
*/
|
|
95
|
+
function schemaKeywordSorter(_a, b) {
|
|
96
|
+
if (b.keyword === "null") return -1;
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
function joinItems(items) {
|
|
100
|
+
switch (items.length) {
|
|
101
|
+
case 0: return "undefined";
|
|
102
|
+
case 1: return items[0];
|
|
103
|
+
default: return fakerKeywordMapper.union(items);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function parse({ current, parent, name, siblings }, options) {
|
|
107
|
+
const value = fakerKeywordMapper[current.keyword];
|
|
108
|
+
if (!value) return void 0;
|
|
109
|
+
if (isKeyword(current, schemaKeywords.union)) {
|
|
110
|
+
if (Array.isArray(current.args) && !current.args.length) return "";
|
|
111
|
+
return fakerKeywordMapper.union(current.args.map((schema) => parse({
|
|
112
|
+
parent: current,
|
|
113
|
+
current: schema,
|
|
114
|
+
siblings
|
|
115
|
+
}, {
|
|
116
|
+
...options,
|
|
117
|
+
canOverride: false
|
|
118
|
+
})).filter(Boolean));
|
|
119
|
+
}
|
|
120
|
+
if (isKeyword(current, schemaKeywords.and)) return fakerKeywordMapper.and(current.args.map((schema) => parse({
|
|
121
|
+
parent: current,
|
|
122
|
+
current: schema,
|
|
123
|
+
siblings
|
|
124
|
+
}, {
|
|
125
|
+
...options,
|
|
126
|
+
canOverride: false
|
|
127
|
+
})).filter(Boolean));
|
|
128
|
+
if (isKeyword(current, schemaKeywords.array)) return fakerKeywordMapper.array(current.args.items.map((schema) => parse({
|
|
129
|
+
parent: current,
|
|
130
|
+
current: schema,
|
|
131
|
+
siblings
|
|
132
|
+
}, {
|
|
133
|
+
...options,
|
|
134
|
+
typeName: `NonNullable<${options.typeName}>[number]`,
|
|
135
|
+
canOverride: false
|
|
136
|
+
})).filter(Boolean), current.args.min, current.args.max);
|
|
137
|
+
if (isKeyword(current, schemaKeywords.enum)) {
|
|
138
|
+
const isParentTuple = parent ? isKeyword(parent, schemaKeywords.tuple) : false;
|
|
139
|
+
if (isParentTuple) return fakerKeywordMapper.enum(current.args.items.map((schema) => {
|
|
140
|
+
if (schema.format === "number") return schema.value;
|
|
141
|
+
if (schema.format === "boolean") return schema.value;
|
|
142
|
+
return transformers.stringify(schema.value);
|
|
143
|
+
}));
|
|
144
|
+
return fakerKeywordMapper.enum(current.args.items.map((schema) => {
|
|
145
|
+
if (schema.format === "number") return schema.value;
|
|
146
|
+
if (schema.format === "boolean") return schema.value;
|
|
147
|
+
return transformers.stringify(schema.value);
|
|
148
|
+
}), name ? options.typeName : void 0);
|
|
149
|
+
}
|
|
150
|
+
if (isKeyword(current, schemaKeywords.ref)) {
|
|
151
|
+
if (!current.args?.name) throw new Error(`Name not defined for keyword ${current.keyword}`);
|
|
152
|
+
if (options.canOverride) return `${current.args.name}(data)`;
|
|
153
|
+
return `${current.args.name}()`;
|
|
154
|
+
}
|
|
155
|
+
if (isKeyword(current, schemaKeywords.object)) {
|
|
156
|
+
const argsObject = Object.entries(current.args?.properties || {}).filter((item) => {
|
|
157
|
+
const schema = item[1];
|
|
158
|
+
return schema && typeof schema.map === "function";
|
|
159
|
+
}).map(([name$1, schemas]) => {
|
|
160
|
+
const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name);
|
|
161
|
+
const mappedName = nameSchema?.args || name$1;
|
|
162
|
+
if (options.mapper?.[mappedName]) return `"${name$1}": ${options.mapper?.[mappedName]}`;
|
|
163
|
+
return `"${name$1}": ${joinItems(schemas.sort(schemaKeywordSorter).map((schema) => parse({
|
|
164
|
+
name: name$1,
|
|
165
|
+
parent: current,
|
|
166
|
+
current: schema,
|
|
167
|
+
siblings: schemas
|
|
168
|
+
}, {
|
|
169
|
+
...options,
|
|
170
|
+
typeName: `NonNullable<${options.typeName}>[${JSON.stringify(name$1)}]`,
|
|
171
|
+
canOverride: false
|
|
172
|
+
})).filter(Boolean))}`;
|
|
173
|
+
}).join(",");
|
|
174
|
+
return `{${argsObject}}`;
|
|
175
|
+
}
|
|
176
|
+
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
177
|
+
if (Array.isArray(current.args.items)) return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse({
|
|
178
|
+
parent: current,
|
|
179
|
+
current: schema,
|
|
180
|
+
siblings
|
|
181
|
+
}, {
|
|
182
|
+
...options,
|
|
183
|
+
canOverride: false
|
|
184
|
+
})).filter(Boolean));
|
|
185
|
+
return parse({
|
|
186
|
+
parent: current,
|
|
187
|
+
current: current.args.items,
|
|
188
|
+
siblings
|
|
189
|
+
}, {
|
|
190
|
+
...options,
|
|
191
|
+
canOverride: false
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
if (isKeyword(current, schemaKeywords.const)) {
|
|
195
|
+
if (current.args.format === "number" && current.args.name !== void 0) return fakerKeywordMapper.const(current.args.name?.toString());
|
|
196
|
+
return fakerKeywordMapper.const(transformers.stringify(current.args.value));
|
|
197
|
+
}
|
|
198
|
+
if (isKeyword(current, schemaKeywords.matches) && current.args) return fakerKeywordMapper.matches(current.args, options.regexGenerator);
|
|
199
|
+
if (isKeyword(current, schemaKeywords.null) || isKeyword(current, schemaKeywords.undefined) || isKeyword(current, schemaKeywords.any)) return value() || "";
|
|
200
|
+
if (isKeyword(current, schemaKeywords.string)) {
|
|
201
|
+
if (siblings) {
|
|
202
|
+
const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min);
|
|
203
|
+
const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max);
|
|
204
|
+
return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args);
|
|
205
|
+
}
|
|
206
|
+
return fakerKeywordMapper.string();
|
|
207
|
+
}
|
|
208
|
+
if (isKeyword(current, schemaKeywords.number)) {
|
|
209
|
+
if (siblings) {
|
|
210
|
+
const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min);
|
|
211
|
+
const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max);
|
|
212
|
+
return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args);
|
|
213
|
+
}
|
|
214
|
+
return fakerKeywordMapper.number();
|
|
215
|
+
}
|
|
216
|
+
if (isKeyword(current, schemaKeywords.integer)) {
|
|
217
|
+
if (siblings) {
|
|
218
|
+
const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min);
|
|
219
|
+
const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max);
|
|
220
|
+
return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args);
|
|
221
|
+
}
|
|
222
|
+
return fakerKeywordMapper.integer();
|
|
223
|
+
}
|
|
224
|
+
if (isKeyword(current, schemaKeywords.datetime)) return fakerKeywordMapper.datetime();
|
|
225
|
+
if (isKeyword(current, schemaKeywords.date)) return fakerKeywordMapper.date(current.args.type, options.dateParser);
|
|
226
|
+
if (isKeyword(current, schemaKeywords.time)) return fakerKeywordMapper.time(current.args.type, options.dateParser);
|
|
227
|
+
if (current.keyword in fakerKeywordMapper && "args" in current) {
|
|
228
|
+
const value$1 = fakerKeywordMapper[current.keyword];
|
|
229
|
+
const options$1 = JSON.stringify(current.args);
|
|
230
|
+
return value$1(options$1);
|
|
231
|
+
}
|
|
232
|
+
if (current.keyword in fakerKeywordMapper) return value();
|
|
233
|
+
return void 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/components/Faker.tsx
|
|
238
|
+
function Faker({ tree, description, name, typeName, seed, regexGenerator, canOverride, mapper, dateParser }) {
|
|
239
|
+
const fakerText = joinItems(tree.map((schema, _index, siblings) => parse({
|
|
240
|
+
parent: void 0,
|
|
241
|
+
current: schema,
|
|
242
|
+
siblings
|
|
243
|
+
}, {
|
|
244
|
+
name,
|
|
245
|
+
typeName,
|
|
246
|
+
seed,
|
|
247
|
+
regexGenerator,
|
|
248
|
+
mapper,
|
|
249
|
+
canOverride,
|
|
250
|
+
dateParser
|
|
251
|
+
})).filter(Boolean));
|
|
252
|
+
const isArray = fakerText.startsWith("faker.helpers.arrayElements") || fakerText.startsWith("faker.helpers.multiple");
|
|
253
|
+
const isObject = fakerText.startsWith("{");
|
|
254
|
+
const isTuple = fakerText.startsWith("faker.helpers.arrayElement");
|
|
255
|
+
let fakerTextWithOverride = fakerText;
|
|
256
|
+
if (canOverride && isObject) fakerTextWithOverride = `{
|
|
257
|
+
...${fakerText},
|
|
258
|
+
...data || {}
|
|
259
|
+
}`;
|
|
260
|
+
if (canOverride && isTuple) fakerTextWithOverride = `data || ${fakerText}`;
|
|
261
|
+
if (canOverride && isArray) fakerTextWithOverride = `[
|
|
262
|
+
...${fakerText},
|
|
263
|
+
...data || []
|
|
264
|
+
]`;
|
|
265
|
+
const params = FunctionParams.factory({ data: {
|
|
266
|
+
type: isArray ? typeName : `Partial<${typeName}>`,
|
|
267
|
+
optional: true
|
|
268
|
+
} });
|
|
269
|
+
return /* @__PURE__ */ jsx(File.Source, {
|
|
270
|
+
name,
|
|
271
|
+
isExportable: true,
|
|
272
|
+
isIndexable: true,
|
|
273
|
+
children: /* @__PURE__ */ jsxs(Function, {
|
|
274
|
+
export: true,
|
|
275
|
+
name,
|
|
276
|
+
JSDoc: { comments: [description ? `@description ${transformers.jsStringEscape(description)}` : void 0].filter(Boolean) },
|
|
277
|
+
params: canOverride ? params.toConstructor() : void 0,
|
|
278
|
+
returnType: canOverride ? typeName : void 0,
|
|
279
|
+
children: [
|
|
280
|
+
seed ? `faker.seed(${JSON.stringify(seed)})` : void 0,
|
|
281
|
+
/* @__PURE__ */ jsx("br", {}),
|
|
282
|
+
`return ${fakerTextWithOverride}`
|
|
283
|
+
]
|
|
284
|
+
})
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
//#endregion
|
|
289
|
+
export { Faker };
|
|
290
|
+
//# sourceMappingURL=components-BwSq4Ikw.js.map
|