@tanstack/preact-query 0.0.1 → 5.91.0

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 (338) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -45
  3. package/build/codemods/src/utils/index.cjs +208 -0
  4. package/build/codemods/src/utils/transformers/query-cache-transformer.cjs +124 -0
  5. package/build/codemods/src/utils/transformers/query-client-transformer.cjs +53 -0
  6. package/build/codemods/src/utils/transformers/use-query-like-transformer.cjs +38 -0
  7. package/build/codemods/src/v4/key-transformation.cjs +181 -0
  8. package/build/codemods/src/v4/replace-import-specifier.cjs +25 -0
  9. package/build/codemods/src/v4/utils/replacers/key-replacer.cjs +164 -0
  10. package/build/codemods/src/v5/is-loading/is-loading.cjs +244 -0
  11. package/build/codemods/src/v5/keep-previous-data/README.md +32 -0
  12. package/build/codemods/src/v5/keep-previous-data/keep-previous-data.cjs +271 -0
  13. package/build/codemods/src/v5/keep-previous-data/utils/already-has-placeholder-data-property.cjs +26 -0
  14. package/build/codemods/src/v5/remove-overloads/remove-overloads.cjs +58 -0
  15. package/build/codemods/src/v5/remove-overloads/transformers/filter-aware-usage-transformer.cjs +271 -0
  16. package/build/codemods/src/v5/remove-overloads/transformers/query-fn-aware-usage-transformer.cjs +185 -0
  17. package/build/codemods/src/v5/remove-overloads/utils/index.cjs +123 -0
  18. package/build/codemods/src/v5/remove-overloads/utils/unknown-usage-error.cjs +27 -0
  19. package/build/codemods/src/v5/rename-hydrate/rename-hydrate.cjs +55 -0
  20. package/build/codemods/src/v5/rename-properties/rename-properties.cjs +41 -0
  21. package/build/legacy/HydrationBoundary.cjs +80 -0
  22. package/build/legacy/HydrationBoundary.cjs.map +1 -0
  23. package/build/legacy/HydrationBoundary.d.cts +14 -0
  24. package/build/legacy/HydrationBoundary.d.ts +14 -0
  25. package/build/legacy/HydrationBoundary.js +55 -0
  26. package/build/legacy/HydrationBoundary.js.map +1 -0
  27. package/build/legacy/IsRestoringProvider.cjs +37 -0
  28. package/build/legacy/IsRestoringProvider.cjs.map +1 -0
  29. package/build/legacy/IsRestoringProvider.d.cts +6 -0
  30. package/build/legacy/IsRestoringProvider.d.ts +6 -0
  31. package/build/legacy/IsRestoringProvider.js +11 -0
  32. package/build/legacy/IsRestoringProvider.js.map +1 -0
  33. package/build/legacy/QueryClientProvider.cjs +62 -0
  34. package/build/legacy/QueryClientProvider.cjs.map +1 -0
  35. package/build/legacy/QueryClientProvider.d.cts +13 -0
  36. package/build/legacy/QueryClientProvider.d.ts +13 -0
  37. package/build/legacy/QueryClientProvider.js +35 -0
  38. package/build/legacy/QueryClientProvider.js.map +1 -0
  39. package/build/legacy/QueryErrorResetBoundary.cjs +57 -0
  40. package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -0
  41. package/build/legacy/QueryErrorResetBoundary.d.cts +19 -0
  42. package/build/legacy/QueryErrorResetBoundary.d.ts +19 -0
  43. package/build/legacy/QueryErrorResetBoundary.js +31 -0
  44. package/build/legacy/QueryErrorResetBoundary.js.map +1 -0
  45. package/build/legacy/errorBoundaryUtils.cjs +57 -0
  46. package/build/legacy/errorBoundaryUtils.cjs.map +1 -0
  47. package/build/legacy/errorBoundaryUtils.d.cts +15 -0
  48. package/build/legacy/errorBoundaryUtils.d.ts +15 -0
  49. package/build/legacy/errorBoundaryUtils.js +30 -0
  50. package/build/legacy/errorBoundaryUtils.js.map +1 -0
  51. package/build/legacy/index.cjs +97 -0
  52. package/build/legacy/index.cjs.map +1 -0
  53. package/build/legacy/index.d.cts +21 -0
  54. package/build/legacy/index.d.ts +21 -0
  55. package/build/legacy/index.js +54 -0
  56. package/build/legacy/index.js.map +1 -0
  57. package/build/legacy/infiniteQueryOptions.cjs +33 -0
  58. package/build/legacy/infiniteQueryOptions.cjs.map +1 -0
  59. package/build/legacy/infiniteQueryOptions.d.cts +23 -0
  60. package/build/legacy/infiniteQueryOptions.d.ts +23 -0
  61. package/build/legacy/infiniteQueryOptions.js +8 -0
  62. package/build/legacy/infiniteQueryOptions.js.map +1 -0
  63. package/build/legacy/mutationOptions.cjs +33 -0
  64. package/build/legacy/mutationOptions.cjs.map +1 -0
  65. package/build/legacy/mutationOptions.d.cts +7 -0
  66. package/build/legacy/mutationOptions.d.ts +7 -0
  67. package/build/legacy/mutationOptions.js +8 -0
  68. package/build/legacy/mutationOptions.js.map +1 -0
  69. package/build/legacy/queryOptions.cjs +33 -0
  70. package/build/legacy/queryOptions.cjs.map +1 -0
  71. package/build/legacy/queryOptions.d.cts +24 -0
  72. package/build/legacy/queryOptions.d.ts +24 -0
  73. package/build/legacy/queryOptions.js +8 -0
  74. package/build/legacy/queryOptions.js.map +1 -0
  75. package/build/legacy/suspense.cjs +58 -0
  76. package/build/legacy/suspense.cjs.map +1 -0
  77. package/build/legacy/suspense.d.cts +11 -0
  78. package/build/legacy/suspense.d.ts +11 -0
  79. package/build/legacy/suspense.js +29 -0
  80. package/build/legacy/suspense.js.map +1 -0
  81. package/build/legacy/types.cjs +19 -0
  82. package/build/legacy/types.cjs.map +1 -0
  83. package/build/legacy/types.d.cts +52 -0
  84. package/build/legacy/types.d.ts +52 -0
  85. package/build/legacy/types.js +1 -0
  86. package/build/legacy/types.js.map +1 -0
  87. package/build/legacy/useBaseQuery.cjs +121 -0
  88. package/build/legacy/useBaseQuery.cjs.map +1 -0
  89. package/build/legacy/useBaseQuery.d.cts +6 -0
  90. package/build/legacy/useBaseQuery.d.ts +6 -0
  91. package/build/legacy/useBaseQuery.js +105 -0
  92. package/build/legacy/useBaseQuery.js.map +1 -0
  93. package/build/legacy/useInfiniteQuery.cjs +39 -0
  94. package/build/legacy/useInfiniteQuery.cjs.map +1 -0
  95. package/build/legacy/useInfiniteQuery.d.cts +9 -0
  96. package/build/legacy/useInfiniteQuery.d.ts +9 -0
  97. package/build/legacy/useInfiniteQuery.js +14 -0
  98. package/build/legacy/useInfiniteQuery.js.map +1 -0
  99. package/build/legacy/useIsFetching.cjs +45 -0
  100. package/build/legacy/useIsFetching.cjs.map +1 -0
  101. package/build/legacy/useIsFetching.d.cts +5 -0
  102. package/build/legacy/useIsFetching.d.ts +5 -0
  103. package/build/legacy/useIsFetching.js +20 -0
  104. package/build/legacy/useIsFetching.js.map +1 -0
  105. package/build/legacy/useMutation.cjs +63 -0
  106. package/build/legacy/useMutation.cjs.map +1 -0
  107. package/build/legacy/useMutation.d.cts +6 -0
  108. package/build/legacy/useMutation.d.ts +6 -0
  109. package/build/legacy/useMutation.js +43 -0
  110. package/build/legacy/useMutation.js.map +1 -0
  111. package/build/legacy/useMutationState.cjs +75 -0
  112. package/build/legacy/useMutationState.cjs.map +1 -0
  113. package/build/legacy/useMutationState.d.cts +10 -0
  114. package/build/legacy/useMutationState.d.ts +10 -0
  115. package/build/legacy/useMutationState.js +49 -0
  116. package/build/legacy/useMutationState.js.map +1 -0
  117. package/build/legacy/usePrefetchInfiniteQuery.cjs +37 -0
  118. package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -0
  119. package/build/legacy/usePrefetchInfiniteQuery.d.cts +5 -0
  120. package/build/legacy/usePrefetchInfiniteQuery.d.ts +5 -0
  121. package/build/legacy/usePrefetchInfiniteQuery.js +12 -0
  122. package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -0
  123. package/build/legacy/usePrefetchQuery.cjs +37 -0
  124. package/build/legacy/usePrefetchQuery.cjs.map +1 -0
  125. package/build/legacy/usePrefetchQuery.d.cts +6 -0
  126. package/build/legacy/usePrefetchQuery.d.ts +6 -0
  127. package/build/legacy/usePrefetchQuery.js +12 -0
  128. package/build/legacy/usePrefetchQuery.js.map +1 -0
  129. package/build/legacy/useQueries.cjs +120 -0
  130. package/build/legacy/useQueries.cjs.map +1 -0
  131. package/build/legacy/useQueries.d.cts +76 -0
  132. package/build/legacy/useQueries.d.ts +76 -0
  133. package/build/legacy/useQueries.js +109 -0
  134. package/build/legacy/useQueries.js.map +1 -0
  135. package/build/legacy/useQuery.cjs +35 -0
  136. package/build/legacy/useQuery.cjs.map +1 -0
  137. package/build/legacy/useQuery.d.cts +9 -0
  138. package/build/legacy/useQuery.d.ts +9 -0
  139. package/build/legacy/useQuery.js +10 -0
  140. package/build/legacy/useQuery.js.map +1 -0
  141. package/build/legacy/useSuspenseInfiniteQuery.cjs +50 -0
  142. package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -0
  143. package/build/legacy/useSuspenseInfiniteQuery.d.cts +6 -0
  144. package/build/legacy/useSuspenseInfiniteQuery.d.ts +6 -0
  145. package/build/legacy/useSuspenseInfiniteQuery.js +25 -0
  146. package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -0
  147. package/build/legacy/useSuspenseQueries.cjs +55 -0
  148. package/build/legacy/useSuspenseQueries.cjs.map +1 -0
  149. package/build/legacy/useSuspenseQueries.d.cts +79 -0
  150. package/build/legacy/useSuspenseQueries.d.ts +79 -0
  151. package/build/legacy/useSuspenseQueries.js +30 -0
  152. package/build/legacy/useSuspenseQueries.js.map +1 -0
  153. package/build/legacy/useSuspenseQuery.cjs +51 -0
  154. package/build/legacy/useSuspenseQuery.cjs.map +1 -0
  155. package/build/legacy/useSuspenseQuery.d.cts +6 -0
  156. package/build/legacy/useSuspenseQuery.d.ts +6 -0
  157. package/build/legacy/useSuspenseQuery.js +26 -0
  158. package/build/legacy/useSuspenseQuery.js.map +1 -0
  159. package/build/legacy/utils.cjs +79 -0
  160. package/build/legacy/utils.cjs.map +1 -0
  161. package/build/legacy/utils.d.cts +4 -0
  162. package/build/legacy/utils.d.ts +4 -0
  163. package/build/legacy/utils.js +53 -0
  164. package/build/legacy/utils.js.map +1 -0
  165. package/build/modern/HydrationBoundary.cjs +80 -0
  166. package/build/modern/HydrationBoundary.cjs.map +1 -0
  167. package/build/modern/HydrationBoundary.d.cts +14 -0
  168. package/build/modern/HydrationBoundary.d.ts +14 -0
  169. package/build/modern/HydrationBoundary.js +55 -0
  170. package/build/modern/HydrationBoundary.js.map +1 -0
  171. package/build/modern/IsRestoringProvider.cjs +37 -0
  172. package/build/modern/IsRestoringProvider.cjs.map +1 -0
  173. package/build/modern/IsRestoringProvider.d.cts +6 -0
  174. package/build/modern/IsRestoringProvider.d.ts +6 -0
  175. package/build/modern/IsRestoringProvider.js +11 -0
  176. package/build/modern/IsRestoringProvider.js.map +1 -0
  177. package/build/modern/QueryClientProvider.cjs +62 -0
  178. package/build/modern/QueryClientProvider.cjs.map +1 -0
  179. package/build/modern/QueryClientProvider.d.cts +13 -0
  180. package/build/modern/QueryClientProvider.d.ts +13 -0
  181. package/build/modern/QueryClientProvider.js +35 -0
  182. package/build/modern/QueryClientProvider.js.map +1 -0
  183. package/build/modern/QueryErrorResetBoundary.cjs +57 -0
  184. package/build/modern/QueryErrorResetBoundary.cjs.map +1 -0
  185. package/build/modern/QueryErrorResetBoundary.d.cts +19 -0
  186. package/build/modern/QueryErrorResetBoundary.d.ts +19 -0
  187. package/build/modern/QueryErrorResetBoundary.js +31 -0
  188. package/build/modern/QueryErrorResetBoundary.js.map +1 -0
  189. package/build/modern/errorBoundaryUtils.cjs +57 -0
  190. package/build/modern/errorBoundaryUtils.cjs.map +1 -0
  191. package/build/modern/errorBoundaryUtils.d.cts +15 -0
  192. package/build/modern/errorBoundaryUtils.d.ts +15 -0
  193. package/build/modern/errorBoundaryUtils.js +30 -0
  194. package/build/modern/errorBoundaryUtils.js.map +1 -0
  195. package/build/modern/index.cjs +97 -0
  196. package/build/modern/index.cjs.map +1 -0
  197. package/build/modern/index.d.cts +21 -0
  198. package/build/modern/index.d.ts +21 -0
  199. package/build/modern/index.js +54 -0
  200. package/build/modern/index.js.map +1 -0
  201. package/build/modern/infiniteQueryOptions.cjs +33 -0
  202. package/build/modern/infiniteQueryOptions.cjs.map +1 -0
  203. package/build/modern/infiniteQueryOptions.d.cts +23 -0
  204. package/build/modern/infiniteQueryOptions.d.ts +23 -0
  205. package/build/modern/infiniteQueryOptions.js +8 -0
  206. package/build/modern/infiniteQueryOptions.js.map +1 -0
  207. package/build/modern/mutationOptions.cjs +33 -0
  208. package/build/modern/mutationOptions.cjs.map +1 -0
  209. package/build/modern/mutationOptions.d.cts +7 -0
  210. package/build/modern/mutationOptions.d.ts +7 -0
  211. package/build/modern/mutationOptions.js +8 -0
  212. package/build/modern/mutationOptions.js.map +1 -0
  213. package/build/modern/queryOptions.cjs +33 -0
  214. package/build/modern/queryOptions.cjs.map +1 -0
  215. package/build/modern/queryOptions.d.cts +24 -0
  216. package/build/modern/queryOptions.d.ts +24 -0
  217. package/build/modern/queryOptions.js +8 -0
  218. package/build/modern/queryOptions.js.map +1 -0
  219. package/build/modern/suspense.cjs +58 -0
  220. package/build/modern/suspense.cjs.map +1 -0
  221. package/build/modern/suspense.d.cts +11 -0
  222. package/build/modern/suspense.d.ts +11 -0
  223. package/build/modern/suspense.js +29 -0
  224. package/build/modern/suspense.js.map +1 -0
  225. package/build/modern/types.cjs +19 -0
  226. package/build/modern/types.cjs.map +1 -0
  227. package/build/modern/types.d.cts +52 -0
  228. package/build/modern/types.d.ts +52 -0
  229. package/build/modern/types.js +1 -0
  230. package/build/modern/types.js.map +1 -0
  231. package/build/modern/useBaseQuery.cjs +118 -0
  232. package/build/modern/useBaseQuery.cjs.map +1 -0
  233. package/build/modern/useBaseQuery.d.cts +6 -0
  234. package/build/modern/useBaseQuery.d.ts +6 -0
  235. package/build/modern/useBaseQuery.js +102 -0
  236. package/build/modern/useBaseQuery.js.map +1 -0
  237. package/build/modern/useInfiniteQuery.cjs +39 -0
  238. package/build/modern/useInfiniteQuery.cjs.map +1 -0
  239. package/build/modern/useInfiniteQuery.d.cts +9 -0
  240. package/build/modern/useInfiniteQuery.d.ts +9 -0
  241. package/build/modern/useInfiniteQuery.js +14 -0
  242. package/build/modern/useInfiniteQuery.js.map +1 -0
  243. package/build/modern/useIsFetching.cjs +45 -0
  244. package/build/modern/useIsFetching.cjs.map +1 -0
  245. package/build/modern/useIsFetching.d.cts +5 -0
  246. package/build/modern/useIsFetching.d.ts +5 -0
  247. package/build/modern/useIsFetching.js +20 -0
  248. package/build/modern/useIsFetching.js.map +1 -0
  249. package/build/modern/useMutation.cjs +63 -0
  250. package/build/modern/useMutation.cjs.map +1 -0
  251. package/build/modern/useMutation.d.cts +6 -0
  252. package/build/modern/useMutation.d.ts +6 -0
  253. package/build/modern/useMutation.js +43 -0
  254. package/build/modern/useMutation.js.map +1 -0
  255. package/build/modern/useMutationState.cjs +75 -0
  256. package/build/modern/useMutationState.cjs.map +1 -0
  257. package/build/modern/useMutationState.d.cts +10 -0
  258. package/build/modern/useMutationState.d.ts +10 -0
  259. package/build/modern/useMutationState.js +49 -0
  260. package/build/modern/useMutationState.js.map +1 -0
  261. package/build/modern/usePrefetchInfiniteQuery.cjs +37 -0
  262. package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -0
  263. package/build/modern/usePrefetchInfiniteQuery.d.cts +5 -0
  264. package/build/modern/usePrefetchInfiniteQuery.d.ts +5 -0
  265. package/build/modern/usePrefetchInfiniteQuery.js +12 -0
  266. package/build/modern/usePrefetchInfiniteQuery.js.map +1 -0
  267. package/build/modern/usePrefetchQuery.cjs +37 -0
  268. package/build/modern/usePrefetchQuery.cjs.map +1 -0
  269. package/build/modern/usePrefetchQuery.d.cts +6 -0
  270. package/build/modern/usePrefetchQuery.d.ts +6 -0
  271. package/build/modern/usePrefetchQuery.js +12 -0
  272. package/build/modern/usePrefetchQuery.js.map +1 -0
  273. package/build/modern/useQueries.cjs +120 -0
  274. package/build/modern/useQueries.cjs.map +1 -0
  275. package/build/modern/useQueries.d.cts +76 -0
  276. package/build/modern/useQueries.d.ts +76 -0
  277. package/build/modern/useQueries.js +109 -0
  278. package/build/modern/useQueries.js.map +1 -0
  279. package/build/modern/useQuery.cjs +35 -0
  280. package/build/modern/useQuery.cjs.map +1 -0
  281. package/build/modern/useQuery.d.cts +9 -0
  282. package/build/modern/useQuery.d.ts +9 -0
  283. package/build/modern/useQuery.js +10 -0
  284. package/build/modern/useQuery.js.map +1 -0
  285. package/build/modern/useSuspenseInfiniteQuery.cjs +50 -0
  286. package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -0
  287. package/build/modern/useSuspenseInfiniteQuery.d.cts +6 -0
  288. package/build/modern/useSuspenseInfiniteQuery.d.ts +6 -0
  289. package/build/modern/useSuspenseInfiniteQuery.js +25 -0
  290. package/build/modern/useSuspenseInfiniteQuery.js.map +1 -0
  291. package/build/modern/useSuspenseQueries.cjs +55 -0
  292. package/build/modern/useSuspenseQueries.cjs.map +1 -0
  293. package/build/modern/useSuspenseQueries.d.cts +79 -0
  294. package/build/modern/useSuspenseQueries.d.ts +79 -0
  295. package/build/modern/useSuspenseQueries.js +30 -0
  296. package/build/modern/useSuspenseQueries.js.map +1 -0
  297. package/build/modern/useSuspenseQuery.cjs +51 -0
  298. package/build/modern/useSuspenseQuery.cjs.map +1 -0
  299. package/build/modern/useSuspenseQuery.d.cts +6 -0
  300. package/build/modern/useSuspenseQuery.d.ts +6 -0
  301. package/build/modern/useSuspenseQuery.js +26 -0
  302. package/build/modern/useSuspenseQuery.js.map +1 -0
  303. package/build/modern/utils.cjs +79 -0
  304. package/build/modern/utils.cjs.map +1 -0
  305. package/build/modern/utils.d.cts +4 -0
  306. package/build/modern/utils.d.ts +4 -0
  307. package/build/modern/utils.js +53 -0
  308. package/build/modern/utils.js.map +1 -0
  309. package/build/query-codemods/eslint.config.js +18 -0
  310. package/build/query-codemods/package.json +38 -0
  311. package/build/query-codemods/root.eslint.config.js +64 -0
  312. package/build/query-codemods/tsconfig.json +8 -0
  313. package/build/query-codemods/vite.config.ts +30 -0
  314. package/package.json +82 -8
  315. package/src/HydrationBoundary.tsx +108 -0
  316. package/src/IsRestoringProvider.ts +7 -0
  317. package/src/QueryClientProvider.tsx +45 -0
  318. package/src/QueryErrorResetBoundary.tsx +57 -0
  319. package/src/errorBoundaryUtils.ts +76 -0
  320. package/src/index.ts +56 -0
  321. package/src/infiniteQueryOptions.ts +150 -0
  322. package/src/mutationOptions.ts +42 -0
  323. package/src/queryOptions.ts +88 -0
  324. package/src/suspense.ts +81 -0
  325. package/src/types.ts +241 -0
  326. package/src/useBaseQuery.ts +169 -0
  327. package/src/useInfiniteQuery.ts +81 -0
  328. package/src/useIsFetching.ts +23 -0
  329. package/src/useMutation.ts +69 -0
  330. package/src/useMutationState.ts +74 -0
  331. package/src/usePrefetchInfiniteQuery.tsx +31 -0
  332. package/src/usePrefetchQuery.tsx +20 -0
  333. package/src/useQueries.ts +331 -0
  334. package/src/useQuery.ts +52 -0
  335. package/src/useSuspenseInfiniteQuery.ts +50 -0
  336. package/src/useSuspenseQueries.ts +211 -0
  337. package/src/useSuspenseQuery.ts +34 -0
  338. package/src/utils.ts +88 -0
@@ -0,0 +1,185 @@
1
+ const createV5UtilsObject = require('../utils/index.cjs')
2
+ const UnknownUsageError = require('../utils/unknown-usage-error.cjs')
3
+ const createQueryClientTransformer = require('../../../utils/transformers/query-client-transformer.cjs')
4
+
5
+ /**
6
+ * @param {import('jscodeshift').api} jscodeshift
7
+ * @param {Object} utils
8
+ * @param {import('jscodeshift').Collection} root
9
+ * @param {string} filePath
10
+ * @param {{keyName: "mutationKey"|"queryKey", queryClientMethods: ReadonlyArray<string>, hooks: ReadonlyArray<string>}} config
11
+ */
12
+ const transformQueryFnAwareUsages = ({
13
+ jscodeshift,
14
+ utils,
15
+ root,
16
+ filePath,
17
+ config,
18
+ }) => {
19
+ const v5Utils = createV5UtilsObject({ jscodeshift, utils })
20
+
21
+ /**
22
+ * @param {import('jscodeshift').CallExpression} node
23
+ * @returns {boolean}
24
+ */
25
+ const canSkipReplacement = (node) => {
26
+ const callArguments = node.arguments
27
+
28
+ const hasKeyProperty = () =>
29
+ callArguments[0].properties.some(
30
+ (property) =>
31
+ utils.isObjectProperty(property) &&
32
+ [config.keyName, 'queryFn'].includes(property.key.name),
33
+ )
34
+
35
+ return (
36
+ callArguments.length > 0 &&
37
+ utils.isObjectExpression(callArguments[0]) &&
38
+ hasKeyProperty()
39
+ )
40
+ }
41
+
42
+ const predicate = (property) => {
43
+ const isSpreadElement = utils.isSpreadElement(property)
44
+ const isObjectProperty = utils.isObjectProperty(property)
45
+
46
+ return (
47
+ isSpreadElement ||
48
+ (isObjectProperty && property.key.name !== config.keyName)
49
+ )
50
+ }
51
+
52
+ const transformArgumentToQueryFunction = (path, node) => {
53
+ const isIdentifier = utils.isIdentifier(node)
54
+ const isFunctionDefinition = utils.isFunctionDefinition(node)
55
+
56
+ if (!isIdentifier && !isFunctionDefinition) {
57
+ return undefined
58
+ }
59
+
60
+ if (isFunctionDefinition) {
61
+ return jscodeshift.property(
62
+ 'init',
63
+ jscodeshift.identifier('queryFn'),
64
+ node,
65
+ )
66
+ }
67
+
68
+ const binding = v5Utils.getBindingFromScope(path, node.name, filePath)
69
+ const initializer = v5Utils.getInitializerByDeclarator(binding)
70
+
71
+ if (!utils.isFunctionDefinition(initializer)) {
72
+ return undefined
73
+ }
74
+
75
+ return jscodeshift.property(
76
+ 'init',
77
+ jscodeshift.identifier('queryFn'),
78
+ binding.id,
79
+ )
80
+ }
81
+
82
+ const transformArgumentToOptionsObject = (path, node) => {
83
+ if (!utils.isIdentifier(node)) {
84
+ return undefined
85
+ }
86
+
87
+ const binding = v5Utils.getBindingFromScope(path, node.name, filePath)
88
+ const initializer = v5Utils.getInitializerByDeclarator(binding)
89
+
90
+ if (utils.isObjectExpression(initializer)) {
91
+ return jscodeshift.spreadElement(binding.id)
92
+ }
93
+ }
94
+
95
+ const replacer = (path) => {
96
+ const node = path.node
97
+
98
+ try {
99
+ // If the given method/function call matches certain criteria, the node doesn't need to be replaced, this step can be skipped.
100
+ if (canSkipReplacement(node)) {
101
+ return node
102
+ }
103
+
104
+ const keyProperty = v5Utils.transformArgumentToKey(
105
+ path,
106
+ node.arguments[0],
107
+ config.keyName,
108
+ filePath,
109
+ )
110
+
111
+ if (!keyProperty) {
112
+ throw new UnknownUsageError(node, filePath)
113
+ }
114
+
115
+ const parameters = [jscodeshift.objectExpression([keyProperty])]
116
+ const createdObjectExpression = parameters[0]
117
+ const secondParameter = node.arguments[1]
118
+
119
+ if (secondParameter) {
120
+ const queryFnProperty = transformArgumentToQueryFunction(
121
+ path,
122
+ secondParameter,
123
+ )
124
+
125
+ if (queryFnProperty) {
126
+ createdObjectExpression.properties.push(queryFnProperty)
127
+
128
+ const thirdParameter = node.arguments[2]
129
+
130
+ if (utils.isObjectExpression(thirdParameter)) {
131
+ v5Utils.copyPropertiesFromSource(
132
+ thirdParameter,
133
+ createdObjectExpression,
134
+ predicate,
135
+ )
136
+ } else {
137
+ createdObjectExpression.properties.push(
138
+ jscodeshift.spreadElement(thirdParameter),
139
+ )
140
+ }
141
+
142
+ return jscodeshift.callExpression(node.original.callee, parameters)
143
+ }
144
+
145
+ const optionsProperty = transformArgumentToOptionsObject(
146
+ path,
147
+ secondParameter,
148
+ )
149
+
150
+ if (optionsProperty) {
151
+ createdObjectExpression.properties.push(optionsProperty)
152
+
153
+ return jscodeshift.callExpression(node.original.callee, parameters)
154
+ }
155
+
156
+ if (utils.isObjectExpression(secondParameter)) {
157
+ v5Utils.copyPropertiesFromSource(
158
+ secondParameter,
159
+ createdObjectExpression,
160
+ predicate,
161
+ )
162
+ }
163
+
164
+ return jscodeshift.callExpression(node.original.callee, parameters)
165
+ }
166
+
167
+ return jscodeshift.callExpression(node.original.callee, parameters)
168
+ } catch (error) {
169
+ utils.warn(
170
+ error.name === UnknownUsageError.name
171
+ ? error.message
172
+ : `An unknown error occurred while processing the "${filePath}" file. Please review this file, because the codemod couldn't be applied.`,
173
+ )
174
+
175
+ return node
176
+ }
177
+ }
178
+
179
+ createQueryClientTransformer({ jscodeshift, utils, root }).execute(
180
+ config.queryClientMethods,
181
+ replacer,
182
+ )
183
+ }
184
+
185
+ module.exports = transformQueryFnAwareUsages
@@ -0,0 +1,123 @@
1
+ const UnknownUsageError = require('./unknown-usage-error.cjs')
2
+
3
+ module.exports = ({ jscodeshift, utils }) => {
4
+ /**
5
+ *
6
+ * @param {import('jscodeshift').ObjectExpression} source
7
+ * @param {import('jscodeshift').ObjectExpression} target
8
+ * @param {(node: import('jscodeshift').Node) => boolean} predicate
9
+ */
10
+ const copyPropertiesFromSource = (source, target, predicate) => {
11
+ source.properties.forEach((property) => {
12
+ if (predicate(property)) {
13
+ target.properties.push(property)
14
+ }
15
+ })
16
+ }
17
+
18
+ /**
19
+ * @param {import('jscodeshift').NodePath} path
20
+ * @param {string} argumentName
21
+ * @param {string} filePath
22
+ * @returns {*}
23
+ */
24
+ const getBindingFromScope = (path, argumentName, filePath) => {
25
+ /**
26
+ * If the current scope contains the declaration then we can use the actual one else we attempt to find the
27
+ * binding from above.
28
+ */
29
+ const scope = path.scope.declares(argumentName)
30
+ ? path.scope
31
+ : path.scope.lookup(argumentName)
32
+
33
+ /**
34
+ * The declaration couldn't be found for some reason, time to move on. We warn the user it needs to be rewritten
35
+ * by themselves.
36
+ */
37
+ if (!scope) {
38
+ return undefined
39
+ }
40
+
41
+ const binding = scope.bindings[argumentName]
42
+ .filter((item) => utils.isIdentifier(item.value))
43
+ .map((item) => item.parentPath.value)
44
+ .at(0)
45
+
46
+ if (!binding) {
47
+ throw new UnknownUsageError(path.node, filePath)
48
+ }
49
+
50
+ return binding
51
+ }
52
+
53
+ /**
54
+ * @param {import('jscodeshift').VariableDeclarator} binding
55
+ * @returns {import('jscodeshift').Node|undefined}
56
+ */
57
+ const getInitializerByDeclarator = (binding) => {
58
+ const isVariableDeclaration = jscodeshift.match(binding, {
59
+ type: jscodeshift.VariableDeclarator.name,
60
+ })
61
+
62
+ if (!isVariableDeclaration) {
63
+ return undefined
64
+ }
65
+
66
+ const isTSAsExpression = jscodeshift.match(binding.init, {
67
+ type: jscodeshift.TSAsExpression.name,
68
+ })
69
+
70
+ return isTSAsExpression ? binding.init.expression : binding.init
71
+ }
72
+
73
+ /**
74
+ * @param {import('jscodeshift').Node} node
75
+ * @returns {boolean}
76
+ */
77
+ const isArrayExpressionVariable = (node) =>
78
+ jscodeshift.match(node, {
79
+ type: jscodeshift.VariableDeclarator.name,
80
+ init: {
81
+ type: jscodeshift.ArrayExpression.name,
82
+ },
83
+ })
84
+
85
+ /**
86
+ * @param {import('jscodeshift').NodePath} path
87
+ * @param {import('jscodeshift').Node} node
88
+ * @param {"queryKey"|"mutationKey"} keyName
89
+ * @param {string} filePath
90
+ * @returns {import('jscodeshift').Property|undefined}
91
+ */
92
+ const transformArgumentToKey = (path, node, keyName, filePath) => {
93
+ // If the first argument is an identifier we have to infer its type if possible.
94
+ if (utils.isIdentifier(node)) {
95
+ const binding = getBindingFromScope(path, node.name, filePath)
96
+
97
+ if (isArrayExpressionVariable(binding)) {
98
+ return jscodeshift.property(
99
+ 'init',
100
+ jscodeshift.identifier(keyName),
101
+ jscodeshift.identifier(binding.id.name),
102
+ )
103
+ }
104
+ }
105
+
106
+ // If the first argument is an array, then it matches the following overload:
107
+ // methodName(queryKey?: QueryKey, firstObject?: TFirstObject, secondObject?: TSecondObject)
108
+ if (utils.isArrayExpression(node)) {
109
+ // Then we create the 'queryKey' property based on it, because it will be passed to the first argument
110
+ // that should be an object according to the new signature.
111
+ return jscodeshift.property('init', jscodeshift.identifier(keyName), node)
112
+ }
113
+
114
+ return undefined
115
+ }
116
+
117
+ return {
118
+ copyPropertiesFromSource,
119
+ getInitializerByDeclarator,
120
+ getBindingFromScope,
121
+ transformArgumentToKey,
122
+ }
123
+ }
@@ -0,0 +1,27 @@
1
+ class UnknownUsageError extends Error {
2
+ /**
3
+ * @param {import('jscodeshift').CallExpression} callExpression
4
+ * @param {string} filePath
5
+ */
6
+ constructor(callExpression, filePath) {
7
+ super('')
8
+ this.message = this.buildMessage(callExpression, filePath)
9
+ this.name = 'UnknownUsageError'
10
+ }
11
+
12
+ /**
13
+ *
14
+ * @param {import('jscodeshift').CallExpression} callExpression
15
+ * @param {string} filePath
16
+ * @returns {string}
17
+ */
18
+ buildMessage(callExpression, filePath) {
19
+ const location = callExpression.callee.loc
20
+ const start = location.start.line
21
+ const end = location.end.line
22
+
23
+ return `The usage in file "${filePath}" at line ${start}:${end} could not be transformed into the new syntax. Please do this manually.`
24
+ }
25
+ }
26
+
27
+ module.exports = UnknownUsageError
@@ -0,0 +1,55 @@
1
+ module.exports = (file, api) => {
2
+ const jscodeshift = api.jscodeshift
3
+ const root = jscodeshift(file.source)
4
+
5
+ const importSpecifiers = root
6
+ .find(jscodeshift.ImportDeclaration, {
7
+ source: {
8
+ value: '@tanstack/react-query',
9
+ },
10
+ })
11
+ .find(jscodeshift.ImportSpecifier, {
12
+ imported: {
13
+ name: 'Hydrate',
14
+ },
15
+ })
16
+
17
+ if (importSpecifiers.length > 0) {
18
+ const names = {
19
+ searched: 'Hydrate', // By default, we want to replace the `Hydrate` usages.
20
+ target: 'HydrationBoundary', // We want to replace them with `HydrationBoundary`.
21
+ }
22
+
23
+ importSpecifiers.replaceWith(({ node: mutableNode }) => {
24
+ /**
25
+ * When the local and imported names match which means the code doesn't contain import aliases, we need
26
+ * to replace only the import specifier.
27
+ * @type {boolean}
28
+ */
29
+ const usesDefaultImport =
30
+ mutableNode.local.name === mutableNode.imported.name
31
+
32
+ if (!usesDefaultImport) {
33
+ // If the code uses import aliases, we must re-use the alias.
34
+ names.searched = mutableNode.local.name
35
+ names.target = mutableNode.local.name
36
+ }
37
+
38
+ // Override the import specifier.
39
+ mutableNode.imported.name = 'HydrationBoundary'
40
+
41
+ return mutableNode
42
+ })
43
+
44
+ root
45
+ .findJSXElements(names.searched)
46
+ .replaceWith(({ node: mutableNode }) => {
47
+ mutableNode.openingElement.name.name = names.target
48
+ mutableNode.closingElement.name.name = names.target
49
+
50
+ return mutableNode
51
+ })
52
+ }
53
+
54
+ return root.toSource({ quote: 'single', lineTerminator: '\n' })
55
+ }
@@ -0,0 +1,41 @@
1
+ module.exports = (file, api) => {
2
+ const jscodeshift = api.jscodeshift
3
+ const root = jscodeshift(file.source)
4
+
5
+ const baseRenameLogic = (kind, from, to) => {
6
+ root
7
+ .find(kind, (node) => {
8
+ return (
9
+ node.computed === false &&
10
+ (node.key?.name === from || node.key?.value === from)
11
+ )
12
+ })
13
+ .replaceWith(({ node: mutableNode }) => {
14
+ if (mutableNode.key.name !== undefined) {
15
+ mutableNode.key.name = to
16
+ }
17
+
18
+ if (mutableNode.key.value !== undefined) {
19
+ mutableNode.key.value = to
20
+ }
21
+
22
+ return mutableNode
23
+ })
24
+ }
25
+
26
+ const renameObjectProperty = (from, to) => {
27
+ baseRenameLogic(jscodeshift.ObjectProperty, from, to)
28
+ }
29
+
30
+ const renameTypeScriptPropertySignature = (from, to) => {
31
+ baseRenameLogic(jscodeshift.TSPropertySignature, from, to)
32
+ }
33
+
34
+ renameObjectProperty('cacheTime', 'gcTime')
35
+ renameObjectProperty('useErrorBoundary', 'throwOnError')
36
+
37
+ renameTypeScriptPropertySignature('cacheTime', 'gcTime')
38
+ renameTypeScriptPropertySignature('useErrorBoundary', 'throwOnError')
39
+
40
+ return root.toSource({ quote: 'single', lineTerminator: '\n' })
41
+ }
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/HydrationBoundary.tsx
21
+ var HydrationBoundary_exports = {};
22
+ __export(HydrationBoundary_exports, {
23
+ HydrationBoundary: () => HydrationBoundary
24
+ });
25
+ module.exports = __toCommonJS(HydrationBoundary_exports);
26
+ var import_query_core = require("@tanstack/query-core");
27
+ var import_hooks = require("preact/hooks");
28
+ var import_QueryClientProvider = require("./QueryClientProvider.cjs");
29
+ var HydrationBoundary = ({
30
+ children,
31
+ options = {},
32
+ state,
33
+ queryClient
34
+ }) => {
35
+ const client = (0, import_QueryClientProvider.useQueryClient)(queryClient);
36
+ const optionsRef = (0, import_hooks.useRef)(options);
37
+ (0, import_hooks.useEffect)(() => {
38
+ optionsRef.current = options;
39
+ });
40
+ const hydrationQueue = (0, import_hooks.useMemo)(() => {
41
+ if (state) {
42
+ if (typeof state !== "object") {
43
+ return;
44
+ }
45
+ const queryCache = client.getQueryCache();
46
+ const queries = state.queries || [];
47
+ const newQueries = [];
48
+ const existingQueries = [];
49
+ for (const dehydratedQuery of queries) {
50
+ const existingQuery = queryCache.get(dehydratedQuery.queryHash);
51
+ if (!existingQuery) {
52
+ newQueries.push(dehydratedQuery);
53
+ } else {
54
+ const hydrationIsNewer = dehydratedQuery.state.dataUpdatedAt > existingQuery.state.dataUpdatedAt || dehydratedQuery.promise && existingQuery.state.status !== "pending" && existingQuery.state.fetchStatus !== "fetching" && dehydratedQuery.dehydratedAt !== void 0 && dehydratedQuery.dehydratedAt > existingQuery.state.dataUpdatedAt;
55
+ if (hydrationIsNewer) {
56
+ existingQueries.push(dehydratedQuery);
57
+ }
58
+ }
59
+ }
60
+ if (newQueries.length > 0) {
61
+ (0, import_query_core.hydrate)(client, { queries: newQueries }, optionsRef.current);
62
+ }
63
+ if (existingQueries.length > 0) {
64
+ return existingQueries;
65
+ }
66
+ }
67
+ return void 0;
68
+ }, [client, state]);
69
+ (0, import_hooks.useEffect)(() => {
70
+ if (hydrationQueue) {
71
+ (0, import_query_core.hydrate)(client, { queries: hydrationQueue }, optionsRef.current);
72
+ }
73
+ }, [client, hydrationQueue]);
74
+ return children;
75
+ };
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ HydrationBoundary
79
+ });
80
+ //# sourceMappingURL=HydrationBoundary.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/HydrationBoundary.tsx"],"sourcesContent":["import { hydrate } from '@tanstack/query-core'\nimport type {\n DehydratedState,\n HydrateOptions,\n OmitKeyof,\n QueryClient,\n} from '@tanstack/query-core'\nimport type { ComponentChildren } from 'preact'\nimport { useEffect, useMemo, useRef } from 'preact/hooks'\n\nimport { useQueryClient } from './QueryClientProvider'\n\nexport interface HydrationBoundaryProps {\n state: DehydratedState | null | undefined\n options?: OmitKeyof<HydrateOptions, 'defaultOptions'> & {\n defaultOptions?: OmitKeyof<\n Exclude<HydrateOptions['defaultOptions'], undefined>,\n 'mutations'\n >\n }\n children?: ComponentChildren\n queryClient?: QueryClient\n}\n\nexport const HydrationBoundary = ({\n children,\n options = {},\n state,\n queryClient,\n}: HydrationBoundaryProps) => {\n const client = useQueryClient(queryClient)\n\n const optionsRef = useRef(options)\n useEffect(() => {\n optionsRef.current = options\n })\n\n // This useMemo is for performance reasons only, everything inside it must\n // be safe to run in every render and code here should be read as \"in render\".\n //\n // This code needs to happen during the render phase, because after initial\n // SSR, hydration needs to happen _before_ children render. Also, if hydrating\n // during a transition, we want to hydrate as much as is safe in render so\n // we can prerender as much as possible.\n //\n // For any queries that already exist in the cache, we want to hold back on\n // hydrating until _after_ the render phase. The reason for this is that during\n // transitions, we don't want the existing queries and observers to update to\n // the new data on the current page, only _after_ the transition is committed.\n // If the transition is aborted, we will have hydrated any _new_ queries, but\n // we throw away the fresh data for any existing ones to avoid unexpectedly\n // updating the UI.\n const hydrationQueue: DehydratedState['queries'] | undefined = useMemo(() => {\n if (state) {\n if (typeof state !== 'object') {\n return\n }\n\n const queryCache = client.getQueryCache()\n // State is supplied from the outside and we might as well fail\n // gracefully if it has the wrong shape, so while we type `queries`\n // as required, we still provide a fallback.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = state.queries || []\n\n const newQueries: DehydratedState['queries'] = []\n const existingQueries: DehydratedState['queries'] = []\n for (const dehydratedQuery of queries) {\n const existingQuery = queryCache.get(dehydratedQuery.queryHash)\n\n if (!existingQuery) {\n newQueries.push(dehydratedQuery)\n } else {\n const hydrationIsNewer =\n dehydratedQuery.state.dataUpdatedAt >\n existingQuery.state.dataUpdatedAt ||\n (dehydratedQuery.promise &&\n existingQuery.state.status !== 'pending' &&\n existingQuery.state.fetchStatus !== 'fetching' &&\n dehydratedQuery.dehydratedAt !== undefined &&\n dehydratedQuery.dehydratedAt > existingQuery.state.dataUpdatedAt)\n\n if (hydrationIsNewer) {\n existingQueries.push(dehydratedQuery)\n }\n }\n }\n\n if (newQueries.length > 0) {\n // It's actually fine to call this with queries/state that already exists\n // in the cache, or is older. hydrate() is idempotent for queries.\n hydrate(client, { queries: newQueries }, optionsRef.current)\n }\n if (existingQueries.length > 0) {\n return existingQueries\n }\n }\n return undefined\n }, [client, state])\n\n useEffect(() => {\n if (hydrationQueue) {\n hydrate(client, { queries: hydrationQueue }, optionsRef.current)\n }\n }, [client, hydrationQueue])\n\n return children as ComponentChildren\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAwB;AAQxB,mBAA2C;AAE3C,iCAA+B;AAcxB,IAAM,oBAAoB,CAAC;AAAA,EAChC;AAAA,EACA,UAAU,CAAC;AAAA,EACX;AAAA,EACA;AACF,MAA8B;AAC5B,QAAM,aAAS,2CAAe,WAAW;AAEzC,QAAM,iBAAa,qBAAO,OAAO;AACjC,8BAAU,MAAM;AACd,eAAW,UAAU;AAAA,EACvB,CAAC;AAiBD,QAAM,qBAAyD,sBAAQ,MAAM;AAC3E,QAAI,OAAO;AACT,UAAI,OAAO,UAAU,UAAU;AAC7B;AAAA,MACF;AAEA,YAAM,aAAa,OAAO,cAAc;AAKxC,YAAM,UAAU,MAAM,WAAW,CAAC;AAElC,YAAM,aAAyC,CAAC;AAChD,YAAM,kBAA8C,CAAC;AACrD,iBAAW,mBAAmB,SAAS;AACrC,cAAM,gBAAgB,WAAW,IAAI,gBAAgB,SAAS;AAE9D,YAAI,CAAC,eAAe;AAClB,qBAAW,KAAK,eAAe;AAAA,QACjC,OAAO;AACL,gBAAM,mBACJ,gBAAgB,MAAM,gBACpB,cAAc,MAAM,iBACrB,gBAAgB,WACf,cAAc,MAAM,WAAW,aAC/B,cAAc,MAAM,gBAAgB,cACpC,gBAAgB,iBAAiB,UACjC,gBAAgB,eAAe,cAAc,MAAM;AAEvD,cAAI,kBAAkB;AACpB,4BAAgB,KAAK,eAAe;AAAA,UACtC;AAAA,QACF;AAAA,MACF;AAEA,UAAI,WAAW,SAAS,GAAG;AAGzB,uCAAQ,QAAQ,EAAE,SAAS,WAAW,GAAG,WAAW,OAAO;AAAA,MAC7D;AACA,UAAI,gBAAgB,SAAS,GAAG;AAC9B,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,EACT,GAAG,CAAC,QAAQ,KAAK,CAAC;AAElB,8BAAU,MAAM;AACd,QAAI,gBAAgB;AAClB,qCAAQ,QAAQ,EAAE,SAAS,eAAe,GAAG,WAAW,OAAO;AAAA,IACjE;AAAA,EACF,GAAG,CAAC,QAAQ,cAAc,CAAC;AAE3B,SAAO;AACT;","names":[]}
@@ -0,0 +1,14 @@
1
+ import { DehydratedState, OmitKeyof, HydrateOptions, QueryClient } from '@tanstack/query-core';
2
+ import { ComponentChildren } from 'preact';
3
+
4
+ interface HydrationBoundaryProps {
5
+ state: DehydratedState | null | undefined;
6
+ options?: OmitKeyof<HydrateOptions, 'defaultOptions'> & {
7
+ defaultOptions?: OmitKeyof<Exclude<HydrateOptions['defaultOptions'], undefined>, 'mutations'>;
8
+ };
9
+ children?: ComponentChildren;
10
+ queryClient?: QueryClient;
11
+ }
12
+ declare const HydrationBoundary: ({ children, options, state, queryClient, }: HydrationBoundaryProps) => ComponentChildren;
13
+
14
+ export { HydrationBoundary, type HydrationBoundaryProps };
@@ -0,0 +1,14 @@
1
+ import { DehydratedState, OmitKeyof, HydrateOptions, QueryClient } from '@tanstack/query-core';
2
+ import { ComponentChildren } from 'preact';
3
+
4
+ interface HydrationBoundaryProps {
5
+ state: DehydratedState | null | undefined;
6
+ options?: OmitKeyof<HydrateOptions, 'defaultOptions'> & {
7
+ defaultOptions?: OmitKeyof<Exclude<HydrateOptions['defaultOptions'], undefined>, 'mutations'>;
8
+ };
9
+ children?: ComponentChildren;
10
+ queryClient?: QueryClient;
11
+ }
12
+ declare const HydrationBoundary: ({ children, options, state, queryClient, }: HydrationBoundaryProps) => ComponentChildren;
13
+
14
+ export { HydrationBoundary, type HydrationBoundaryProps };
@@ -0,0 +1,55 @@
1
+ // src/HydrationBoundary.tsx
2
+ import { hydrate } from "@tanstack/query-core";
3
+ import { useEffect, useMemo, useRef } from "preact/hooks";
4
+ import { useQueryClient } from "./QueryClientProvider.js";
5
+ var HydrationBoundary = ({
6
+ children,
7
+ options = {},
8
+ state,
9
+ queryClient
10
+ }) => {
11
+ const client = useQueryClient(queryClient);
12
+ const optionsRef = useRef(options);
13
+ useEffect(() => {
14
+ optionsRef.current = options;
15
+ });
16
+ const hydrationQueue = useMemo(() => {
17
+ if (state) {
18
+ if (typeof state !== "object") {
19
+ return;
20
+ }
21
+ const queryCache = client.getQueryCache();
22
+ const queries = state.queries || [];
23
+ const newQueries = [];
24
+ const existingQueries = [];
25
+ for (const dehydratedQuery of queries) {
26
+ const existingQuery = queryCache.get(dehydratedQuery.queryHash);
27
+ if (!existingQuery) {
28
+ newQueries.push(dehydratedQuery);
29
+ } else {
30
+ const hydrationIsNewer = dehydratedQuery.state.dataUpdatedAt > existingQuery.state.dataUpdatedAt || dehydratedQuery.promise && existingQuery.state.status !== "pending" && existingQuery.state.fetchStatus !== "fetching" && dehydratedQuery.dehydratedAt !== void 0 && dehydratedQuery.dehydratedAt > existingQuery.state.dataUpdatedAt;
31
+ if (hydrationIsNewer) {
32
+ existingQueries.push(dehydratedQuery);
33
+ }
34
+ }
35
+ }
36
+ if (newQueries.length > 0) {
37
+ hydrate(client, { queries: newQueries }, optionsRef.current);
38
+ }
39
+ if (existingQueries.length > 0) {
40
+ return existingQueries;
41
+ }
42
+ }
43
+ return void 0;
44
+ }, [client, state]);
45
+ useEffect(() => {
46
+ if (hydrationQueue) {
47
+ hydrate(client, { queries: hydrationQueue }, optionsRef.current);
48
+ }
49
+ }, [client, hydrationQueue]);
50
+ return children;
51
+ };
52
+ export {
53
+ HydrationBoundary
54
+ };
55
+ //# sourceMappingURL=HydrationBoundary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/HydrationBoundary.tsx"],"sourcesContent":["import { hydrate } from '@tanstack/query-core'\nimport type {\n DehydratedState,\n HydrateOptions,\n OmitKeyof,\n QueryClient,\n} from '@tanstack/query-core'\nimport type { ComponentChildren } from 'preact'\nimport { useEffect, useMemo, useRef } from 'preact/hooks'\n\nimport { useQueryClient } from './QueryClientProvider'\n\nexport interface HydrationBoundaryProps {\n state: DehydratedState | null | undefined\n options?: OmitKeyof<HydrateOptions, 'defaultOptions'> & {\n defaultOptions?: OmitKeyof<\n Exclude<HydrateOptions['defaultOptions'], undefined>,\n 'mutations'\n >\n }\n children?: ComponentChildren\n queryClient?: QueryClient\n}\n\nexport const HydrationBoundary = ({\n children,\n options = {},\n state,\n queryClient,\n}: HydrationBoundaryProps) => {\n const client = useQueryClient(queryClient)\n\n const optionsRef = useRef(options)\n useEffect(() => {\n optionsRef.current = options\n })\n\n // This useMemo is for performance reasons only, everything inside it must\n // be safe to run in every render and code here should be read as \"in render\".\n //\n // This code needs to happen during the render phase, because after initial\n // SSR, hydration needs to happen _before_ children render. Also, if hydrating\n // during a transition, we want to hydrate as much as is safe in render so\n // we can prerender as much as possible.\n //\n // For any queries that already exist in the cache, we want to hold back on\n // hydrating until _after_ the render phase. The reason for this is that during\n // transitions, we don't want the existing queries and observers to update to\n // the new data on the current page, only _after_ the transition is committed.\n // If the transition is aborted, we will have hydrated any _new_ queries, but\n // we throw away the fresh data for any existing ones to avoid unexpectedly\n // updating the UI.\n const hydrationQueue: DehydratedState['queries'] | undefined = useMemo(() => {\n if (state) {\n if (typeof state !== 'object') {\n return\n }\n\n const queryCache = client.getQueryCache()\n // State is supplied from the outside and we might as well fail\n // gracefully if it has the wrong shape, so while we type `queries`\n // as required, we still provide a fallback.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = state.queries || []\n\n const newQueries: DehydratedState['queries'] = []\n const existingQueries: DehydratedState['queries'] = []\n for (const dehydratedQuery of queries) {\n const existingQuery = queryCache.get(dehydratedQuery.queryHash)\n\n if (!existingQuery) {\n newQueries.push(dehydratedQuery)\n } else {\n const hydrationIsNewer =\n dehydratedQuery.state.dataUpdatedAt >\n existingQuery.state.dataUpdatedAt ||\n (dehydratedQuery.promise &&\n existingQuery.state.status !== 'pending' &&\n existingQuery.state.fetchStatus !== 'fetching' &&\n dehydratedQuery.dehydratedAt !== undefined &&\n dehydratedQuery.dehydratedAt > existingQuery.state.dataUpdatedAt)\n\n if (hydrationIsNewer) {\n existingQueries.push(dehydratedQuery)\n }\n }\n }\n\n if (newQueries.length > 0) {\n // It's actually fine to call this with queries/state that already exists\n // in the cache, or is older. hydrate() is idempotent for queries.\n hydrate(client, { queries: newQueries }, optionsRef.current)\n }\n if (existingQueries.length > 0) {\n return existingQueries\n }\n }\n return undefined\n }, [client, state])\n\n useEffect(() => {\n if (hydrationQueue) {\n hydrate(client, { queries: hydrationQueue }, optionsRef.current)\n }\n }, [client, hydrationQueue])\n\n return children as ComponentChildren\n}\n"],"mappings":";AAAA,SAAS,eAAe;AAQxB,SAAS,WAAW,SAAS,cAAc;AAE3C,SAAS,sBAAsB;AAcxB,IAAM,oBAAoB,CAAC;AAAA,EAChC;AAAA,EACA,UAAU,CAAC;AAAA,EACX;AAAA,EACA;AACF,MAA8B;AAC5B,QAAM,SAAS,eAAe,WAAW;AAEzC,QAAM,aAAa,OAAO,OAAO;AACjC,YAAU,MAAM;AACd,eAAW,UAAU;AAAA,EACvB,CAAC;AAiBD,QAAM,iBAAyD,QAAQ,MAAM;AAC3E,QAAI,OAAO;AACT,UAAI,OAAO,UAAU,UAAU;AAC7B;AAAA,MACF;AAEA,YAAM,aAAa,OAAO,cAAc;AAKxC,YAAM,UAAU,MAAM,WAAW,CAAC;AAElC,YAAM,aAAyC,CAAC;AAChD,YAAM,kBAA8C,CAAC;AACrD,iBAAW,mBAAmB,SAAS;AACrC,cAAM,gBAAgB,WAAW,IAAI,gBAAgB,SAAS;AAE9D,YAAI,CAAC,eAAe;AAClB,qBAAW,KAAK,eAAe;AAAA,QACjC,OAAO;AACL,gBAAM,mBACJ,gBAAgB,MAAM,gBACpB,cAAc,MAAM,iBACrB,gBAAgB,WACf,cAAc,MAAM,WAAW,aAC/B,cAAc,MAAM,gBAAgB,cACpC,gBAAgB,iBAAiB,UACjC,gBAAgB,eAAe,cAAc,MAAM;AAEvD,cAAI,kBAAkB;AACpB,4BAAgB,KAAK,eAAe;AAAA,UACtC;AAAA,QACF;AAAA,MACF;AAEA,UAAI,WAAW,SAAS,GAAG;AAGzB,gBAAQ,QAAQ,EAAE,SAAS,WAAW,GAAG,WAAW,OAAO;AAAA,MAC7D;AACA,UAAI,gBAAgB,SAAS,GAAG;AAC9B,eAAO;AAAA,MACT;AAAA,IACF;AACA,WAAO;AAAA,EACT,GAAG,CAAC,QAAQ,KAAK,CAAC;AAElB,YAAU,MAAM;AACd,QAAI,gBAAgB;AAClB,cAAQ,QAAQ,EAAE,SAAS,eAAe,GAAG,WAAW,OAAO;AAAA,IACjE;AAAA,EACF,GAAG,CAAC,QAAQ,cAAc,CAAC;AAE3B,SAAO;AACT;","names":[]}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/IsRestoringProvider.ts
21
+ var IsRestoringProvider_exports = {};
22
+ __export(IsRestoringProvider_exports, {
23
+ IsRestoringProvider: () => IsRestoringProvider,
24
+ useIsRestoring: () => useIsRestoring
25
+ });
26
+ module.exports = __toCommonJS(IsRestoringProvider_exports);
27
+ var import_preact = require("preact");
28
+ var import_hooks = require("preact/hooks");
29
+ var IsRestoringContext = (0, import_preact.createContext)(false);
30
+ var useIsRestoring = () => (0, import_hooks.useContext)(IsRestoringContext);
31
+ var IsRestoringProvider = IsRestoringContext.Provider;
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ IsRestoringProvider,
35
+ useIsRestoring
36
+ });
37
+ //# sourceMappingURL=IsRestoringProvider.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/IsRestoringProvider.ts"],"sourcesContent":["import { createContext } from 'preact'\nimport { useContext } from 'preact/hooks'\n\nconst IsRestoringContext = createContext(false)\n\nexport const useIsRestoring = () => useContext(IsRestoringContext)\nexport const IsRestoringProvider = IsRestoringContext.Provider\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA8B;AAC9B,mBAA2B;AAE3B,IAAM,yBAAqB,6BAAc,KAAK;AAEvC,IAAM,iBAAiB,UAAM,yBAAW,kBAAkB;AAC1D,IAAM,sBAAsB,mBAAmB;","names":[]}
@@ -0,0 +1,6 @@
1
+ import * as preact from 'preact';
2
+
3
+ declare const useIsRestoring: () => boolean;
4
+ declare const IsRestoringProvider: preact.Provider<boolean>;
5
+
6
+ export { IsRestoringProvider, useIsRestoring };