@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 CHANGED
@@ -532,15 +532,23 @@ function getScalarType(node, typeName) {
532
532
  }
533
533
  }
534
534
  /**
535
+ * A nullable scalar inlines its primitive instead of naming the generated type, so the
536
+ * nullability the type plugin emitted has to be reapplied here. Non-scalars keep `typeName`,
537
+ * which already carries it.
538
+ */
539
+ function withNullability(type, node) {
540
+ return node.nullable ? `${type} | null` : type;
541
+ }
542
+ /**
535
543
  * Resolves faker type usage information for a schema.
536
544
  * Determines the data type, return type, and whether it uses the type name.
537
545
  */
538
546
  function resolveFakerTypeUsage(node, typeName, canOverride) {
539
547
  const isScalar = SCALAR_TYPES$1.has(node.type);
540
548
  let dataType = `Partial<${typeName}>`;
541
- if (isScalar) dataType = getScalarType(node, typeName);
549
+ if (isScalar) dataType = withNullability(getScalarType(node, typeName), node);
542
550
  let returnType = canOverride ? typeName : null;
543
- if (isScalar) returnType = getScalarType(node, typeName);
551
+ if (isScalar) returnType = withNullability(getScalarType(node, typeName), node);
544
552
  return {
545
553
  dataType,
546
554
  returnType,
@@ -577,7 +585,7 @@ function Faker({ node, description, name, typeName, printer, canOverride }) {
577
585
  const fakerTextWithOverride = (() => {
578
586
  if (canOverride && node.type === "tuple") return `data && data.length === ${node.items?.length ?? 0} && !data.includes(undefined) ? data : ${fakerText}`;
579
587
  if (canOverride && isArray) return `[\n ...${fakerText},\n ...(data || []).filter((item) => item !== undefined),\n]`;
580
- if (canOverride && (isScalar || isUnion)) return `data ?? ${fakerText}`;
588
+ if (canOverride && (isScalar || isUnion)) return node.nullable ? `data !== undefined ? data : ${fakerText}` : `data ?? ${fakerText}`;
581
589
  return fakerText;
582
590
  })();
583
591
  const { dataType, returnType: resolvedReturnType } = resolveFakerTypeUsage(node, typeName, canOverride);
@@ -617,13 +625,13 @@ function Faker({ node, description, name, typeName, printer, canOverride }) {
617
625
  Object.defineProperty(defaultFakeData, key, { value, configurable: true, writable: true, enumerable: true })
618
626
  }
619
627
  }
620
- return defaultFakeData as Omit<typeof defaultFakeData, keyof TData> & TData
628
+ return defaultFakeData as Omit<typeof defaultFakeData, keyof NoInfer<TData>> & NoInfer<TData>
621
629
  }` : `{
622
630
  const defaultFakeData = ${fakerText}
623
631
  return {
624
632
  ...defaultFakeData,
625
633
  ...(data || {}),
626
- } as Omit<typeof defaultFakeData, keyof TData> & TData
634
+ } as Omit<typeof defaultFakeData, keyof NoInfer<TData>> & NoInfer<TData>
627
635
  }`;
628
636
  return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File.Source, {
629
637
  name,
@@ -1064,7 +1072,7 @@ const printerFaker = kubb_kit.ast.createPrinter((options) => {
1064
1072
  if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
1065
1073
  const resolvedName = node.ref ? this.options.resolver.name(refName) : refName;
1066
1074
  if (!this.options.nestedInObject) return `${resolvedName}(data)`;
1067
- return `${resolvedName}${this.options.nestedInUnion && (node.schema?.type === "object" || node.schema?.type === "intersection") ? "<object>" : ""}()`;
1075
+ return `${resolvedName}()`;
1068
1076
  },
1069
1077
  enum(node) {
1070
1078
  return fakerKeywordMapper.enum(kubb_kit.ast.getSchemaLiteralValues(node).map(parseEnumValue), this.options.typeName);