@tanstack/preact-query 5.102.2 → 5.102.4

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 (305) hide show
  1. package/build/legacy/HydrationBoundary.cjs +43 -0
  2. package/build/legacy/HydrationBoundary.cjs.map +1 -1
  3. package/build/legacy/HydrationBoundary.d.cts +61 -0
  4. package/build/legacy/HydrationBoundary.d.cts.map +1 -1
  5. package/build/legacy/HydrationBoundary.d.ts +61 -0
  6. package/build/legacy/HydrationBoundary.d.ts.map +1 -1
  7. package/build/legacy/HydrationBoundary.js +43 -0
  8. package/build/legacy/HydrationBoundary.js.map +1 -1
  9. package/build/legacy/IsRestoringProvider.cjs +11 -0
  10. package/build/legacy/IsRestoringProvider.cjs.map +1 -1
  11. package/build/legacy/IsRestoringProvider.d.cts +11 -0
  12. package/build/legacy/IsRestoringProvider.d.cts.map +1 -1
  13. package/build/legacy/IsRestoringProvider.d.ts +11 -0
  14. package/build/legacy/IsRestoringProvider.d.ts.map +1 -1
  15. package/build/legacy/IsRestoringProvider.js +11 -0
  16. package/build/legacy/IsRestoringProvider.js.map +1 -1
  17. package/build/legacy/QueryClientProvider.cjs +30 -0
  18. package/build/legacy/QueryClientProvider.cjs.map +1 -1
  19. package/build/legacy/QueryClientProvider.d.cts +41 -0
  20. package/build/legacy/QueryClientProvider.d.cts.map +1 -1
  21. package/build/legacy/QueryClientProvider.d.ts +41 -0
  22. package/build/legacy/QueryClientProvider.d.ts.map +1 -1
  23. package/build/legacy/QueryClientProvider.js +30 -0
  24. package/build/legacy/QueryClientProvider.js.map +1 -1
  25. package/build/legacy/QueryErrorResetBoundary.cjs +75 -0
  26. package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
  27. package/build/legacy/QueryErrorResetBoundary.d.cts +106 -0
  28. package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
  29. package/build/legacy/QueryErrorResetBoundary.d.ts +106 -0
  30. package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
  31. package/build/legacy/QueryErrorResetBoundary.js +75 -0
  32. package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
  33. package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
  34. package/build/legacy/infiniteQueryOptions.d.cts +187 -0
  35. package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
  36. package/build/legacy/infiniteQueryOptions.d.ts +187 -0
  37. package/build/legacy/infiniteQueryOptions.d.ts.map +1 -1
  38. package/build/legacy/infiniteQueryOptions.js.map +1 -1
  39. package/build/legacy/mutationOptions.cjs.map +1 -1
  40. package/build/legacy/mutationOptions.d.cts +68 -0
  41. package/build/legacy/mutationOptions.d.cts.map +1 -1
  42. package/build/legacy/mutationOptions.d.ts +68 -0
  43. package/build/legacy/mutationOptions.d.ts.map +1 -1
  44. package/build/legacy/mutationOptions.js.map +1 -1
  45. package/build/legacy/queryOptions.cjs.map +1 -1
  46. package/build/legacy/queryOptions.d.cts +198 -0
  47. package/build/legacy/queryOptions.d.cts.map +1 -1
  48. package/build/legacy/queryOptions.d.ts +198 -0
  49. package/build/legacy/queryOptions.d.ts.map +1 -1
  50. package/build/legacy/queryOptions.js.map +1 -1
  51. package/build/legacy/types.cjs.map +1 -1
  52. package/build/legacy/types.d.cts +228 -2
  53. package/build/legacy/types.d.cts.map +1 -1
  54. package/build/legacy/types.d.ts +228 -2
  55. package/build/legacy/types.d.ts.map +1 -1
  56. package/build/legacy/types.js.map +1 -1
  57. package/build/legacy/useInfiniteQuery.cjs.map +1 -1
  58. package/build/legacy/useInfiniteQuery.d.cts +116 -0
  59. package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
  60. package/build/legacy/useInfiniteQuery.d.ts +116 -0
  61. package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
  62. package/build/legacy/useInfiniteQuery.js.map +1 -1
  63. package/build/legacy/useIsFetching.cjs +20 -0
  64. package/build/legacy/useIsFetching.cjs.map +1 -1
  65. package/build/legacy/useIsFetching.d.cts +20 -0
  66. package/build/legacy/useIsFetching.d.cts.map +1 -1
  67. package/build/legacy/useIsFetching.d.ts +20 -0
  68. package/build/legacy/useIsFetching.d.ts.map +1 -1
  69. package/build/legacy/useIsFetching.js +20 -0
  70. package/build/legacy/useIsFetching.js.map +1 -1
  71. package/build/legacy/useMutation.cjs +67 -0
  72. package/build/legacy/useMutation.cjs.map +1 -1
  73. package/build/legacy/useMutation.d.cts +67 -0
  74. package/build/legacy/useMutation.d.cts.map +1 -1
  75. package/build/legacy/useMutation.d.ts +67 -0
  76. package/build/legacy/useMutation.d.ts.map +1 -1
  77. package/build/legacy/useMutation.js +67 -0
  78. package/build/legacy/useMutation.js.map +1 -1
  79. package/build/legacy/useMutationState.cjs +74 -0
  80. package/build/legacy/useMutationState.cjs.map +1 -1
  81. package/build/legacy/useMutationState.d.cts +74 -0
  82. package/build/legacy/useMutationState.d.cts.map +1 -1
  83. package/build/legacy/useMutationState.d.ts +74 -0
  84. package/build/legacy/useMutationState.d.ts.map +1 -1
  85. package/build/legacy/useMutationState.js +74 -0
  86. package/build/legacy/useMutationState.js.map +1 -1
  87. package/build/legacy/usePrefetchInfiniteQuery.cjs +44 -0
  88. package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
  89. package/build/legacy/usePrefetchInfiniteQuery.d.cts +44 -0
  90. package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
  91. package/build/legacy/usePrefetchInfiniteQuery.d.ts +44 -0
  92. package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
  93. package/build/legacy/usePrefetchInfiniteQuery.js +44 -0
  94. package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
  95. package/build/legacy/usePrefetchQuery.cjs +36 -1
  96. package/build/legacy/usePrefetchQuery.cjs.map +1 -1
  97. package/build/legacy/usePrefetchQuery.d.cts +35 -0
  98. package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
  99. package/build/legacy/usePrefetchQuery.d.ts +35 -0
  100. package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
  101. package/build/legacy/usePrefetchQuery.js +36 -1
  102. package/build/legacy/usePrefetchQuery.js.map +1 -1
  103. package/build/legacy/useQueries.cjs +50 -0
  104. package/build/legacy/useQueries.cjs.map +1 -1
  105. package/build/legacy/useQueries.d.cts +85 -2
  106. package/build/legacy/useQueries.d.cts.map +1 -1
  107. package/build/legacy/useQueries.d.ts +85 -2
  108. package/build/legacy/useQueries.d.ts.map +1 -1
  109. package/build/legacy/useQueries.js +50 -0
  110. package/build/legacy/useQueries.js.map +1 -1
  111. package/build/legacy/useQuery.cjs.map +1 -1
  112. package/build/legacy/useQuery.d.cts +134 -0
  113. package/build/legacy/useQuery.d.cts.map +1 -1
  114. package/build/legacy/useQuery.d.ts +134 -0
  115. package/build/legacy/useQuery.d.ts.map +1 -1
  116. package/build/legacy/useQuery.js.map +1 -1
  117. package/build/legacy/useSuspenseInfiniteQuery.cjs +48 -0
  118. package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
  119. package/build/legacy/useSuspenseInfiniteQuery.d.cts +48 -0
  120. package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
  121. package/build/legacy/useSuspenseInfiniteQuery.d.ts +48 -0
  122. package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
  123. package/build/legacy/useSuspenseInfiniteQuery.js +48 -0
  124. package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
  125. package/build/legacy/useSuspenseQueries.cjs.map +1 -1
  126. package/build/legacy/useSuspenseQueries.d.cts +128 -2
  127. package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
  128. package/build/legacy/useSuspenseQueries.d.ts +128 -2
  129. package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
  130. package/build/legacy/useSuspenseQueries.js.map +1 -1
  131. package/build/legacy/useSuspenseQuery.cjs +43 -0
  132. package/build/legacy/useSuspenseQuery.cjs.map +1 -1
  133. package/build/legacy/useSuspenseQuery.d.cts +43 -0
  134. package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
  135. package/build/legacy/useSuspenseQuery.d.ts +43 -0
  136. package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
  137. package/build/legacy/useSuspenseQuery.js +43 -0
  138. package/build/legacy/useSuspenseQuery.js.map +1 -1
  139. package/build/legacy/utils.cjs.map +1 -1
  140. package/build/legacy/utils.d.cts.map +1 -1
  141. package/build/legacy/utils.d.ts.map +1 -1
  142. package/build/legacy/utils.js.map +1 -1
  143. package/build/modern/HydrationBoundary.cjs +43 -0
  144. package/build/modern/HydrationBoundary.cjs.map +1 -1
  145. package/build/modern/HydrationBoundary.d.cts +61 -0
  146. package/build/modern/HydrationBoundary.d.cts.map +1 -1
  147. package/build/modern/HydrationBoundary.d.ts +61 -0
  148. package/build/modern/HydrationBoundary.d.ts.map +1 -1
  149. package/build/modern/HydrationBoundary.js +43 -0
  150. package/build/modern/HydrationBoundary.js.map +1 -1
  151. package/build/modern/IsRestoringProvider.cjs +11 -0
  152. package/build/modern/IsRestoringProvider.cjs.map +1 -1
  153. package/build/modern/IsRestoringProvider.d.cts +11 -0
  154. package/build/modern/IsRestoringProvider.d.cts.map +1 -1
  155. package/build/modern/IsRestoringProvider.d.ts +11 -0
  156. package/build/modern/IsRestoringProvider.d.ts.map +1 -1
  157. package/build/modern/IsRestoringProvider.js +11 -0
  158. package/build/modern/IsRestoringProvider.js.map +1 -1
  159. package/build/modern/QueryClientProvider.cjs +30 -0
  160. package/build/modern/QueryClientProvider.cjs.map +1 -1
  161. package/build/modern/QueryClientProvider.d.cts +41 -0
  162. package/build/modern/QueryClientProvider.d.cts.map +1 -1
  163. package/build/modern/QueryClientProvider.d.ts +41 -0
  164. package/build/modern/QueryClientProvider.d.ts.map +1 -1
  165. package/build/modern/QueryClientProvider.js +30 -0
  166. package/build/modern/QueryClientProvider.js.map +1 -1
  167. package/build/modern/QueryErrorResetBoundary.cjs +75 -0
  168. package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
  169. package/build/modern/QueryErrorResetBoundary.d.cts +106 -0
  170. package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
  171. package/build/modern/QueryErrorResetBoundary.d.ts +106 -0
  172. package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
  173. package/build/modern/QueryErrorResetBoundary.js +75 -0
  174. package/build/modern/QueryErrorResetBoundary.js.map +1 -1
  175. package/build/modern/infiniteQueryOptions.cjs.map +1 -1
  176. package/build/modern/infiniteQueryOptions.d.cts +187 -0
  177. package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
  178. package/build/modern/infiniteQueryOptions.d.ts +187 -0
  179. package/build/modern/infiniteQueryOptions.d.ts.map +1 -1
  180. package/build/modern/infiniteQueryOptions.js.map +1 -1
  181. package/build/modern/mutationOptions.cjs.map +1 -1
  182. package/build/modern/mutationOptions.d.cts +68 -0
  183. package/build/modern/mutationOptions.d.cts.map +1 -1
  184. package/build/modern/mutationOptions.d.ts +68 -0
  185. package/build/modern/mutationOptions.d.ts.map +1 -1
  186. package/build/modern/mutationOptions.js.map +1 -1
  187. package/build/modern/queryOptions.cjs.map +1 -1
  188. package/build/modern/queryOptions.d.cts +198 -0
  189. package/build/modern/queryOptions.d.cts.map +1 -1
  190. package/build/modern/queryOptions.d.ts +198 -0
  191. package/build/modern/queryOptions.d.ts.map +1 -1
  192. package/build/modern/queryOptions.js.map +1 -1
  193. package/build/modern/types.cjs.map +1 -1
  194. package/build/modern/types.d.cts +228 -2
  195. package/build/modern/types.d.cts.map +1 -1
  196. package/build/modern/types.d.ts +228 -2
  197. package/build/modern/types.d.ts.map +1 -1
  198. package/build/modern/types.js.map +1 -1
  199. package/build/modern/useInfiniteQuery.cjs.map +1 -1
  200. package/build/modern/useInfiniteQuery.d.cts +116 -0
  201. package/build/modern/useInfiniteQuery.d.cts.map +1 -1
  202. package/build/modern/useInfiniteQuery.d.ts +116 -0
  203. package/build/modern/useInfiniteQuery.d.ts.map +1 -1
  204. package/build/modern/useInfiniteQuery.js.map +1 -1
  205. package/build/modern/useIsFetching.cjs +20 -0
  206. package/build/modern/useIsFetching.cjs.map +1 -1
  207. package/build/modern/useIsFetching.d.cts +20 -0
  208. package/build/modern/useIsFetching.d.cts.map +1 -1
  209. package/build/modern/useIsFetching.d.ts +20 -0
  210. package/build/modern/useIsFetching.d.ts.map +1 -1
  211. package/build/modern/useIsFetching.js +20 -0
  212. package/build/modern/useIsFetching.js.map +1 -1
  213. package/build/modern/useMutation.cjs +67 -0
  214. package/build/modern/useMutation.cjs.map +1 -1
  215. package/build/modern/useMutation.d.cts +67 -0
  216. package/build/modern/useMutation.d.cts.map +1 -1
  217. package/build/modern/useMutation.d.ts +67 -0
  218. package/build/modern/useMutation.d.ts.map +1 -1
  219. package/build/modern/useMutation.js +67 -0
  220. package/build/modern/useMutation.js.map +1 -1
  221. package/build/modern/useMutationState.cjs +74 -0
  222. package/build/modern/useMutationState.cjs.map +1 -1
  223. package/build/modern/useMutationState.d.cts +74 -0
  224. package/build/modern/useMutationState.d.cts.map +1 -1
  225. package/build/modern/useMutationState.d.ts +74 -0
  226. package/build/modern/useMutationState.d.ts.map +1 -1
  227. package/build/modern/useMutationState.js +74 -0
  228. package/build/modern/useMutationState.js.map +1 -1
  229. package/build/modern/usePrefetchInfiniteQuery.cjs +44 -0
  230. package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
  231. package/build/modern/usePrefetchInfiniteQuery.d.cts +44 -0
  232. package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
  233. package/build/modern/usePrefetchInfiniteQuery.d.ts +44 -0
  234. package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
  235. package/build/modern/usePrefetchInfiniteQuery.js +44 -0
  236. package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
  237. package/build/modern/usePrefetchQuery.cjs +36 -1
  238. package/build/modern/usePrefetchQuery.cjs.map +1 -1
  239. package/build/modern/usePrefetchQuery.d.cts +35 -0
  240. package/build/modern/usePrefetchQuery.d.cts.map +1 -1
  241. package/build/modern/usePrefetchQuery.d.ts +35 -0
  242. package/build/modern/usePrefetchQuery.d.ts.map +1 -1
  243. package/build/modern/usePrefetchQuery.js +36 -1
  244. package/build/modern/usePrefetchQuery.js.map +1 -1
  245. package/build/modern/useQueries.cjs +50 -0
  246. package/build/modern/useQueries.cjs.map +1 -1
  247. package/build/modern/useQueries.d.cts +85 -2
  248. package/build/modern/useQueries.d.cts.map +1 -1
  249. package/build/modern/useQueries.d.ts +85 -2
  250. package/build/modern/useQueries.d.ts.map +1 -1
  251. package/build/modern/useQueries.js +50 -0
  252. package/build/modern/useQueries.js.map +1 -1
  253. package/build/modern/useQuery.cjs.map +1 -1
  254. package/build/modern/useQuery.d.cts +134 -0
  255. package/build/modern/useQuery.d.cts.map +1 -1
  256. package/build/modern/useQuery.d.ts +134 -0
  257. package/build/modern/useQuery.d.ts.map +1 -1
  258. package/build/modern/useQuery.js.map +1 -1
  259. package/build/modern/useSuspenseInfiniteQuery.cjs +48 -0
  260. package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
  261. package/build/modern/useSuspenseInfiniteQuery.d.cts +48 -0
  262. package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
  263. package/build/modern/useSuspenseInfiniteQuery.d.ts +48 -0
  264. package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
  265. package/build/modern/useSuspenseInfiniteQuery.js +48 -0
  266. package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
  267. package/build/modern/useSuspenseQueries.cjs.map +1 -1
  268. package/build/modern/useSuspenseQueries.d.cts +128 -2
  269. package/build/modern/useSuspenseQueries.d.cts.map +1 -1
  270. package/build/modern/useSuspenseQueries.d.ts +128 -2
  271. package/build/modern/useSuspenseQueries.d.ts.map +1 -1
  272. package/build/modern/useSuspenseQueries.js.map +1 -1
  273. package/build/modern/useSuspenseQuery.cjs +43 -0
  274. package/build/modern/useSuspenseQuery.cjs.map +1 -1
  275. package/build/modern/useSuspenseQuery.d.cts +43 -0
  276. package/build/modern/useSuspenseQuery.d.cts.map +1 -1
  277. package/build/modern/useSuspenseQuery.d.ts +43 -0
  278. package/build/modern/useSuspenseQuery.d.ts.map +1 -1
  279. package/build/modern/useSuspenseQuery.js +43 -0
  280. package/build/modern/useSuspenseQuery.js.map +1 -1
  281. package/build/modern/utils.cjs.map +1 -1
  282. package/build/modern/utils.d.cts.map +1 -1
  283. package/build/modern/utils.d.ts.map +1 -1
  284. package/build/modern/utils.js.map +1 -1
  285. package/package.json +3 -3
  286. package/src/HydrationBoundary.tsx +61 -1
  287. package/src/IsRestoringProvider.ts +12 -0
  288. package/src/QueryClientProvider.tsx +41 -0
  289. package/src/QueryErrorResetBoundary.tsx +109 -0
  290. package/src/infiniteQueryOptions.ts +187 -0
  291. package/src/mutationOptions.ts +68 -0
  292. package/src/queryOptions.ts +198 -0
  293. package/src/types.ts +228 -2
  294. package/src/useInfiniteQuery.ts +116 -0
  295. package/src/useIsFetching.ts +20 -0
  296. package/src/useMutation.ts +67 -0
  297. package/src/useMutationState.ts +74 -0
  298. package/src/usePrefetchInfiniteQuery.tsx +44 -0
  299. package/src/usePrefetchQuery.tsx +36 -1
  300. package/src/useQueries.ts +85 -2
  301. package/src/useQuery.ts +134 -0
  302. package/src/useSuspenseInfiniteQuery.ts +48 -0
  303. package/src/useSuspenseQueries.ts +128 -2
  304. package/src/useSuspenseQuery.ts +43 -0
  305. package/src/utils.ts +0 -1
@@ -40,16 +40,99 @@ type GetUseQueryResult<T> = T extends {
40
40
  throwOnError?: ThrowOnError<any, infer TError, any, any>;
41
41
  } ? GetDefinedOrUndefinedQueryResult<T, unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError> : UseQueryResult;
42
42
  /**
43
- * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
43
+ * The `queries` array accepted by `useQueries`. Recursively unwraps each tuple element so every entry's
44
+ * `queryFn`/`select`/`throwOnError` are inferred individually, up to 20 elements. An opaque array (e.g.
45
+ * `unknown[]`) is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls
46
+ * back to a single homogeneous options type.
47
+ *
48
+ * @template T - The type of the `queries` array as written at the call site.
49
+ * @template TResults - The internal accumulator that this type builds during recursion. It is not meant
50
+ * to be set explicitly.
51
+ * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
52
+ * meant to be set explicitly.
44
53
  */
45
54
  type QueriesOptions<T extends Array<any>, TResults extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<UseQueryOptionsForUseQueries> : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetUseQueryOptionsForUseQueries<Head>] : T extends [infer Head, ...infer Tails] ? QueriesOptions<[...Tails], [...TResults, GetUseQueryOptionsForUseQueries<Head>], [...TDepth, 1]> : ReadonlyArray<unknown> extends T ? T : T extends Array<UseQueryOptionsForUseQueries<infer TQueryFnData, infer TError, infer TData, infer TQueryKey>> ? Array<UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>> : Array<UseQueryOptionsForUseQueries>;
46
55
  /**
47
- * QueriesResults reducer recursively maps type param to results
56
+ * The result type returned by `useQueries`, when no `combine` is provided. Mirrors {@link QueriesOptions}: each
57
+ * tuple element's result type is inferred individually, up to 20 elements. A non-tuple array is mapped
58
+ * per-element instead, still inferring each entry individually; only past 20 elements does this fall back to a
59
+ * single homogeneous {@link UseQueryResult} type.
60
+ *
61
+ * @template T - The type of the `queries` array, as inferred by {@link QueriesOptions}.
62
+ * @template TResults - The internal accumulator that this type builds during recursion. It is not meant
63
+ * to be set explicitly.
64
+ * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
65
+ * meant to be set explicitly.
48
66
  */
49
67
  type QueriesResults<T extends Array<any>, TResults extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<UseQueryResult> : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetUseQueryResult<Head>] : T extends [infer Head, ...infer Tails] ? QueriesResults<[...Tails], [...TResults, GetUseQueryResult<Head>], [...TDepth, 1]> : { [K in keyof T]: GetUseQueryResult<T[K]>; };
68
+ /**
69
+ * The `useQueries` hook can be used to fetch a variable number of queries.
70
+ *
71
+ * The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the
72
+ * `queryClient` option - because the `QueryClient` can be passed in on the top level).
73
+ *
74
+ * Having the same query key more than once in the array of query objects may cause some data to be shared
75
+ * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
76
+ * structure.
77
+ *
78
+ * The `combine` option can be used to combine the results of the queries into a single value. The result will
79
+ * be structurally shared to be as referentially stable as possible.
80
+ *
81
+ * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
82
+ * will be used.
83
+ * @returns The combined result. Without `combine`, this is an array with all the query results, in the same
84
+ * order as the input. When `combine` is provided, this is the value returned by `combine` instead.
85
+ *
86
+ * @example
87
+ * ```tsx
88
+ * import { useQueries } from '@tanstack/preact-query'
89
+ *
90
+ * const ids = [1, 2, 3]
91
+ * const results = useQueries({
92
+ * queries: ids.map((id) => ({
93
+ * queryKey: ['post', id],
94
+ * queryFn: () => fetchPost(id),
95
+ * staleTime: Infinity,
96
+ * })),
97
+ * })
98
+ * ```
99
+ *
100
+ * @example
101
+ * Combining results into a single value:
102
+ * ```tsx
103
+ * const ids = [1, 2, 3]
104
+ * const combinedQueries = useQueries({
105
+ * queries: ids.map((id) => ({
106
+ * queryKey: ['post', id],
107
+ * queryFn: () => fetchPost(id),
108
+ * })),
109
+ * combine: (results) => {
110
+ * return {
111
+ * data: results.map((result) => result.data),
112
+ * pending: results.some((result) => result.isPending),
113
+ * }
114
+ * },
115
+ * })
116
+ * ```
117
+ */
50
118
  declare function useQueries<T extends Array<any>, TCombinedResult = QueriesResults<T>>({ queries, ...options }: {
119
+ /**
120
+ * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
121
+ * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
122
+ * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
123
+ * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
124
+ */
51
125
  queries: readonly [...QueriesOptions<T>] | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]>; }];
126
+ /**
127
+ * Use this to combine the results of the queries into a single value. The result will be structurally
128
+ * shared to be as referentially stable as possible.
129
+ */
52
130
  combine?: (result: QueriesResults<T>) => TCombinedResult;
131
+ /**
132
+ * Set this to `false` to unsubscribe this observer from updates to the query cache.
133
+ *
134
+ * @defaultValue true
135
+ */
53
136
  subscribed?: boolean;
54
137
  }, queryClient?: QueryClient): TCombinedResult;
55
138
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useQueries.d.cts","names":[],"sources":["../../src/useQueries.ts"],"mappings":";;;KAyCK,6BACH,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,YAC3B,UACF,gBAAgB,cAAc,QAAQ,OAAO;EAG7C,kBAAkB,eAAe,+BAA+B;;KAI7D;KAGA;KAEA,gCAAgC,KAEnC;EACE,mBAAmB;EACnB,cAAc;EACd,YAAY;IAEV,6BAA6B,cAAc,QAAQ,SACnD;EAAY,mBAAmB;EAAc,cAAc;IACzD,6BAA6B,cAAc,UAC3C;EAAY,YAAY;EAAO,cAAc;IAC3C,sCAAsC,QAAQ,SAE9C,iBAAiB,oBAAoB,cAAc,SACjD,6BAA6B,cAAc,QAAQ,SACnD,iBAAiB,oBAAoB,UACnC,6BAA6B,cAAc,UAC3C,iBAAiB,gBACf,6BAA6B,gBAE7B;EACI,UACI,oBAAoB,oBAAoB,aACxC;EACJ,UAAU,oBAAoB;EAC9B,eAAe,wBAAwB;IAEzC,6BACE,8BACgB,SAAS,eAAe,wBACxB,QAAQ,eAAe,OACvC,aAGF;KAGb,iCAAiC,GAAG,OAAO,oBAAoB;EAClE,oBAAoB;oBAEF,eACd,eA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C3C,iBAAiB,oBAAoB,UACnC,iCAAiC,GAAG,cAAc,UAClD,iBAAiB,gBACf,iCAAiC,GAAG,gBAEpC;EACI,UACI,oBAAoB,qBACpB;EACJ,UAAU,oBAAoB;EAC9B,eAAe,wBAAwB;IAEzC,iCACE,mBACgB,QAAQ,eAAe,uBACvB,SAAS,eAAe,UAG1C;;;;;;;;;;;;;KAcN,eACV,UAAU,YACV,iBAAiB,iBACjB,eAAe,8BACb,yBAAyB,gBACzB,MAAM,gCACN,oBAEE,iBAAiB,YACX,UAAU,gCAAgC,SAC9C,iBAAiB,eAAe,SAC9B,mBACM,YACA,UAAU,gCAAgC,YAC1C,cAEN,+BAA+B,IAC7B,IAGA,UAAU,MACN,mCACQ,oBACA,cACA,aACA,cAGV,MACE,6BACE,cACA,QACA,OACA,cAIJ,MAAM;;;;;;;;;;;;;KAcR,eACV,UAAU,YACV,iBAAiB,iBACjB,eAAe,8BACb,yBAAyB,gBACzB,MAAM,kBACN,oBAEE,iBAAiB,YACX,UAAU,kBAAkB,SAChC,iBAAiB,eAAe,SAC9B,mBACM,YACA,UAAU,kBAAkB,YAC5B,iBAEH,WAAW,IAAI,kBAAkB,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoDhC,WACd,UAAU,YACV,kBAAkB,eAAe,MAG/B,YACG;;;;;;;EAQH,sBACiB,eAAe,sBACZ,WAAW,IAAI,gCAAgC,EAAE;;;;;EAKrE,WAAW,QAAQ,eAAe,OAAO;;;;;;EAMzC;GAEF,cAAc,cACb"}
@@ -40,16 +40,99 @@ type GetUseQueryResult<T> = T extends {
40
40
  throwOnError?: ThrowOnError<any, infer TError, any, any>;
41
41
  } ? GetDefinedOrUndefinedQueryResult<T, unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError> : UseQueryResult;
42
42
  /**
43
- * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
43
+ * The `queries` array accepted by `useQueries`. Recursively unwraps each tuple element so every entry's
44
+ * `queryFn`/`select`/`throwOnError` are inferred individually, up to 20 elements. An opaque array (e.g.
45
+ * `unknown[]`) is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls
46
+ * back to a single homogeneous options type.
47
+ *
48
+ * @template T - The type of the `queries` array as written at the call site.
49
+ * @template TResults - The internal accumulator that this type builds during recursion. It is not meant
50
+ * to be set explicitly.
51
+ * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
52
+ * meant to be set explicitly.
44
53
  */
45
54
  type QueriesOptions<T extends Array<any>, TResults extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<UseQueryOptionsForUseQueries> : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetUseQueryOptionsForUseQueries<Head>] : T extends [infer Head, ...infer Tails] ? QueriesOptions<[...Tails], [...TResults, GetUseQueryOptionsForUseQueries<Head>], [...TDepth, 1]> : ReadonlyArray<unknown> extends T ? T : T extends Array<UseQueryOptionsForUseQueries<infer TQueryFnData, infer TError, infer TData, infer TQueryKey>> ? Array<UseQueryOptionsForUseQueries<TQueryFnData, TError, TData, TQueryKey>> : Array<UseQueryOptionsForUseQueries>;
46
55
  /**
47
- * QueriesResults reducer recursively maps type param to results
56
+ * The result type returned by `useQueries`, when no `combine` is provided. Mirrors {@link QueriesOptions}: each
57
+ * tuple element's result type is inferred individually, up to 20 elements. A non-tuple array is mapped
58
+ * per-element instead, still inferring each entry individually; only past 20 elements does this fall back to a
59
+ * single homogeneous {@link UseQueryResult} type.
60
+ *
61
+ * @template T - The type of the `queries` array, as inferred by {@link QueriesOptions}.
62
+ * @template TResults - The internal accumulator that this type builds during recursion. It is not meant
63
+ * to be set explicitly.
64
+ * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
65
+ * meant to be set explicitly.
48
66
  */
49
67
  type QueriesResults<T extends Array<any>, TResults extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<UseQueryResult> : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetUseQueryResult<Head>] : T extends [infer Head, ...infer Tails] ? QueriesResults<[...Tails], [...TResults, GetUseQueryResult<Head>], [...TDepth, 1]> : { [K in keyof T]: GetUseQueryResult<T[K]>; };
68
+ /**
69
+ * The `useQueries` hook can be used to fetch a variable number of queries.
70
+ *
71
+ * The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the
72
+ * `queryClient` option - because the `QueryClient` can be passed in on the top level).
73
+ *
74
+ * Having the same query key more than once in the array of query objects may cause some data to be shared
75
+ * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
76
+ * structure.
77
+ *
78
+ * The `combine` option can be used to combine the results of the queries into a single value. The result will
79
+ * be structurally shared to be as referentially stable as possible.
80
+ *
81
+ * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
82
+ * will be used.
83
+ * @returns The combined result. Without `combine`, this is an array with all the query results, in the same
84
+ * order as the input. When `combine` is provided, this is the value returned by `combine` instead.
85
+ *
86
+ * @example
87
+ * ```tsx
88
+ * import { useQueries } from '@tanstack/preact-query'
89
+ *
90
+ * const ids = [1, 2, 3]
91
+ * const results = useQueries({
92
+ * queries: ids.map((id) => ({
93
+ * queryKey: ['post', id],
94
+ * queryFn: () => fetchPost(id),
95
+ * staleTime: Infinity,
96
+ * })),
97
+ * })
98
+ * ```
99
+ *
100
+ * @example
101
+ * Combining results into a single value:
102
+ * ```tsx
103
+ * const ids = [1, 2, 3]
104
+ * const combinedQueries = useQueries({
105
+ * queries: ids.map((id) => ({
106
+ * queryKey: ['post', id],
107
+ * queryFn: () => fetchPost(id),
108
+ * })),
109
+ * combine: (results) => {
110
+ * return {
111
+ * data: results.map((result) => result.data),
112
+ * pending: results.some((result) => result.isPending),
113
+ * }
114
+ * },
115
+ * })
116
+ * ```
117
+ */
50
118
  declare function useQueries<T extends Array<any>, TCombinedResult = QueriesResults<T>>({ queries, ...options }: {
119
+ /**
120
+ * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
121
+ * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
122
+ * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
123
+ * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
124
+ */
51
125
  queries: readonly [...QueriesOptions<T>] | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]>; }];
126
+ /**
127
+ * Use this to combine the results of the queries into a single value. The result will be structurally
128
+ * shared to be as referentially stable as possible.
129
+ */
52
130
  combine?: (result: QueriesResults<T>) => TCombinedResult;
131
+ /**
132
+ * Set this to `false` to unsubscribe this observer from updates to the query cache.
133
+ *
134
+ * @defaultValue true
135
+ */
53
136
  subscribed?: boolean;
54
137
  }, queryClient?: QueryClient): TCombinedResult;
55
138
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useQueries.d.ts","names":[],"sources":["../../src/useQueries.ts"],"mappings":";;;KAyCK,6BACH,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,YAC3B,UACF,gBAAgB,cAAc,QAAQ,OAAO;EAG7C,kBAAkB,eAAe,+BAA+B;;KAI7D;KAGA;KAEA,gCAAgC,KAEnC;EACE,mBAAmB;EACnB,cAAc;EACd,YAAY;IAEV,6BAA6B,cAAc,QAAQ,SACnD;EAAY,mBAAmB;EAAc,cAAc;IACzD,6BAA6B,cAAc,UAC3C;EAAY,YAAY;EAAO,cAAc;IAC3C,sCAAsC,QAAQ,SAE9C,iBAAiB,oBAAoB,cAAc,SACjD,6BAA6B,cAAc,QAAQ,SACnD,iBAAiB,oBAAoB,UACnC,6BAA6B,cAAc,UAC3C,iBAAiB,gBACf,6BAA6B,gBAE7B;EACI,UACI,oBAAoB,oBAAoB,aACxC;EACJ,UAAU,oBAAoB;EAC9B,eAAe,wBAAwB;IAEzC,6BACE,8BACgB,SAAS,eAAe,wBACxB,QAAQ,eAAe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AC3C,iCAAiC,GAAG,OAAO,UAE3C,sBAAsB,cAAc,SAClC,iCAAiC,GAAG,OAAO,UAC3C,iBAAiB,oBAAoB,UACnC,iCAAiC,GAAG,cAAc,UAClD,iBAAiB,gBACf,iCAAiC,GAAG,gBAEpC;EACI,UACI,oBAAoB,qBACpB;EACJ,UAAU,oBAAoB;EAC9B,eAAe,wBAAwB;IAEzC,iCACE,mBACgB,QAAQ,eAAe,uBACvB,SAAS,eAAe,UAG1C;;;;;;;;;;;;;KAcN,eACV,UAAU,YACV,iBAAiB,iBACjB,eAAe,8BACb,yBAAyB,gBACzB,MAAM,gCACN,oBAEE,iBAAiB,YACX,UAAU,gCAAgC,SAC9C,iBAAiB,eAAe,SAC9B,mBACM,YACA,UAAU,gCAAgC,YAC1C,cAEN,+BAA+B,IAC7B,IAGA,UAAU,MACN,mCACQ,oBACA,cACA,aACA,cAGV,MACE,6BACE,cACA,QACA,OACA,cAIJ,MAAM;;;;;;;;;;;;;KAcR,eACV,UAAU,YACV,iBAAiB,iBACjB,eAAe,8BACb,yBAAyB,gBACzB,MAAM,kBACN,oBAEE,iBAAiB,YACX,UAAU,kBAAkB,SAChC,iBAAiB,eAAe,SAC9B,mBACM,YACA,UAAU,kBAAkB,YAC5B,iBAEH,WAAW,IAAI,kBAAkB,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoDhC,WACd,UAAU,YACV,kBAAkB,eAAe,MAG/B,YACG;;;;;;;EAQH,sBACiB,eAAe,sBACZ,WAAW,IAAI,gCAAgC,EAAE;;;;;EAKrE,WAAW,QAAQ,eAAe,OAAO;;;;;;EAMzC;GAEF,cAAc,cACb"}
@@ -7,6 +7,56 @@ import { useSyncExternalStore } from "./utils.js";
7
7
  import { QueriesObserver, QueryObserver, noop, notifyManager } from "@tanstack/query-core";
8
8
  import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
9
9
  //#region src/useQueries.ts
10
+ /**
11
+ * The `useQueries` hook can be used to fetch a variable number of queries.
12
+ *
13
+ * The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the
14
+ * `queryClient` option - because the `QueryClient` can be passed in on the top level).
15
+ *
16
+ * Having the same query key more than once in the array of query objects may cause some data to be shared
17
+ * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
18
+ * structure.
19
+ *
20
+ * The `combine` option can be used to combine the results of the queries into a single value. The result will
21
+ * be structurally shared to be as referentially stable as possible.
22
+ *
23
+ * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
24
+ * will be used.
25
+ * @returns The combined result. Without `combine`, this is an array with all the query results, in the same
26
+ * order as the input. When `combine` is provided, this is the value returned by `combine` instead.
27
+ *
28
+ * @example
29
+ * ```tsx
30
+ * import { useQueries } from '@tanstack/preact-query'
31
+ *
32
+ * const ids = [1, 2, 3]
33
+ * const results = useQueries({
34
+ * queries: ids.map((id) => ({
35
+ * queryKey: ['post', id],
36
+ * queryFn: () => fetchPost(id),
37
+ * staleTime: Infinity,
38
+ * })),
39
+ * })
40
+ * ```
41
+ *
42
+ * @example
43
+ * Combining results into a single value:
44
+ * ```tsx
45
+ * const ids = [1, 2, 3]
46
+ * const combinedQueries = useQueries({
47
+ * queries: ids.map((id) => ({
48
+ * queryKey: ['post', id],
49
+ * queryFn: () => fetchPost(id),
50
+ * })),
51
+ * combine: (results) => {
52
+ * return {
53
+ * data: results.map((result) => result.data),
54
+ * pending: results.some((result) => result.isPending),
55
+ * }
56
+ * },
57
+ * })
58
+ * ```
59
+ */
10
60
  function useQueries({ queries, ...options }, queryClient) {
11
61
  const client = useQueryClient(queryClient);
12
62
  const isRestoring = useIsRestoring();
@@ -1 +1 @@
1
- {"version":3,"file":"useQueries.js","names":[],"sources":["../../src/useQueries.ts"],"sourcesContent":["import {\n QueriesObserver,\n QueryObserver,\n noop,\n notifyManager,\n} from '@tanstack/query-core'\nimport type {\n DefaultError,\n OmitKeyof,\n QueriesObserverOptions,\n QueriesPlaceholderDataFunction,\n QueryClient,\n QueryFunction,\n QueryKey,\n QueryObserverOptions,\n ThrowOnError,\n} from '@tanstack/query-core'\nimport { useCallback, useEffect, useMemo, useState } from 'preact/hooks'\n\nimport { useIsRestoring } from './IsRestoringProvider'\nimport { useQueryClient } from './QueryClientProvider'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureSuspenseTimers,\n fetchOptimistic,\n shouldSuspend,\n} from './suspense'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useSyncExternalStore } from './utils'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function always gets undefined passed\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData' | 'subscribed'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetUseQueryOptionsForUseQueries<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : // Fallback\n UseQueryOptionsForUseQueries\n\n// A defined initialData setting should return a DefinedUseQueryResult rather than UseQueryResult\ntype GetDefinedOrUndefinedQueryResult<T, TData, TError = unknown> = T extends {\n initialData?: infer TInitialData\n}\n ? unknown extends TInitialData\n ? UseQueryResult<TData, TError>\n : TInitialData extends TData\n ? DefinedUseQueryResult<TData, TError>\n : TInitialData extends () => infer TInitialDataResult\n ? unknown extends TInitialDataResult\n ? UseQueryResult<TData, TError>\n : TInitialDataResult extends TData\n ? DefinedUseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n\ntype GetUseQueryResult<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n UseQueryResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type QueriesOptions<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryOptionsForUseQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryOptionsForUseQueries<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesOptions<\n [...Tails],\n [...TResults, GetUseQueryOptionsForUseQueries<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogeneous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<UseQueryOptionsForUseQueries>\n\n/**\n * QueriesResults reducer recursively maps type param to results\n */\nexport type QueriesResults<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryResult<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesResults<\n [...Tails],\n [...TResults, GetUseQueryResult<Head>],\n [...TDepth, 1]\n >\n : { [K in keyof T]: GetUseQueryResult<T[K]> }\n\nexport function useQueries<\n T extends Array<any>,\n TCombinedResult = QueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: {\n queries:\n | readonly [...QueriesOptions<T>]\n | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]\n combine?: (result: QueriesResults<T>) => TCombinedResult\n subscribed?: boolean\n },\n queryClient?: QueryClient,\n): TCombinedResult {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n const defaultedQueries = useMemo(\n () =>\n queries.map((opts) => {\n const defaultedOptions = client.defaultQueryOptions(\n opts as QueryObserverOptions,\n )\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n defaultedQueries.forEach((queryOptions) => {\n ensureSuspenseTimers(queryOptions)\n const query = client.getQueryCache().get(queryOptions.queryHash)\n ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const [observer] = useState(\n () =>\n new QueriesObserver<TCombinedResult>(\n client,\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n ),\n )\n\n // note: this must be called before useSyncExternalStore\n const [optimisticResult, getCombinedResult, trackResult] =\n observer.getOptimisticResult(\n defaultedQueries,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n const shouldSubscribe = !isRestoring && options.subscribed !== false\n useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n shouldSubscribe\n ? observer.subscribe(notifyManager.batchCalls(onStoreChange))\n : noop,\n [observer, shouldSubscribe],\n ),\n () => observer.getCurrentResult(),\n )\n\n useEffect(() => {\n observer.setQueries(\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n }, [defaultedQueries, options, observer])\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const opts = defaultedQueries[index]\n\n if (opts && shouldSuspend(opts, result)) {\n const queryObserver = new QueryObserver(client, opts)\n return fetchOptimistic(opts, queryObserver, errorResetBoundary)\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) => {\n const query = defaultedQueries[index]\n return (\n query &&\n getHasError({\n result,\n errorResetBoundary,\n throwOnError: query.throwOnError,\n query: client.getQueryCache().get(query.queryHash),\n suspense: query.suspense,\n })\n )\n },\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return getCombinedResult(trackResult())\n}\n"],"mappings":";;;;;;;;;AA8MA,SAAgB,WAId,EACE,SACA,GAAG,WAQL,aACiB;CACjB,MAAM,SAAS,eAAe,WAAW;CACzC,MAAM,cAAc,eAAe;CACnC,MAAM,qBAAqB,2BAA2B;CAEtD,MAAM,mBAAmB,cAErB,QAAQ,KAAK,SAAS;EACpB,MAAM,mBAAmB,OAAO,oBAC9B,IACF;EAGA,iBAAiB,qBAAqB,cAClC,gBACA;EAEJ,OAAO;CACT,CAAC,GACH;EAAC;EAAS;EAAQ;CAAW,CAC/B;CAEA,iBAAiB,SAAS,iBAAiB;EACzC,qBAAqB,YAAY;EACjC,MAAM,QAAQ,OAAO,cAAc,CAAC,CAAC,IAAI,aAAa,SAAS;EAC/D,gCAAgC,cAAc,oBAAoB,KAAK;CACzE,CAAC;CAED,2BAA2B,kBAAkB;CAE7C,MAAM,CAAC,YAAY,eAEf,IAAI,gBACF,QACA,kBACA,OACF,CACJ;CAGA,MAAM,CAAC,kBAAkB,mBAAmB,eAC1C,SAAS,oBACP,kBACC,QAAoD,OACvD;CAEF,MAAM,kBAAkB,CAAC,eAAe,QAAQ,eAAe;CAC/D,qBACE,aACG,kBACC,kBACI,SAAS,UAAU,cAAc,WAAW,aAAa,CAAC,IAC1D,MACN,CAAC,UAAU,eAAe,CAC5B,SACM,SAAS,iBAAiB,CAClC;CAEA,gBAAgB;EACd,SAAS,WACP,kBACA,OACF;CACF,GAAG;EAAC;EAAkB;EAAS;CAAQ,CAAC;CAMxC,MAAM,mBAJ0B,iBAAiB,MAAM,QAAQ,UAC7D,cAAc,iBAAiB,QAAQ,MAAM,CAGA,IAC3C,iBAAiB,SAAS,QAAQ,UAAU;EAC1C,MAAM,OAAO,iBAAiB;EAE9B,IAAI,QAAQ,cAAc,MAAM,MAAM,GAAG;GACvC,MAAM,gBAAgB,IAAI,cAAc,QAAQ,IAAI;GACpD,OAAO,gBAAgB,MAAM,eAAe,kBAAkB;EAChE;EACA,OAAO,CAAC;CACV,CAAC,IACD,CAAC;CAEL,IAAI,iBAAiB,SAAS,GAC5B,MAAM,QAAQ,IAAI,gBAAgB;CAEpC,MAAM,oCAAoC,iBAAiB,MACxD,QAAQ,UAAU;EACjB,MAAM,QAAQ,iBAAiB;EAC/B,OACE,SACA,YAAY;GACV;GACA;GACA,cAAc,MAAM;GACpB,OAAO,OAAO,cAAc,CAAC,CAAC,IAAI,MAAM,SAAS;GACjD,UAAU,MAAM;EAClB,CAAC;CAEL,CACF;CAEA,IAAA,sCAAA,QAAA,sCAAA,KAAA,IAAA,KAAA,IAAI,kCAAmC,OACrC,MAAM,kCAAkC;CAG1C,OAAO,kBAAkB,YAAY,CAAC;AACxC"}
1
+ {"version":3,"file":"useQueries.js","names":[],"sources":["../../src/useQueries.ts"],"sourcesContent":["import {\n QueriesObserver,\n QueryObserver,\n noop,\n notifyManager,\n} from '@tanstack/query-core'\nimport type {\n DefaultError,\n OmitKeyof,\n QueriesObserverOptions,\n QueriesPlaceholderDataFunction,\n QueryClient,\n QueryFunction,\n QueryKey,\n QueryObserverOptions,\n ThrowOnError,\n} from '@tanstack/query-core'\nimport { useCallback, useEffect, useMemo, useState } from 'preact/hooks'\n\nimport { useIsRestoring } from './IsRestoringProvider'\nimport { useQueryClient } from './QueryClientProvider'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureSuspenseTimers,\n fetchOptimistic,\n shouldSuspend,\n} from './suspense'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useSyncExternalStore } from './utils'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function always gets undefined passed\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData' | 'subscribed'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetUseQueryOptionsForUseQueries<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : // Fallback\n UseQueryOptionsForUseQueries\n\n// A defined initialData setting should return a DefinedUseQueryResult rather than UseQueryResult\ntype GetDefinedOrUndefinedQueryResult<T, TData, TError = unknown> = T extends {\n initialData?: infer TInitialData\n}\n ? unknown extends TInitialData\n ? UseQueryResult<TData, TError>\n : TInitialData extends TData\n ? DefinedUseQueryResult<TData, TError>\n : TInitialData extends () => infer TInitialDataResult\n ? unknown extends TInitialDataResult\n ? UseQueryResult<TData, TError>\n : TInitialDataResult extends TData\n ? DefinedUseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n\ntype GetUseQueryResult<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n UseQueryResult\n\n/**\n * The `queries` array accepted by `useQueries`. Recursively unwraps each tuple element so every entry's\n * `queryFn`/`select`/`throwOnError` are inferred individually, up to 20 elements. An opaque array (e.g.\n * `unknown[]`) is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls\n * back to a single homogeneous options type.\n *\n * @template T - The type of the `queries` array as written at the call site.\n * @template TResults - The internal accumulator that this type builds during recursion. It is not meant\n * to be set explicitly.\n * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not\n * meant to be set explicitly.\n */\nexport type QueriesOptions<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryOptionsForUseQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryOptionsForUseQueries<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesOptions<\n [...Tails],\n [...TResults, GetUseQueryOptionsForUseQueries<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogeneous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<UseQueryOptionsForUseQueries>\n\n/**\n * The result type returned by `useQueries`, when no `combine` is provided. Mirrors {@link QueriesOptions}: each\n * tuple element's result type is inferred individually, up to 20 elements. A non-tuple array is mapped\n * per-element instead, still inferring each entry individually; only past 20 elements does this fall back to a\n * single homogeneous {@link UseQueryResult} type.\n *\n * @template T - The type of the `queries` array, as inferred by {@link QueriesOptions}.\n * @template TResults - The internal accumulator that this type builds during recursion. It is not meant\n * to be set explicitly.\n * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not\n * meant to be set explicitly.\n */\nexport type QueriesResults<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryResult<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesResults<\n [...Tails],\n [...TResults, GetUseQueryResult<Head>],\n [...TDepth, 1]\n >\n : { [K in keyof T]: GetUseQueryResult<T[K]> }\n\n/**\n * The `useQueries` hook can be used to fetch a variable number of queries.\n *\n * The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the\n * `queryClient` option - because the `QueryClient` can be passed in on the top level).\n *\n * Having the same query key more than once in the array of query objects may cause some data to be shared\n * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired\n * structure.\n *\n * The `combine` option can be used to combine the results of the queries into a single value. The result will\n * be structurally shared to be as referentially stable as possible.\n *\n * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context\n * will be used.\n * @returns The combined result. Without `combine`, this is an array with all the query results, in the same\n * order as the input. When `combine` is provided, this is the value returned by `combine` instead.\n *\n * @example\n * ```tsx\n * import { useQueries } from '@tanstack/preact-query'\n *\n * const ids = [1, 2, 3]\n * const results = useQueries({\n * queries: ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * staleTime: Infinity,\n * })),\n * })\n * ```\n *\n * @example\n * Combining results into a single value:\n * ```tsx\n * const ids = [1, 2, 3]\n * const combinedQueries = useQueries({\n * queries: ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * })),\n * combine: (results) => {\n * return {\n * data: results.map((result) => result.data),\n * pending: results.some((result) => result.isPending),\n * }\n * },\n * })\n * ```\n */\nexport function useQueries<\n T extends Array<any>,\n TCombinedResult = QueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: {\n /**\n * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and\n * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and\n * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`\n * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.\n */\n queries:\n | readonly [...QueriesOptions<T>]\n | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]\n /**\n * Use this to combine the results of the queries into a single value. The result will be structurally\n * shared to be as referentially stable as possible.\n */\n combine?: (result: QueriesResults<T>) => TCombinedResult\n /**\n * Set this to `false` to unsubscribe this observer from updates to the query cache.\n *\n * @defaultValue true\n */\n subscribed?: boolean\n },\n queryClient?: QueryClient,\n): TCombinedResult {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n const defaultedQueries = useMemo(\n () =>\n queries.map((opts) => {\n const defaultedOptions = client.defaultQueryOptions(\n opts as QueryObserverOptions,\n )\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n defaultedQueries.forEach((queryOptions) => {\n ensureSuspenseTimers(queryOptions)\n const query = client.getQueryCache().get(queryOptions.queryHash)\n ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const [observer] = useState(\n () =>\n new QueriesObserver<TCombinedResult>(\n client,\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n ),\n )\n\n // note: this must be called before useSyncExternalStore\n const [optimisticResult, getCombinedResult, trackResult] =\n observer.getOptimisticResult(\n defaultedQueries,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n const shouldSubscribe = !isRestoring && options.subscribed !== false\n useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n shouldSubscribe\n ? observer.subscribe(notifyManager.batchCalls(onStoreChange))\n : noop,\n [observer, shouldSubscribe],\n ),\n () => observer.getCurrentResult(),\n )\n\n useEffect(() => {\n observer.setQueries(\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n }, [defaultedQueries, options, observer])\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const opts = defaultedQueries[index]\n\n if (opts && shouldSuspend(opts, result)) {\n const queryObserver = new QueryObserver(client, opts)\n return fetchOptimistic(opts, queryObserver, errorResetBoundary)\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) => {\n const query = defaultedQueries[index]\n return (\n query &&\n getHasError({\n result,\n errorResetBoundary,\n throwOnError: query.throwOnError,\n query: client.getQueryCache().get(query.queryHash),\n suspense: query.suspense,\n })\n )\n },\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return getCombinedResult(trackResult())\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkRA,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S,GAC5B,MAAM,QAAQ,IAAI,gBAAgB;CAEpC,MAAM,oCAAoC,iBAAiB,MACxD,QAAQ,UAAU;EACjB,MAAM,QAAQ,iBAAiB;EAC/B,OACE,SACA,YAAY;GACV;GACA;GACA,cAAc,MAAM;GACpB,OAAO,OAAO,cAAc,CAAC,CAAC,IAAI,MAAM,SAAS;GACjD,UAAU,MAAM;EAClB,CAAC;CAEL,CACF;CAEA,IAAA,sCAAA,QAAA,sCAAA,KAAA,IAAA,KAAA,IAAI,kCAAmC,OACrC,MAAM,kCAAkC;CAG1C,OAAO,kBAAkB,YAAY,CAAC;AACxC"}
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.cjs","names":["useBaseQuery","QueryObserver"],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\n\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './queryOptions'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useBaseQuery } from './useBaseQuery'\n\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): DefinedUseQueryResult<TData, TError>\n\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryResult<TData, TError>\n\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryResult<TData, TError>\n\nexport function useQuery(options: UseQueryOptions, queryClient?: QueryClient) {\n return useBaseQuery(options, QueryObserver, queryClient)\n}\n"],"mappings":";;;;AA4CA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAOA,qBAAAA,aAAa,SAASC,qBAAAA,eAAe,WAAW;AACzD"}
1
+ {"version":3,"file":"useQuery.cjs","names":["useBaseQuery","QueryObserver"],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\n\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './queryOptions'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useBaseQuery } from './useBaseQuery'\n\n/**\n * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.\n *\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` set.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt\n * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,\n * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Posts() {\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData`.\n * const { data } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n *\n * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\n * }\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): DefinedUseQueryResult<TData, TError>\n\n/**\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result. `status` is `pending` if there is no cached data and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\n * }\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryResult<TData, TError>\n\n/**\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result. `status` is `pending` if there is no cached data and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * A dependent query, only enabled once `postId` is set:\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number | undefined }) {\n * const { data } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId!),\n * enabled: postId != null,\n * })\n *\n * return <h1>{data?.title}</h1>\n * }\n * ```\n *\n * @example\n * Seeding a detail query from an already-cached list, to skip the loading state:\n * ```tsx\n * import { useQuery, useQueryClient } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number }) {\n * const queryClient = useQueryClient()\n *\n * const { data } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId),\n * initialData: () =>\n * queryClient\n * .getQueryData<Array<Post>>(['posts'])\n * ?.find((post) => post.id === postId),\n * })\n *\n * return <h1>{data?.title}</h1>\n * }\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryResult<TData, TError>\n\nexport function useQuery(options: UseQueryOptions, queryClient?: QueryClient) {\n return useBaseQuery(options, QueryObserver, queryClient)\n}\n"],"mappings":";;;;AAkLA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAOA,qBAAAA,aAAa,SAASC,qBAAAA,eAAe,WAAW;AACzD"}
@@ -2,8 +2,142 @@ import { DefinedUseQueryResult, UseQueryOptions, UseQueryResult } from "./types.
2
2
  import { DefinedInitialDataOptions, UndefinedInitialDataOptions } from "./queryOptions.cjs";
3
3
  import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
4
4
  //#region src/useQuery.d.ts
5
+ /**
6
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
7
+ *
8
+ * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
9
+ * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` set.
10
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
11
+ * be used.
12
+ * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
13
+ * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
14
+ * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
15
+ *
16
+ * @example
17
+ * ```tsx
18
+ * import { useQuery } from '@tanstack/preact-query'
19
+ *
20
+ * function Posts() {
21
+ * // `data` is `Post[]`, never `undefined`, thanks to `initialData`.
22
+ * const { data } = useQuery({
23
+ * queryKey: ['posts'],
24
+ * queryFn: fetchPosts,
25
+ * initialData: [],
26
+ * })
27
+ *
28
+ * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>
29
+ * }
30
+ * ```
31
+ */
5
32
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): DefinedUseQueryResult<TData, TError>;
33
+ /**
34
+ * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
35
+ * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
36
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
37
+ * be used.
38
+ * @returns The current query result. `status` is `pending` if there is no cached data and no query attempt
39
+ * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to
40
+ * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.
41
+ *
42
+ * @example
43
+ * ```tsx
44
+ * import { queryOptions, useQuery } from '@tanstack/preact-query'
45
+ *
46
+ * const postsOptions = queryOptions({
47
+ * queryKey: ['posts'],
48
+ * queryFn: fetchPosts,
49
+ * })
50
+ *
51
+ * function Posts() {
52
+ * const { status, data, error, isFetching } = useQuery(postsOptions)
53
+ *
54
+ * if (status === 'pending') return 'Loading...'
55
+ * if (status === 'error') return <span>Error: {error.message}</span>
56
+ *
57
+ * return (
58
+ * <div>
59
+ * {data.map((post) => (
60
+ * <p key={post.id}>{post.title}</p>
61
+ * ))}
62
+ * <div>{isFetching ? 'Background Updating...' : ' '}</div>
63
+ * </div>
64
+ * )
65
+ * }
66
+ * ```
67
+ */
6
68
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
69
+ /**
70
+ * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
71
+ * @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.
72
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
73
+ * be used.
74
+ * @returns The current query result. `status` is `pending` if there is no cached data and no query attempt
75
+ * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to
76
+ * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.
77
+ *
78
+ * @example
79
+ * ```tsx
80
+ * import { queryOptions, useQuery } from '@tanstack/preact-query'
81
+ *
82
+ * const postsOptions = queryOptions({
83
+ * queryKey: ['posts'],
84
+ * queryFn: fetchPosts,
85
+ * })
86
+ *
87
+ * function Posts() {
88
+ * const { status, data, error, isFetching } = useQuery(postsOptions)
89
+ *
90
+ * if (status === 'pending') return 'Loading...'
91
+ * if (status === 'error') return <span>Error: {error.message}</span>
92
+ *
93
+ * return (
94
+ * <div>
95
+ * {data.map((post) => (
96
+ * <p key={post.id}>{post.title}</p>
97
+ * ))}
98
+ * <div>{isFetching ? 'Background Updating...' : ' '}</div>
99
+ * </div>
100
+ * )
101
+ * }
102
+ * ```
103
+ *
104
+ * @example
105
+ * A dependent query, only enabled once `postId` is set:
106
+ * ```tsx
107
+ * import { useQuery } from '@tanstack/preact-query'
108
+ *
109
+ * function Post({ postId }: { postId: number | undefined }) {
110
+ * const { data } = useQuery({
111
+ * queryKey: ['post', postId],
112
+ * queryFn: () => fetchPost(postId!),
113
+ * enabled: postId != null,
114
+ * })
115
+ *
116
+ * return <h1>{data?.title}</h1>
117
+ * }
118
+ * ```
119
+ *
120
+ * @example
121
+ * Seeding a detail query from an already-cached list, to skip the loading state:
122
+ * ```tsx
123
+ * import { useQuery, useQueryClient } from '@tanstack/preact-query'
124
+ *
125
+ * function Post({ postId }: { postId: number }) {
126
+ * const queryClient = useQueryClient()
127
+ *
128
+ * const { data } = useQuery({
129
+ * queryKey: ['post', postId],
130
+ * queryFn: () => fetchPost(postId),
131
+ * initialData: () =>
132
+ * queryClient
133
+ * .getQueryData<Array<Post>>(['posts'])
134
+ * ?.find((post) => post.id === postId),
135
+ * })
136
+ *
137
+ * return <h1>{data?.title}</h1>
138
+ * }
139
+ * ```
140
+ */
7
141
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
8
142
  //#endregion
9
143
  export { useQuery };
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.d.cts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;iBAcgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;iBAEhB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;iBAET,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}
1
+ {"version":3,"file":"useQuery.d.cts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqChB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0ET,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}