@kubb/plugin-faker 5.0.0-beta.4 → 5.0.0-beta.56
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 +39 -22
- package/dist/{Faker-CdyPfOPg.d.ts → Faker-A5UuxwJj.d.ts} +3 -3
- package/dist/{Faker-fcQEB9i5.js → Faker-CHh0JtBG.js} +41 -145
- package/dist/Faker-CHh0JtBG.js.map +1 -0
- package/dist/{Faker-BgleOzVN.cjs → Faker-CcGjn5ZM.cjs} +40 -174
- package/dist/Faker-CcGjn5ZM.cjs.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +1 -1
- package/dist/components.js +1 -1
- package/dist/{fakerGenerator-D7daHCh6.js → fakerGenerator-DDNsdbH2.js} +237 -94
- package/dist/fakerGenerator-DDNsdbH2.js.map +1 -0
- package/dist/{fakerGenerator-VJEVzLjc.cjs → fakerGenerator-DrwGWYwv.cjs} +240 -97
- package/dist/fakerGenerator-DrwGWYwv.cjs.map +1 -0
- package/dist/fakerGenerator-KKVr-CA2.d.ts +14 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +1 -1
- package/dist/generators.js +1 -1
- package/dist/index.cjs +240 -69
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +35 -15
- package/dist/index.js +241 -70
- package/dist/index.js.map +1 -1
- package/dist/{printerFaker-CJiwzoto.d.ts → printerFaker-CMCJT3FB.d.ts} +68 -35
- package/package.json +12 -22
- package/src/components/Faker.tsx +51 -65
- package/src/generators/fakerGenerator.tsx +108 -72
- package/src/plugin.ts +27 -23
- package/src/printers/printerFaker.ts +102 -40
- package/src/resolvers/resolverFaker.ts +31 -39
- package/src/types.ts +40 -31
- package/src/utils.ts +7 -106
- package/dist/Faker-BgleOzVN.cjs.map +0 -1
- package/dist/Faker-fcQEB9i5.js.map +0 -1
- package/dist/fakerGenerator-C3Ho3BaI.d.ts +0 -9
- package/dist/fakerGenerator-D7daHCh6.js.map +0 -1
- package/dist/fakerGenerator-VJEVzLjc.cjs.map +0 -1
- package/extension.yaml +0 -364
- /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
|
@@ -1,40 +1,102 @@
|
|
|
1
|
-
const require_Faker = require("./Faker-
|
|
1
|
+
const require_Faker = require("./Faker-CcGjn5ZM.cjs");
|
|
2
|
+
let _kubb_ast_utils = require("@kubb/ast/utils");
|
|
2
3
|
let _kubb_core = require("@kubb/core");
|
|
3
4
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
4
5
|
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
5
6
|
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
6
|
-
//#region ../../internals/utils/src/
|
|
7
|
+
//#region ../../internals/utils/src/imports.ts
|
|
8
|
+
function escapeRegExp(value) {
|
|
9
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
10
|
+
}
|
|
11
|
+
function getImportNames(entry) {
|
|
12
|
+
return (Array.isArray(entry.name) ? entry.name : [entry.name]).map((name) => {
|
|
13
|
+
if (typeof name === "string") return name;
|
|
14
|
+
return name.name ?? name.propertyName;
|
|
15
|
+
}).filter((name) => Boolean(name));
|
|
16
|
+
}
|
|
17
|
+
function filterUsedImports(imports, text, skipImportNames = []) {
|
|
18
|
+
const skip = new Set(skipImportNames);
|
|
19
|
+
return imports.filter((entry) => {
|
|
20
|
+
return getImportNames(entry).some((name) => {
|
|
21
|
+
if (skip.has(name)) return false;
|
|
22
|
+
return new RegExp(`\\b${escapeRegExp(name)}\\b(?=\\s*\\()`).test(text);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function aliasConflictingImports(imports, reservedNames) {
|
|
27
|
+
const reservedNameSet = new Set(reservedNames);
|
|
28
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
29
|
+
return {
|
|
30
|
+
imports: imports.map((entry) => {
|
|
31
|
+
const aliasedNames = (Array.isArray(entry.name) ? entry.name : [entry.name]).map((item) => {
|
|
32
|
+
if (typeof item !== "string" || !reservedNameSet.has(item)) return item;
|
|
33
|
+
const alias = `${item}Schema`;
|
|
34
|
+
aliases.set(item, alias);
|
|
35
|
+
return {
|
|
36
|
+
propertyName: item,
|
|
37
|
+
name: alias
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
return aliasedNames.some((item) => typeof item === "object" && item.name) ? {
|
|
41
|
+
...entry,
|
|
42
|
+
name: aliasedNames
|
|
43
|
+
} : entry;
|
|
44
|
+
}),
|
|
45
|
+
aliases
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function rewriteAliasedImports(text, aliases) {
|
|
49
|
+
return Array.from(aliases).reduce((acc, [name, alias]) => acc.replace(new RegExp(`\\b${escapeRegExp(name)}\\b`, "g"), alias), text);
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region ../../internals/shared/src/operation.ts
|
|
7
53
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* stringify('hello') // '"hello"'
|
|
12
|
-
* stringify('"hello"') // '"hello"'
|
|
54
|
+
* Maps a content type to the PascalCase suffix used to name per-content-type variants
|
|
55
|
+
* (e.g. `application/json` → `Json`, `application/xml` → `Xml`, `multipart/form-data` → `FormData`).
|
|
13
56
|
*/
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
57
|
+
function getContentTypeSuffix(contentType) {
|
|
58
|
+
const baseType = contentType.split(";")[0].trim();
|
|
59
|
+
if (baseType === "application/json") return "Json";
|
|
60
|
+
if (baseType === "multipart/form-data") return "FormData";
|
|
61
|
+
if (baseType === "application/x-www-form-urlencoded") return "FormUrlEncoded";
|
|
62
|
+
const parts = (baseType.split("/").pop() ?? baseType).split(/[^a-zA-Z0-9]+/).filter(Boolean);
|
|
63
|
+
if (parts.length === 0) return "Unknown";
|
|
64
|
+
return parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
17
65
|
}
|
|
18
|
-
//#endregion
|
|
19
|
-
//#region ../../internals/utils/src/regexp.ts
|
|
20
66
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* toRegExpString('^(?im)foo') // → 'new RegExp("foo", "im")'
|
|
27
|
-
* toRegExpString('^(?im)foo', null) // → '/foo/im'
|
|
67
|
+
* Appends a content-type suffix to a base name, keeping a trailing `Data` segment last
|
|
68
|
+
* (e.g. `AddPetData` + `Json` → `AddPetJsonData`, `AddPetStatus200` + `Xml` → `AddPetStatus200Xml`).
|
|
28
69
|
*/
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
70
|
+
function getPerContentTypeName(baseName, suffix) {
|
|
71
|
+
if (baseName.endsWith("Data")) return suffix.endsWith("Data") ? baseName.slice(0, -4) + suffix : `${baseName.slice(0, -4)}${suffix}Data`;
|
|
72
|
+
return baseName + suffix;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Resolves per-content-type variant names for a set of content entries, deduplicating suffix
|
|
76
|
+
* collisions with a numeric counter. Entries without a schema are skipped. The returned `suffix` is
|
|
77
|
+
* the final (possibly counter-augmented) value, so callers can derive parallel names in another
|
|
78
|
+
* namespace (e.g. plugin-faker deriving the matching plugin-ts type name).
|
|
79
|
+
*/
|
|
80
|
+
function resolveContentTypeVariants(entries, baseName) {
|
|
81
|
+
const usedNames = /* @__PURE__ */ new Set();
|
|
82
|
+
return entries.filter((entry) => entry.schema).map((entry) => {
|
|
83
|
+
const baseSuffix = getContentTypeSuffix(entry.contentType);
|
|
84
|
+
let suffix = baseSuffix;
|
|
85
|
+
let name = getPerContentTypeName(baseName, suffix);
|
|
86
|
+
let counter = 2;
|
|
87
|
+
while (usedNames.has(name)) {
|
|
88
|
+
suffix = `${baseSuffix}${counter++}`;
|
|
89
|
+
name = getPerContentTypeName(baseName, suffix);
|
|
90
|
+
}
|
|
91
|
+
usedNames.add(name);
|
|
92
|
+
return {
|
|
93
|
+
name,
|
|
94
|
+
suffix,
|
|
95
|
+
schema: entry.schema,
|
|
96
|
+
keysToOmit: entry.keysToOmit,
|
|
97
|
+
contentType: entry.contentType
|
|
98
|
+
};
|
|
99
|
+
});
|
|
38
100
|
}
|
|
39
101
|
//#endregion
|
|
40
102
|
//#region src/printers/printerFaker.ts
|
|
@@ -99,7 +161,7 @@ const fakerKeywordMapper = {
|
|
|
99
161
|
return `{...${items.join(", ...")}}`;
|
|
100
162
|
},
|
|
101
163
|
matches: (value = "", regexGenerator = "faker") => {
|
|
102
|
-
if (regexGenerator === "randexp") return `${toRegExpString(value, "RandExp")}.gen()`;
|
|
164
|
+
if (regexGenerator === "randexp") return `${(0, _kubb_ast_utils.toRegExpString)(value, "RandExp")}.gen()`;
|
|
103
165
|
return `faker.helpers.fromRegExp("${value}")`;
|
|
104
166
|
},
|
|
105
167
|
email: () => "faker.internet.email()",
|
|
@@ -110,9 +172,25 @@ function getEnumValues(node) {
|
|
|
110
172
|
return node.enumValues ?? [];
|
|
111
173
|
}
|
|
112
174
|
function parseEnumValue(value) {
|
|
113
|
-
if (typeof value === "string") return stringify(value);
|
|
175
|
+
if (typeof value === "string") return (0, _kubb_ast_utils.stringify)(value);
|
|
114
176
|
return value;
|
|
115
177
|
}
|
|
178
|
+
/** Reads the discriminator literal off a variant, or `undefined` when it can't be determined. */
|
|
179
|
+
function getDiscriminatorValue(member, discriminatorPropertyName) {
|
|
180
|
+
const prop = _kubb_core.ast.narrowSchema(member, "object")?.properties?.find((p) => p.name === discriminatorPropertyName);
|
|
181
|
+
const enumNode = prop ? _kubb_core.ast.narrowSchema(prop.schema, "enum") : null;
|
|
182
|
+
return enumNode ? getEnumValues(enumNode)[0] : void 0;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Type expression for an object property's value, indexed off the parent `typeName`.
|
|
186
|
+
*
|
|
187
|
+
* In a union (`oneOf`), a key that only some branches declare turns a plain `NonNullable<T>[K]`
|
|
188
|
+
* into a TS2339 error, so union members guard the access. The breakdown is below.
|
|
189
|
+
*/
|
|
190
|
+
function indexedTypeName(typeName, propertyName, nestedInUnion) {
|
|
191
|
+
const key = JSON.stringify(propertyName);
|
|
192
|
+
return nestedInUnion ? `(NonNullable<${typeName}> & Record<${key}, unknown>)[${key}]` : `NonNullable<${typeName}>[${key}]`;
|
|
193
|
+
}
|
|
116
194
|
/**
|
|
117
195
|
* Creates a Faker printer that generates mock data generation code from schema nodes.
|
|
118
196
|
* Handles circular references gracefully by emitting memoizing getters for cyclic properties.
|
|
@@ -157,7 +235,7 @@ const printerFaker = _kubb_core.ast.definePrinter((options) => {
|
|
|
157
235
|
return fakerKeywordMapper.time(node.representation ?? "string", this.options.dateParser);
|
|
158
236
|
},
|
|
159
237
|
ref(node) {
|
|
160
|
-
const refName = node.ref ?
|
|
238
|
+
const refName = node.ref ? (0, _kubb_ast_utils.extractRefName)(node.ref) ?? node.name ?? node.schema?.name : node.name ?? node.schema?.name;
|
|
161
239
|
if (!refName) throw new Error("Name not defined for ref node");
|
|
162
240
|
if (this.options.schemaName && refName === this.options.schemaName) return "undefined as any";
|
|
163
241
|
const resolvedName = node.ref ? this.options.resolver.resolveName(refName) : refName;
|
|
@@ -168,7 +246,20 @@ const printerFaker = _kubb_core.ast.definePrinter((options) => {
|
|
|
168
246
|
return fakerKeywordMapper.enum(getEnumValues(node).map(parseEnumValue), this.options.typeName);
|
|
169
247
|
},
|
|
170
248
|
union(node) {
|
|
171
|
-
const
|
|
249
|
+
const { discriminatorPropertyName } = node;
|
|
250
|
+
const baseTypeName = this.options.typeName;
|
|
251
|
+
const items = (node.members ?? []).map((member) => {
|
|
252
|
+
const value = discriminatorPropertyName ? getDiscriminatorValue(member, discriminatorPropertyName) : void 0;
|
|
253
|
+
if (baseTypeName && value !== void 0) return printNested(member, {
|
|
254
|
+
typeName: `Extract<NonNullable<${baseTypeName}>, { ${JSON.stringify(discriminatorPropertyName)}: ${parseEnumValue(value)} }>`,
|
|
255
|
+
nestedInObject: true
|
|
256
|
+
});
|
|
257
|
+
return printNested(member, {
|
|
258
|
+
typeName: baseTypeName,
|
|
259
|
+
nestedInObject: true,
|
|
260
|
+
nestedInUnion: true
|
|
261
|
+
});
|
|
262
|
+
}).filter((item) => Boolean(item));
|
|
172
263
|
return fakerKeywordMapper.union(items);
|
|
173
264
|
},
|
|
174
265
|
intersection(node) {
|
|
@@ -191,18 +282,18 @@ const printerFaker = _kubb_core.ast.definePrinter((options) => {
|
|
|
191
282
|
},
|
|
192
283
|
object(node) {
|
|
193
284
|
const cyclicSchemas = this.options.cyclicSchemas;
|
|
194
|
-
return
|
|
195
|
-
if (this.options.mapper && Object.hasOwn(this.options.mapper, property.name)) return
|
|
285
|
+
return (0, _kubb_ast_utils.buildObject)((node.properties ?? []).map((property) => {
|
|
286
|
+
if (this.options.mapper && Object.hasOwn(this.options.mapper, property.name)) return `${(0, _kubb_ast_utils.objectKey)(property.name)}: ${this.options.mapper[property.name]}`;
|
|
196
287
|
const value = printNested(property.schema, {
|
|
197
|
-
typeName: this.options.typeName ?
|
|
288
|
+
typeName: this.options.typeName ? indexedTypeName(this.options.typeName, property.name, this.options.nestedInUnion) : void 0,
|
|
198
289
|
nestedInObject: true
|
|
199
290
|
}) ?? "undefined";
|
|
200
291
|
if (cyclicSchemas && _kubb_core.ast.containsCircularRef(property.schema, {
|
|
201
292
|
circularSchemas: cyclicSchemas,
|
|
202
293
|
excludeName: this.options.schemaName
|
|
203
|
-
})) return `get ${property.name}() { const _value = ${value}; Object.defineProperty(this, ${JSON.stringify(property.name)}, { value: _value, configurable: true, writable: true, enumerable: true }); return _value }`;
|
|
204
|
-
return
|
|
205
|
-
})
|
|
294
|
+
})) return `get ${(0, _kubb_ast_utils.objectKey)(property.name)}() { const _value = ${value}; Object.defineProperty(this, ${JSON.stringify(property.name)}, { value: _value, configurable: true, writable: true, enumerable: true }); return _value }`;
|
|
295
|
+
return `${(0, _kubb_ast_utils.objectKey)(property.name)}: ${value}`;
|
|
296
|
+
}));
|
|
206
297
|
},
|
|
207
298
|
...options.nodes
|
|
208
299
|
},
|
|
@@ -213,6 +304,12 @@ const printerFaker = _kubb_core.ast.definePrinter((options) => {
|
|
|
213
304
|
});
|
|
214
305
|
//#endregion
|
|
215
306
|
//#region src/generators/fakerGenerator.tsx
|
|
307
|
+
/**
|
|
308
|
+
* Built-in generator for `@kubb/plugin-faker`. Emits one `createX` factory
|
|
309
|
+
* per schema in the spec plus per-operation request/response factories. Each
|
|
310
|
+
* factory returns a value matching the corresponding TypeScript type from
|
|
311
|
+
* `@kubb/plugin-ts`.
|
|
312
|
+
*/
|
|
216
313
|
const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
217
314
|
name: "faker",
|
|
218
315
|
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
@@ -220,11 +317,13 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
220
317
|
const { adapter, config, resolver, root } = ctx;
|
|
221
318
|
const { output, group, dateParser, regexGenerator, mapper, seed, locale, printer } = ctx.options;
|
|
222
319
|
const pluginTs = ctx.driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
223
|
-
if (!node.name || !pluginTs
|
|
320
|
+
if (!node.name || !pluginTs) return;
|
|
224
321
|
const tsResolver = ctx.driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
225
|
-
const
|
|
226
|
-
const
|
|
227
|
-
const
|
|
322
|
+
const schemaName = node.name;
|
|
323
|
+
const isEnumSchema = !!_kubb_core.ast.narrowSchema(node, _kubb_core.ast.schemaTypes.enum);
|
|
324
|
+
const tsEnumType = pluginTs.options?.enum?.type;
|
|
325
|
+
const tsEnumTypeSuffix = pluginTs.options?.enum?.typeSuffix ?? "Key";
|
|
326
|
+
const schemaTypeName = isEnumSchema && tsEnumType === "asConst" ? tsResolver.resolveEnumKeyName({ name: schemaName }, tsEnumTypeSuffix) : tsResolver.resolveTypeName(schemaName);
|
|
228
327
|
const meta = {
|
|
229
328
|
name: resolver.resolveName(schemaName),
|
|
230
329
|
file: resolver.resolveFile({
|
|
@@ -233,20 +332,20 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
233
332
|
}, {
|
|
234
333
|
root,
|
|
235
334
|
output,
|
|
236
|
-
group
|
|
335
|
+
group: group ?? void 0
|
|
237
336
|
}),
|
|
238
|
-
typeName:
|
|
337
|
+
typeName: schemaTypeName,
|
|
239
338
|
typeFile: tsResolver.resolveFile({
|
|
240
339
|
name: schemaName,
|
|
241
340
|
extname: ".ts"
|
|
242
341
|
}, {
|
|
243
342
|
root,
|
|
244
343
|
output: pluginTs.options?.output ?? output,
|
|
245
|
-
group: pluginTs.options?.group
|
|
344
|
+
group: pluginTs.options?.group ?? void 0
|
|
246
345
|
})
|
|
247
346
|
};
|
|
248
|
-
const canOverride = require_Faker.canOverrideSchema(
|
|
249
|
-
const cyclicSchemas =
|
|
347
|
+
const canOverride = require_Faker.canOverrideSchema(node);
|
|
348
|
+
const cyclicSchemas = new Set(ctx.meta.circularNames);
|
|
250
349
|
const printerInstance = printerFaker({
|
|
251
350
|
resolver,
|
|
252
351
|
schemaName,
|
|
@@ -257,16 +356,16 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
257
356
|
nodes: printer?.nodes,
|
|
258
357
|
cyclicSchemas
|
|
259
358
|
});
|
|
260
|
-
const fakerText = printerInstance.print(
|
|
359
|
+
const fakerText = printerInstance.print(node) ?? "undefined";
|
|
261
360
|
const typeReference = require_Faker.resolveTypeReference({
|
|
262
|
-
node
|
|
361
|
+
node,
|
|
263
362
|
canOverride,
|
|
264
363
|
name: meta.name,
|
|
265
364
|
typeName: meta.typeName,
|
|
266
365
|
filePath: meta.file.path,
|
|
267
366
|
typeFilePath: meta.typeFile.path
|
|
268
367
|
});
|
|
269
|
-
const usedImports =
|
|
368
|
+
const usedImports = filterUsedImports(adapter.getImports(node, (schemaName) => ({
|
|
270
369
|
name: resolver.resolveName(schemaName),
|
|
271
370
|
path: resolver.resolveFile({
|
|
272
371
|
name: schemaName,
|
|
@@ -274,20 +373,28 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
274
373
|
}, {
|
|
275
374
|
root,
|
|
276
375
|
output,
|
|
277
|
-
group
|
|
376
|
+
group: group ?? void 0
|
|
278
377
|
}).path
|
|
279
|
-
}))
|
|
378
|
+
})), fakerText);
|
|
280
379
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
281
380
|
baseName: meta.file.baseName,
|
|
282
381
|
path: meta.file.path,
|
|
283
382
|
meta: meta.file.meta,
|
|
284
|
-
banner: resolver.resolveBanner(
|
|
383
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
285
384
|
output,
|
|
286
|
-
config
|
|
385
|
+
config,
|
|
386
|
+
file: {
|
|
387
|
+
path: meta.file.path,
|
|
388
|
+
baseName: meta.file.baseName
|
|
389
|
+
}
|
|
287
390
|
}),
|
|
288
|
-
footer: resolver.resolveFooter(
|
|
391
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
289
392
|
output,
|
|
290
|
-
config
|
|
393
|
+
config,
|
|
394
|
+
file: {
|
|
395
|
+
path: meta.file.path,
|
|
396
|
+
baseName: meta.file.baseName
|
|
397
|
+
}
|
|
291
398
|
}),
|
|
292
399
|
children: [
|
|
293
400
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
@@ -311,7 +418,7 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
311
418
|
path: typeReference.importPath,
|
|
312
419
|
name: [meta.typeName]
|
|
313
420
|
}),
|
|
314
|
-
|
|
421
|
+
usedImports.map((imp) => /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
315
422
|
root: meta.file.path,
|
|
316
423
|
path: imp.path,
|
|
317
424
|
name: imp.name
|
|
@@ -323,8 +430,8 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
323
430
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(require_Faker.Faker, {
|
|
324
431
|
name: meta.name,
|
|
325
432
|
typeName: typeReference.typeName,
|
|
326
|
-
description:
|
|
327
|
-
node
|
|
433
|
+
description: node.description,
|
|
434
|
+
node,
|
|
328
435
|
printer: printerInstance,
|
|
329
436
|
seed,
|
|
330
437
|
canOverride
|
|
@@ -343,27 +450,54 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
343
450
|
name: require_Faker.resolveParamNameByLocation(resolver, node, param),
|
|
344
451
|
typeName: require_Faker.resolveParamNameByLocation(tsResolver, node, param)
|
|
345
452
|
}));
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
453
|
+
function expandContentUnits(entries, baseName, tsBaseName, description, decorate) {
|
|
454
|
+
const withSchema = entries.filter((entry) => entry.schema);
|
|
455
|
+
if (withSchema.length <= 1) {
|
|
456
|
+
const primary = withSchema[0] ?? entries[0];
|
|
457
|
+
if (!primary?.schema) return [];
|
|
458
|
+
return [{
|
|
459
|
+
schema: decorate ? decorate(primary.schema) : primary.schema,
|
|
460
|
+
name: baseName,
|
|
461
|
+
typeName: tsBaseName,
|
|
462
|
+
description,
|
|
463
|
+
skipImportNames: []
|
|
464
|
+
}];
|
|
465
|
+
}
|
|
466
|
+
const variants = resolveContentTypeVariants(entries, baseName);
|
|
467
|
+
const unionSchema = _kubb_core.ast.createSchema({
|
|
468
|
+
type: "union",
|
|
469
|
+
members: variants.map((variant) => _kubb_core.ast.createSchema({
|
|
470
|
+
type: "ref",
|
|
471
|
+
name: variant.name
|
|
472
|
+
}))
|
|
473
|
+
});
|
|
474
|
+
return [...variants.map((variant) => ({
|
|
475
|
+
schema: decorate ? decorate(variant.schema) : variant.schema,
|
|
476
|
+
name: variant.name,
|
|
477
|
+
typeName: getPerContentTypeName(tsBaseName, variant.suffix),
|
|
478
|
+
description,
|
|
479
|
+
skipImportNames: []
|
|
480
|
+
})), {
|
|
481
|
+
schema: unionSchema,
|
|
482
|
+
name: baseName,
|
|
483
|
+
typeName: tsBaseName,
|
|
484
|
+
description,
|
|
485
|
+
skipImportNames: variants.map((variant) => variant.name)
|
|
486
|
+
}];
|
|
487
|
+
}
|
|
488
|
+
const responseUnits = node.responses.flatMap((response) => expandContentUnits(response.content ?? [], resolver.resolveResponseStatusName(node, response.statusCode), tsResolver.resolveResponseStatusName(node, response.statusCode), response.description));
|
|
489
|
+
const dataUnits = expandContentUnits(node.requestBody?.content ?? [], resolver.resolveDataName(node), tsResolver.resolveDataName(node), node.requestBody?.description, (schema) => ({
|
|
490
|
+
...schema,
|
|
491
|
+
description: node.requestBody?.description ?? schema.description
|
|
350
492
|
}));
|
|
351
|
-
const dataEntry = node.requestBody?.content?.[0]?.schema ? {
|
|
352
|
-
schema: {
|
|
353
|
-
...node.requestBody.content[0].schema,
|
|
354
|
-
description: node.requestBody.description ?? node.requestBody.content[0].schema.description
|
|
355
|
-
},
|
|
356
|
-
name: resolver.resolveDataName(node),
|
|
357
|
-
typeName: tsResolver.resolveDataName(node),
|
|
358
|
-
description: node.requestBody.description ?? node.requestBody.content[0].schema.description
|
|
359
|
-
} : null;
|
|
360
493
|
const responseName = resolver.resolveResponseName(node);
|
|
361
494
|
const localHelperNames = new Set([
|
|
362
495
|
...paramEntries.map((entry) => entry.name),
|
|
363
|
-
...
|
|
364
|
-
...
|
|
496
|
+
...responseUnits.map((unit) => unit.name),
|
|
497
|
+
...dataUnits.map((unit) => unit.name),
|
|
365
498
|
responseName
|
|
366
499
|
]);
|
|
500
|
+
const cyclicSchemas = new Set(ctx.meta.circularNames);
|
|
367
501
|
const meta = {
|
|
368
502
|
file: resolver.resolveFile({
|
|
369
503
|
name: node.operationId,
|
|
@@ -373,7 +507,7 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
373
507
|
}, {
|
|
374
508
|
root,
|
|
375
509
|
output,
|
|
376
|
-
group
|
|
510
|
+
group: group ?? void 0
|
|
377
511
|
}),
|
|
378
512
|
typeFile: tsResolver.resolveFile({
|
|
379
513
|
name: node.operationId,
|
|
@@ -383,7 +517,7 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
383
517
|
}, {
|
|
384
518
|
root,
|
|
385
519
|
output: pluginTs.options?.output ?? output,
|
|
386
|
-
group: pluginTs.options?.group
|
|
520
|
+
group: pluginTs.options?.group ?? void 0
|
|
387
521
|
})
|
|
388
522
|
};
|
|
389
523
|
function resolveMockImports(schema) {
|
|
@@ -395,14 +529,13 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
395
529
|
}, {
|
|
396
530
|
root,
|
|
397
531
|
output,
|
|
398
|
-
group
|
|
532
|
+
group: group ?? void 0
|
|
399
533
|
}).path
|
|
400
534
|
})).filter((entry) => entry.path !== meta.file.path);
|
|
401
535
|
}
|
|
402
536
|
function renderEntry({ schema, name, typeName, description, skipImportNames = [] }) {
|
|
403
537
|
if (!schema) return null;
|
|
404
538
|
const canOverride = require_Faker.canOverrideSchema(schema);
|
|
405
|
-
const cyclicSchemas = adapter.inputNode ? _kubb_core.ast.findCircularSchemas(adapter.inputNode.schemas) : void 0;
|
|
406
539
|
const printerInstance = printerFaker({
|
|
407
540
|
resolver,
|
|
408
541
|
schemaName: name,
|
|
@@ -414,8 +547,8 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
414
547
|
cyclicSchemas
|
|
415
548
|
});
|
|
416
549
|
const fakerText = printerInstance.print(schema) ?? "undefined";
|
|
417
|
-
const { imports, aliases } =
|
|
418
|
-
const rewrittenFakerText =
|
|
550
|
+
const { imports, aliases } = aliasConflictingImports(filterUsedImports(resolveMockImports(schema), fakerText, skipImportNames), localHelperNames);
|
|
551
|
+
const rewrittenFakerText = rewriteAliasedImports(fakerText, aliases);
|
|
419
552
|
const typeReference = require_Faker.resolveTypeReference({
|
|
420
553
|
node: schema,
|
|
421
554
|
canOverride,
|
|
@@ -458,13 +591,21 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
458
591
|
baseName: meta.file.baseName,
|
|
459
592
|
path: meta.file.path,
|
|
460
593
|
meta: meta.file.meta,
|
|
461
|
-
banner: resolver.resolveBanner(
|
|
594
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
462
595
|
output,
|
|
463
|
-
config
|
|
596
|
+
config,
|
|
597
|
+
file: {
|
|
598
|
+
path: meta.file.path,
|
|
599
|
+
baseName: meta.file.baseName
|
|
600
|
+
}
|
|
464
601
|
}),
|
|
465
|
-
footer: resolver.resolveFooter(
|
|
602
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
466
603
|
output,
|
|
467
|
-
config
|
|
604
|
+
config,
|
|
605
|
+
file: {
|
|
606
|
+
path: meta.file.path,
|
|
607
|
+
baseName: meta.file.baseName
|
|
608
|
+
}
|
|
468
609
|
}),
|
|
469
610
|
children: [
|
|
470
611
|
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
@@ -487,23 +628,25 @@ const fakerGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
487
628
|
name,
|
|
488
629
|
typeName
|
|
489
630
|
})),
|
|
490
|
-
|
|
491
|
-
schema:
|
|
492
|
-
name,
|
|
493
|
-
typeName,
|
|
494
|
-
description:
|
|
631
|
+
responseUnits.map((unit) => renderEntry({
|
|
632
|
+
schema: unit.schema,
|
|
633
|
+
name: unit.name,
|
|
634
|
+
typeName: unit.typeName,
|
|
635
|
+
description: unit.description,
|
|
636
|
+
skipImportNames: unit.skipImportNames
|
|
637
|
+
})),
|
|
638
|
+
dataUnits.map((unit) => renderEntry({
|
|
639
|
+
schema: unit.schema,
|
|
640
|
+
name: unit.name,
|
|
641
|
+
typeName: unit.typeName,
|
|
642
|
+
description: unit.description,
|
|
643
|
+
skipImportNames: unit.skipImportNames
|
|
495
644
|
})),
|
|
496
|
-
dataEntry ? renderEntry({
|
|
497
|
-
schema: dataEntry.schema,
|
|
498
|
-
name: dataEntry.name,
|
|
499
|
-
typeName: dataEntry.typeName,
|
|
500
|
-
description: dataEntry.description
|
|
501
|
-
}) : null,
|
|
502
645
|
renderEntry({
|
|
503
646
|
schema: require_Faker.buildResponseUnionSchema(node, resolver),
|
|
504
647
|
name: responseName,
|
|
505
648
|
typeName: tsResolver.resolveResponseName(node),
|
|
506
|
-
skipImportNames:
|
|
649
|
+
skipImportNames: responseUnits.map((unit) => unit.name)
|
|
507
650
|
})
|
|
508
651
|
]
|
|
509
652
|
});
|
|
@@ -523,4 +666,4 @@ Object.defineProperty(exports, "printerFaker", {
|
|
|
523
666
|
}
|
|
524
667
|
});
|
|
525
668
|
|
|
526
|
-
//# sourceMappingURL=fakerGenerator-
|
|
669
|
+
//# sourceMappingURL=fakerGenerator-DrwGWYwv.cjs.map
|