@kubb/plugin-faker 5.0.0-beta.80 → 5.0.0-beta.84
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 +57 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -14
- package/dist/index.js +25 -27
- package/dist/index.js.map +1 -1
- package/package.json +4 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "
|
|
3
|
-
import { KubbReactNode } from "
|
|
2
|
+
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "kubb/kit";
|
|
3
|
+
import { KubbReactNode } from "kubb/jsx";
|
|
4
|
+
|
|
4
5
|
//#region ../../internals/shared/src/operation.d.ts
|
|
5
6
|
/**
|
|
6
7
|
* Resolver interface for building operation parameters.
|
|
@@ -151,11 +152,6 @@ type Options = OutputOptions & {
|
|
|
151
152
|
* @default 'faker'
|
|
152
153
|
*/
|
|
153
154
|
regexGenerator?: 'faker' | 'randexp';
|
|
154
|
-
/**
|
|
155
|
-
* Map a schema name to a custom Faker expression. Use this when the schema name
|
|
156
|
-
* does not give Faker enough context (`'email'`, `'avatarUrl'`, `'phoneNumber'`).
|
|
157
|
-
*/
|
|
158
|
-
mapper?: Record<string, string>;
|
|
159
155
|
/**
|
|
160
156
|
* Faker locale code. Switches the named import to `fakerXX` from `@faker-js/faker`
|
|
161
157
|
* so names, addresses, and phone numbers reflect the target region.
|
|
@@ -198,7 +194,6 @@ type ResolvedOptions = {
|
|
|
198
194
|
override: NonNullable<Options['override']>;
|
|
199
195
|
dateParser: NonNullable<Options['dateParser']>;
|
|
200
196
|
regexGenerator: NonNullable<Options['regexGenerator']>;
|
|
201
|
-
mapper: NonNullable<Options['mapper']>;
|
|
202
197
|
seed: NonNullable<Options['seed']> | undefined;
|
|
203
198
|
locale: Options['locale'];
|
|
204
199
|
printer: Options['printer'];
|
|
@@ -235,13 +230,12 @@ declare global {
|
|
|
235
230
|
type PrinterFakerNodes = ast.PrinterPartial<string, PrinterFakerOptions>;
|
|
236
231
|
/**
|
|
237
232
|
* Options passed to the Faker printer at instantiation: the parser library
|
|
238
|
-
* for date strings, the regex generator, the
|
|
239
|
-
*
|
|
233
|
+
* for date strings, the regex generator, and the resolver used to compute
|
|
234
|
+
* identifiers.
|
|
240
235
|
*/
|
|
241
236
|
type PrinterFakerOptions = {
|
|
242
237
|
dateParser?: PluginFaker['resolvedOptions']['dateParser'];
|
|
243
238
|
regexGenerator?: PluginFaker['resolvedOptions']['regexGenerator'];
|
|
244
|
-
mapper?: PluginFaker['resolvedOptions']['mapper'];
|
|
245
239
|
resolver: ResolverFaker;
|
|
246
240
|
typeName?: string;
|
|
247
241
|
schemaName?: string;
|
|
@@ -304,7 +298,7 @@ declare function Faker({
|
|
|
304
298
|
* factory returns a value matching the corresponding TypeScript type from
|
|
305
299
|
* `@kubb/plugin-ts`.
|
|
306
300
|
*/
|
|
307
|
-
declare const fakerGenerator: import("
|
|
301
|
+
declare const fakerGenerator: import("kubb/kit").Generator<PluginFaker, unknown>;
|
|
308
302
|
//#endregion
|
|
309
303
|
//#region src/plugin.d.ts
|
|
310
304
|
/**
|
|
@@ -319,7 +313,7 @@ declare const pluginFakerName = "plugin-faker";
|
|
|
319
313
|
*
|
|
320
314
|
* @example
|
|
321
315
|
* ```ts
|
|
322
|
-
* import { defineConfig } from 'kubb'
|
|
316
|
+
* import { defineConfig } from 'kubb/config'
|
|
323
317
|
* import { pluginTs } from '@kubb/plugin-ts'
|
|
324
318
|
* import { pluginFaker } from '@kubb/plugin-faker'
|
|
325
319
|
*
|
|
@@ -336,7 +330,7 @@ declare const pluginFakerName = "plugin-faker";
|
|
|
336
330
|
* })
|
|
337
331
|
* ```
|
|
338
332
|
*/
|
|
339
|
-
declare const pluginFaker: (options?: Options | undefined) => import("
|
|
333
|
+
declare const pluginFaker: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginFaker>;
|
|
340
334
|
//#endregion
|
|
341
335
|
//#region src/resolvers/resolverFaker.d.ts
|
|
342
336
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ast, defineGenerator, definePlugin, defineResolver } from "kubb/kit";
|
|
3
3
|
import { createFunctionParameter, createFunctionParameters, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
4
|
-
import { File, Function, jsxRenderer } from "
|
|
4
|
+
import { File, Function, jsxRenderer } from "kubb/jsx";
|
|
5
5
|
import path, { posix } from "node:path";
|
|
6
|
-
import {
|
|
7
|
-
import { Fragment, jsx, jsxs } from "@kubb/renderer-jsx/jsx-runtime";
|
|
6
|
+
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
8
7
|
import { createHash } from "node:crypto";
|
|
9
8
|
//#region src/utils.ts
|
|
10
9
|
/**
|
|
@@ -236,17 +235,17 @@ function Faker({ node, description, name, typeName, printer, seed, canOverride }
|
|
|
236
235
|
children: /* @__PURE__ */ jsxs(Function, {
|
|
237
236
|
export: true,
|
|
238
237
|
name,
|
|
239
|
-
JSDoc: { comments: description ? [`@description ${jsStringEscape(description)}`] : [] },
|
|
238
|
+
JSDoc: { comments: description ? [`@description ${ast.jsStringEscape(description)}`] : [] },
|
|
240
239
|
params: canOverride ? paramsSignature : void 0,
|
|
241
240
|
returnType: returnType ?? void 0,
|
|
242
241
|
children: [seed ? /* @__PURE__ */ jsxs(Fragment, { children: [`faker.seed(${JSON.stringify(seed)})`, /* @__PURE__ */ jsx("br", {})] }) : void 0, `return ${returnExpression}`]
|
|
243
242
|
})
|
|
244
243
|
});
|
|
245
244
|
}
|
|
246
|
-
const functionSignature = `${description ? `/**\n * @description ${jsStringEscape(description)}\n */\n ` : ""}export function ${name}<TData extends Partial<${typeName}> = object>(data?: TData)`;
|
|
245
|
+
const functionSignature = `${description ? `/**\n * @description ${ast.jsStringEscape(description)}\n */\n ` : ""}export function ${name}<TData extends Partial<${typeName}> = object>(data?: TData)`;
|
|
247
246
|
const seedCode = seed ? `faker.seed(${JSON.stringify(seed)})\n ` : "";
|
|
248
247
|
const { cyclicSchemas, schemaName } = printer.options;
|
|
249
|
-
const functionBody = node.type === "object" && !!cyclicSchemas && (node.properties ?? []).some((p) => containsCircularRef(p.schema, {
|
|
248
|
+
const functionBody = node.type === "object" && !!cyclicSchemas && (node.properties ?? []).some((p) => ast.containsCircularRef(p.schema, {
|
|
250
249
|
circularSchemas: cyclicSchemas,
|
|
251
250
|
excludeName: schemaName
|
|
252
251
|
})) ? `{
|
|
@@ -656,7 +655,7 @@ const fakerKeywordMapper = {
|
|
|
656
655
|
return `{...${items.join(", ...")}}`;
|
|
657
656
|
},
|
|
658
657
|
matches: (value = "", regexGenerator = "faker") => {
|
|
659
|
-
if (regexGenerator === "randexp") return `${toRegExpString(value, "RandExp")}.gen()`;
|
|
658
|
+
if (regexGenerator === "randexp") return `${ast.toRegExpString(value, "RandExp")}.gen()`;
|
|
660
659
|
return `faker.helpers.fromRegExp("${value}")`;
|
|
661
660
|
},
|
|
662
661
|
email: () => "faker.internet.email()",
|
|
@@ -667,10 +666,12 @@ function getEnumValues(node) {
|
|
|
667
666
|
return node.enumValues ?? [];
|
|
668
667
|
}
|
|
669
668
|
function parseEnumValue(value) {
|
|
670
|
-
if (typeof value === "string") return stringify(value);
|
|
669
|
+
if (typeof value === "string") return ast.stringify(value);
|
|
671
670
|
return value;
|
|
672
671
|
}
|
|
673
|
-
/**
|
|
672
|
+
/**
|
|
673
|
+
* Reads the discriminator literal off a variant, or `undefined` when it can't be determined.
|
|
674
|
+
*/
|
|
674
675
|
function getDiscriminatorValue(member, discriminatorPropertyName) {
|
|
675
676
|
const prop = ast.narrowSchema(member, "object")?.properties?.find((p) => p.name === discriminatorPropertyName);
|
|
676
677
|
const enumNode = prop ? ast.narrowSchema(prop.schema, "enum") : null;
|
|
@@ -730,7 +731,7 @@ const printerFaker = ast.createPrinter((options) => {
|
|
|
730
731
|
return fakerKeywordMapper.time(node.representation ?? "string", this.options.dateParser);
|
|
731
732
|
},
|
|
732
733
|
ref(node) {
|
|
733
|
-
const refName = node.ref ? this.options.nameMapping?.get(node.ref) ?? extractRefName(node.ref) ?? node.name ?? node.schema?.name : node.name ?? node.schema?.name;
|
|
734
|
+
const refName = node.ref ? this.options.nameMapping?.get(node.ref) ?? ast.extractRefName(node.ref) ?? node.name ?? node.schema?.name : node.name ?? node.schema?.name;
|
|
734
735
|
if (!refName) throw new Error("Name not defined for ref node");
|
|
735
736
|
if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
|
|
736
737
|
const resolvedName = node.ref ? this.options.resolver.resolveName(refName) : refName;
|
|
@@ -743,7 +744,7 @@ const printerFaker = ast.createPrinter((options) => {
|
|
|
743
744
|
union(node) {
|
|
744
745
|
const { discriminatorPropertyName } = node;
|
|
745
746
|
const baseTypeName = this.options.typeName;
|
|
746
|
-
const items = mapSchemaMembers(node, (member) => {
|
|
747
|
+
const items = ast.mapSchemaMembers(node, (member) => {
|
|
747
748
|
const value = discriminatorPropertyName ? getDiscriminatorValue(member, discriminatorPropertyName) : void 0;
|
|
748
749
|
if (baseTypeName && value !== void 0) return printNested(member, {
|
|
749
750
|
typeName: `Extract<NonNullable<${baseTypeName}>, { ${JSON.stringify(discriminatorPropertyName)}: ${parseEnumValue(value)} }>`,
|
|
@@ -758,11 +759,11 @@ const printerFaker = ast.createPrinter((options) => {
|
|
|
758
759
|
return fakerKeywordMapper.union(items);
|
|
759
760
|
},
|
|
760
761
|
intersection(node) {
|
|
761
|
-
const items = mapSchemaMembers(node, (member) => printNested(member, { nestedInObject: true })).map(({ output }) => output).filter((item) => Boolean(item) && item !== "undefined");
|
|
762
|
+
const items = ast.mapSchemaMembers(node, (member) => printNested(member, { nestedInObject: true })).map(({ output }) => output).filter((item) => Boolean(item) && item !== "undefined");
|
|
762
763
|
return fakerKeywordMapper.and(items);
|
|
763
764
|
},
|
|
764
765
|
array(node) {
|
|
765
|
-
const items = mapSchemaItems(node, (member) => printNested(member, {
|
|
766
|
+
const items = ast.mapSchemaItems(node, (member) => printNested(member, {
|
|
766
767
|
typeName: this.options.typeName ? `NonNullable<${this.options.typeName}>[number]` : void 0,
|
|
767
768
|
nestedInObject: true
|
|
768
769
|
})).map(({ output }) => output).filter((item) => Boolean(item));
|
|
@@ -777,18 +778,18 @@ const printerFaker = ast.createPrinter((options) => {
|
|
|
777
778
|
},
|
|
778
779
|
object(node) {
|
|
779
780
|
const cyclicSchemas = this.options.cyclicSchemas;
|
|
780
|
-
|
|
781
|
-
if (this.options.mapper && Object.hasOwn(this.options.mapper, property.name)) return `${objectKey(property.name)}: ${this.options.mapper[property.name]}`;
|
|
781
|
+
const entries = (node.properties ?? []).map((property) => {
|
|
782
782
|
const value = printNested(property.schema, {
|
|
783
783
|
typeName: this.options.typeName ? indexedTypeName(this.options.typeName, property.name, this.options.nestedInUnion) : void 0,
|
|
784
784
|
nestedInObject: true
|
|
785
785
|
}) ?? "undefined";
|
|
786
|
-
if (cyclicSchemas && containsCircularRef(property.schema, {
|
|
786
|
+
if (cyclicSchemas && ast.containsCircularRef(property.schema, {
|
|
787
787
|
circularSchemas: cyclicSchemas,
|
|
788
788
|
excludeName: this.options.schemaName
|
|
789
|
-
})) 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 }`;
|
|
790
|
-
return `${objectKey(property.name)}: ${value}`;
|
|
791
|
-
})
|
|
789
|
+
})) return `get ${ast.objectKey(property.name)}() { const _value = ${value}; Object.defineProperty(this, ${JSON.stringify(property.name)}, { value: _value, configurable: true, writable: true, enumerable: true }); return _value }`;
|
|
790
|
+
return `${ast.objectKey(property.name)}: ${value}`;
|
|
791
|
+
});
|
|
792
|
+
return ast.buildObject(entries);
|
|
792
793
|
},
|
|
793
794
|
...options.nodes
|
|
794
795
|
},
|
|
@@ -810,7 +811,7 @@ const fakerGenerator = defineGenerator({
|
|
|
810
811
|
renderer: jsxRenderer,
|
|
811
812
|
schema(node, ctx) {
|
|
812
813
|
const { adapter, config, resolver, root } = ctx;
|
|
813
|
-
const { output, group, dateParser, regexGenerator,
|
|
814
|
+
const { output, group, dateParser, regexGenerator, seed, locale, printer } = ctx.options;
|
|
814
815
|
const pluginTs = ctx.driver.getPlugin(pluginTsName);
|
|
815
816
|
if (!node.name || !pluginTs) return;
|
|
816
817
|
const tsResolver = ctx.driver.getResolver(pluginTsName);
|
|
@@ -847,7 +848,6 @@ const fakerGenerator = defineGenerator({
|
|
|
847
848
|
typeName: meta.typeName,
|
|
848
849
|
dateParser,
|
|
849
850
|
regexGenerator,
|
|
850
|
-
mapper,
|
|
851
851
|
nodes: printer?.nodes,
|
|
852
852
|
cyclicSchemas,
|
|
853
853
|
nameMapping: adapter.options.nameMapping
|
|
@@ -937,7 +937,7 @@ const fakerGenerator = defineGenerator({
|
|
|
937
937
|
},
|
|
938
938
|
operation(node, ctx) {
|
|
939
939
|
const { adapter, config, resolver, root } = ctx;
|
|
940
|
-
const { output, group, dateParser, regexGenerator,
|
|
940
|
+
const { output, group, dateParser, regexGenerator, seed, locale, printer } = ctx.options;
|
|
941
941
|
const pluginTs = ctx.driver.getPlugin(pluginTsName);
|
|
942
942
|
if (!pluginTs) return;
|
|
943
943
|
const tsResolver = ctx.driver.getResolver(pluginTsName);
|
|
@@ -1038,7 +1038,6 @@ const fakerGenerator = defineGenerator({
|
|
|
1038
1038
|
typeName,
|
|
1039
1039
|
dateParser,
|
|
1040
1040
|
regexGenerator,
|
|
1041
|
-
mapper,
|
|
1042
1041
|
nodes: printer?.nodes,
|
|
1043
1042
|
cyclicSchemas,
|
|
1044
1043
|
nameMapping: adapter.options.nameMapping
|
|
@@ -1238,7 +1237,7 @@ const pluginFakerName = "plugin-faker";
|
|
|
1238
1237
|
*
|
|
1239
1238
|
* @example
|
|
1240
1239
|
* ```ts
|
|
1241
|
-
* import { defineConfig } from 'kubb'
|
|
1240
|
+
* import { defineConfig } from 'kubb/config'
|
|
1242
1241
|
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1243
1242
|
* import { pluginFaker } from '@kubb/plugin-faker'
|
|
1244
1243
|
*
|
|
@@ -1259,7 +1258,7 @@ const pluginFaker = definePlugin((options) => {
|
|
|
1259
1258
|
const { output = {
|
|
1260
1259
|
path: "mocks",
|
|
1261
1260
|
barrel: { type: "named" }
|
|
1262
|
-
}, seed, locale = "en", group, exclude = [], include, override = [],
|
|
1261
|
+
}, seed, locale = "en", group, exclude = [], include, override = [], dateParser = "faker", regexGenerator = "faker", printer, resolver: userResolver, macros: userMacros } = options;
|
|
1263
1262
|
const groupConfig = createGroupConfig(group);
|
|
1264
1263
|
return {
|
|
1265
1264
|
name: pluginFakerName,
|
|
@@ -1274,7 +1273,6 @@ const pluginFaker = definePlugin((options) => {
|
|
|
1274
1273
|
include,
|
|
1275
1274
|
override,
|
|
1276
1275
|
group: groupConfig,
|
|
1277
|
-
mapper,
|
|
1278
1276
|
dateParser,
|
|
1279
1277
|
regexGenerator,
|
|
1280
1278
|
printer
|