@kubb/plugin-faker 5.1.9 → 5.1.11
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/index.cjs +39 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +40 -43
- package/dist/index.js.map +1 -1
- package/kubb-extension.json +30 -0
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -16,12 +16,23 @@ let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
|
16
16
|
*
|
|
17
17
|
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
18
18
|
*/
|
|
19
|
-
function
|
|
19
|
+
function convert(text, pascal) {
|
|
20
20
|
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
21
21
|
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
22
22
|
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
23
23
|
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
24
24
|
}
|
|
25
|
+
const cache = /* @__PURE__ */ new Map();
|
|
26
|
+
const cacheLimit = 1e4;
|
|
27
|
+
function toCamelOrPascal(text, pascal) {
|
|
28
|
+
const key = `${pascal ? "P" : "c"}${text}`;
|
|
29
|
+
const cached = cache.get(key);
|
|
30
|
+
if (cached !== void 0) return cached;
|
|
31
|
+
const result = convert(text, pascal);
|
|
32
|
+
if (cache.size >= cacheLimit) cache.clear();
|
|
33
|
+
cache.set(key, result);
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
25
36
|
/**
|
|
26
37
|
* Converts `text` to camelCase.
|
|
27
38
|
*
|
|
@@ -224,29 +235,16 @@ function stringify(value) {
|
|
|
224
235
|
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
|
|
225
236
|
}
|
|
226
237
|
/**
|
|
227
|
-
* Escapes
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
238
|
+
* Escapes an `@` at the start of a line, so a multi-line description can't start a new JSDoc tag.
|
|
239
|
+
* Editors render `\@` as `@` in hovers.
|
|
231
240
|
*
|
|
232
241
|
* @example
|
|
233
242
|
* ```ts
|
|
234
|
-
*
|
|
243
|
+
* jsDocEscape('intro\n@example value') // 'intro\n\\@example value'
|
|
235
244
|
* ```
|
|
236
245
|
*/
|
|
237
|
-
function
|
|
238
|
-
return `${input}`.replace(
|
|
239
|
-
switch (character) {
|
|
240
|
-
case "\"":
|
|
241
|
-
case "'":
|
|
242
|
-
case "\\": return `\\${character}`;
|
|
243
|
-
case "\n": return "\\n";
|
|
244
|
-
case "\r": return "\\r";
|
|
245
|
-
case "\u2028": return "\\u2028";
|
|
246
|
-
case "\u2029": return "\\u2029";
|
|
247
|
-
default: return "";
|
|
248
|
-
}
|
|
249
|
-
});
|
|
246
|
+
function jsDocEscape(input) {
|
|
247
|
+
return `${input}`.replace(/^(\s*)@/gm, "$1\\@");
|
|
250
248
|
}
|
|
251
249
|
/**
|
|
252
250
|
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
@@ -402,6 +400,12 @@ const NON_INLINABLE_RESPONSE_TYPES = /* @__PURE__ */ new Set([
|
|
|
402
400
|
function canOverrideSchema(node) {
|
|
403
401
|
return !NON_OVERRIDABLE_TYPES.has(node.type);
|
|
404
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* Follows a ref, including alias chains, to the schema it resolves to. Unresolved refs are returned as is.
|
|
405
|
+
*/
|
|
406
|
+
function resolveRefTarget(node) {
|
|
407
|
+
return node.type === "ref" && node.schema ? resolveRefTarget(node.schema) : node;
|
|
408
|
+
}
|
|
405
409
|
function shouldInlineSingleResponseSchema(schema) {
|
|
406
410
|
return !NON_INLINABLE_RESPONSE_TYPES.has(schema.type);
|
|
407
411
|
}
|
|
@@ -492,7 +496,7 @@ function resolveFakerTypeUsage(node, typeName, canOverride) {
|
|
|
492
496
|
return {
|
|
493
497
|
dataType,
|
|
494
498
|
returnType,
|
|
495
|
-
usesTypeName: dataType.includes(typeName) || Boolean(returnType?.includes(typeName))
|
|
499
|
+
usesTypeName: canOverride && dataType.includes(typeName) || Boolean(returnType?.includes(typeName))
|
|
496
500
|
};
|
|
497
501
|
}
|
|
498
502
|
//#endregion
|
|
@@ -524,7 +528,7 @@ function Faker({ node, description, name, typeName, printer, canOverride, typeMo
|
|
|
524
528
|
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
|
|
525
529
|
export: true,
|
|
526
530
|
name,
|
|
527
|
-
JSDoc: { comments: description ? [`@description ${
|
|
531
|
+
JSDoc: { comments: description ? [`@description ${jsDocEscape(description)}`] : [] },
|
|
528
532
|
params: canOverride ? paramsSignature : void 0,
|
|
529
533
|
returnType: returnType ?? void 0,
|
|
530
534
|
children: `return ${returnExpression}`
|
|
@@ -554,7 +558,7 @@ return {
|
|
|
554
558
|
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
|
|
555
559
|
export: true,
|
|
556
560
|
name,
|
|
557
|
-
JSDoc: { comments: description ? [`@description ${
|
|
561
|
+
JSDoc: { comments: description ? [`@description ${jsDocEscape(description)}`] : [] },
|
|
558
562
|
generics: typeMode === "schema" ? void 0 : `TData extends Partial<${typeName}> = object`,
|
|
559
563
|
params: typeMode === "schema" ? `data?: Partial<${typeName}>` : "data?: TData",
|
|
560
564
|
returnType: typeMode === "schema" ? typeName : void 0,
|
|
@@ -896,18 +900,22 @@ function parseEnumValue(value) {
|
|
|
896
900
|
return value;
|
|
897
901
|
}
|
|
898
902
|
/**
|
|
899
|
-
* Reads the discriminator literal off a variant,
|
|
903
|
+
* Reads the discriminator literal off a variant, with whether its property is required, or
|
|
904
|
+
* `undefined` when it can't be determined.
|
|
900
905
|
*
|
|
901
906
|
* A variant carries exactly one discriminator value, so a property resolving to several literals
|
|
902
907
|
* (a `ref` to the enum shared by every branch, say) identifies no branch and narrows nothing.
|
|
903
908
|
*/
|
|
904
|
-
function
|
|
909
|
+
function getDiscriminator(member, discriminatorPropertyName) {
|
|
905
910
|
for (const property of kubb_kit.ast.resolveSchemaProperties({
|
|
906
911
|
node: member,
|
|
907
912
|
propertyName: discriminatorPropertyName
|
|
908
913
|
})) {
|
|
909
914
|
const values = kubb_kit.ast.getSchemaLiteralValues(property.schema);
|
|
910
|
-
if (values.length === 1) return
|
|
915
|
+
if (values.length === 1) return {
|
|
916
|
+
value: values[0],
|
|
917
|
+
required: property.required
|
|
918
|
+
};
|
|
911
919
|
}
|
|
912
920
|
}
|
|
913
921
|
/**
|
|
@@ -979,7 +987,7 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
979
987
|
});
|
|
980
988
|
if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
|
|
981
989
|
const resolvedName = node.ref ? this.options.resolver.name(refName) : refName;
|
|
982
|
-
if (!this.options.nestedInObject) return `${resolvedName}(data)`;
|
|
990
|
+
if (!this.options.nestedInObject && canOverrideSchema(resolveRefTarget(node))) return `${resolvedName}(data)`;
|
|
983
991
|
return `${resolvedName}()`;
|
|
984
992
|
},
|
|
985
993
|
enum(node) {
|
|
@@ -990,9 +998,10 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
990
998
|
const { discriminatorPropertyName } = node;
|
|
991
999
|
const baseTypeName = this.options.typeName;
|
|
992
1000
|
return `faker.helpers.arrayElement([${mapSchemaMembers(node, (member) => {
|
|
993
|
-
const
|
|
994
|
-
if (baseTypeName &&
|
|
995
|
-
const
|
|
1001
|
+
const discriminator = discriminatorPropertyName ? getDiscriminator(member, discriminatorPropertyName) : void 0;
|
|
1002
|
+
if (baseTypeName && discriminator) {
|
|
1003
|
+
const key = `${JSON.stringify(discriminatorPropertyName)}${discriminator.required ? "" : "?"}`;
|
|
1004
|
+
const typeName = `Extract<NonNullable<${baseTypeName}>, { ${key}: ${parseEnumValue(discriminator.value)} }>`;
|
|
996
1005
|
return printNested(member, {
|
|
997
1006
|
typeName,
|
|
998
1007
|
nestedInObject: true,
|
|
@@ -1402,7 +1411,7 @@ var compatibility = {
|
|
|
1402
1411
|
};
|
|
1403
1412
|
//#endregion
|
|
1404
1413
|
//#region package.json
|
|
1405
|
-
var version = "5.1.
|
|
1414
|
+
var version = "5.1.11";
|
|
1406
1415
|
//#endregion
|
|
1407
1416
|
//#region src/resolvers/resolverFaker.ts
|
|
1408
1417
|
/**
|
|
@@ -1470,18 +1479,6 @@ const pluginFaker = (0, kubb_kit.defineExtension)({
|
|
|
1470
1479
|
path: "mocks",
|
|
1471
1480
|
barrel: { type: "named" }
|
|
1472
1481
|
}, seed, locale = "en", group, exclude = [], include, override = [], typeMode = "inferred", dateParser = "faker", regexGenerator = "faker", printer, resolver: userResolver, macros: userMacros } = options;
|
|
1473
|
-
(0, kubb_kit.assertPluginOption)({
|
|
1474
|
-
plugin: pluginFakerName,
|
|
1475
|
-
option: "typeMode",
|
|
1476
|
-
value: typeMode,
|
|
1477
|
-
allowed: ["inferred", "schema"]
|
|
1478
|
-
});
|
|
1479
|
-
(0, kubb_kit.assertPluginOption)({
|
|
1480
|
-
plugin: pluginFakerName,
|
|
1481
|
-
option: "regexGenerator",
|
|
1482
|
-
value: regexGenerator,
|
|
1483
|
-
allowed: ["faker", "randexp"]
|
|
1484
|
-
});
|
|
1485
1482
|
(0, kubb_kit.setResolver)(userResolver ? kubb_kit.Resolver.merge(resolverFaker, userResolver) : resolverFaker);
|
|
1486
1483
|
if (userMacros?.length) (0, kubb_kit.setMacros)(userMacros);
|
|
1487
1484
|
(0, kubb_kit.addGenerator)(fakerGenerator);
|