@kubb/plugin-faker 5.0.10 → 5.0.12
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 +14 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.js +14 -6
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -203,10 +203,6 @@ type PrinterFakerOptions = {
|
|
|
203
203
|
* Set while printing the members of a union (`oneOf`). Object properties then index their
|
|
204
204
|
* type as `(NonNullable<T> & Record<K, unknown>)[K]` instead of `NonNullable<T>[K]`, so a key
|
|
205
205
|
* carried by only some branches stays valid (a plain index would be a TS2339).
|
|
206
|
-
*
|
|
207
|
-
* Referenced object factories also receive their explicit default type argument (`object`).
|
|
208
|
-
* This prevents a surrounding generic helper from inferring `TData` as `Partial<T>` when the
|
|
209
|
-
* factory is called without override data, which would make required properties optional.
|
|
210
206
|
*/
|
|
211
207
|
nestedInUnion?: boolean;
|
|
212
208
|
nodes?: PrinterFakerNodes;
|
package/dist/index.js
CHANGED
|
@@ -528,15 +528,23 @@ function getScalarType(node, typeName) {
|
|
|
528
528
|
}
|
|
529
529
|
}
|
|
530
530
|
/**
|
|
531
|
+
* A nullable scalar inlines its primitive instead of naming the generated type, so the
|
|
532
|
+
* nullability the type plugin emitted has to be reapplied here. Non-scalars keep `typeName`,
|
|
533
|
+
* which already carries it.
|
|
534
|
+
*/
|
|
535
|
+
function withNullability(type, node) {
|
|
536
|
+
return node.nullable ? `${type} | null` : type;
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
531
539
|
* Resolves faker type usage information for a schema.
|
|
532
540
|
* Determines the data type, return type, and whether it uses the type name.
|
|
533
541
|
*/
|
|
534
542
|
function resolveFakerTypeUsage(node, typeName, canOverride) {
|
|
535
543
|
const isScalar = SCALAR_TYPES$1.has(node.type);
|
|
536
544
|
let dataType = `Partial<${typeName}>`;
|
|
537
|
-
if (isScalar) dataType = getScalarType(node, typeName);
|
|
545
|
+
if (isScalar) dataType = withNullability(getScalarType(node, typeName), node);
|
|
538
546
|
let returnType = canOverride ? typeName : null;
|
|
539
|
-
if (isScalar) returnType = getScalarType(node, typeName);
|
|
547
|
+
if (isScalar) returnType = withNullability(getScalarType(node, typeName), node);
|
|
540
548
|
return {
|
|
541
549
|
dataType,
|
|
542
550
|
returnType,
|
|
@@ -573,7 +581,7 @@ function Faker({ node, description, name, typeName, printer, canOverride }) {
|
|
|
573
581
|
const fakerTextWithOverride = (() => {
|
|
574
582
|
if (canOverride && node.type === "tuple") return `data && data.length === ${node.items?.length ?? 0} && !data.includes(undefined) ? data : ${fakerText}`;
|
|
575
583
|
if (canOverride && isArray) return `[\n ...${fakerText},\n ...(data || []).filter((item) => item !== undefined),\n]`;
|
|
576
|
-
if (canOverride && (isScalar || isUnion)) return `data ?? ${fakerText}`;
|
|
584
|
+
if (canOverride && (isScalar || isUnion)) return node.nullable ? `data !== undefined ? data : ${fakerText}` : `data ?? ${fakerText}`;
|
|
577
585
|
return fakerText;
|
|
578
586
|
})();
|
|
579
587
|
const { dataType, returnType: resolvedReturnType } = resolveFakerTypeUsage(node, typeName, canOverride);
|
|
@@ -613,13 +621,13 @@ function Faker({ node, description, name, typeName, printer, canOverride }) {
|
|
|
613
621
|
Object.defineProperty(defaultFakeData, key, { value, configurable: true, writable: true, enumerable: true })
|
|
614
622
|
}
|
|
615
623
|
}
|
|
616
|
-
return defaultFakeData as Omit<typeof defaultFakeData, keyof TData
|
|
624
|
+
return defaultFakeData as Omit<typeof defaultFakeData, keyof NoInfer<TData>> & NoInfer<TData>
|
|
617
625
|
}` : `{
|
|
618
626
|
const defaultFakeData = ${fakerText}
|
|
619
627
|
return {
|
|
620
628
|
...defaultFakeData,
|
|
621
629
|
...(data || {}),
|
|
622
|
-
} as Omit<typeof defaultFakeData, keyof TData
|
|
630
|
+
} as Omit<typeof defaultFakeData, keyof NoInfer<TData>> & NoInfer<TData>
|
|
623
631
|
}`;
|
|
624
632
|
return /* @__PURE__ */ jsxs(File.Source, {
|
|
625
633
|
name,
|
|
@@ -1060,7 +1068,7 @@ const printerFaker = ast.createPrinter((options) => {
|
|
|
1060
1068
|
if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
|
|
1061
1069
|
const resolvedName = node.ref ? this.options.resolver.name(refName) : refName;
|
|
1062
1070
|
if (!this.options.nestedInObject) return `${resolvedName}(data)`;
|
|
1063
|
-
return `${resolvedName}
|
|
1071
|
+
return `${resolvedName}()`;
|
|
1064
1072
|
},
|
|
1065
1073
|
enum(node) {
|
|
1066
1074
|
return fakerKeywordMapper.enum(ast.getSchemaLiteralValues(node).map(parseEnumValue), this.options.typeName);
|