@kubb/plugin-swr 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.cjs CHANGED
@@ -28,10 +28,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  enumerable: true
29
29
  }) : target, mod));
30
30
  //#endregion
31
- let kubb_kit = require("kubb/kit");
32
- let _kubb_plugin_ts = require("@kubb/plugin-ts");
33
31
  let node_path = require("node:path");
34
32
  node_path = __toESM(node_path, 1);
33
+ let kubb_kit = require("kubb/kit");
34
+ let _kubb_plugin_ts = require("@kubb/plugin-ts");
35
35
  let kubb_jsx = require("kubb/jsx");
36
36
  let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
37
37
  //#region ../../internals/utils/src/casing.ts
@@ -60,6 +60,16 @@ function toCamelOrPascal(text, pascal) {
60
60
  function camelCase(text, { prefix = "", suffix = "" } = {}) {
61
61
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
62
62
  }
63
+ /**
64
+ * Uppercases only the first character of `text`, leaving the rest untouched.
65
+ * Unlike {@link pascalCase} it never re-splits word boundaries or strips characters.
66
+ *
67
+ * @example
68
+ * `capitalize('getPetById') // 'GetPetById'`
69
+ */
70
+ function capitalize(text) {
71
+ return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
72
+ }
63
73
  //#endregion
64
74
  //#region ../../internals/utils/src/reserved.ts
65
75
  /**
@@ -609,6 +619,20 @@ function resolveClient(options) {
609
619
  message: "No client plugin is registered. Add `@kubb/plugin-axios` or `@kubb/plugin-fetch` to `plugins` so the generated code has an HTTP client to call."
610
620
  };
611
621
  }
622
+ /**
623
+ * Resolves the contract client during a consumer plugin's setup hook. Extracts the plugin names
624
+ * from the raw `plugins` config, applies {@link resolveClient}, and throws the diagnostic on a
625
+ * misconfiguration so every consumer fails fast with the same message.
626
+ */
627
+ function resolveContractClient(options) {
628
+ const { client, plugins = [] } = options;
629
+ const resolved = resolveClient({
630
+ client,
631
+ pluginNames: plugins.map((plugin) => plugin.name).filter((name) => Boolean(name))
632
+ });
633
+ if (resolved.kind === "error") throw new Error(resolved.message);
634
+ return resolved;
635
+ }
612
636
  //#endregion
613
637
  //#region ../../internals/client/src/resolveClientOperation.ts
614
638
  /**
@@ -636,34 +660,6 @@ function resolveClientOperation(options) {
636
660
  };
637
661
  }
638
662
  //#endregion
639
- //#region ../../internals/tanstack-query/src/components/MutationKey.tsx
640
- const declarationPrinter$4 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
641
- const mutationKeyTransformer = ({ node }) => {
642
- if (!node.path) return [];
643
- return [`{ url: '${Url.toPath(node.path)}' }`];
644
- };
645
- function MutationKey$1({ name, node, transformer }) {
646
- const paramsNode = (0, _kubb_plugin_ts.createFunctionParameters)({ params: [] });
647
- const paramsSignature = declarationPrinter$4.print(paramsNode) ?? "";
648
- const keys = (transformer ?? mutationKeyTransformer)({
649
- node,
650
- casing: "camelcase"
651
- });
652
- return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
653
- name,
654
- isExportable: true,
655
- isIndexable: true,
656
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function.Arrow, {
657
- name,
658
- export: true,
659
- params: paramsSignature,
660
- singleLine: true,
661
- children: `[${keys.join(", ")}] as const`
662
- })
663
- });
664
- }
665
- __name(MutationKey$1, "MutationKey");
666
- //#endregion
667
663
  //#region ../../internals/tanstack-query/src/utils.ts
668
664
  /**
669
665
  * The grouped request options, ordered for both the destructured signature and the
@@ -744,6 +740,9 @@ function buildQueryOptionsParams(node, options) {
744
740
  * shorthand alongside the spread `config`, with `throwOnError: true` pinned last so a caller's config
745
741
  * can't flip the query's error semantics. Queries thread the TanStack `signal`; mutations omit it.
746
742
  *
743
+ * When `unwrapName` is set, each request group is passed as an explicit member unwrapped through it,
744
+ * used by vue-query to resolve refs and getters with `toValue(...)` at call time.
745
+ *
747
746
  * @example
748
747
  * ```ts
749
748
  * buildClientCall(node, { clientName: 'getPetById', signal: true })
@@ -751,16 +750,46 @@ function buildQueryOptionsParams(node, options) {
751
750
  * ```
752
751
  */
753
752
  function buildClientCall(node, options) {
754
- const { clientName, signal = false } = options;
753
+ const { clientName, signal = false, unwrapName } = options;
755
754
  const { groups } = getRequestGroupOptionality(node);
756
755
  return `${clientName}({ ${[
757
756
  "...config",
758
- ...requestGroupOrder.filter((key) => groups[key]),
757
+ ...requestGroupOrder.filter((key) => groups[key]).map((name) => unwrapName ? `${name}: ${unwrapName(name)}` : name),
759
758
  signal ? "signal: config.signal ?? signal" : null,
760
759
  "throwOnError: true"
761
760
  ].filter((part) => part !== null).join(", ")} })`;
762
761
  }
763
762
  /**
763
+ * Applies the shared query defaults during plugin setup: `false` disables query generation, and an
764
+ * object merges over `methods: ['GET']` and the plugin's runtime `importPath`.
765
+ */
766
+ function resolveQueryConfig(query, options) {
767
+ if (query === false) return false;
768
+ return {
769
+ importPath: options.importPath,
770
+ methods: ["GET"],
771
+ ...query
772
+ };
773
+ }
774
+ /**
775
+ * Applies the shared mutation defaults during plugin setup: `false` disables mutation generation,
776
+ * and an object merges over `methods: ['POST', 'PUT', 'PATCH', 'DELETE']` and the plugin's runtime
777
+ * `importPath`.
778
+ */
779
+ function resolveMutationConfig(mutation, options) {
780
+ if (mutation === false) return false;
781
+ return {
782
+ importPath: options.importPath,
783
+ methods: [
784
+ "POST",
785
+ "PUT",
786
+ "PATCH",
787
+ "DELETE"
788
+ ],
789
+ ...mutation
790
+ };
791
+ }
792
+ /**
764
793
  * Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
765
794
  * typed from the operation's `Options` minus `url`. The query key transformer reads the
766
795
  * grouped `path`/`query`/`body` bindings.
@@ -778,6 +807,87 @@ function buildQueryKeyParams(node, options) {
778
807
  return (0, _kubb_plugin_ts.createFunctionParameters)({ params: groupedParam ? [groupedParam] : [] });
779
808
  }
780
809
  //#endregion
810
+ //#region ../../internals/tanstack-query/src/components/MutationKey.tsx
811
+ const declarationPrinter$4 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
812
+ const mutationKeyTransformer = ({ node }) => {
813
+ if (!node.path) return [];
814
+ return [`{ url: '${Url.toPath(node.path)}' }`];
815
+ };
816
+ function MutationKey$1({ name, node, transformer }) {
817
+ const paramsNode = (0, _kubb_plugin_ts.createFunctionParameters)({ params: [] });
818
+ const paramsSignature = declarationPrinter$4.print(paramsNode) ?? "";
819
+ const keys = (transformer ?? mutationKeyTransformer)({
820
+ node,
821
+ casing: "camelcase"
822
+ });
823
+ return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
824
+ name,
825
+ isExportable: true,
826
+ isIndexable: true,
827
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function.Arrow, {
828
+ name,
829
+ export: true,
830
+ params: paramsSignature,
831
+ singleLine: true,
832
+ children: `[${keys.join(", ")}] as const`
833
+ })
834
+ });
835
+ }
836
+ __name(MutationKey$1, "MutationKey");
837
+ //#endregion
838
+ //#region ../../internals/tanstack-query/src/resolver.ts
839
+ /**
840
+ * Builds the shared query namespace for a variant. Spread the result into
841
+ * `createResolver` once per variant the plugin supports.
842
+ *
843
+ * @example
844
+ * ```ts
845
+ * createResolver<PluginReactQuery>({
846
+ * query: createQueryResolver(),
847
+ * suspenseQuery: createQueryResolver('Suspense'),
848
+ * })
849
+ * ```
850
+ */
851
+ function createQueryResolver(variant = "") {
852
+ return {
853
+ name(node) {
854
+ return `use${capitalize(this.name(node.operationId))}${variant}`;
855
+ },
856
+ optionsName(node) {
857
+ return `${this.name(node.operationId)}${variant}QueryOptions`;
858
+ },
859
+ keyName(node) {
860
+ return `${this.name(node.operationId)}${variant}QueryKey`;
861
+ },
862
+ keyTypeName(node) {
863
+ return `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
864
+ },
865
+ clientName(node) {
866
+ return `${this.name(node.operationId)}${variant}`;
867
+ }
868
+ };
869
+ }
870
+ /**
871
+ * Builds the shared mutation namespace. Spread the result into
872
+ * `createResolver` and add plugin-specific methods (`optionsName`,
873
+ * `typeName`, `argTypeName`) next to it.
874
+ *
875
+ * @example
876
+ * ```ts
877
+ * createResolver<PluginSwr>({ mutation: { ...createMutationResolver(), argTypeName(node) {...} } })
878
+ * ```
879
+ */
880
+ function createMutationResolver() {
881
+ return {
882
+ name(node) {
883
+ return `use${capitalize(this.name(node.operationId))}`;
884
+ },
885
+ keyName(node) {
886
+ return `${this.name(node.operationId)}MutationKey`;
887
+ }
888
+ };
889
+ }
890
+ //#endregion
781
891
  //#region ../../internals/tanstack-query/src/components/QueryKey.tsx
782
892
  const declarationPrinter$3 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
783
893
  const queryKeyTransformer = ({ node }) => {
@@ -1267,9 +1377,6 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
1267
1377
  });
1268
1378
  //#endregion
1269
1379
  //#region src/resolvers/resolverSwr.ts
1270
- function capitalize(name) {
1271
- return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
1272
- }
1273
1380
  /**
1274
1381
  * Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
1275
1382
  * SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
@@ -1288,30 +1395,9 @@ function capitalize(name) {
1288
1395
  */
1289
1396
  const resolverSwr = (0, kubb_kit.createResolver)({
1290
1397
  pluginName: "plugin-swr",
1291
- query: {
1292
- name(node) {
1293
- return `use${capitalize(this.name(node.operationId))}`;
1294
- },
1295
- optionsName(node) {
1296
- return `${this.name(node.operationId)}QueryOptions`;
1297
- },
1298
- keyName(node) {
1299
- return `${this.name(node.operationId)}QueryKey`;
1300
- },
1301
- keyTypeName(node) {
1302
- return `${capitalize(this.name(node.operationId))}QueryKey`;
1303
- },
1304
- clientName(node) {
1305
- return this.name(node.operationId);
1306
- }
1307
- },
1398
+ query: createQueryResolver(),
1308
1399
  mutation: {
1309
- name(node) {
1310
- return `use${capitalize(this.name(node.operationId))}`;
1311
- },
1312
- keyName(node) {
1313
- return `${this.name(node.operationId)}MutationKey`;
1314
- },
1400
+ ...createMutationResolver(),
1315
1401
  keyTypeName(node) {
1316
1402
  return `${capitalize(this.name(node.operationId))}MutationKey`;
1317
1403
  },
@@ -1361,35 +1447,16 @@ const pluginSwr = (0, kubb_kit.definePlugin)((options) => {
1361
1447
  dependencies: [_kubb_plugin_ts.pluginTsName],
1362
1448
  hooks: { "kubb:plugin:setup"(ctx) {
1363
1449
  const resolver = userResolver ? kubb_kit.Resolver.merge(resolverSwr, userResolver) : resolverSwr;
1364
- const resolvedClient = resolveClient({
1365
- client,
1366
- pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
1367
- });
1368
- if (resolvedClient.kind === "error") throw new Error(resolvedClient.message);
1369
- const resolvedClientDescriptor = {
1370
- kind: "contract",
1371
- pluginName: resolvedClient.pluginName
1372
- };
1373
1450
  ctx.setOptions({
1374
1451
  output,
1375
- client: resolvedClientDescriptor,
1452
+ client: resolveContractClient({
1453
+ client,
1454
+ plugins: ctx.config.plugins
1455
+ }),
1376
1456
  queryKey,
1377
- query: query === false ? false : {
1378
- importPath: "swr",
1379
- methods: ["GET"],
1380
- ...query
1381
- },
1457
+ query: resolveQueryConfig(query, { importPath: "swr" }),
1382
1458
  mutationKey,
1383
- mutation: mutation === false ? false : {
1384
- importPath: "swr/mutation",
1385
- methods: [
1386
- "POST",
1387
- "PUT",
1388
- "PATCH",
1389
- "DELETE"
1390
- ],
1391
- ...mutation
1392
- },
1459
+ mutation: resolveMutationConfig(mutation, { importPath: "swr/mutation" }),
1393
1460
  group: groupConfig,
1394
1461
  exclude,
1395
1462
  include,
@@ -1406,5 +1473,6 @@ const pluginSwr = (0, kubb_kit.definePlugin)((options) => {
1406
1473
  exports.default = pluginSwr;
1407
1474
  exports.pluginSwr = pluginSwr;
1408
1475
  exports.pluginSwrName = pluginSwrName;
1476
+ exports.resolverSwr = resolverSwr;
1409
1477
 
1410
1478
  //# sourceMappingURL=index.cjs.map