@kubb/plugin-swr 5.0.0-beta.100 → 5.0.0-beta.104

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
@@ -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
@@ -908,7 +946,8 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
908
946
  driver,
909
947
  node,
910
948
  root,
911
- output
949
+ output,
950
+ cache: ctx.cache
912
951
  });
913
952
  if (!contractOp) return null;
914
953
  const mutationHookName = resolver.mutation.name(node);
@@ -925,11 +964,10 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
925
964
  output,
926
965
  group
927
966
  }),
928
- fileTs: tsResolver.file({
929
- name: node.operationId,
930
- extname: ".ts",
931
- tag: node.tags[0] ?? "default",
932
- path: node.path,
967
+ fileTs: resolveDependencyOperationFile({
968
+ cache: ctx.cache,
969
+ node,
970
+ resolver: tsResolver,
933
971
  root,
934
972
  output: pluginTs.options?.output ?? output,
935
973
  group: pluginTs.options?.group
@@ -1035,7 +1073,8 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
1035
1073
  driver,
1036
1074
  node,
1037
1075
  root,
1038
- output
1076
+ output,
1077
+ cache: ctx.cache
1039
1078
  });
1040
1079
  if (!contractOp) return null;
1041
1080
  const queryName = resolver.query.name(node);
@@ -1052,11 +1091,10 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
1052
1091
  output,
1053
1092
  group
1054
1093
  }),
1055
- fileTs: tsResolver.file({
1056
- name: node.operationId,
1057
- extname: ".ts",
1058
- tag: node.tags[0] ?? "default",
1059
- path: node.path,
1094
+ fileTs: resolveDependencyOperationFile({
1095
+ cache: ctx.cache,
1096
+ node,
1097
+ resolver: tsResolver,
1060
1098
  root,
1061
1099
  output: pluginTs.options?.output ?? output,
1062
1100
  group: pluginTs.options?.group