@kubb/plugin-faker 5.0.0-beta.94 → 5.0.0-beta.98

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
@@ -1,7 +1,6 @@
1
1
  import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
2
2
  import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ResolverPatch, ast } from "kubb/kit";
3
3
  import { KubbReactNode } from "kubb/jsx";
4
-
5
4
  //#region src/types.d.ts
6
5
  /**
7
6
  * Resolver for Faker that provides naming methods for mock functions.
@@ -213,12 +212,6 @@ type PrinterFakerOptions = {
213
212
  * the recursive faker call from ever executing (avoiding stack overflow).
214
213
  */
215
214
  cyclicSchemas?: ReadonlySet<string>;
216
- /**
217
- * Maps a component `$ref` path to its collision-resolved name. When two components collide
218
- * (across sections or by case), the adapter renames one of them; the `ref()` handler resolves
219
- * the referenced name through this map so the emitted faker reference matches the renamed component.
220
- */
221
- nameMapping?: ReadonlyMap<string, string>;
222
215
  };
223
216
  /**
224
217
  * Factory options for the Faker printer, defining input/output types and configuration.
@@ -240,15 +233,7 @@ type Props = {
240
233
  description?: string;
241
234
  canOverride: boolean;
242
235
  };
243
- declare function Faker({
244
- node,
245
- description,
246
- name,
247
- typeName,
248
- printer,
249
- seed,
250
- canOverride
251
- }: Props): KubbReactNode;
236
+ declare function Faker({ node, description, name, typeName, printer, seed, canOverride }: Props): KubbReactNode;
252
237
  //#endregion
253
238
  //#region src/generators/fakerGenerator.d.ts
254
239
  /**
package/dist/index.js CHANGED
@@ -724,22 +724,79 @@ function getOperationParameters(node, options = {}) {
724
724
  };
725
725
  }
726
726
  //#endregion
727
- //#region ../../internals/shared/src/adapter.ts
727
+ //#region ../../internals/shared/src/resolver.ts
728
728
  /**
729
- * Narrows the generic `Adapter` from a generator context to the OpenAPI adapter,
730
- * so OAS-only options (`dateType`, `nameMapping`) and the parsed `document` are typed.
729
+ * Resolves a single operation parameter name with the
730
+ * `<operationId> <in> <name>` template.
731
731
  *
732
- * Throws when a non-OAS adapter is configured, turning a silently wrong cast into a
733
- * clear, actionable error at the point of use.
732
+ * @example
733
+ * `operationParamName.call(resolver, node, param) // 'DeletePetPathPetId'`
734
+ */
735
+ function operationParamName(node, param) {
736
+ return this.name(`${node.operationId} ${param.in} ${param.name}`);
737
+ }
738
+ /**
739
+ * Builds the shared `param` namespace. Spread the result into `createResolver`
740
+ * and override individual methods next to it when a plugin deviates.
741
+ *
742
+ * @example
743
+ * ```ts
744
+ * createResolver<PluginTs>({ param: createOperationParamResolver(), ... })
745
+ * ```
746
+ */
747
+ function createOperationParamResolver() {
748
+ return {
749
+ name: operationParamName,
750
+ path(node) {
751
+ return this.name(`${node.operationId} Path`);
752
+ },
753
+ query(node) {
754
+ return this.name(`${node.operationId} Query`);
755
+ },
756
+ headers(node) {
757
+ return this.name(`${node.operationId} Headers`);
758
+ }
759
+ };
760
+ }
761
+ /**
762
+ * Builds the shared `response` namespace. Spread the result into
763
+ * `createResolver` and add plugin-specific methods (`options`, `error`) next
764
+ * to it.
765
+ *
766
+ * @example
767
+ * ```ts
768
+ * createResolver<PluginTs>({ response: { ...createOperationResponseResolver(), options(node) {...} }, ... })
769
+ * ```
770
+ */
771
+ function createOperationResponseResolver() {
772
+ return {
773
+ status(node, statusCode) {
774
+ return this.name(`${node.operationId} Status ${statusCode}`);
775
+ },
776
+ body(node) {
777
+ return this.name(`${node.operationId} Body`);
778
+ },
779
+ responses(node) {
780
+ return this.name(`${node.operationId} Responses`);
781
+ },
782
+ response(node) {
783
+ return this.name(`${node.operationId} Response`);
784
+ }
785
+ };
786
+ }
787
+ /**
788
+ * Builds a resolver `file` override whose base name runs every path segment
789
+ * through `toFilePath`, casing the final segment with `caseLast`.
734
790
  *
735
791
  * @example
736
792
  * ```ts
737
- * const { dateType } = getOasAdapter(ctx.adapter).options
793
+ * createResolver<PluginTs>({ file: createCasedFile(pascalCase), ... })
738
794
  * ```
739
795
  */
740
- function getOasAdapter(adapter) {
741
- if (adapter.name !== "oas") throw new Error(`Expected the OpenAPI adapter (adapterOas), but received "${adapter.name}". Configure \`adapter: adapterOas()\` in your Kubb config.`);
742
- return adapter;
796
+ function createCasedFile(caseLast) {
797
+ return { baseName({ name, extname }) {
798
+ return `${toFilePath(name, caseLast)}${extname}`;
799
+ } };
743
800
  }
744
801
  //#endregion
745
802
  //#region ../../internals/shared/src/group.ts
@@ -911,7 +968,7 @@ const printerFaker = ast.createPrinter((options) => {
911
968
  return fakerKeywordMapper.time(node.representation ?? "string", this.options.dateParser);
912
969
  },
913
970
  ref(node) {
914
- const refName = node.ref ? this.options.nameMapping?.get(node.ref) ?? ast.extractRefName(node.ref) ?? node.name ?? node.schema?.name : node.name ?? node.schema?.name;
971
+ const refName = ast.resolveRefName(node);
915
972
  if (!refName) throw new Error("Name not defined for ref node");
916
973
  if (this.options.schemaName && refName === this.options.schemaName) return this.options.typeName ? `undefined as unknown as ${this.options.typeName}` : "undefined as unknown";
917
974
  const resolvedName = node.ref ? this.options.resolver.name(refName) : refName;
@@ -926,10 +983,13 @@ const printerFaker = ast.createPrinter((options) => {
926
983
  const baseTypeName = this.options.typeName;
927
984
  const items = ast.mapSchemaMembers(node, (member) => {
928
985
  const value = discriminatorPropertyName ? getDiscriminatorValue(member, discriminatorPropertyName) : void 0;
929
- if (baseTypeName && value !== void 0) return printNested(member, {
930
- typeName: `Extract<NonNullable<${baseTypeName}>, { ${JSON.stringify(discriminatorPropertyName)}: ${parseEnumValue(value)} }>`,
931
- nestedInObject: true
932
- });
986
+ if (baseTypeName && value !== void 0) {
987
+ const typeName = `Extract<NonNullable<${baseTypeName}>, { ${JSON.stringify(discriminatorPropertyName)}: ${parseEnumValue(value)} }>`;
988
+ return printNested(member, {
989
+ typeName,
990
+ nestedInObject: true
991
+ });
992
+ }
933
993
  return printNested(member, {
934
994
  typeName: baseTypeName,
935
995
  nestedInObject: true,
@@ -989,7 +1049,7 @@ const fakerGenerator = defineGenerator({
989
1049
  name: "faker",
990
1050
  renderer: jsxRenderer,
991
1051
  schema(node, ctx) {
992
- const { adapter, config, resolver, root } = ctx;
1052
+ const { config, resolver, root } = ctx;
993
1053
  const { output, group, dateParser, regexGenerator, seed, locale, printer } = ctx.options;
994
1054
  const pluginTs = ctx.driver.getPlugin(pluginTsName);
995
1055
  if (!node.name || !pluginTs) return;
@@ -1026,8 +1086,7 @@ const fakerGenerator = defineGenerator({
1026
1086
  dateParser,
1027
1087
  regexGenerator,
1028
1088
  nodes: printer?.nodes,
1029
- cyclicSchemas,
1030
- nameMapping: getOasAdapter(adapter).options.nameMapping
1089
+ cyclicSchemas
1031
1090
  });
1032
1091
  const fakerText = printerInstance.print(node) ?? "undefined";
1033
1092
  const typeReference = resolveTypeReference({
@@ -1038,16 +1097,12 @@ const fakerGenerator = defineGenerator({
1038
1097
  filePath: meta.file.path,
1039
1098
  typeFilePath: meta.typeFile.path
1040
1099
  });
1041
- const usedImports = filterUsedImports(adapter.getImports(node, (schemaName) => ({
1042
- name: resolver.name(schemaName),
1043
- path: resolver.file({
1044
- name: schemaName,
1045
- extname: ".ts",
1046
- root,
1047
- output,
1048
- group: group ?? void 0
1049
- }).path
1050
- })), fakerText);
1100
+ const usedImports = filterUsedImports(resolver.imports({
1101
+ node,
1102
+ root,
1103
+ output,
1104
+ group: group ?? void 0
1105
+ }), fakerText);
1051
1106
  return /* @__PURE__ */ jsxs(File, {
1052
1107
  baseName: meta.file.baseName,
1053
1108
  path: meta.file.path,
@@ -1112,7 +1167,7 @@ const fakerGenerator = defineGenerator({
1112
1167
  });
1113
1168
  },
1114
1169
  operation(node, ctx) {
1115
- const { adapter, config, resolver, root } = ctx;
1170
+ const { config, resolver, root } = ctx;
1116
1171
  const { output, group, dateParser, regexGenerator, seed, locale, printer } = ctx.options;
1117
1172
  const pluginTs = ctx.driver.getPlugin(pluginTsName);
1118
1173
  if (!pluginTs) return;
@@ -1212,16 +1267,12 @@ const fakerGenerator = defineGenerator({
1212
1267
  })
1213
1268
  };
1214
1269
  function resolveMockImports(schema) {
1215
- return adapter.getImports(schema, (schemaName) => ({
1216
- name: resolver.name(schemaName),
1217
- path: resolver.file({
1218
- name: schemaName,
1219
- extname: ".ts",
1220
- root,
1221
- output,
1222
- group: group ?? void 0
1223
- }).path
1224
- })).filter((entry) => entry.path !== meta.file.path);
1270
+ return resolver.imports({
1271
+ node: schema,
1272
+ root,
1273
+ output,
1274
+ group: group ?? void 0
1275
+ }).filter((entry) => entry.path !== meta.file.path);
1225
1276
  }
1226
1277
  function renderEntry({ schema, name, typeName, description, skipImportNames = [] }) {
1227
1278
  if (!schema) return null;
@@ -1233,8 +1284,7 @@ const fakerGenerator = defineGenerator({
1233
1284
  dateParser,
1234
1285
  regexGenerator,
1235
1286
  nodes: printer?.nodes,
1236
- cyclicSchemas,
1237
- nameMapping: getOasAdapter(adapter).options.nameMapping
1287
+ cyclicSchemas
1238
1288
  });
1239
1289
  const fakerText = printerInstance.print(schema) ?? "undefined";
1240
1290
  const { imports, aliases } = aliasConflictingImports(filterUsedImports(resolveMockImports(schema), fakerText, skipImportNames), localHelperNames);
@@ -1357,37 +1407,9 @@ const resolverFaker = createResolver({
1357
1407
  name(name) {
1358
1408
  return ensureValidVarName(camelCase(name, { prefix: "create" }));
1359
1409
  },
1360
- file: { baseName({ name, extname }) {
1361
- return `${toFilePath(name, (part) => camelCase(part, { prefix: "create" }))}${extname}`;
1362
- } },
1363
- param: {
1364
- name(node, param) {
1365
- return this.name(`${node.operationId} ${param.in} ${param.name}`);
1366
- },
1367
- path(node) {
1368
- return this.name(`${node.operationId} Path`);
1369
- },
1370
- query(node) {
1371
- return this.name(`${node.operationId} Query`);
1372
- },
1373
- headers(node) {
1374
- return this.name(`${node.operationId} Headers`);
1375
- }
1376
- },
1377
- response: {
1378
- status(node, statusCode) {
1379
- return this.name(`${node.operationId} Status ${statusCode}`);
1380
- },
1381
- body(node) {
1382
- return this.name(`${node.operationId} Body`);
1383
- },
1384
- response(node) {
1385
- return this.name(`${node.operationId} Response`);
1386
- },
1387
- responses(node) {
1388
- return this.name(`${node.operationId} Responses`);
1389
- }
1390
- }
1410
+ file: createCasedFile((part) => camelCase(part, { prefix: "create" })),
1411
+ param: createOperationParamResolver(),
1412
+ response: createOperationResponseResolver()
1391
1413
  });
1392
1414
  //#endregion
1393
1415
  //#region src/plugin.ts