@kubb/plugin-faker 5.0.0 → 5.0.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 +15 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +15 -10
- package/dist/index.js.map +1 -1
- package/package.json +15 -4
package/dist/index.d.ts
CHANGED
|
@@ -202,6 +202,10 @@ type PrinterFakerOptions = {
|
|
|
202
202
|
* Set while printing the members of a union (`oneOf`). Object properties then index their
|
|
203
203
|
* type as `(NonNullable<T> & Record<K, unknown>)[K]` instead of `NonNullable<T>[K]`, so a key
|
|
204
204
|
* carried by only some branches stays valid (a plain index would be a TS2339).
|
|
205
|
+
*
|
|
206
|
+
* Referenced object factories also receive their explicit default type argument (`object`).
|
|
207
|
+
* This prevents a surrounding generic helper from inferring `TData` as `Partial<T>` when the
|
|
208
|
+
* factory is called without override data, which would make required properties optional.
|
|
205
209
|
*/
|
|
206
210
|
nestedInUnion?: boolean;
|
|
207
211
|
nodes?: PrinterFakerNodes;
|
|
@@ -229,11 +233,10 @@ type Props = {
|
|
|
229
233
|
typeName: string;
|
|
230
234
|
node: ast.SchemaNode;
|
|
231
235
|
printer: ast.Printer<PrinterFakerFactory>;
|
|
232
|
-
seed?: PluginFaker['options']['seed'];
|
|
233
236
|
description?: string;
|
|
234
237
|
canOverride: boolean;
|
|
235
238
|
};
|
|
236
|
-
declare function Faker({ node, description, name, typeName, printer,
|
|
239
|
+
declare function Faker({ node, description, name, typeName, printer, canOverride }: Props): KubbReactNode;
|
|
237
240
|
//#endregion
|
|
238
241
|
//#region src/generators/fakerGenerator.d.ts
|
|
239
242
|
/**
|
package/dist/index.js
CHANGED
|
@@ -344,7 +344,7 @@ function filterUsedImports(imports, text, skipImportNames = []) {
|
|
|
344
344
|
return imports.filter((entry) => {
|
|
345
345
|
return getImportNames(entry).some((name) => {
|
|
346
346
|
if (skip.has(name)) return false;
|
|
347
|
-
return new RegExp(`\\b${escapeRegExp(name)}\\b(?=\\s
|
|
347
|
+
return new RegExp(`\\b${escapeRegExp(name)}\\b(?=\\s*[<(])`).test(text);
|
|
348
348
|
});
|
|
349
349
|
});
|
|
350
350
|
}
|
|
@@ -561,7 +561,7 @@ const SCALAR_TYPES = /* @__PURE__ */ new Set([
|
|
|
561
561
|
"enum"
|
|
562
562
|
]);
|
|
563
563
|
const declarationPrinter = functionPrinter({ mode: "declaration" });
|
|
564
|
-
function Faker({ node, description, name, typeName, printer,
|
|
564
|
+
function Faker({ node, description, name, typeName, printer, canOverride }) {
|
|
565
565
|
const fakerText = printer.print(node) ?? "undefined";
|
|
566
566
|
const isArray = node.type === "array";
|
|
567
567
|
const isObject = OBJECT_TYPES.has(node.type);
|
|
@@ -589,24 +589,23 @@ function Faker({ node, description, name, typeName, printer, seed, canOverride }
|
|
|
589
589
|
name,
|
|
590
590
|
isExportable: true,
|
|
591
591
|
isIndexable: true,
|
|
592
|
-
children: /* @__PURE__ */
|
|
592
|
+
children: /* @__PURE__ */ jsx(Function, {
|
|
593
593
|
export: true,
|
|
594
594
|
name,
|
|
595
595
|
JSDoc: { comments: description ? [`@description ${jsStringEscape(description)}`] : [] },
|
|
596
596
|
params: canOverride ? paramsSignature : void 0,
|
|
597
597
|
returnType: returnType ?? void 0,
|
|
598
|
-
children:
|
|
598
|
+
children: `return ${returnExpression}`
|
|
599
599
|
})
|
|
600
600
|
});
|
|
601
601
|
}
|
|
602
602
|
const functionSignature = `${description ? `/**\n * @description ${jsStringEscape(description)}\n */\n ` : ""}export function ${name}<TData extends Partial<${typeName}> = object>(data?: TData)`;
|
|
603
|
-
const seedCode = seed ? `faker.seed(${JSON.stringify(seed)})\n ` : "";
|
|
604
603
|
const { cyclicSchemas, schemaName } = printer.options;
|
|
605
604
|
const functionBody = node.type === "object" && !!cyclicSchemas && (node.properties ?? []).some((p) => containsCircularRef(p.schema, {
|
|
606
605
|
circularSchemas: cyclicSchemas,
|
|
607
606
|
excludeName: schemaName
|
|
608
607
|
})) ? `{
|
|
609
|
-
|
|
608
|
+
const defaultFakeData = ${fakerText}
|
|
610
609
|
if (data) {
|
|
611
610
|
for (const [key, value] of Object.entries(data)) {
|
|
612
611
|
Object.defineProperty(defaultFakeData, key, { value, configurable: true, writable: true, enumerable: true })
|
|
@@ -614,7 +613,7 @@ function Faker({ node, description, name, typeName, printer, seed, canOverride }
|
|
|
614
613
|
}
|
|
615
614
|
return defaultFakeData as Omit<typeof defaultFakeData, keyof TData> & TData
|
|
616
615
|
}` : `{
|
|
617
|
-
|
|
616
|
+
const defaultFakeData = ${fakerText}
|
|
618
617
|
return {
|
|
619
618
|
...defaultFakeData,
|
|
620
619
|
...(data || {}),
|
|
@@ -1042,7 +1041,7 @@ const printerFaker = ast.createPrinter((options) => {
|
|
|
1042
1041
|
if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
|
|
1043
1042
|
const resolvedName = node.ref ? this.options.resolver.name(refName) : refName;
|
|
1044
1043
|
if (!this.options.nestedInObject) return `${resolvedName}(data)`;
|
|
1045
|
-
return `${resolvedName}()`;
|
|
1044
|
+
return `${resolvedName}${this.options.nestedInUnion && (node.schema?.type === "object" || node.schema?.type === "intersection") ? "<object>" : ""}()`;
|
|
1046
1045
|
},
|
|
1047
1046
|
enum(node) {
|
|
1048
1047
|
return fakerKeywordMapper.enum(getEnumValues(node).map(parseEnumValue), this.options.typeName);
|
|
@@ -1223,13 +1222,16 @@ const fakerGenerator = defineGenerator({
|
|
|
1223
1222
|
imp.path,
|
|
1224
1223
|
imp.name
|
|
1225
1224
|
].join("-"))),
|
|
1225
|
+
seed ? /* @__PURE__ */ jsx(File.Source, {
|
|
1226
|
+
name: "faker-seed",
|
|
1227
|
+
children: `faker.seed(${JSON.stringify(seed)})`
|
|
1228
|
+
}) : void 0,
|
|
1226
1229
|
/* @__PURE__ */ jsx(Faker, {
|
|
1227
1230
|
name: meta.name,
|
|
1228
1231
|
typeName: typeReference.typeName,
|
|
1229
1232
|
description: node.description,
|
|
1230
1233
|
node,
|
|
1231
1234
|
printer: printerInstance,
|
|
1232
|
-
seed,
|
|
1233
1235
|
canOverride
|
|
1234
1236
|
})
|
|
1235
1237
|
]
|
|
@@ -1386,7 +1388,6 @@ const fakerGenerator = defineGenerator({
|
|
|
1386
1388
|
...printerInstance,
|
|
1387
1389
|
print: () => rewrittenFakerText
|
|
1388
1390
|
},
|
|
1389
|
-
seed,
|
|
1390
1391
|
canOverride
|
|
1391
1392
|
})
|
|
1392
1393
|
] });
|
|
@@ -1427,6 +1428,10 @@ const fakerGenerator = defineGenerator({
|
|
|
1427
1428
|
path: dateParser,
|
|
1428
1429
|
name: dateParser
|
|
1429
1430
|
}),
|
|
1431
|
+
seed ? /* @__PURE__ */ jsx(File.Source, {
|
|
1432
|
+
name: "faker-seed",
|
|
1433
|
+
children: `faker.seed(${JSON.stringify(seed)})`
|
|
1434
|
+
}) : void 0,
|
|
1430
1435
|
paramGroups.map((group) => renderEntry(group)),
|
|
1431
1436
|
responseUnits.map((unit) => renderEntry({
|
|
1432
1437
|
schema: unit.schema,
|