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