@kubb/plugin-swr 5.0.0-beta.103 → 5.0.0-beta.106

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
@@ -23,7 +23,7 @@ var __copyProps = (to, from, except, desc) => {
23
23
  }
24
24
  return to;
25
25
  };
26
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
27
27
  value: mod,
28
28
  enumerable: true
29
29
  }) : target, mod));
@@ -72,6 +72,26 @@ function operationFileEntry(node, name, extname = ".ts") {
72
72
  path: node.path
73
73
  };
74
74
  }
75
+ /**
76
+ * Resolves a dependency plugin's generated file for `node.operationId`, cached in `cache` (the
77
+ * current node's `ctx.cache`) under the resolver's own plugin name. Several dependents reading the
78
+ * same dependency for the same operation in one pass (a query plugin's several hook generators, the
79
+ * MCP handler, ...) share one computed name and path instead of each calling `resolver.file` again.
80
+ *
81
+ * @example Cache `plugin-ts`'s file for the current operation
82
+ * ```ts
83
+ * const fileTs = resolveDependencyOperationFile({ cache: ctx.cache, node, resolver: tsResolver, root, output })
84
+ * ```
85
+ */
86
+ function resolveDependencyOperationFile(options) {
87
+ const { cache, node, resolver, root, output, group } = options;
88
+ return cache.getOrSet(`${resolver.pluginName}:operationFile`, () => resolver.file({
89
+ ...operationFileEntry(node, node.operationId),
90
+ root,
91
+ output,
92
+ group: group ?? void 0
93
+ }));
94
+ }
75
95
  function getOperationLink(node, link) {
76
96
  if (!link) return null;
77
97
  if (typeof link === "function") return link(node) ?? null;
@@ -165,13 +185,24 @@ function buildOperationComments(node, options = {}) {
165
185
  if (!splitLines) return filteredComments;
166
186
  return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
167
187
  }
188
+ const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
189
+ /**
190
+ * Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
191
+ * group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
192
+ * (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
193
+ * same parameters once per plugin.
194
+ */
168
195
  function getOperationParameters(node) {
169
- return {
196
+ const cached = operationParameterGroupsByNode.get(node);
197
+ if (cached) return cached;
198
+ const groups = {
170
199
  path: dedupeParams(node.parameters.filter((param) => param.in === "path")),
171
200
  query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
172
201
  header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
173
202
  cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
174
203
  };
204
+ operationParameterGroupsByNode.set(node, groups);
205
+ return groups;
175
206
  }
176
207
  function getStatusCodeNumber(statusCode) {
177
208
  const code = Number(statusCode);
@@ -389,23 +420,30 @@ function resolveContractClient(options) {
389
420
  * (plugin-fetch or plugin-axios). Returns `null` when no contract plugin is in play (the inline
390
421
  * path). The plugin injects `.kubb/client.ts` at the global output root, the same path consumers
391
422
  * read `RequestConfig` / `ResponseErrorConfig` from.
423
+ *
424
+ * The result is cached in the current node's `cache` (`ctx.cache`) under the client plugin's name,
425
+ * so several dependents reading the same client plugin for one operation in a single pass
426
+ * (react-query's query/mutation/infinite generators, vue-query, swr, the MCP handler, ...) share
427
+ * one computed result instead of each re-deriving the name and path.
392
428
  */
393
429
  function resolveClientOperation(options) {
394
- const { clientPlugin, driver, node, root, output } = options;
430
+ const { clientPlugin, driver, node, root, output, cache } = options;
395
431
  if (!clientPlugin) return null;
396
- const resolver = driver.getResolver(clientPlugin.pluginName);
397
- const plugin = driver.getPlugin(clientPlugin.pluginName);
398
- const file = resolver.file({
399
- ...operationFileEntry(node, node.operationId),
400
- root,
401
- output: plugin?.options?.output ?? output,
402
- group: plugin?.options?.group ?? void 0
432
+ return cache.getOrSet(`${clientPlugin.pluginName}:clientOperation`, () => {
433
+ const resolver = driver.getResolver(clientPlugin.pluginName);
434
+ const plugin = driver.getPlugin(clientPlugin.pluginName);
435
+ const file = resolver.file({
436
+ ...operationFileEntry(node, node.operationId),
437
+ root,
438
+ output: plugin?.options?.output ?? output,
439
+ group: plugin?.options?.group ?? void 0
440
+ });
441
+ return {
442
+ name: resolver.name(node.operationId),
443
+ path: file.path,
444
+ clientPath: node_path.default.resolve(root, ".kubb/client.ts")
445
+ };
403
446
  });
404
- return {
405
- name: resolver.name(node.operationId),
406
- path: file.path,
407
- clientPath: node_path.default.resolve(root, ".kubb/client.ts")
408
- };
409
447
  }
410
448
  //#endregion
411
449
  //#region ../../internals/tanstack-query/src/utils.ts
@@ -473,14 +511,16 @@ function buildGroupedRequestParam(node, options) {
473
511
  */
474
512
  function buildQueryOptionsParams(node, options) {
475
513
  const { resolver, memberTypeWrapper } = options;
476
- return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, {
514
+ const groupedParam = buildGroupedRequestParam(node, {
477
515
  resolver,
478
516
  memberTypeWrapper
479
- }), (0, _kubb_plugin_ts.createFunctionParameter)({
517
+ });
518
+ const configParam = (0, _kubb_plugin_ts.createFunctionParameter)({
480
519
  name: "config",
481
520
  type: `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`,
482
521
  default: "{}"
483
- })].filter((param) => param !== null) });
522
+ });
523
+ return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [groupedParam, configParam].filter((param) => param !== null) });
484
524
  }
485
525
  /**
486
526
  * Builds the call to a client `<op>` function inside a query/mutation hook body. The function takes
@@ -697,10 +737,12 @@ function buildMutationParamsNode(node, options) {
697
737
  const { mutationKeyTypeName, mutationArgTypeName, resolver } = options;
698
738
  const responseName = resolver.response.response(node);
699
739
  const errorNames = resolveErrorNames(node, resolver);
740
+ const TData = responseName;
741
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
700
742
  return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
701
743
  name: "options",
702
744
  type: `{
703
- mutation?: SWRMutationConfiguration<${responseName}, ${`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`}, ${mutationKeyTypeName} | null, ${mutationArgTypeName}> & { throwOnError?: boolean },
745
+ mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${mutationArgTypeName}> & { throwOnError?: boolean },
704
746
  client?: ${buildRequestConfigType(node)},
705
747
  shouldFetch?: boolean,
706
748
  }`,
@@ -795,17 +837,20 @@ function buildQueryParamsNode(node, options) {
795
837
  const { resolver } = options;
796
838
  const responseName = resolver.response.response(node);
797
839
  const errorNames = resolveErrorNames(node, resolver);
840
+ const TData = responseName;
841
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
798
842
  const optionsParam = (0, _kubb_plugin_ts.createFunctionParameter)({
799
843
  name: "options",
800
844
  type: `{
801
- query?: SWRConfiguration<${[responseName, `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`].join(", ")}>,
845
+ query?: SWRConfiguration<${[TData, TError].join(", ")}>,
802
846
  client?: ${buildClientOptionType()},
803
847
  shouldFetch?: boolean,
804
848
  immutable?: boolean
805
849
  }`,
806
850
  default: "{}"
807
851
  });
808
- return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
852
+ const groupedParam = buildGroupedRequestParam(node, { resolver });
853
+ return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [groupedParam, optionsParam].filter((param) => param !== null) });
809
854
  }
810
855
  function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver }) {
811
856
  const responseName = tsResolver.response.response(node);
@@ -908,7 +953,8 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
908
953
  driver,
909
954
  node,
910
955
  root,
911
- output
956
+ output,
957
+ cache: ctx.cache
912
958
  });
913
959
  if (!contractOp) return null;
914
960
  const mutationHookName = resolver.mutation.name(node);
@@ -925,11 +971,10 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
925
971
  output,
926
972
  group
927
973
  }),
928
- fileTs: tsResolver.file({
929
- name: node.operationId,
930
- extname: ".ts",
931
- tag: node.tags[0] ?? "default",
932
- path: node.path,
974
+ fileTs: resolveDependencyOperationFile({
975
+ cache: ctx.cache,
976
+ node,
977
+ resolver: tsResolver,
933
978
  root,
934
979
  output: pluginTs.options?.output ?? output,
935
980
  group: pluginTs.options?.group
@@ -1035,7 +1080,8 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
1035
1080
  driver,
1036
1081
  node,
1037
1082
  root,
1038
- output
1083
+ output,
1084
+ cache: ctx.cache
1039
1085
  });
1040
1086
  if (!contractOp) return null;
1041
1087
  const queryName = resolver.query.name(node);
@@ -1052,11 +1098,10 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
1052
1098
  output,
1053
1099
  group
1054
1100
  }),
1055
- fileTs: tsResolver.file({
1056
- name: node.operationId,
1057
- extname: ".ts",
1058
- tag: node.tags[0] ?? "default",
1059
- path: node.path,
1101
+ fileTs: resolveDependencyOperationFile({
1102
+ cache: ctx.cache,
1103
+ node,
1104
+ resolver: tsResolver,
1060
1105
  root,
1061
1106
  output: pluginTs.options?.output ?? output,
1062
1107
  group: pluginTs.options?.group