@kubb/plugin-faker 5.2.1 → 5.2.2
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 +171 -77
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -13
- package/dist/index.d.ts +2 -13
- package/dist/index.js +172 -78
- package/dist/index.js.map +1 -1
- package/docs/options.md +2 -0
- package/kubb-extension.json +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -2,13 +2,12 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
-
//#endregion
|
|
6
5
|
let node_path = require("node:path");
|
|
7
6
|
let kubb_kit = require("kubb/kit");
|
|
8
7
|
let kubb_jsx = require("kubb/jsx");
|
|
9
8
|
let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
|
|
10
9
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
11
|
-
|
|
10
|
+
// ../../internals/utils/src/casing.ts
|
|
12
11
|
/**
|
|
13
12
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
14
13
|
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
@@ -45,8 +44,7 @@ function toCamelOrPascal(text, pascal) {
|
|
|
45
44
|
function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
46
45
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
47
46
|
}
|
|
48
|
-
|
|
49
|
-
//#region ../../internals/utils/src/reserved.ts
|
|
47
|
+
// ../../internals/utils/src/reserved.ts
|
|
50
48
|
/**
|
|
51
49
|
* JavaScript and Java reserved words.
|
|
52
50
|
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
@@ -54,6 +52,7 @@ function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
|
54
52
|
const reservedWords = /* @__PURE__ */ new Set([
|
|
55
53
|
"abstract",
|
|
56
54
|
"arguments",
|
|
55
|
+
"await",
|
|
57
56
|
"boolean",
|
|
58
57
|
"break",
|
|
59
58
|
"byte",
|
|
@@ -184,8 +183,7 @@ function ensureValidVarName(name) {
|
|
|
184
183
|
function isIdentifier(name) {
|
|
185
184
|
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
186
185
|
}
|
|
187
|
-
|
|
188
|
-
//#region ../../internals/utils/src/strings.ts
|
|
186
|
+
// ../../internals/utils/src/strings.ts
|
|
189
187
|
/**
|
|
190
188
|
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
|
|
191
189
|
* any backslash or single quote in the content.
|
|
@@ -246,6 +244,34 @@ function stringify(value) {
|
|
|
246
244
|
function jsDocEscape(input) {
|
|
247
245
|
return `${input}`.replace(/^(\s*)@/gm, "$1\\@");
|
|
248
246
|
}
|
|
247
|
+
const NEEDS_UNICODE = /(?<!\\)(?:\\\\)*\\[pPu]\{|[\u{10000}-\u{10FFFF}]/u;
|
|
248
|
+
const KEPT_ESCAPE = /[A-Za-z0-9^$\\.*+?()[\]{}|/]/;
|
|
249
|
+
/**
|
|
250
|
+
* Drops the backslash from escapes that the `u` flag rejects: an escaped punctuation character that is
|
|
251
|
+
* not a syntax character, and `\-` outside a class. Both match the same character without the backslash.
|
|
252
|
+
*/
|
|
253
|
+
function dropIdentityEscapes(pattern) {
|
|
254
|
+
let inClass = false;
|
|
255
|
+
return pattern.replace(/\\(.)|[[\]]/gs, (match, escaped) => {
|
|
256
|
+
if (escaped === void 0) {
|
|
257
|
+
inClass = match === "[";
|
|
258
|
+
return match;
|
|
259
|
+
}
|
|
260
|
+
return KEPT_ESCAPE.test(escaped) || escaped === "-" && inClass ? match : escaped;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Builds the `RegExp` for a pattern, adding the `u` flag when it uses `\p{...}`, `\P{...}` or `\u{...}`, or
|
|
265
|
+
* holds a character outside the BMP. A pattern that is not valid under `u` keeps the flags it came with.
|
|
266
|
+
*/
|
|
267
|
+
function toRegExp(pattern, flags) {
|
|
268
|
+
if (flags?.includes("u") || !NEEDS_UNICODE.test(pattern)) return new RegExp(pattern, flags);
|
|
269
|
+
try {
|
|
270
|
+
return new RegExp(dropIdentityEscapes(pattern), `${flags ?? ""}u`);
|
|
271
|
+
} catch {
|
|
272
|
+
return new RegExp(pattern, flags);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
249
275
|
/**
|
|
250
276
|
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
251
277
|
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
|
|
@@ -262,13 +288,11 @@ function toRegExpString(text, func = "RegExp") {
|
|
|
262
288
|
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
|
|
263
289
|
const replacementTarget = match?.[1] ?? "";
|
|
264
290
|
const matchedFlags = match?.[2];
|
|
265
|
-
const
|
|
266
|
-
const { source, flags } = new RegExp(cleaned, matchedFlags);
|
|
291
|
+
const { source, flags } = toRegExp(raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, ""), matchedFlags);
|
|
267
292
|
if (func === null) return `/${source}/${flags}`;
|
|
268
293
|
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
|
|
269
294
|
}
|
|
270
|
-
|
|
271
|
-
//#region ../../internals/utils/src/codegen.ts
|
|
295
|
+
// ../../internals/utils/src/codegen.ts
|
|
272
296
|
const INDENT = " ";
|
|
273
297
|
/**
|
|
274
298
|
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
|
|
@@ -305,8 +329,7 @@ function buildObject(entries) {
|
|
|
305
329
|
if (entries.length === 0) return "{}";
|
|
306
330
|
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
|
|
307
331
|
}
|
|
308
|
-
|
|
309
|
-
//#region ../../internals/utils/src/fs.ts
|
|
332
|
+
// ../../internals/utils/src/fs.ts
|
|
310
333
|
function toSlash(p) {
|
|
311
334
|
if (p.startsWith("\\\\?\\")) return p;
|
|
312
335
|
return p.replaceAll("\\", "/");
|
|
@@ -348,8 +371,7 @@ function toFilePath(name, caseLast = camelCase) {
|
|
|
348
371
|
const parts = name.split(/\.(?=[a-zA-Z])/);
|
|
349
372
|
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
350
373
|
}
|
|
351
|
-
|
|
352
|
-
//#region src/utils.ts
|
|
374
|
+
// src/utils.ts
|
|
353
375
|
/**
|
|
354
376
|
* Returns the `@faker-js/faker` named export for a locale code.
|
|
355
377
|
*
|
|
@@ -400,6 +422,18 @@ const NON_INLINABLE_RESPONSE_TYPES = /* @__PURE__ */ new Set([
|
|
|
400
422
|
function canOverrideSchema(node) {
|
|
401
423
|
return !NON_OVERRIDABLE_TYPES.has(node.type);
|
|
402
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* Whether a schema can supply object properties when merging faker values.
|
|
427
|
+
*/
|
|
428
|
+
function isObjectSchema(node, seen = /* @__PURE__ */ new Set()) {
|
|
429
|
+
const resolved = kubb_kit.ast.resolveRef(node);
|
|
430
|
+
if (seen.has(resolved)) return false;
|
|
431
|
+
seen.add(resolved);
|
|
432
|
+
if (kubb_kit.ast.narrowSchema(resolved, kubb_kit.ast.schemaTypes.object) || kubb_kit.ast.narrowSchema(resolved, kubb_kit.ast.schemaTypes.ref)) return true;
|
|
433
|
+
const composition = kubb_kit.ast.narrowSchema(resolved, kubb_kit.ast.schemaTypes.intersection) ?? kubb_kit.ast.narrowSchema(resolved, kubb_kit.ast.schemaTypes.union);
|
|
434
|
+
if (!composition) return false;
|
|
435
|
+
return composition.type === kubb_kit.ast.schemaTypes.intersection && !composition.members?.length || (composition.members ?? []).some((member) => isObjectSchema(member, seen));
|
|
436
|
+
}
|
|
403
437
|
function shouldInlineSingleResponseSchema(schema) {
|
|
404
438
|
return !NON_INLINABLE_RESPONSE_TYPES.has(schema.type);
|
|
405
439
|
}
|
|
@@ -411,11 +445,16 @@ function buildResponseUnionSchema(node, resolver) {
|
|
|
411
445
|
const responses = node.responses.filter((response) => response.content?.[0]?.schema);
|
|
412
446
|
if (!responses.length) return null;
|
|
413
447
|
if (responses.length === 1) {
|
|
414
|
-
const
|
|
448
|
+
const schemas = responses[0].content?.flatMap((content) => content.schema ? [content.schema] : []) ?? [];
|
|
449
|
+
const schema = schemas.length > 1 ? kubb_kit.ast.factory.createSchema({
|
|
450
|
+
type: "union",
|
|
451
|
+
members: schemas
|
|
452
|
+
}) : schemas[0];
|
|
415
453
|
if (schema && shouldInlineSingleResponseSchema(schema)) return schema;
|
|
416
454
|
return kubb_kit.ast.factory.createSchema({
|
|
417
455
|
type: "ref",
|
|
418
|
-
name: resolver.response.status(node, responses[0].statusCode)
|
|
456
|
+
name: resolver.response.status(node, responses[0].statusCode),
|
|
457
|
+
schema: responses[0].content?.length === 1 ? schema : void 0
|
|
419
458
|
});
|
|
420
459
|
}
|
|
421
460
|
return kubb_kit.ast.factory.createSchema({
|
|
@@ -484,7 +523,8 @@ function getScalarType(node, typeName) {
|
|
|
484
523
|
* Determines the data type, return type, and whether it uses the type name.
|
|
485
524
|
*/
|
|
486
525
|
function resolveFakerTypeUsage(node, typeName, canOverride) {
|
|
487
|
-
const
|
|
526
|
+
const resolvedNode = kubb_kit.ast.resolveRef(node);
|
|
527
|
+
const scalarType = SCALAR_TYPES.has(resolvedNode.type) ? `${getScalarType(resolvedNode, typeName)}${node.nullable || resolvedNode.nullable ? " | null" : ""}` : void 0;
|
|
488
528
|
const dataType = scalarType ?? `Partial<${typeName}>`;
|
|
489
529
|
const returnType = scalarType ?? (canOverride ? typeName : null);
|
|
490
530
|
return {
|
|
@@ -493,28 +533,27 @@ function resolveFakerTypeUsage(node, typeName, canOverride) {
|
|
|
493
533
|
usesTypeName: canOverride && dataType.includes(typeName) || Boolean(returnType?.includes(typeName))
|
|
494
534
|
};
|
|
495
535
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
const OBJECT_TYPES = /* @__PURE__ */ new Set(["object", "intersection"]);
|
|
499
|
-
function Faker({ node, description, name, typeName, printer, canOverride, typeMode = "inferred" }) {
|
|
536
|
+
// src/components/Faker.tsx
|
|
537
|
+
function Faker({ node, description, name, typeName, printer, canOverride, typeMode = "inferred", keysToOmit }) {
|
|
500
538
|
const fakerText = printer.print(node) ?? "undefined";
|
|
501
539
|
const isArray = node.type === "array";
|
|
502
|
-
const isObject =
|
|
540
|
+
const isObject = !!kubb_kit.ast.narrowSchema(node, kubb_kit.ast.schemaTypes.object) || node.type === kubb_kit.ast.schemaTypes.intersection && isObjectSchema(node);
|
|
541
|
+
const isValueIntersection = node.type === kubb_kit.ast.schemaTypes.intersection && !isObject;
|
|
503
542
|
const isTuple = node.type === "tuple";
|
|
504
543
|
const isScalar = SCALAR_TYPES.has(node.type);
|
|
505
544
|
const isUnion = node.type === "union";
|
|
506
|
-
const useObjectOverride = canOverride && isObject;
|
|
545
|
+
const useObjectOverride = canOverride && (isObject || !!keysToOmit?.length);
|
|
507
546
|
const fakerTextWithOverride = (() => {
|
|
508
547
|
if (canOverride && node.type === "tuple") return `data && data.length === ${node.items?.length ?? 0} && !data.includes(undefined) ? data : ${fakerText}`;
|
|
509
548
|
if (canOverride && isArray) return `[\n ...${fakerText},\n ...(data || []).filter((item) => item !== undefined),\n]`;
|
|
510
|
-
if (canOverride && (isScalar || isUnion)) return node.nullable ? `data !== undefined ? data : ${fakerText}` : `data ?? ${fakerText}`;
|
|
549
|
+
if (canOverride && (isScalar || isUnion || isValueIntersection)) return node.nullable ? `data !== undefined ? data : ${fakerText}` : `data ?? ${fakerText}`;
|
|
511
550
|
return fakerText;
|
|
512
551
|
})();
|
|
513
552
|
const { dataType, returnType: resolvedReturnType } = resolveFakerTypeUsage(node, typeName, canOverride);
|
|
514
553
|
if (!useObjectOverride) {
|
|
515
554
|
const paramsSignature = `${/\bdata\b/.test(fakerTextWithOverride) ? "data" : "_data"}?: ${dataType}`;
|
|
516
555
|
const returnType = resolvedReturnType;
|
|
517
|
-
const returnExpression = canOverride && !!returnType && (node.type === "ref" || isArray || isTuple || isUnion) ? `${isTuple || isUnion ? `(${fakerTextWithOverride})` : fakerTextWithOverride} as ${returnType}` : fakerTextWithOverride;
|
|
556
|
+
const returnExpression = canOverride && !!returnType && (node.type === "ref" && !SCALAR_TYPES.has(kubb_kit.ast.resolveRef(node).type) || isArray || isTuple || isUnion) ? `${isTuple || isUnion ? `(${fakerTextWithOverride})` : fakerTextWithOverride} as ${returnType}` : fakerTextWithOverride;
|
|
518
557
|
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
519
558
|
name,
|
|
520
559
|
isExportable: true,
|
|
@@ -529,18 +568,21 @@ function Faker({ node, description, name, typeName, printer, canOverride, typeMo
|
|
|
529
568
|
})
|
|
530
569
|
});
|
|
531
570
|
}
|
|
532
|
-
const returnType = typeMode === "schema" ? typeName : "Omit<typeof defaultFakeData, keyof NoInfer<TData>> & NoInfer<TData>";
|
|
533
571
|
const { cyclicSchemas, schemaName } = printer.options;
|
|
534
|
-
const
|
|
572
|
+
const isCyclic = !!schemaName && !!cyclicSchemas?.has(schemaName);
|
|
573
|
+
const returnType = typeMode === "schema" ? typeName : `Omit<${isCyclic ? typeName : "typeof defaultFakeData"}, keyof NoInfer<TData>> & NoInfer<TData>`;
|
|
574
|
+
const hasGetters = node.type === "object" && !!cyclicSchemas && (node.properties ?? []).some((p) => (0, kubb_kit.containsCircularRef)(p.schema, {
|
|
535
575
|
circularSchemas: cyclicSchemas,
|
|
536
576
|
excludeName: schemaName
|
|
537
|
-
}))
|
|
577
|
+
}));
|
|
578
|
+
const declaration = `const ${keysToOmit?.length ? `{ ${keysToOmit.map((key, index) => `${objectKey(key)}: _omit${index}`).join(", ")}, ...defaultFakeData }` : "defaultFakeData"} = ${fakerText}`;
|
|
579
|
+
const functionBody = hasGetters ? `${declaration}
|
|
538
580
|
if (data) {
|
|
539
581
|
for (const [key, value] of Object.entries(data)) {
|
|
540
582
|
Object.defineProperty(defaultFakeData, key, { value, configurable: true, writable: true, enumerable: true })
|
|
541
583
|
}
|
|
542
584
|
}
|
|
543
|
-
return defaultFakeData as ${returnType}` :
|
|
585
|
+
return defaultFakeData as ${returnType}` : `${declaration}
|
|
544
586
|
return {
|
|
545
587
|
...defaultFakeData,
|
|
546
588
|
...(data || {}),
|
|
@@ -555,13 +597,12 @@ return {
|
|
|
555
597
|
JSDoc: { comments: description ? [`@description ${jsDocEscape(description)}`] : [] },
|
|
556
598
|
generics: typeMode === "schema" ? void 0 : `TData extends Partial<${typeName}> = object`,
|
|
557
599
|
params: typeMode === "schema" ? `data?: Partial<${typeName}>` : "data?: TData",
|
|
558
|
-
returnType: typeMode === "schema" ?
|
|
600
|
+
returnType: typeMode === "schema" || isCyclic ? returnType : void 0,
|
|
559
601
|
children: functionBody
|
|
560
602
|
})
|
|
561
603
|
});
|
|
562
604
|
}
|
|
563
|
-
|
|
564
|
-
//#region ../../internals/shared/src/operation.ts
|
|
605
|
+
// ../../internals/shared/src/operation.ts
|
|
565
606
|
/**
|
|
566
607
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
567
608
|
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
@@ -676,17 +717,24 @@ function getOperationParameters(node) {
|
|
|
676
717
|
operationParameterGroupsByNode.set(node, groups);
|
|
677
718
|
return groups;
|
|
678
719
|
}
|
|
679
|
-
|
|
680
|
-
//#region ../../internals/shared/src/resolver.ts
|
|
720
|
+
// ../../internals/shared/src/resolver.ts
|
|
681
721
|
/**
|
|
682
|
-
*
|
|
683
|
-
*
|
|
722
|
+
* Builds the allocation key for a declaration an operation owns. `scope` names which declaration
|
|
723
|
+
* it is, so two of them never share an allocation even when a custom resolver gives both the same
|
|
724
|
+
* name. Leave `scope` empty for the operation's own declaration.
|
|
684
725
|
*
|
|
685
726
|
* @example
|
|
686
|
-
*
|
|
727
|
+
* ```ts
|
|
728
|
+
* resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
|
|
729
|
+
* ```
|
|
687
730
|
*/
|
|
688
|
-
function
|
|
689
|
-
return
|
|
731
|
+
function operationKey(node, ...scope) {
|
|
732
|
+
return [
|
|
733
|
+
"operation",
|
|
734
|
+
node.method,
|
|
735
|
+
node.path,
|
|
736
|
+
...scope.map(String)
|
|
737
|
+
];
|
|
690
738
|
}
|
|
691
739
|
/**
|
|
692
740
|
* Builds the shared `param` namespace. Spread the result into `createResolver`
|
|
@@ -699,15 +747,33 @@ function operationParamName(node, param) {
|
|
|
699
747
|
*/
|
|
700
748
|
function createOperationParamResolver() {
|
|
701
749
|
return {
|
|
702
|
-
name
|
|
750
|
+
name(node, param) {
|
|
751
|
+
const name = this.name(`${node.operationId} ${param.in} ${param.name}`);
|
|
752
|
+
return this.resolveName({
|
|
753
|
+
key: operationKey(node, "param", "name", param.in, param.name),
|
|
754
|
+
name
|
|
755
|
+
});
|
|
756
|
+
},
|
|
703
757
|
path(node) {
|
|
704
|
-
|
|
758
|
+
const name = this.name(`${node.operationId} Path`);
|
|
759
|
+
return this.resolveName({
|
|
760
|
+
key: operationKey(node, "param", "path"),
|
|
761
|
+
name
|
|
762
|
+
});
|
|
705
763
|
},
|
|
706
764
|
query(node) {
|
|
707
|
-
|
|
765
|
+
const name = this.name(`${node.operationId} Query`);
|
|
766
|
+
return this.resolveName({
|
|
767
|
+
key: operationKey(node, "param", "query"),
|
|
768
|
+
name
|
|
769
|
+
});
|
|
708
770
|
},
|
|
709
771
|
headers(node) {
|
|
710
|
-
|
|
772
|
+
const name = this.name(`${node.operationId} Headers`);
|
|
773
|
+
return this.resolveName({
|
|
774
|
+
key: operationKey(node, "param", "headers"),
|
|
775
|
+
name
|
|
776
|
+
});
|
|
711
777
|
}
|
|
712
778
|
};
|
|
713
779
|
}
|
|
@@ -724,16 +790,32 @@ function createOperationParamResolver() {
|
|
|
724
790
|
function createOperationResponseResolver() {
|
|
725
791
|
return {
|
|
726
792
|
status(node, statusCode) {
|
|
727
|
-
|
|
793
|
+
const name = this.name(`${node.operationId} Status ${statusCode}`);
|
|
794
|
+
return this.resolveName({
|
|
795
|
+
key: operationKey(node, "response", "status", statusCode),
|
|
796
|
+
name
|
|
797
|
+
});
|
|
728
798
|
},
|
|
729
799
|
body(node) {
|
|
730
|
-
|
|
800
|
+
const name = this.name(`${node.operationId} Body`);
|
|
801
|
+
return this.resolveName({
|
|
802
|
+
key: operationKey(node, "response", "body"),
|
|
803
|
+
name
|
|
804
|
+
});
|
|
731
805
|
},
|
|
732
806
|
responses(node) {
|
|
733
|
-
|
|
807
|
+
const name = this.name(`${node.operationId} Responses`);
|
|
808
|
+
return this.resolveName({
|
|
809
|
+
key: operationKey(node, "response", "responses"),
|
|
810
|
+
name
|
|
811
|
+
});
|
|
734
812
|
},
|
|
735
813
|
response(node) {
|
|
736
|
-
|
|
814
|
+
const name = this.name(`${node.operationId} Response`);
|
|
815
|
+
return this.resolveName({
|
|
816
|
+
key: operationKey(node, "response", "response"),
|
|
817
|
+
name
|
|
818
|
+
});
|
|
737
819
|
}
|
|
738
820
|
};
|
|
739
821
|
}
|
|
@@ -751,8 +833,7 @@ function createCasedFile(caseLast) {
|
|
|
751
833
|
return `${toFilePath(name, caseLast)}${extname}`;
|
|
752
834
|
} };
|
|
753
835
|
}
|
|
754
|
-
|
|
755
|
-
//#region ../../internals/shared/src/group.ts
|
|
836
|
+
// ../../internals/shared/src/group.ts
|
|
756
837
|
/**
|
|
757
838
|
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
758
839
|
* shared default naming so every plugin groups output consistently:
|
|
@@ -782,8 +863,7 @@ function createGroupConfig(group) {
|
|
|
782
863
|
name: group.name ? group.name : defaultName
|
|
783
864
|
};
|
|
784
865
|
}
|
|
785
|
-
|
|
786
|
-
//#region ../../internals/shared/src/schemaTraversal.ts
|
|
866
|
+
// ../../internals/shared/src/schemaTraversal.ts
|
|
787
867
|
/**
|
|
788
868
|
* Maps each member of a union or intersection schema to its transformed output, pairing every
|
|
789
869
|
* result with the original member.
|
|
@@ -804,8 +884,7 @@ function mapSchemaItems(node, transform) {
|
|
|
804
884
|
output: transform(schema)
|
|
805
885
|
}));
|
|
806
886
|
}
|
|
807
|
-
|
|
808
|
-
//#region src/utils/imports.ts
|
|
887
|
+
// src/utils/imports.ts
|
|
809
888
|
function escapeRegExp(value) {
|
|
810
889
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
811
890
|
}
|
|
@@ -853,8 +932,7 @@ function aliasConflictingImports(imports, reservedNames) {
|
|
|
853
932
|
function rewriteAliasedImports(text, aliases) {
|
|
854
933
|
return Array.from(aliases).reduce((acc, [name, alias]) => acc.replace(new RegExp(`\\b${escapeRegExp(name)}\\b`, "g"), alias), text);
|
|
855
934
|
}
|
|
856
|
-
|
|
857
|
-
//#region src/printers/printerFaker.ts
|
|
935
|
+
// src/printers/printerFaker.ts
|
|
858
936
|
/**
|
|
859
937
|
* Formats that put a whole number inside a `type: 'string'` schema, the way ProtoJSON encodes
|
|
860
938
|
* 64-bit integers, so the mock has to be digits rather than letters.
|
|
@@ -990,7 +1068,9 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
990
1068
|
},
|
|
991
1069
|
enum(node) {
|
|
992
1070
|
const { typeName } = this.options;
|
|
993
|
-
|
|
1071
|
+
const values = kubb_kit.ast.getSchemaLiteralValues(node);
|
|
1072
|
+
if (values.length === 1 && values[0] !== null && typeof values[0] === "object") return JSON.stringify(values[0]);
|
|
1073
|
+
return `faker.helpers.arrayElement${typeName ? `<${typeName}>` : ""}([${values.map(parseEnumValue).join(", ")}])`;
|
|
994
1074
|
},
|
|
995
1075
|
union(node) {
|
|
996
1076
|
const { discriminatorPropertyName } = node;
|
|
@@ -1014,9 +1094,14 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
1014
1094
|
}).map(({ output }) => output).filter((item) => Boolean(item)).join(", ")}])`;
|
|
1015
1095
|
},
|
|
1016
1096
|
intersection(node) {
|
|
1017
|
-
const
|
|
1097
|
+
const objectMembers = (node.members ?? []).filter((member) => isObjectSchema(member));
|
|
1098
|
+
const members = objectMembers.length ? objectMembers : node.members;
|
|
1099
|
+
const items = mapSchemaMembers({
|
|
1100
|
+
...node,
|
|
1101
|
+
members
|
|
1102
|
+
}, (member) => printNested(member, { nestedInObject: true })).map(({ output }) => output).filter((item) => Boolean(item) && item !== "undefined");
|
|
1018
1103
|
if (items.length === 0) return "{}";
|
|
1019
|
-
return items.length === 1 ? items
|
|
1104
|
+
return !objectMembers.length || items.length === 1 ? items.at(-1) : `{...${items.join(", ...")}}`;
|
|
1020
1105
|
},
|
|
1021
1106
|
array(node) {
|
|
1022
1107
|
const items = mapSchemaItems(node, (member) => printNested(member, {
|
|
@@ -1039,7 +1124,13 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
1039
1124
|
},
|
|
1040
1125
|
object(node) {
|
|
1041
1126
|
const cyclicSchemas = this.options.cyclicSchemas;
|
|
1042
|
-
|
|
1127
|
+
const keySchema = node.propertyNames && (0, kubb_kit.syncSchemaRef)(node.propertyNames);
|
|
1128
|
+
const valueSchema = node.additionalProperties;
|
|
1129
|
+
const recordProperties = keySchema?.type === "enum" && typeof valueSchema === "object" ? kubb_kit.ast.getSchemaLiteralValues(keySchema).filter((key) => typeof key === "string" && !node.properties.some((property) => property.name === key)).map((name) => kubb_kit.ast.factory.createProperty({
|
|
1130
|
+
name,
|
|
1131
|
+
schema: valueSchema
|
|
1132
|
+
})) : [];
|
|
1133
|
+
return buildObject([...node.properties, ...recordProperties].map((property) => {
|
|
1043
1134
|
const value = printNested(property.schema, {
|
|
1044
1135
|
typeName: this.options.typeName ? indexedTypeName(this.options.typeName, property.name, this.options.nestedInUnion) : void 0,
|
|
1045
1136
|
nestedInObject: true
|
|
@@ -1055,8 +1146,7 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
|
|
|
1055
1146
|
overrides: options.nodes
|
|
1056
1147
|
};
|
|
1057
1148
|
});
|
|
1058
|
-
|
|
1059
|
-
//#region src/generators/fakerGenerator.tsx
|
|
1149
|
+
// src/generators/fakerGenerator.tsx
|
|
1060
1150
|
/**
|
|
1061
1151
|
* Built-in generator for `@kubb/plugin-faker`. Emits one `createX` factory
|
|
1062
1152
|
* per schema in the spec plus per-operation request/response factories. Each
|
|
@@ -1228,7 +1318,7 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1228
1318
|
name: baseName,
|
|
1229
1319
|
typeName: tsBaseName,
|
|
1230
1320
|
description,
|
|
1231
|
-
|
|
1321
|
+
keysToOmit: primary.keysToOmit
|
|
1232
1322
|
}];
|
|
1233
1323
|
}
|
|
1234
1324
|
const variants = resolveContentTypeVariants(entries, baseName);
|
|
@@ -1245,7 +1335,7 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1245
1335
|
name: variant.name,
|
|
1246
1336
|
typeName: getPerContentTypeName(tsBaseName, variant.suffix),
|
|
1247
1337
|
description,
|
|
1248
|
-
|
|
1338
|
+
keysToOmit: variant.keysToOmit
|
|
1249
1339
|
})), {
|
|
1250
1340
|
schema: unionSchema,
|
|
1251
1341
|
name: baseName,
|
|
@@ -1295,8 +1385,15 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1295
1385
|
group: group ?? void 0
|
|
1296
1386
|
}).filter((entry) => entry.path !== meta.file.path);
|
|
1297
1387
|
}
|
|
1298
|
-
function renderEntry({ schema, name, typeName, description, skipImportNames = [] }) {
|
|
1388
|
+
function renderEntry({ schema, name, typeName, description, keysToOmit, skipImportNames = [] }) {
|
|
1299
1389
|
if (!schema) return null;
|
|
1390
|
+
if (schema.type === kubb_kit.ast.schemaTypes.object && keysToOmit?.length) {
|
|
1391
|
+
schema = {
|
|
1392
|
+
...schema,
|
|
1393
|
+
properties: schema.properties.filter((property) => !keysToOmit?.includes(property.name))
|
|
1394
|
+
};
|
|
1395
|
+
keysToOmit = void 0;
|
|
1396
|
+
}
|
|
1300
1397
|
const canOverride = canOverrideSchema(schema);
|
|
1301
1398
|
const printerInstance = printerFaker({
|
|
1302
1399
|
resolver,
|
|
@@ -1305,7 +1402,8 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1305
1402
|
dateParser,
|
|
1306
1403
|
regexGenerator,
|
|
1307
1404
|
nodes: printer?.nodes,
|
|
1308
|
-
cyclicSchemas
|
|
1405
|
+
cyclicSchemas,
|
|
1406
|
+
nestedInObject: !!keysToOmit?.length
|
|
1309
1407
|
});
|
|
1310
1408
|
const fakerText = printerInstance.print(schema) ?? "undefined";
|
|
1311
1409
|
const { imports, aliases } = aliasConflictingImports(filterUsedImports(resolveMockImports(schema), fakerText, skipImportNames), localHelperNames);
|
|
@@ -1344,7 +1442,8 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1344
1442
|
print: () => rewrittenFakerText
|
|
1345
1443
|
},
|
|
1346
1444
|
canOverride,
|
|
1347
|
-
typeMode
|
|
1445
|
+
typeMode,
|
|
1446
|
+
keysToOmit
|
|
1348
1447
|
})
|
|
1349
1448
|
] });
|
|
1350
1449
|
}
|
|
@@ -1401,17 +1500,14 @@ const fakerGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1401
1500
|
});
|
|
1402
1501
|
}
|
|
1403
1502
|
});
|
|
1404
|
-
|
|
1405
|
-
//#region kubb-extension.json
|
|
1503
|
+
// kubb-extension.json
|
|
1406
1504
|
var compatibility = {
|
|
1407
|
-
"kubb": ">=5.
|
|
1505
|
+
"kubb": ">=5.14.6",
|
|
1408
1506
|
"node": ">=22.22"
|
|
1409
1507
|
};
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
//#endregion
|
|
1414
|
-
//#region src/resolvers/resolverFaker.ts
|
|
1508
|
+
// package.json
|
|
1509
|
+
var version = "5.2.2";
|
|
1510
|
+
// src/resolvers/resolverFaker.ts
|
|
1415
1511
|
/**
|
|
1416
1512
|
* Default resolver used by `@kubb/plugin-faker`. Decides the names and file
|
|
1417
1513
|
* paths for every generated mock factory. Functions and files are prefixed
|
|
@@ -1433,8 +1529,7 @@ const resolverFaker = (0, kubb_kit.createResolver)({
|
|
|
1433
1529
|
param: createOperationParamResolver(),
|
|
1434
1530
|
response: createOperationResponseResolver()
|
|
1435
1531
|
});
|
|
1436
|
-
|
|
1437
|
-
//#region src/plugin.ts
|
|
1532
|
+
// src/plugin.ts
|
|
1438
1533
|
/**
|
|
1439
1534
|
* Canonical plugin name for `@kubb/plugin-faker`. Used for driver lookups and
|
|
1440
1535
|
* cross-plugin dependency references.
|
|
@@ -1495,7 +1590,6 @@ const pluginFaker = (0, kubb_kit.defineExtension)({
|
|
|
1495
1590
|
};
|
|
1496
1591
|
}
|
|
1497
1592
|
});
|
|
1498
|
-
//#endregion
|
|
1499
1593
|
exports.Faker = Faker;
|
|
1500
1594
|
exports.default = pluginFaker;
|
|
1501
1595
|
exports.fakerGenerator = fakerGenerator;
|