@kubb/plugin-faker 5.0.9 → 5.0.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.d.ts CHANGED
@@ -170,7 +170,8 @@ declare global {
170
170
  * Partial map of node-type overrides for the Faker printer. Each key is a
171
171
  * `SchemaType` (`'string'`, `'date'`, ...) and each handler returns the
172
172
  * Faker expression for that schema as a string. Use `this.transform` to
173
- * recurse into nested schema nodes and `this.options` to read printer options.
173
+ * recurse into nested schema nodes, `this.base` to reuse the built-in handler,
174
+ * and `this.options` to read printer options.
174
175
  *
175
176
  * @example Override the integer handler
176
177
  * ```ts
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);
@@ -1117,9 +1125,9 @@ const printerFaker = ast.createPrinter((options) => {
1117
1125
  })) 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 }`;
1118
1126
  return `${objectKey(property.name)}: ${value}`;
1119
1127
  }));
1120
- },
1121
- ...options.nodes
1128
+ }
1122
1129
  },
1130
+ overrides: options.nodes,
1123
1131
  print(node) {
1124
1132
  return this.transform(node) ?? null;
1125
1133
  }