@kubb/plugin-react-query 5.0.0-beta.104 → 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.js CHANGED
@@ -43,6 +43,26 @@ function operationFileEntry(node, name, extname = ".ts") {
43
43
  path: node.path
44
44
  };
45
45
  }
46
+ /**
47
+ * Resolves a dependency plugin's generated file for `node.operationId`, cached in `cache` (the
48
+ * current node's `ctx.cache`) under the resolver's own plugin name. Several dependents reading the
49
+ * same dependency for the same operation in one pass (a query plugin's several hook generators, the
50
+ * MCP handler, ...) share one computed name and path instead of each calling `resolver.file` again.
51
+ *
52
+ * @example Cache `plugin-ts`'s file for the current operation
53
+ * ```ts
54
+ * const fileTs = resolveDependencyOperationFile({ cache: ctx.cache, node, resolver: tsResolver, root, output })
55
+ * ```
56
+ */
57
+ function resolveDependencyOperationFile(options) {
58
+ const { cache, node, resolver, root, output, group } = options;
59
+ return cache.getOrSet(`${resolver.pluginName}:operationFile`, () => resolver.file({
60
+ ...operationFileEntry(node, node.operationId),
61
+ root,
62
+ output,
63
+ group: group ?? void 0
64
+ }));
65
+ }
46
66
  function getOperationLink(node, link) {
47
67
  if (!link) return null;
48
68
  if (typeof link === "function") return link(node) ?? null;
@@ -391,23 +411,30 @@ function resolveContractClient(options) {
391
411
  * (plugin-fetch or plugin-axios). Returns `null` when no contract plugin is in play (the inline
392
412
  * path). The plugin injects `.kubb/client.ts` at the global output root, the same path consumers
393
413
  * read `RequestConfig` / `ResponseErrorConfig` from.
414
+ *
415
+ * The result is cached in the current node's `cache` (`ctx.cache`) under the client plugin's name,
416
+ * so several dependents reading the same client plugin for one operation in a single pass
417
+ * (react-query's query/mutation/infinite generators, vue-query, swr, the MCP handler, ...) share
418
+ * one computed result instead of each re-deriving the name and path.
394
419
  */
395
420
  function resolveClientOperation(options) {
396
- const { clientPlugin, driver, node, root, output } = options;
421
+ const { clientPlugin, driver, node, root, output, cache } = options;
397
422
  if (!clientPlugin) return null;
398
- const resolver = driver.getResolver(clientPlugin.pluginName);
399
- const plugin = driver.getPlugin(clientPlugin.pluginName);
400
- const file = resolver.file({
401
- ...operationFileEntry(node, node.operationId),
402
- root,
403
- output: plugin?.options?.output ?? output,
404
- group: plugin?.options?.group ?? void 0
423
+ return cache.getOrSet(`${clientPlugin.pluginName}:clientOperation`, () => {
424
+ const resolver = driver.getResolver(clientPlugin.pluginName);
425
+ const plugin = driver.getPlugin(clientPlugin.pluginName);
426
+ const file = resolver.file({
427
+ ...operationFileEntry(node, node.operationId),
428
+ root,
429
+ output: plugin?.options?.output ?? output,
430
+ group: plugin?.options?.group ?? void 0
431
+ });
432
+ return {
433
+ name: resolver.name(node.operationId),
434
+ path: file.path,
435
+ clientPath: path.resolve(root, ".kubb/client.ts")
436
+ };
405
437
  });
406
- return {
407
- name: resolver.name(node.operationId),
408
- path: file.path,
409
- clientPath: path.resolve(root, ".kubb/client.ts")
410
- };
411
438
  }
412
439
  //#endregion
413
440
  //#region ../../internals/tanstack-query/src/utils.ts
@@ -478,14 +505,16 @@ function buildGroupedRequestParam(node, options) {
478
505
  */
479
506
  function buildQueryOptionsParams(node, options) {
480
507
  const { resolver, memberTypeWrapper } = options;
481
- return createFunctionParameters({ params: [buildGroupedRequestParam(node, {
508
+ const groupedParam = buildGroupedRequestParam(node, {
482
509
  resolver,
483
510
  memberTypeWrapper
484
- }), createFunctionParameter({
511
+ });
512
+ const configParam = createFunctionParameter({
485
513
  name: "config",
486
514
  type: `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`,
487
515
  default: "{}"
488
- })].filter((param) => param !== null) });
516
+ });
517
+ return createFunctionParameters({ params: [groupedParam, configParam].filter((param) => param !== null) });
489
518
  }
490
519
  /**
491
520
  * Builds the call to a client `<op>` function inside a query/mutation hook body. The function takes
@@ -1139,10 +1168,11 @@ function buildInfiniteQueryParamsNode(node, options) {
1139
1168
  }`,
1140
1169
  default: "{}"
1141
1170
  });
1142
- return createFunctionParameters({ params: [buildGroupedRequestParam(node, {
1171
+ const groupedParam = buildGroupedRequestParam(node, {
1143
1172
  resolver,
1144
1173
  memberTypeWrapper: maybeValueOrGetter
1145
- }), optionsParam].filter((param) => param !== null) });
1174
+ });
1175
+ return createFunctionParameters({ params: [groupedParam, optionsParam].filter((param) => param !== null) });
1146
1176
  }
1147
1177
  function InfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, initialPageParam, queryParam, customOptions, suspense }) {
1148
1178
  const { TData: responseType, TError: errorType } = buildResponseTypes(node, tsResolver);
@@ -1255,15 +1285,16 @@ function resolveMutationRequestType(node, resolver) {
1255
1285
  function buildMutationParamsNode(node, options) {
1256
1286
  const { resolver } = options;
1257
1287
  const { TData, TError } = buildResponseTypes(node, resolver);
1288
+ const generics = [
1289
+ TData,
1290
+ TError,
1291
+ resolveMutationRequestType(node, resolver),
1292
+ "TContext"
1293
+ ].join(", ");
1258
1294
  return createFunctionParameters({ params: [createFunctionParameter({
1259
1295
  name: "options",
1260
1296
  type: `{
1261
- mutation?: UseMutationOptions<${[
1262
- TData,
1263
- TError,
1264
- resolveMutationRequestType(node, resolver),
1265
- "TContext"
1266
- ].join(", ")}> & { client?: QueryClient },
1297
+ mutation?: UseMutationOptions<${generics}> & { client?: QueryClient },
1267
1298
  client?: ${buildRequestConfigType(node)},
1268
1299
  }`,
1269
1300
  default: "{}"
@@ -1321,10 +1352,11 @@ function buildQueryParamsNode(node, options) {
1321
1352
  }`,
1322
1353
  default: "{}"
1323
1354
  });
1324
- return createFunctionParameters({ params: [buildGroupedRequestParam(node, {
1355
+ const groupedParam = buildGroupedRequestParam(node, {
1325
1356
  resolver,
1326
1357
  memberTypeWrapper: maybeValueOrGetter
1327
- }), optionsParam].filter((param) => param !== null) });
1358
+ });
1359
+ return createFunctionParameters({ params: [groupedParam, optionsParam].filter((param) => param !== null) });
1328
1360
  }
1329
1361
  function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, customOptions, suspense }) {
1330
1362
  const { TData, TError } = buildResponseTypes(node, tsResolver);
@@ -1460,7 +1492,8 @@ const infiniteQueryGenerator = defineGenerator({
1460
1492
  driver,
1461
1493
  node,
1462
1494
  root,
1463
- output
1495
+ output,
1496
+ cache: ctx.cache
1464
1497
  });
1465
1498
  if (!contractOp) return null;
1466
1499
  const queryName = resolver.infiniteQuery.name(node);
@@ -1474,11 +1507,13 @@ const infiniteQueryGenerator = defineGenerator({
1474
1507
  output,
1475
1508
  group: group ?? void 0
1476
1509
  }),
1477
- fileTs: tsResolver.file({
1478
- ...operationFileEntry(node, node.operationId),
1510
+ fileTs: resolveDependencyOperationFile({
1511
+ cache: ctx.cache,
1512
+ node,
1513
+ resolver: tsResolver,
1479
1514
  root,
1480
1515
  output: pluginTs.options?.output ?? output,
1481
- group: pluginTs.options?.group ?? void 0
1516
+ group: pluginTs.options?.group
1482
1517
  })
1483
1518
  };
1484
1519
  const { queryParamsTypeName } = resolvePageParamType(node, {
@@ -1627,7 +1662,8 @@ const mutationGenerator = defineGenerator({
1627
1662
  driver,
1628
1663
  node,
1629
1664
  root,
1630
- output
1665
+ output,
1666
+ cache: ctx.cache
1631
1667
  });
1632
1668
  if (!contractOp) return null;
1633
1669
  const mutationHookName = resolver.mutation.name(node);
@@ -1641,11 +1677,13 @@ const mutationGenerator = defineGenerator({
1641
1677
  output,
1642
1678
  group: group ?? void 0
1643
1679
  }),
1644
- fileTs: tsResolver.file({
1645
- ...operationFileEntry(node, node.operationId),
1680
+ fileTs: resolveDependencyOperationFile({
1681
+ cache: ctx.cache,
1682
+ node,
1683
+ resolver: tsResolver,
1646
1684
  root,
1647
1685
  output: pluginTs.options?.output ?? output,
1648
- group: pluginTs.options?.group ?? void 0
1686
+ group: pluginTs.options?.group
1649
1687
  })
1650
1688
  };
1651
1689
  const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {
@@ -1772,7 +1810,8 @@ const queryGenerator = defineGenerator({
1772
1810
  driver,
1773
1811
  node,
1774
1812
  root,
1775
- output
1813
+ output,
1814
+ cache: ctx.cache
1776
1815
  });
1777
1816
  if (!contractOp) return null;
1778
1817
  const queryName = resolver.query.name(node);
@@ -1786,11 +1825,13 @@ const queryGenerator = defineGenerator({
1786
1825
  output,
1787
1826
  group: group ?? void 0
1788
1827
  }),
1789
- fileTs: tsResolver.file({
1790
- ...operationFileEntry(node, node.operationId),
1828
+ fileTs: resolveDependencyOperationFile({
1829
+ cache: ctx.cache,
1830
+ node,
1831
+ resolver: tsResolver,
1791
1832
  root,
1792
1833
  output: pluginTs.options?.output ?? output,
1793
- group: pluginTs.options?.group ?? void 0
1834
+ group: pluginTs.options?.group
1794
1835
  })
1795
1836
  };
1796
1837
  const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {
@@ -1927,7 +1968,8 @@ const suspenseInfiniteQueryGenerator = defineGenerator({
1927
1968
  driver,
1928
1969
  node,
1929
1970
  root,
1930
- output
1971
+ output,
1972
+ cache: ctx.cache
1931
1973
  });
1932
1974
  if (!contractOp) return null;
1933
1975
  const queryName = resolver.suspenseInfiniteQuery.name(node);
@@ -1941,11 +1983,13 @@ const suspenseInfiniteQueryGenerator = defineGenerator({
1941
1983
  output,
1942
1984
  group: group ?? void 0
1943
1985
  }),
1944
- fileTs: tsResolver.file({
1945
- ...operationFileEntry(node, node.operationId),
1986
+ fileTs: resolveDependencyOperationFile({
1987
+ cache: ctx.cache,
1988
+ node,
1989
+ resolver: tsResolver,
1946
1990
  root,
1947
1991
  output: pluginTs.options?.output ?? output,
1948
- group: pluginTs.options?.group ?? void 0
1992
+ group: pluginTs.options?.group
1949
1993
  })
1950
1994
  };
1951
1995
  const rawQueryParams = getOperationParameters(node).query;
@@ -2094,7 +2138,8 @@ const suspenseQueryGenerator = defineGenerator({
2094
2138
  driver,
2095
2139
  node,
2096
2140
  root,
2097
- output
2141
+ output,
2142
+ cache: ctx.cache
2098
2143
  });
2099
2144
  if (!contractOp) return null;
2100
2145
  const queryName = resolver.suspenseQuery.name(node);
@@ -2108,11 +2153,13 @@ const suspenseQueryGenerator = defineGenerator({
2108
2153
  output,
2109
2154
  group: group ?? void 0
2110
2155
  }),
2111
- fileTs: tsResolver.file({
2112
- ...operationFileEntry(node, node.operationId),
2156
+ fileTs: resolveDependencyOperationFile({
2157
+ cache: ctx.cache,
2158
+ node,
2159
+ resolver: tsResolver,
2113
2160
  root,
2114
2161
  output: pluginTs.options?.output ?? output,
2115
- group: pluginTs.options?.group ?? void 0
2162
+ group: pluginTs.options?.group
2116
2163
  })
2117
2164
  };
2118
2165
  const importedTypeNames = [tsResolver.response.options(node), ...resolveOperationTypeNames(node, tsResolver, {