@kubb/plugin-react-query 5.0.0-beta.30 → 5.0.0-beta.31

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.
Files changed (35) hide show
  1. package/dist/{components-CDmg-RPi.js → components-BVmVgpLX.js} +142 -62
  2. package/dist/components-BVmVgpLX.js.map +1 -0
  3. package/dist/{components-MPBTffPl.cjs → components-DLUeLMsz.cjs} +147 -61
  4. package/dist/components-DLUeLMsz.cjs.map +1 -0
  5. package/dist/components.cjs +1 -1
  6. package/dist/components.d.ts +3 -1
  7. package/dist/components.js +1 -1
  8. package/dist/{generators-BtsWNz-6.cjs → generators--AcF4Y4n.cjs} +35 -153
  9. package/dist/generators--AcF4Y4n.cjs.map +1 -0
  10. package/dist/{generators-Bma51Uar.js → generators-BFn9CLBS.js} +36 -154
  11. package/dist/generators-BFn9CLBS.js.map +1 -0
  12. package/dist/generators.cjs +1 -1
  13. package/dist/generators.js +1 -1
  14. package/dist/index.cjs +40 -10
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.js +40 -10
  17. package/dist/index.js.map +1 -1
  18. package/extension.yaml +3 -3
  19. package/package.json +7 -7
  20. package/src/components/InfiniteQuery.tsx +13 -8
  21. package/src/components/InfiniteQueryOptions.tsx +8 -8
  22. package/src/components/Query.tsx +6 -1
  23. package/src/components/QueryOptions.tsx +12 -8
  24. package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -6
  25. package/src/generators/hookOptionsGenerator.tsx +12 -22
  26. package/src/generators/infiniteQueryGenerator.tsx +19 -22
  27. package/src/generators/mutationGenerator.tsx +19 -22
  28. package/src/generators/queryGenerator.tsx +19 -22
  29. package/src/generators/suspenseInfiniteQueryGenerator.tsx +19 -22
  30. package/src/generators/suspenseQueryGenerator.tsx +20 -22
  31. package/src/plugin.ts +4 -16
  32. package/dist/components-CDmg-RPi.js.map +0 -1
  33. package/dist/components-MPBTffPl.cjs.map +0 -1
  34. package/dist/generators-Bma51Uar.js.map +0 -1
  35. package/dist/generators-BtsWNz-6.cjs.map +0 -1
@@ -335,38 +335,31 @@ var URLPath = class {
335
335
  }
336
336
  };
337
337
  //#endregion
338
- //#region ../../internals/tanstack-query/src/components/MutationKey.tsx
339
- const declarationPrinter$10 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
340
- const mutationKeyTransformer = ({ node, casing }) => {
341
- return [`{ url: '${new URLPath(node.path, { casing }).toURLPath()}' }`];
342
- };
343
- function MutationKey({ name, paramsCasing, node, transformer }) {
344
- const paramsNode = _kubb_core.ast.createFunctionParameters({ params: [] });
345
- const paramsSignature = declarationPrinter$10.print(paramsNode) ?? "";
346
- const keys = (transformer ?? mutationKeyTransformer)({
347
- node,
348
- casing: paramsCasing
349
- });
350
- return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
338
+ //#region ../../internals/shared/src/operation.ts
339
+ /**
340
+ * Builds the `ResolverFileParams` every operation generator passes to
341
+ * `resolver.resolveFile`: a file named `name`, tagged by the operation's first
342
+ * tag (or `'default'`), at the operation's path. Centralizes the entry object
343
+ * that was repeated at dozens of call sites across the client and query plugins.
344
+ *
345
+ * @example
346
+ * ```ts
347
+ * resolver.resolveFile(operationFileEntry(node, node.operationId), { root, output, group })
348
+ * ```
349
+ */
350
+ function operationFileEntry(node, name, extname = ".ts") {
351
+ return {
351
352
  name,
352
- isExportable: true,
353
- isIndexable: true,
354
- children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function.Arrow, {
355
- name,
356
- export: true,
357
- params: paramsSignature,
358
- singleLine: true,
359
- children: `[${keys.join(", ")}] as const`
360
- })
361
- });
353
+ extname,
354
+ tag: node.tags[0] ?? "default",
355
+ path: node.path
356
+ };
362
357
  }
363
- //#endregion
364
- //#region ../../internals/shared/src/operation.ts
365
358
  function getOperationLink(node, link) {
366
359
  if (!link) return null;
367
360
  if (typeof link === "function") return link(node) ?? null;
368
361
  if (link === "urlPath") return node.path ? `{@link ${new URLPath(node.path).URL}}` : null;
369
- return `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}`;
362
+ return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
370
363
  }
371
364
  function getContentTypeInfo(node) {
372
365
  const contentTypes = node.requestBody?.content?.map((e) => e.contentType) ?? [];
@@ -464,14 +457,41 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
464
457
  return result;
465
458
  }
466
459
  //#endregion
460
+ //#region ../../internals/tanstack-query/src/components/MutationKey.tsx
461
+ const declarationPrinter$10 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
462
+ const mutationKeyTransformer = ({ node, casing }) => {
463
+ if (!node.path) return [];
464
+ return [`{ url: '${new URLPath(node.path, { casing }).toURLPath()}' }`];
465
+ };
466
+ function MutationKey({ name, paramsCasing, node, transformer }) {
467
+ const paramsNode = _kubb_core.ast.createFunctionParameters({ params: [] });
468
+ const paramsSignature = declarationPrinter$10.print(paramsNode) ?? "";
469
+ const keys = (transformer ?? mutationKeyTransformer)({
470
+ node,
471
+ casing: paramsCasing
472
+ });
473
+ return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
474
+ name,
475
+ isExportable: true,
476
+ isIndexable: true,
477
+ children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function.Arrow, {
478
+ name,
479
+ export: true,
480
+ params: paramsSignature,
481
+ singleLine: true,
482
+ children: `[${keys.join(", ")}] as const`
483
+ })
484
+ });
485
+ }
486
+ //#endregion
467
487
  //#region ../../internals/tanstack-query/src/utils.ts
468
488
  function matchesPattern(node, ov) {
469
489
  const { type, pattern } = ov;
470
490
  const matches = (value) => typeof pattern === "string" ? value === pattern : pattern.test(value);
471
491
  if (type === "operationId") return matches(node.operationId);
472
492
  if (type === "tag") return node.tags.some((t) => matches(t));
473
- if (type === "path") return matches(node.path);
474
- if (type === "method") return matches(node.method);
493
+ if (type === "path") return node.path !== void 0 && matches(node.path);
494
+ if (type === "method") return node.method !== void 0 && matches(node.method);
475
495
  return false;
476
496
  }
477
497
  /**
@@ -599,7 +619,12 @@ function buildQueryKeyParams(node, options) {
599
619
  params.push(...buildGroupParam("params", node, queryParams, queryGroupType, resolver));
600
620
  return _kubb_core.ast.createFunctionParameters({ params });
601
621
  }
602
- function buildEnabledCheck(paramsNode) {
622
+ /**
623
+ * Collect the names of the required params (no default) that drive the `enabled`
624
+ * guard. These are exactly the params that should be made optional in the
625
+ * generated signatures so callers can pass `undefined` to reach the disabled state.
626
+ */
627
+ function getEnabledParamNames(paramsNode) {
603
628
  const required = [];
604
629
  for (const param of paramsNode.params) if ("kind" in param && param.kind === "ParameterGroup") {
605
630
  const group = param;
@@ -608,12 +633,67 @@ function buildEnabledCheck(paramsNode) {
608
633
  const fp = param;
609
634
  if (!fp.optional && fp.default === void 0) required.push(fp.name);
610
635
  }
611
- return required.join(" && ");
636
+ return required;
637
+ }
638
+ /**
639
+ * Return a copy of `paramsNode` with the named params marked optional.
640
+ *
641
+ * Used to align signatures with the `enabled` guard: the guard already disables
642
+ * the query while a param is falsy, so the param should accept `undefined`. The
643
+ * change is type-only — the `queryFn` keeps calling the client with a non-null
644
+ * assertion (see `injectNonNullAssertions`), so the emitted runtime is unchanged.
645
+ */
646
+ function markParamsOptional(paramsNode, names) {
647
+ if (names.length === 0) return paramsNode;
648
+ const nameSet = new Set(names);
649
+ const params = paramsNode.params.map((param) => {
650
+ if ("kind" in param && param.kind === "ParameterGroup") {
651
+ const group = param;
652
+ return {
653
+ ...group,
654
+ properties: group.properties.map((child) => nameSet.has(child.name) ? _kubb_core.ast.createFunctionParameter({
655
+ name: child.name,
656
+ type: child.type,
657
+ rest: child.rest,
658
+ optional: true
659
+ }) : child)
660
+ };
661
+ }
662
+ const fp = param;
663
+ return nameSet.has(fp.name) ? _kubb_core.ast.createFunctionParameter({
664
+ name: fp.name,
665
+ type: fp.type,
666
+ rest: fp.rest,
667
+ optional: true
668
+ }) : fp;
669
+ });
670
+ return _kubb_core.ast.createFunctionParameters({ params });
671
+ }
672
+ /**
673
+ * Add a non-null assertion (`!`) to the named params inside a printed client-call
674
+ * string. Bridges the type gap created by `markParamsOptional` while keeping the
675
+ * runtime identical (the `!` is erased at compile time).
676
+ *
677
+ * Handles destructured shorthand groups (`{ petId }` → `{ petId: petId! }`) and
678
+ * standalone identifiers (`params` → `params!`).
679
+ */
680
+ function injectNonNullAssertions(callStr, names) {
681
+ if (names.length === 0) return callStr;
682
+ const nameSet = new Set(names);
683
+ let result = callStr.replace(/\{\s*([\w,\s]+)\s*\}(?=\s*,)/g, (match, inner) => {
684
+ if (inner.includes(":") || inner.includes("...")) return match;
685
+ const keys = inner.split(",").map((k) => k.trim()).filter(Boolean);
686
+ if (!keys.some((k) => nameSet.has(k))) return match;
687
+ return `{ ${keys.map((k) => nameSet.has(k) ? `${k}: ${k}!` : k).join(", ")} }`;
688
+ });
689
+ result = result.replace(/(?<![{.:?])\b(\w+)\b(?=\s*,)/g, (match, name) => nameSet.has(name) ? `${name}!` : match);
690
+ return result;
612
691
  }
613
692
  //#endregion
614
693
  //#region ../../internals/tanstack-query/src/components/QueryKey.tsx
615
694
  const declarationPrinter$9 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
616
695
  const queryKeyTransformer = ({ node, casing }) => {
696
+ if (!node.path) return [];
617
697
  const path = new URLPath(node.path, { casing });
618
698
  const hasQueryParams = getOperationParameters(node).query.length > 0;
619
699
  const hasRequestBody = !!node.requestBody?.content?.[0]?.schema;
@@ -627,11 +707,12 @@ const queryKeyTransformer = ({ node, casing }) => {
627
707
  ].filter(Boolean);
628
708
  };
629
709
  function QueryKey({ name, node, tsResolver, paramsCasing, pathParamsType, typeName, transformer }) {
630
- const paramsNode = buildQueryKeyParams(node, {
710
+ const baseParamsNode = buildQueryKeyParams(node, {
631
711
  pathParamsType,
632
712
  paramsCasing,
633
713
  resolver: tsResolver
634
714
  });
715
+ const paramsNode = markParamsOptional(baseParamsNode, getEnabledParamNames(baseParamsNode));
635
716
  const paramsSignature = declarationPrinter$9.print(paramsNode) ?? "";
636
717
  const keys = (transformer ?? queryKeyTransformer)({
637
718
  node,
@@ -679,28 +760,29 @@ function getQueryOptionsParams(node, options) {
679
760
  })]
680
761
  });
681
762
  }
682
- function QueryOptions({ name, clientName, dataReturnType, node, tsResolver, paramsCasing, paramsType, pathParamsType, queryKeyName }) {
763
+ function QueryOptions({ name, clientName, dataReturnType, node, tsResolver, paramsCasing, paramsType, pathParamsType, queryKeyName, suspense }) {
683
764
  const successNames = resolveSuccessNames(node, tsResolver);
684
765
  const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
685
766
  const errorNames = resolveErrorNames(node, tsResolver);
686
767
  const TData = dataReturnType === "data" ? responseName : `ResponseConfig<${responseName}>`;
687
768
  const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
688
- const paramsNode = getQueryOptionsParams(node, {
689
- paramsType,
690
- paramsCasing,
691
- pathParamsType,
692
- resolver: tsResolver
693
- });
694
- const paramsSignature = declarationPrinter$8.print(paramsNode) ?? "";
695
- const clientCallStr = (callPrinter$8.print(paramsNode) ?? "").replace(/\bconfig\b(?=[^,]*$)/, "{ ...config, signal: config.signal ?? signal }");
696
769
  const queryKeyParamsNode = buildQueryKeyParams(node, {
697
770
  pathParamsType,
698
771
  paramsCasing,
699
772
  resolver: tsResolver
700
773
  });
701
774
  const queryKeyParamsCall = callPrinter$8.print(queryKeyParamsNode) ?? "";
702
- const enabledSource = buildEnabledCheck(queryKeyParamsNode);
703
- const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : "";
775
+ const enabledNames = getEnabledParamNames(queryKeyParamsNode);
776
+ const optionalNames = suspense ? [] : enabledNames;
777
+ const enabledText = suspense ? "" : enabledNames.length ? `enabled: !!(${enabledNames.join(" && ")}),` : "";
778
+ const paramsNode = markParamsOptional(getQueryOptionsParams(node, {
779
+ paramsType,
780
+ paramsCasing,
781
+ pathParamsType,
782
+ resolver: tsResolver
783
+ }), optionalNames);
784
+ const paramsSignature = declarationPrinter$8.print(paramsNode) ?? "";
785
+ const clientCallStr = injectNonNullAssertions((callPrinter$8.print(paramsNode) ?? "").replace(/\bconfig\b(?=[^,]*$)/, "{ ...config, signal: config.signal ?? signal }"), optionalNames);
704
786
  return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
705
787
  name,
706
788
  isExportable: true,
@@ -780,6 +862,7 @@ function InfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName,
780
862
  resolver: tsResolver
781
863
  });
782
864
  const queryKeyParamsCall = callPrinter$7.print(queryKeyParamsNode) ?? "";
865
+ const enabledNames = getEnabledParamNames(queryKeyParamsNode);
783
866
  const queryOptionsParamsNode = getQueryOptionsParams(node, {
784
867
  paramsType,
785
868
  paramsCasing,
@@ -787,14 +870,14 @@ function InfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName,
787
870
  resolver: tsResolver
788
871
  });
789
872
  const queryOptionsParamsCall = callPrinter$7.print(queryOptionsParamsNode) ?? "";
790
- const paramsNode = buildInfiniteQueryParamsNode(node, {
873
+ const paramsNode = markParamsOptional(buildInfiniteQueryParamsNode(node, {
791
874
  paramsType,
792
875
  paramsCasing,
793
876
  pathParamsType,
794
877
  dataReturnType,
795
878
  resolver: tsResolver,
796
879
  pageParamGeneric: "TPageParam"
797
- });
880
+ }), enabledNames);
798
881
  const paramsSignature = declarationPrinter$7.print(paramsNode) ?? "";
799
882
  return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
800
883
  name,
@@ -848,22 +931,22 @@ function InfiniteQueryOptions({ name, clientName, initialPageParam, cursorParam,
848
931
  })() : null;
849
932
  const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null;
850
933
  const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType;
851
- const paramsNode = getQueryOptionsParams(node, {
852
- paramsType,
853
- paramsCasing,
854
- pathParamsType,
855
- resolver: tsResolver
856
- });
857
- const paramsSignature = declarationPrinter$6.print(paramsNode) ?? "";
858
- const clientCallStr = (callPrinter$6.print(paramsNode) ?? "").replace(/\bconfig\b(?=[^,]*$)/, "{ ...config, signal: config.signal ?? signal }");
859
934
  const queryKeyParamsNode = buildQueryKeyParams(node, {
860
935
  pathParamsType,
861
936
  paramsCasing,
862
937
  resolver: tsResolver
863
938
  });
864
939
  const queryKeyParamsCall = callPrinter$6.print(queryKeyParamsNode) ?? "";
865
- const enabledSource = buildEnabledCheck(queryKeyParamsNode);
866
- const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : "";
940
+ const enabledNames = getEnabledParamNames(queryKeyParamsNode);
941
+ const enabledText = enabledNames.length ? `enabled: !!(${enabledNames.join(" && ")}),` : "";
942
+ const paramsNode = markParamsOptional(getQueryOptionsParams(node, {
943
+ paramsType,
944
+ paramsCasing,
945
+ pathParamsType,
946
+ resolver: tsResolver
947
+ }), enabledNames);
948
+ const paramsSignature = declarationPrinter$6.print(paramsNode) ?? "";
949
+ const clientCallStr = injectNonNullAssertions((callPrinter$6.print(paramsNode) ?? "").replace(/\bconfig\b(?=[^,]*$)/, "{ ...config, signal: config.signal ?? signal }"), enabledNames);
867
950
  const hasNewParams = nextParam != null || previousParam != null;
868
951
  const [getNextPageParamExpr, getPreviousPageParamExpr] = (() => {
869
952
  if (hasNewParams) {
@@ -1145,6 +1228,7 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsT
1145
1228
  resolver: tsResolver
1146
1229
  });
1147
1230
  const queryKeyParamsCall = callPrinter$3.print(queryKeyParamsNode) ?? "";
1231
+ const enabledNames = getEnabledParamNames(queryKeyParamsNode);
1148
1232
  const queryOptionsParamsNode = getQueryOptionsParams(node, {
1149
1233
  paramsType,
1150
1234
  paramsCasing,
@@ -1152,13 +1236,13 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, paramsT
1152
1236
  resolver: tsResolver
1153
1237
  });
1154
1238
  const queryOptionsParamsCall = callPrinter$3.print(queryOptionsParamsNode) ?? "";
1155
- const paramsNode = buildQueryParamsNode(node, {
1239
+ const paramsNode = markParamsOptional(buildQueryParamsNode(node, {
1156
1240
  paramsType,
1157
1241
  paramsCasing,
1158
1242
  pathParamsType,
1159
1243
  dataReturnType,
1160
1244
  resolver: tsResolver
1161
- });
1245
+ }), enabledNames);
1162
1246
  const paramsSignature = declarationPrinter$3.print(paramsNode) ?? "";
1163
1247
  return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
1164
1248
  name,
@@ -1330,8 +1414,6 @@ function SuspenseInfiniteQueryOptions({ name, clientName, initialPageParam, curs
1330
1414
  resolver: tsResolver
1331
1415
  });
1332
1416
  const queryKeyParamsCall = callPrinter$1.print(queryKeyParamsNode) ?? "";
1333
- const enabledSource = buildEnabledCheck(queryKeyParamsNode);
1334
- const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : "";
1335
1417
  const hasNewParams = nextParam != null || previousParam != null;
1336
1418
  const [getNextPageParamExpr, getPreviousPageParamExpr] = (() => {
1337
1419
  if (hasNewParams) {
@@ -1363,7 +1445,6 @@ function SuspenseInfiniteQueryOptions({ name, clientName, initialPageParam, curs
1363
1445
  children: `
1364
1446
  const queryKey = ${queryKeyName}(${queryKeyParamsCall})
1365
1447
  return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
1366
- ${enabledText}
1367
1448
  queryKey,
1368
1449
  queryFn: async ({ signal, pageParam }) => {
1369
1450
  ${infiniteOverrideParams}
@@ -1385,7 +1466,6 @@ function SuspenseInfiniteQueryOptions({ name, clientName, initialPageParam, curs
1385
1466
  children: `
1386
1467
  const queryKey = ${queryKeyName}(${queryKeyParamsCall})
1387
1468
  return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
1388
- ${enabledText}
1389
1469
  queryKey,
1390
1470
  queryFn: async ({ signal }) => {
1391
1471
  return ${clientName}(${clientCallStr})
@@ -1587,6 +1667,12 @@ Object.defineProperty(exports, "mutationKeyTransformer", {
1587
1667
  return mutationKeyTransformer;
1588
1668
  }
1589
1669
  });
1670
+ Object.defineProperty(exports, "operationFileEntry", {
1671
+ enumerable: true,
1672
+ get: function() {
1673
+ return operationFileEntry;
1674
+ }
1675
+ });
1590
1676
  Object.defineProperty(exports, "queryKeyTransformer", {
1591
1677
  enumerable: true,
1592
1678
  get: function() {
@@ -1612,4 +1698,4 @@ Object.defineProperty(exports, "resolveZodSchemaNames", {
1612
1698
  }
1613
1699
  });
1614
1700
 
1615
- //# sourceMappingURL=components-MPBTffPl.cjs.map
1701
+ //# sourceMappingURL=components-DLUeLMsz.cjs.map