@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.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
2
+ import path from "node:path";
2
3
  import { Resolver, ast, createResolver, defineGenerator, definePlugin } from "kubb/kit";
3
4
  import { createFunctionParameter, createFunctionParameters, createObjectBindingPattern, createTypeLiteral, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
4
- import path from "node:path";
5
5
  import { File, Function, Type, jsxRenderer } from "kubb/jsx";
6
6
  import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
7
7
  //#region ../../internals/utils/src/casing.ts
@@ -30,6 +30,16 @@ function toCamelOrPascal(text, pascal) {
30
30
  function camelCase(text, { prefix = "", suffix = "" } = {}) {
31
31
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
32
32
  }
33
+ /**
34
+ * Uppercases only the first character of `text`, leaving the rest untouched.
35
+ * Unlike {@link pascalCase} it never re-splits word boundaries or strips characters.
36
+ *
37
+ * @example
38
+ * `capitalize('getPetById') // 'GetPetById'`
39
+ */
40
+ function capitalize(text) {
41
+ return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
42
+ }
33
43
  //#endregion
34
44
  //#region ../../internals/utils/src/reserved.ts
35
45
  /**
@@ -579,6 +589,20 @@ function resolveClient(options) {
579
589
  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."
580
590
  };
581
591
  }
592
+ /**
593
+ * Resolves the contract client during a consumer plugin's setup hook. Extracts the plugin names
594
+ * from the raw `plugins` config, applies {@link resolveClient}, and throws the diagnostic on a
595
+ * misconfiguration so every consumer fails fast with the same message.
596
+ */
597
+ function resolveContractClient(options) {
598
+ const { client, plugins = [] } = options;
599
+ const resolved = resolveClient({
600
+ client,
601
+ pluginNames: plugins.map((plugin) => plugin.name).filter((name) => Boolean(name))
602
+ });
603
+ if (resolved.kind === "error") throw new Error(resolved.message);
604
+ return resolved;
605
+ }
582
606
  //#endregion
583
607
  //#region ../../internals/client/src/resolveClientOperation.ts
584
608
  /**
@@ -606,34 +630,6 @@ function resolveClientOperation(options) {
606
630
  };
607
631
  }
608
632
  //#endregion
609
- //#region ../../internals/tanstack-query/src/components/MutationKey.tsx
610
- const declarationPrinter$4 = functionPrinter({ mode: "declaration" });
611
- const mutationKeyTransformer = ({ node }) => {
612
- if (!node.path) return [];
613
- return [`{ url: '${Url.toPath(node.path)}' }`];
614
- };
615
- function MutationKey$1({ name, node, transformer }) {
616
- const paramsNode = createFunctionParameters({ params: [] });
617
- const paramsSignature = declarationPrinter$4.print(paramsNode) ?? "";
618
- const keys = (transformer ?? mutationKeyTransformer)({
619
- node,
620
- casing: "camelcase"
621
- });
622
- return /* @__PURE__ */ jsx(File.Source, {
623
- name,
624
- isExportable: true,
625
- isIndexable: true,
626
- children: /* @__PURE__ */ jsx(Function.Arrow, {
627
- name,
628
- export: true,
629
- params: paramsSignature,
630
- singleLine: true,
631
- children: `[${keys.join(", ")}] as const`
632
- })
633
- });
634
- }
635
- __name(MutationKey$1, "MutationKey");
636
- //#endregion
637
633
  //#region ../../internals/tanstack-query/src/utils.ts
638
634
  /**
639
635
  * The grouped request options, ordered for both the destructured signature and the
@@ -714,6 +710,9 @@ function buildQueryOptionsParams(node, options) {
714
710
  * shorthand alongside the spread `config`, with `throwOnError: true` pinned last so a caller's config
715
711
  * can't flip the query's error semantics. Queries thread the TanStack `signal`; mutations omit it.
716
712
  *
713
+ * When `unwrapName` is set, each request group is passed as an explicit member unwrapped through it,
714
+ * used by vue-query to resolve refs and getters with `toValue(...)` at call time.
715
+ *
717
716
  * @example
718
717
  * ```ts
719
718
  * buildClientCall(node, { clientName: 'getPetById', signal: true })
@@ -721,16 +720,46 @@ function buildQueryOptionsParams(node, options) {
721
720
  * ```
722
721
  */
723
722
  function buildClientCall(node, options) {
724
- const { clientName, signal = false } = options;
723
+ const { clientName, signal = false, unwrapName } = options;
725
724
  const { groups } = getRequestGroupOptionality(node);
726
725
  return `${clientName}({ ${[
727
726
  "...config",
728
- ...requestGroupOrder.filter((key) => groups[key]),
727
+ ...requestGroupOrder.filter((key) => groups[key]).map((name) => unwrapName ? `${name}: ${unwrapName(name)}` : name),
729
728
  signal ? "signal: config.signal ?? signal" : null,
730
729
  "throwOnError: true"
731
730
  ].filter((part) => part !== null).join(", ")} })`;
732
731
  }
733
732
  /**
733
+ * Applies the shared query defaults during plugin setup: `false` disables query generation, and an
734
+ * object merges over `methods: ['GET']` and the plugin's runtime `importPath`.
735
+ */
736
+ function resolveQueryConfig(query, options) {
737
+ if (query === false) return false;
738
+ return {
739
+ importPath: options.importPath,
740
+ methods: ["GET"],
741
+ ...query
742
+ };
743
+ }
744
+ /**
745
+ * Applies the shared mutation defaults during plugin setup: `false` disables mutation generation,
746
+ * and an object merges over `methods: ['POST', 'PUT', 'PATCH', 'DELETE']` and the plugin's runtime
747
+ * `importPath`.
748
+ */
749
+ function resolveMutationConfig(mutation, options) {
750
+ if (mutation === false) return false;
751
+ return {
752
+ importPath: options.importPath,
753
+ methods: [
754
+ "POST",
755
+ "PUT",
756
+ "PATCH",
757
+ "DELETE"
758
+ ],
759
+ ...mutation
760
+ };
761
+ }
762
+ /**
734
763
  * Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
735
764
  * typed from the operation's `Options` minus `url`. The query key transformer reads the
736
765
  * grouped `path`/`query`/`body` bindings.
@@ -748,6 +777,87 @@ function buildQueryKeyParams(node, options) {
748
777
  return createFunctionParameters({ params: groupedParam ? [groupedParam] : [] });
749
778
  }
750
779
  //#endregion
780
+ //#region ../../internals/tanstack-query/src/components/MutationKey.tsx
781
+ const declarationPrinter$4 = functionPrinter({ mode: "declaration" });
782
+ const mutationKeyTransformer = ({ node }) => {
783
+ if (!node.path) return [];
784
+ return [`{ url: '${Url.toPath(node.path)}' }`];
785
+ };
786
+ function MutationKey$1({ name, node, transformer }) {
787
+ const paramsNode = createFunctionParameters({ params: [] });
788
+ const paramsSignature = declarationPrinter$4.print(paramsNode) ?? "";
789
+ const keys = (transformer ?? mutationKeyTransformer)({
790
+ node,
791
+ casing: "camelcase"
792
+ });
793
+ return /* @__PURE__ */ jsx(File.Source, {
794
+ name,
795
+ isExportable: true,
796
+ isIndexable: true,
797
+ children: /* @__PURE__ */ jsx(Function.Arrow, {
798
+ name,
799
+ export: true,
800
+ params: paramsSignature,
801
+ singleLine: true,
802
+ children: `[${keys.join(", ")}] as const`
803
+ })
804
+ });
805
+ }
806
+ __name(MutationKey$1, "MutationKey");
807
+ //#endregion
808
+ //#region ../../internals/tanstack-query/src/resolver.ts
809
+ /**
810
+ * Builds the shared query namespace for a variant. Spread the result into
811
+ * `createResolver` once per variant the plugin supports.
812
+ *
813
+ * @example
814
+ * ```ts
815
+ * createResolver<PluginReactQuery>({
816
+ * query: createQueryResolver(),
817
+ * suspenseQuery: createQueryResolver('Suspense'),
818
+ * })
819
+ * ```
820
+ */
821
+ function createQueryResolver(variant = "") {
822
+ return {
823
+ name(node) {
824
+ return `use${capitalize(this.name(node.operationId))}${variant}`;
825
+ },
826
+ optionsName(node) {
827
+ return `${this.name(node.operationId)}${variant}QueryOptions`;
828
+ },
829
+ keyName(node) {
830
+ return `${this.name(node.operationId)}${variant}QueryKey`;
831
+ },
832
+ keyTypeName(node) {
833
+ return `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
834
+ },
835
+ clientName(node) {
836
+ return `${this.name(node.operationId)}${variant}`;
837
+ }
838
+ };
839
+ }
840
+ /**
841
+ * Builds the shared mutation namespace. Spread the result into
842
+ * `createResolver` and add plugin-specific methods (`optionsName`,
843
+ * `typeName`, `argTypeName`) next to it.
844
+ *
845
+ * @example
846
+ * ```ts
847
+ * createResolver<PluginSwr>({ mutation: { ...createMutationResolver(), argTypeName(node) {...} } })
848
+ * ```
849
+ */
850
+ function createMutationResolver() {
851
+ return {
852
+ name(node) {
853
+ return `use${capitalize(this.name(node.operationId))}`;
854
+ },
855
+ keyName(node) {
856
+ return `${this.name(node.operationId)}MutationKey`;
857
+ }
858
+ };
859
+ }
860
+ //#endregion
751
861
  //#region ../../internals/tanstack-query/src/components/QueryKey.tsx
752
862
  const declarationPrinter$3 = functionPrinter({ mode: "declaration" });
753
863
  const queryKeyTransformer = ({ node }) => {
@@ -1237,9 +1347,6 @@ const queryGenerator = defineGenerator({
1237
1347
  });
1238
1348
  //#endregion
1239
1349
  //#region src/resolvers/resolverSwr.ts
1240
- function capitalize(name) {
1241
- return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
1242
- }
1243
1350
  /**
1244
1351
  * Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
1245
1352
  * SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
@@ -1258,30 +1365,9 @@ function capitalize(name) {
1258
1365
  */
1259
1366
  const resolverSwr = createResolver({
1260
1367
  pluginName: "plugin-swr",
1261
- query: {
1262
- name(node) {
1263
- return `use${capitalize(this.name(node.operationId))}`;
1264
- },
1265
- optionsName(node) {
1266
- return `${this.name(node.operationId)}QueryOptions`;
1267
- },
1268
- keyName(node) {
1269
- return `${this.name(node.operationId)}QueryKey`;
1270
- },
1271
- keyTypeName(node) {
1272
- return `${capitalize(this.name(node.operationId))}QueryKey`;
1273
- },
1274
- clientName(node) {
1275
- return this.name(node.operationId);
1276
- }
1277
- },
1368
+ query: createQueryResolver(),
1278
1369
  mutation: {
1279
- name(node) {
1280
- return `use${capitalize(this.name(node.operationId))}`;
1281
- },
1282
- keyName(node) {
1283
- return `${this.name(node.operationId)}MutationKey`;
1284
- },
1370
+ ...createMutationResolver(),
1285
1371
  keyTypeName(node) {
1286
1372
  return `${capitalize(this.name(node.operationId))}MutationKey`;
1287
1373
  },
@@ -1331,35 +1417,16 @@ const pluginSwr = definePlugin((options) => {
1331
1417
  dependencies: [pluginTsName],
1332
1418
  hooks: { "kubb:plugin:setup"(ctx) {
1333
1419
  const resolver = userResolver ? Resolver.merge(resolverSwr, userResolver) : resolverSwr;
1334
- const resolvedClient = resolveClient({
1335
- client,
1336
- pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
1337
- });
1338
- if (resolvedClient.kind === "error") throw new Error(resolvedClient.message);
1339
- const resolvedClientDescriptor = {
1340
- kind: "contract",
1341
- pluginName: resolvedClient.pluginName
1342
- };
1343
1420
  ctx.setOptions({
1344
1421
  output,
1345
- client: resolvedClientDescriptor,
1422
+ client: resolveContractClient({
1423
+ client,
1424
+ plugins: ctx.config.plugins
1425
+ }),
1346
1426
  queryKey,
1347
- query: query === false ? false : {
1348
- importPath: "swr",
1349
- methods: ["GET"],
1350
- ...query
1351
- },
1427
+ query: resolveQueryConfig(query, { importPath: "swr" }),
1352
1428
  mutationKey,
1353
- mutation: mutation === false ? false : {
1354
- importPath: "swr/mutation",
1355
- methods: [
1356
- "POST",
1357
- "PUT",
1358
- "PATCH",
1359
- "DELETE"
1360
- ],
1361
- ...mutation
1362
- },
1429
+ mutation: resolveMutationConfig(mutation, { importPath: "swr/mutation" }),
1363
1430
  group: groupConfig,
1364
1431
  exclude,
1365
1432
  include,
@@ -1373,6 +1440,6 @@ const pluginSwr = definePlugin((options) => {
1373
1440
  };
1374
1441
  });
1375
1442
  //#endregion
1376
- export { pluginSwr as default, pluginSwr, pluginSwrName };
1443
+ export { pluginSwr as default, pluginSwr, pluginSwrName, resolverSwr };
1377
1444
 
1378
1445
  //# sourceMappingURL=index.js.map