@kubb/plugin-faker 5.0.4 → 5.0.6

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
@@ -225,7 +225,7 @@ type PrinterFakerFactory = ast.PrinterFactoryOptions<'faker', PrinterFakerOption
225
225
  * Creates a Faker printer that generates mock data generation code from schema nodes.
226
226
  * Handles circular references gracefully by emitting memoizing getters for cyclic properties.
227
227
  */
228
- declare const printerFaker: (options: PrinterFakerOptions) => ast.Printer<PrinterFakerFactory>;
228
+ export declare const printerFaker: (options: PrinterFakerOptions) => ast.Printer<PrinterFakerFactory>;
229
229
  //#endregion
230
230
  //#region src/components/Faker.d.ts
231
231
  type Props = {
@@ -236,7 +236,7 @@ type Props = {
236
236
  description?: string;
237
237
  canOverride: boolean;
238
238
  };
239
- declare function Faker({ node, description, name, typeName, printer, canOverride }: Props): KubbReactNode;
239
+ export declare function Faker({ node, description, name, typeName, printer, canOverride }: Props): KubbReactNode;
240
240
  //#endregion
241
241
  //#region src/generators/fakerGenerator.d.ts
242
242
  /**
@@ -245,14 +245,14 @@ declare function Faker({ node, description, name, typeName, printer, canOverride
245
245
  * factory returns a value matching the corresponding TypeScript type from
246
246
  * `@kubb/plugin-ts`.
247
247
  */
248
- declare const fakerGenerator: import("kubb/kit").Generator<PluginFaker, unknown>;
248
+ export declare const fakerGenerator: import("kubb/kit").Generator<PluginFaker, unknown>;
249
249
  //#endregion
250
250
  //#region src/plugin.d.ts
251
251
  /**
252
252
  * Canonical plugin name for `@kubb/plugin-faker`. Used for driver lookups and
253
253
  * cross-plugin dependency references.
254
254
  */
255
- declare const pluginFakerName = "plugin-faker";
255
+ export declare const pluginFakerName = "plugin-faker";
256
256
  /**
257
257
  * Generates one mock-data factory per OpenAPI schema using Faker.js. Call
258
258
  * `createPet()` to get a realistic `Pet` object. Useful for tests, Storybook,
@@ -277,7 +277,7 @@ declare const pluginFakerName = "plugin-faker";
277
277
  * })
278
278
  * ```
279
279
  */
280
- declare const pluginFaker: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginFaker>;
280
+ export declare const pluginFaker: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginFaker>;
281
281
  //#endregion
282
282
  //#region src/resolvers/resolverFaker.d.ts
283
283
  /**
@@ -292,7 +292,7 @@ declare const pluginFaker: (options?: Options | undefined) => import("kubb/kit")
292
292
  * resolverFaker.name('list pets') // 'createListPets'
293
293
  * ```
294
294
  */
295
- declare const resolverFaker: ResolverFaker;
295
+ export declare const resolverFaker: ResolverFaker;
296
296
  //#endregion
297
- export { Faker, type PluginFaker, type PrinterFakerFactory, type PrinterFakerNodes, type PrinterFakerOptions, type ResolverFaker, pluginFaker as default, pluginFaker, fakerGenerator, pluginFakerName, printerFaker, resolverFaker };
297
+ export { type PluginFaker, type PrinterFakerFactory, type PrinterFakerNodes, type PrinterFakerOptions, type ResolverFaker, pluginFaker as default };
298
298
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -463,10 +463,14 @@ function buildResponseUnionSchema(node, resolver) {
463
463
  }
464
464
  return ast.factory.createSchema({
465
465
  type: "union",
466
- members: responses.map((response) => ast.factory.createSchema({
467
- type: "ref",
468
- name: resolver.response.status(node, response.statusCode)
469
- }))
466
+ members: responses.map((response) => {
467
+ const schema = (response.content?.length ?? 0) === 1 ? response.content?.[0]?.schema : void 0;
468
+ return ast.factory.createSchema({
469
+ type: "ref",
470
+ name: resolver.response.status(node, response.statusCode),
471
+ schema
472
+ });
473
+ })
470
474
  });
471
475
  }
472
476
  const SCALAR_TYPES$1 = /* @__PURE__ */ new Set([
@@ -965,27 +969,23 @@ const fakerKeywordMapper = {
965
969
  email: () => "faker.internet.email()",
966
970
  blob: () => "faker.image.url() as unknown as Blob"
967
971
  };
968
- function getEnumValues(node) {
969
- if (node.namedEnumValues?.length) return node.namedEnumValues.map((item) => item.value);
970
- return node.enumValues ?? [];
971
- }
972
972
  function parseEnumValue(value) {
973
973
  if (typeof value === "string") return stringify(value);
974
974
  return value;
975
975
  }
976
976
  /**
977
977
  * Reads the discriminator literal off a variant, or `undefined` when it can't be determined.
978
+ *
979
+ * A variant carries exactly one discriminator value, so a property resolving to several literals
980
+ * (a `ref` to the enum shared by every branch, say) identifies no branch and narrows nothing.
978
981
  */
979
982
  function getDiscriminatorValue(member, discriminatorPropertyName) {
980
- const prop = ast.narrowSchema(member, "object")?.properties?.find((property) => property.name === discriminatorPropertyName);
981
- const enumNode = prop ? ast.narrowSchema(prop.schema, "enum") : null;
982
- if (enumNode) return getEnumValues(enumNode)[0];
983
- const refNode = ast.narrowSchema(member, "ref");
984
- if (refNode?.schema) return getDiscriminatorValue(refNode.schema, discriminatorPropertyName);
985
- const intersectionNode = ast.narrowSchema(member, "intersection");
986
- for (const intersectionMember of intersectionNode?.members ?? []) {
987
- const value = getDiscriminatorValue(intersectionMember, discriminatorPropertyName);
988
- if (value !== void 0) return value;
983
+ for (const property of ast.resolveSchemaProperties({
984
+ node: member,
985
+ propertyName: discriminatorPropertyName
986
+ })) {
987
+ const values = ast.getSchemaLiteralValues(property.schema);
988
+ if (values.length === 1) return values[0];
989
989
  }
990
990
  }
991
991
  /**
@@ -1051,7 +1051,7 @@ const printerFaker = ast.createPrinter((options) => {
1051
1051
  return `${resolvedName}${this.options.nestedInUnion && (node.schema?.type === "object" || node.schema?.type === "intersection") ? "<object>" : ""}()`;
1052
1052
  },
1053
1053
  enum(node) {
1054
- return fakerKeywordMapper.enum(getEnumValues(node).map(parseEnumValue), this.options.typeName);
1054
+ return fakerKeywordMapper.enum(ast.getSchemaLiteralValues(node).map(parseEnumValue), this.options.typeName);
1055
1055
  },
1056
1056
  union(node) {
1057
1057
  const { discriminatorPropertyName } = node;
@@ -1291,7 +1291,8 @@ const fakerGenerator = defineGenerator({
1291
1291
  type: "union",
1292
1292
  members: variants.map((variant) => ast.factory.createSchema({
1293
1293
  type: "ref",
1294
- name: variant.name
1294
+ name: variant.name,
1295
+ schema: variant.schema
1295
1296
  }))
1296
1297
  });
1297
1298
  return [...variants.map((variant) => ({