@tanstack/vue-query 5.102.8 → 5.103.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.
- package/build/legacy/devtools/devtools.d.cts +1 -1
- package/build/legacy/devtools/devtools.d.ts +1 -1
- package/build/legacy/index.d.cts +8 -6
- package/build/legacy/index.d.ts +8 -6
- package/build/legacy/infiniteQueryOptions-CEDiLwh5.d.ts +365 -0
- package/build/legacy/infiniteQueryOptions-CEDiLwh5.d.ts.map +1 -0
- package/build/legacy/infiniteQueryOptions-qN6sOJM-.d.cts +365 -0
- package/build/legacy/infiniteQueryOptions-qN6sOJM-.d.cts.map +1 -0
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +1 -1
- package/build/legacy/infiniteQueryOptions.js.map +1 -1
- package/build/legacy/mutationCache.cjs +5 -0
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.d.cts +6 -1
- package/build/legacy/mutationCache.d.cts.map +1 -1
- package/build/legacy/mutationCache.d.ts +6 -1
- package/build/legacy/mutationCache.d.ts.map +1 -1
- package/build/legacy/mutationCache.js +5 -0
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationOptions.cjs.map +1 -1
- package/build/legacy/mutationOptions.d.cts +121 -1
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +121 -1
- package/build/legacy/mutationOptions.d.ts.map +1 -1
- package/build/legacy/mutationOptions.js.map +1 -1
- package/build/legacy/queryCache.cjs +5 -0
- package/build/legacy/queryCache.cjs.map +1 -1
- package/build/legacy/queryCache.d.cts +6 -1
- package/build/legacy/queryCache.d.cts.map +1 -1
- package/build/legacy/queryCache.d.ts +6 -1
- package/build/legacy/queryCache.d.ts.map +1 -1
- package/build/legacy/queryCache.js +5 -0
- package/build/legacy/queryCache.js.map +1 -1
- package/build/legacy/queryClient-CZeNAt68.d.cts +71 -0
- package/build/legacy/queryClient-CZeNAt68.d.cts.map +1 -0
- package/build/legacy/queryClient-Cv73nzNT.d.ts +71 -0
- package/build/legacy/queryClient-Cv73nzNT.d.ts.map +1 -0
- package/build/legacy/queryClient.cjs +7 -0
- package/build/legacy/queryClient.cjs.map +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/legacy/queryClient.js +7 -0
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryOptions-DPDwdVBu.d.ts +227 -0
- package/build/legacy/queryOptions-DPDwdVBu.d.ts.map +1 -0
- package/build/legacy/queryOptions-rkZ3sk6g.d.cts +227 -0
- package/build/legacy/queryOptions-rkZ3sk6g.d.cts.map +1 -0
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +2 -19
- package/build/legacy/queryOptions.d.ts +2 -19
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/{modern/types-BzZi3_rS.d.cts → legacy/types-JpJB72CS.d.cts} +19 -1
- package/build/legacy/types-JpJB72CS.d.cts.map +1 -0
- package/build/legacy/{types-BzZi3_rS.d.cts → types-JpJB72CS.d.ts} +19 -1
- package/build/legacy/types-JpJB72CS.d.ts.map +1 -0
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/useBaseQuery.cjs +8 -0
- package/build/legacy/useBaseQuery.cjs.map +1 -1
- package/build/legacy/useBaseQuery.d.cts +1 -1
- package/build/legacy/useBaseQuery.d.ts +1 -1
- package/build/legacy/useBaseQuery.js +8 -0
- package/build/legacy/useBaseQuery.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +41 -0
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +47 -6
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +47 -6
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +41 -0
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +170 -9
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +164 -3
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +164 -3
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +170 -9
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +103 -0
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +111 -7
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +111 -7
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +103 -0
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.cjs +46 -0
- package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +48 -2
- package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +48 -2
- package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.js +46 -0
- package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/legacy/usePrefetchQuery.cjs +37 -0
- package/build/legacy/usePrefetchQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +39 -2
- package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +39 -2
- package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchQuery.js +37 -0
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +123 -0
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +126 -2
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +126 -2
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +123 -0
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +248 -2
- package/build/legacy/useQuery.d.cts.map +1 -0
- package/build/legacy/useQuery.d.ts +248 -2
- package/build/legacy/useQuery.d.ts.map +1 -0
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useQueryClient.cjs +21 -0
- package/build/legacy/useQueryClient.cjs.map +1 -1
- package/build/legacy/useQueryClient.d.cts +22 -1
- package/build/legacy/useQueryClient.d.cts.map +1 -1
- package/build/legacy/useQueryClient.d.ts +22 -1
- package/build/legacy/useQueryClient.d.ts.map +1 -1
- package/build/legacy/useQueryClient.js +21 -0
- package/build/legacy/useQueryClient.js.map +1 -1
- package/build/legacy/utils.cjs +15 -0
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +16 -1
- package/build/legacy/utils.d.cts.map +1 -1
- package/build/legacy/utils.d.ts +16 -1
- package/build/legacy/utils.d.ts.map +1 -1
- package/build/legacy/utils.js +15 -0
- package/build/legacy/utils.js.map +1 -1
- package/build/legacy/vueQueryPlugin.cjs +34 -0
- package/build/legacy/vueQueryPlugin.cjs.map +1 -1
- package/build/legacy/vueQueryPlugin.d.cts +48 -2
- package/build/legacy/vueQueryPlugin.d.cts.map +1 -1
- package/build/legacy/vueQueryPlugin.d.ts +48 -2
- package/build/legacy/vueQueryPlugin.d.ts.map +1 -1
- package/build/legacy/vueQueryPlugin.js +34 -0
- package/build/legacy/vueQueryPlugin.js.map +1 -1
- package/build/modern/devtools/devtools.d.cts +1 -1
- package/build/modern/devtools/devtools.d.ts +1 -1
- package/build/modern/index.d.cts +8 -6
- package/build/modern/index.d.ts +8 -6
- package/build/modern/infiniteQueryOptions-CEDiLwh5.d.ts +365 -0
- package/build/modern/infiniteQueryOptions-CEDiLwh5.d.ts.map +1 -0
- package/build/modern/infiniteQueryOptions-qN6sOJM-.d.cts +365 -0
- package/build/modern/infiniteQueryOptions-qN6sOJM-.d.cts.map +1 -0
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +1 -1
- package/build/modern/infiniteQueryOptions.js.map +1 -1
- package/build/modern/mutationCache.cjs +5 -0
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.d.cts +6 -1
- package/build/modern/mutationCache.d.cts.map +1 -1
- package/build/modern/mutationCache.d.ts +6 -1
- package/build/modern/mutationCache.d.ts.map +1 -1
- package/build/modern/mutationCache.js +5 -0
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationOptions.cjs.map +1 -1
- package/build/modern/mutationOptions.d.cts +121 -1
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +121 -1
- package/build/modern/mutationOptions.d.ts.map +1 -1
- package/build/modern/mutationOptions.js.map +1 -1
- package/build/modern/queryCache.cjs +5 -0
- package/build/modern/queryCache.cjs.map +1 -1
- package/build/modern/queryCache.d.cts +6 -1
- package/build/modern/queryCache.d.cts.map +1 -1
- package/build/modern/queryCache.d.ts +6 -1
- package/build/modern/queryCache.d.ts.map +1 -1
- package/build/modern/queryCache.js +5 -0
- package/build/modern/queryCache.js.map +1 -1
- package/build/modern/queryClient-CZeNAt68.d.cts +71 -0
- package/build/modern/queryClient-CZeNAt68.d.cts.map +1 -0
- package/build/modern/queryClient-Cv73nzNT.d.ts +71 -0
- package/build/modern/queryClient-Cv73nzNT.d.ts.map +1 -0
- package/build/modern/queryClient.cjs +7 -0
- package/build/modern/queryClient.cjs.map +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/modern/queryClient.js +7 -0
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryOptions-DPDwdVBu.d.ts +227 -0
- package/build/modern/queryOptions-DPDwdVBu.d.ts.map +1 -0
- package/build/modern/queryOptions-rkZ3sk6g.d.cts +227 -0
- package/build/modern/queryOptions-rkZ3sk6g.d.cts.map +1 -0
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +2 -19
- package/build/modern/queryOptions.d.ts +2 -19
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/{types-BzZi3_rS.d.ts → types-JpJB72CS.d.cts} +19 -1
- package/build/modern/types-JpJB72CS.d.cts.map +1 -0
- package/build/{legacy/types-BzZi3_rS.d.ts → modern/types-JpJB72CS.d.ts} +19 -1
- package/build/modern/types-JpJB72CS.d.ts.map +1 -0
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/useBaseQuery.cjs +8 -0
- package/build/modern/useBaseQuery.cjs.map +1 -1
- package/build/modern/useBaseQuery.d.cts +1 -1
- package/build/modern/useBaseQuery.d.ts +1 -1
- package/build/modern/useBaseQuery.js +8 -0
- package/build/modern/useBaseQuery.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +1 -1
- package/build/modern/useInfiniteQuery.d.ts +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +41 -0
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +47 -6
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +47 -6
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +41 -0
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +170 -9
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +164 -3
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +164 -3
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +170 -9
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +103 -0
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +111 -7
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +111 -7
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +103 -0
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.cjs +46 -0
- package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +48 -2
- package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +48 -2
- package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.js +46 -0
- package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/modern/usePrefetchQuery.cjs +37 -0
- package/build/modern/usePrefetchQuery.cjs.map +1 -1
- package/build/modern/usePrefetchQuery.d.cts +39 -2
- package/build/modern/usePrefetchQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchQuery.d.ts +39 -2
- package/build/modern/usePrefetchQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchQuery.js +37 -0
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +123 -0
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +126 -2
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +126 -2
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +123 -0
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +248 -2
- package/build/modern/useQuery.d.cts.map +1 -0
- package/build/modern/useQuery.d.ts +248 -2
- package/build/modern/useQuery.d.ts.map +1 -0
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useQueryClient.cjs +21 -0
- package/build/modern/useQueryClient.cjs.map +1 -1
- package/build/modern/useQueryClient.d.cts +22 -1
- package/build/modern/useQueryClient.d.cts.map +1 -1
- package/build/modern/useQueryClient.d.ts +22 -1
- package/build/modern/useQueryClient.d.ts.map +1 -1
- package/build/modern/useQueryClient.js +21 -0
- package/build/modern/useQueryClient.js.map +1 -1
- package/build/modern/utils.cjs +15 -0
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +16 -1
- package/build/modern/utils.d.cts.map +1 -1
- package/build/modern/utils.d.ts +16 -1
- package/build/modern/utils.d.ts.map +1 -1
- package/build/modern/utils.js +15 -0
- package/build/modern/utils.js.map +1 -1
- package/build/modern/vueQueryPlugin.cjs +34 -0
- package/build/modern/vueQueryPlugin.cjs.map +1 -1
- package/build/modern/vueQueryPlugin.d.cts +48 -2
- package/build/modern/vueQueryPlugin.d.cts.map +1 -1
- package/build/modern/vueQueryPlugin.d.ts +48 -2
- package/build/modern/vueQueryPlugin.d.ts.map +1 -1
- package/build/modern/vueQueryPlugin.js +34 -0
- package/build/modern/vueQueryPlugin.js.map +1 -1
- package/package.json +10 -10
- package/src/index.ts +9 -9
- package/src/infiniteQueryOptions.ts +86 -0
- package/src/mutationCache.ts +5 -0
- package/src/mutationOptions.ts +123 -0
- package/src/queryCache.ts +5 -0
- package/src/queryClient.ts +17 -1
- package/src/queryOptions.ts +276 -10
- package/src/types.ts +18 -0
- package/src/useBaseQuery.ts +9 -1
- package/src/useInfiniteQuery.ts +240 -0
- package/src/useIsFetching.ts +46 -3
- package/src/useMutation.ts +170 -17
- package/src/useMutationState.ts +111 -4
- package/src/usePrefetchInfiniteQuery.ts +46 -0
- package/src/usePrefetchQuery.ts +37 -0
- package/src/useQueries.ts +124 -1
- package/src/useQuery.ts +240 -79
- package/src/useQueryClient.ts +21 -0
- package/src/utils.ts +15 -0
- package/src/vueQueryPlugin.ts +46 -0
- package/build/legacy/queryClient-C07d0chJ.d.cts +0 -97
- package/build/legacy/queryClient-C07d0chJ.d.cts.map +0 -1
- package/build/legacy/queryClient-hf6i_vCa.d.ts +0 -97
- package/build/legacy/queryClient-hf6i_vCa.d.ts.map +0 -1
- package/build/legacy/queryOptions.d.cts.map +0 -1
- package/build/legacy/queryOptions.d.ts.map +0 -1
- package/build/legacy/types-BzZi3_rS.d.cts.map +0 -1
- package/build/legacy/types-BzZi3_rS.d.ts.map +0 -1
- package/build/modern/queryClient-C07d0chJ.d.cts +0 -97
- package/build/modern/queryClient-C07d0chJ.d.cts.map +0 -1
- package/build/modern/queryClient-hf6i_vCa.d.ts +0 -97
- package/build/modern/queryClient-hf6i_vCa.d.ts.map +0 -1
- package/build/modern/queryOptions.d.cts.map +0 -1
- package/build/modern/queryOptions.d.ts.map +0 -1
- package/build/modern/types-BzZi3_rS.d.cts.map +0 -1
- package/build/modern/types-BzZi3_rS.d.ts.map +0 -1
package/src/useIsFetching.ts
CHANGED
|
@@ -2,14 +2,57 @@ import { getCurrentScope, onScopeDispose, ref, watchEffect } from 'vue-demi'
|
|
|
2
2
|
import { useQueryClient } from './useQueryClient'
|
|
3
3
|
import { cloneDeepUnref } from './utils'
|
|
4
4
|
import type { Ref } from 'vue-demi'
|
|
5
|
-
import type { QueryFilters
|
|
5
|
+
import type { QueryFilters } from '@tanstack/query-core'
|
|
6
6
|
import type { MaybeRefDeep } from './types'
|
|
7
7
|
import type { QueryClient } from './queryClient'
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type UseIsFetchingFilters =
|
|
10
|
+
| MaybeRefDeep<QueryFilters>
|
|
11
|
+
| (() => MaybeRefDeep<QueryFilters>)
|
|
10
12
|
|
|
13
|
+
/**
|
|
14
|
+
* The `useIsFetching` composable returns a `ref` to the `number` of the queries that your application is
|
|
15
|
+
* loading or fetching in the background (useful for app-wide loading indicators).
|
|
16
|
+
*
|
|
17
|
+
* `fetchingFilters` may be a plain object, `MaybeRefDeep`, or a reactive getter (`() => ({ ... })`) — pass a
|
|
18
|
+
* getter if the filters themselves depend on other reactive state.
|
|
19
|
+
*
|
|
20
|
+
* @param fetchingFilters - The {@link QueryFilters} to narrow down the matched queries.
|
|
21
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
22
|
+
* will be used.
|
|
23
|
+
* @returns A `ref` to the `number` of the queries that your application is currently loading or fetching in
|
|
24
|
+
* the background.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```vue
|
|
28
|
+
* <script setup lang="ts">
|
|
29
|
+
* import { useIsFetching } from '@tanstack/vue-query'
|
|
30
|
+
*
|
|
31
|
+
* // How many queries matching the posts prefix are fetching?
|
|
32
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* <template>
|
|
36
|
+
* <span v-if="isFetchingPosts">Refreshing posts...</span>
|
|
37
|
+
* </template>
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
42
|
+
* ```vue
|
|
43
|
+
* <script setup lang="ts">
|
|
44
|
+
* import { useIsFetching } from '@tanstack/vue-query'
|
|
45
|
+
*
|
|
46
|
+
* const isFetching = useIsFetching()
|
|
47
|
+
* </script>
|
|
48
|
+
*
|
|
49
|
+
* <template>
|
|
50
|
+
* <div v-if="isFetching">Queries are fetching in the background...</div>
|
|
51
|
+
* </template>
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
11
54
|
export function useIsFetching(
|
|
12
|
-
fetchingFilters:
|
|
55
|
+
fetchingFilters: UseIsFetchingFilters = {},
|
|
13
56
|
queryClient?: QueryClient,
|
|
14
57
|
): Ref<number> {
|
|
15
58
|
if (process.env.NODE_ENV === 'development') {
|
package/src/useMutation.ts
CHANGED
|
@@ -67,18 +67,174 @@ export type UseMutationReturnType<
|
|
|
67
67
|
>['reset']
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
|
|
72
|
+
* `useMutation` is the composable for that.
|
|
73
|
+
*
|
|
74
|
+
* `options` may be a plain object, a `ref`, or a reactive getter (`() => ({ ... })`) — pass a getter
|
|
75
|
+
* if the options themselves depend on other reactive state.
|
|
76
|
+
*
|
|
77
|
+
* @see {@link mutationOptions} to share these options across multiple `useMutation` call sites, or to look
|
|
78
|
+
* the mutation up elsewhere via its `mutationKey` (e.g. with `useMutationState`).
|
|
79
|
+
* @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
|
|
80
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
81
|
+
* will be used.
|
|
82
|
+
* @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second
|
|
83
|
+
* argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
|
|
84
|
+
* mutation definition. Hook-level callbacks (passed to `options`) fire for every mutation; per-call
|
|
85
|
+
* callbacks fire only for the latest call you've made.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```vue
|
|
89
|
+
* <script setup lang="ts">
|
|
90
|
+
* import { useMutation, useQueryClient } from '@tanstack/vue-query'
|
|
91
|
+
*
|
|
92
|
+
* const queryClient = useQueryClient()
|
|
93
|
+
*
|
|
94
|
+
* const addMutation = useMutation({
|
|
95
|
+
* mutationFn: addTodo,
|
|
96
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
97
|
+
* })
|
|
98
|
+
*
|
|
99
|
+
* function onAdd() {
|
|
100
|
+
* addMutation.mutate('Item', {
|
|
101
|
+
* onError: (error) => console.error('Failed to add item:', error),
|
|
102
|
+
* })
|
|
103
|
+
* }
|
|
104
|
+
* </script>
|
|
105
|
+
*
|
|
106
|
+
* <template>
|
|
107
|
+
* <button @click="onAdd">Add</button>
|
|
108
|
+
* </template>
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* Rendering the mutation's own state, rather than just firing it off:
|
|
113
|
+
* ```vue
|
|
114
|
+
* <script setup lang="ts">
|
|
115
|
+
* import { useMutation, useQueryClient } from '@tanstack/vue-query'
|
|
116
|
+
*
|
|
117
|
+
* const queryClient = useQueryClient()
|
|
118
|
+
*
|
|
119
|
+
* const addMutation = useMutation({
|
|
120
|
+
* mutationFn: addTodo,
|
|
121
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
122
|
+
* })
|
|
123
|
+
* </script>
|
|
124
|
+
*
|
|
125
|
+
* <template>
|
|
126
|
+
* <div v-if="addMutation.isPending.value">Adding todo...</div>
|
|
127
|
+
* <div v-else>
|
|
128
|
+
* <div v-if="addMutation.isError.value">An error occurred: {{ addMutation.error.value.message }}</div>
|
|
129
|
+
* <button @click="addMutation.mutate('Item')">Add</button>
|
|
130
|
+
* </div>
|
|
131
|
+
* </template>
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* Optimistic update via `onMutate`, rolling back on `onError`:
|
|
136
|
+
* ```vue
|
|
137
|
+
* <script setup lang="ts">
|
|
138
|
+
* import { useMutation, useQueryClient } from '@tanstack/vue-query'
|
|
139
|
+
*
|
|
140
|
+
* const queryClient = useQueryClient()
|
|
141
|
+
*
|
|
142
|
+
* const addMutation = useMutation({
|
|
143
|
+
* mutationFn: addTodo,
|
|
144
|
+
* onMutate: async (newTodo: string) => {
|
|
145
|
+
* await queryClient.cancelQueries({ queryKey: ['todos'] })
|
|
146
|
+
* const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
|
|
147
|
+
*
|
|
148
|
+
* queryClient.setQueryData<Array<string>>(['todos'], (old) => [
|
|
149
|
+
* ...(old ?? []),
|
|
150
|
+
* newTodo,
|
|
151
|
+
* ])
|
|
152
|
+
*
|
|
153
|
+
* // Passed to `onError` as `onMutateResult` if the mutation fails.
|
|
154
|
+
* return { previousTodos }
|
|
155
|
+
* },
|
|
156
|
+
* onError: (_err, _newTodo, onMutateResult) => {
|
|
157
|
+
* queryClient.setQueryData(['todos'], onMutateResult?.previousTodos)
|
|
158
|
+
* },
|
|
159
|
+
* onSettled: () => {
|
|
160
|
+
* queryClient.invalidateQueries({ queryKey: ['todos'] })
|
|
161
|
+
* },
|
|
162
|
+
* })
|
|
163
|
+
* </script>
|
|
164
|
+
*
|
|
165
|
+
* <template>
|
|
166
|
+
* <button @click="addMutation.mutate('Item')">Add</button>
|
|
167
|
+
* </template>
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
|
|
172
|
+
* promise per call instead, so you can wait for all of them when they succeed:
|
|
173
|
+
* ```vue
|
|
174
|
+
* <script setup lang="ts">
|
|
175
|
+
* import { useMutation, useQueryClient } from '@tanstack/vue-query'
|
|
176
|
+
*
|
|
177
|
+
* const queryClient = useQueryClient()
|
|
178
|
+
*
|
|
179
|
+
* const addMutation = useMutation({
|
|
180
|
+
* mutationFn: addTodo,
|
|
181
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
182
|
+
* })
|
|
183
|
+
*
|
|
184
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
185
|
+
* try {
|
|
186
|
+
* await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
|
|
187
|
+
* } catch (error) {
|
|
188
|
+
* console.error('Failed to add todos:', error)
|
|
189
|
+
* }
|
|
190
|
+
* }
|
|
191
|
+
* </script>
|
|
192
|
+
*
|
|
193
|
+
* <template>
|
|
194
|
+
* <button @click="handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])">Add all</button>
|
|
195
|
+
* </template>
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* If some of the mutations above can fail independently of the others, and you want to know which ones
|
|
200
|
+
* did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
|
|
201
|
+
* `Promise.allSettled`:
|
|
202
|
+
* ```vue
|
|
203
|
+
* <script setup lang="ts">
|
|
204
|
+
* import { useMutation, useQueryClient } from '@tanstack/vue-query'
|
|
205
|
+
*
|
|
206
|
+
* const queryClient = useQueryClient()
|
|
207
|
+
*
|
|
208
|
+
* const addMutation = useMutation({
|
|
209
|
+
* mutationFn: addTodo,
|
|
210
|
+
* onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
|
|
211
|
+
* })
|
|
212
|
+
*
|
|
213
|
+
* async function handleAddAll(todos: Array<string>) {
|
|
214
|
+
* const addResults = await Promise.allSettled(
|
|
215
|
+
* todos.map((todo) => addMutation.mutateAsync(todo)),
|
|
216
|
+
* )
|
|
217
|
+
*
|
|
218
|
+
* addResults.forEach((addResult, index) => {
|
|
219
|
+
* if (addResult.status === 'rejected') {
|
|
220
|
+
* console.error(`Failed to add "${todos[index]}":`, addResult.reason)
|
|
221
|
+
* }
|
|
222
|
+
* })
|
|
223
|
+
* }
|
|
224
|
+
* </script>
|
|
225
|
+
*
|
|
226
|
+
* <template>
|
|
227
|
+
* <button @click="handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])">Add all</button>
|
|
228
|
+
* </template>
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
70
231
|
export function useMutation<
|
|
71
232
|
TData = unknown,
|
|
72
233
|
TError = DefaultError,
|
|
73
234
|
TVariables = void,
|
|
74
235
|
TOnMutateResult = unknown,
|
|
75
236
|
>(
|
|
76
|
-
|
|
77
|
-
TData,
|
|
78
|
-
TError,
|
|
79
|
-
TVariables,
|
|
80
|
-
TOnMutateResult
|
|
81
|
-
>,
|
|
237
|
+
options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
|
|
82
238
|
queryClient?: QueryClient,
|
|
83
239
|
): UseMutationReturnType<TData, TError, TVariables, TOnMutateResult> {
|
|
84
240
|
if (process.env.NODE_ENV === 'development') {
|
|
@@ -90,15 +246,12 @@ export function useMutation<
|
|
|
90
246
|
}
|
|
91
247
|
|
|
92
248
|
const client = queryClient || useQueryClient()
|
|
93
|
-
const
|
|
94
|
-
const resolvedOptions =
|
|
95
|
-
typeof mutationOptions === 'function'
|
|
96
|
-
? mutationOptions()
|
|
97
|
-
: mutationOptions
|
|
249
|
+
const defaultedOptions = computed(() => {
|
|
250
|
+
const resolvedOptions = typeof options === 'function' ? options() : options
|
|
98
251
|
return client.defaultMutationOptions(cloneDeepUnref(resolvedOptions))
|
|
99
252
|
})
|
|
100
|
-
const observer = new MutationObserver(client,
|
|
101
|
-
const state =
|
|
253
|
+
const observer = new MutationObserver(client, defaultedOptions.value)
|
|
254
|
+
const state = defaultedOptions.value.shallow
|
|
102
255
|
? shallowReactive(observer.getCurrentResult())
|
|
103
256
|
: reactive(observer.getCurrentResult())
|
|
104
257
|
|
|
@@ -116,15 +269,15 @@ export function useMutation<
|
|
|
116
269
|
})
|
|
117
270
|
}
|
|
118
271
|
|
|
119
|
-
watch(
|
|
120
|
-
observer.setOptions(
|
|
272
|
+
watch(defaultedOptions, () => {
|
|
273
|
+
observer.setOptions(defaultedOptions.value)
|
|
121
274
|
})
|
|
122
275
|
|
|
123
276
|
onScopeDispose(() => {
|
|
124
277
|
unsubscribe()
|
|
125
278
|
})
|
|
126
279
|
|
|
127
|
-
const readonlyState =
|
|
280
|
+
const readonlyState = defaultedOptions.value.shallow
|
|
128
281
|
? shallowReadonly(state)
|
|
129
282
|
: readonly(state)
|
|
130
283
|
|
|
@@ -137,7 +290,7 @@ export function useMutation<
|
|
|
137
290
|
(error) => {
|
|
138
291
|
if (
|
|
139
292
|
error &&
|
|
140
|
-
shouldThrowError(
|
|
293
|
+
shouldThrowError(defaultedOptions.value.throwOnError, [error as TError])
|
|
141
294
|
) {
|
|
142
295
|
throw error
|
|
143
296
|
}
|
package/src/useMutationState.ts
CHANGED
|
@@ -10,18 +10,48 @@ import { useQueryClient } from './useQueryClient'
|
|
|
10
10
|
import { cloneDeepUnref } from './utils'
|
|
11
11
|
import type { Ref } from 'vue-demi'
|
|
12
12
|
import type {
|
|
13
|
-
MutationFilters as MF,
|
|
14
13
|
Mutation,
|
|
14
|
+
MutationFilters,
|
|
15
15
|
MutationState,
|
|
16
16
|
} from '@tanstack/query-core'
|
|
17
17
|
import type { QueryClient } from './queryClient'
|
|
18
18
|
import type { MaybeRefDeep } from './types'
|
|
19
19
|
import type { MutationCache } from './mutationCache'
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
type VueMutationFilters = MaybeRefDeep<MutationFilters>
|
|
22
22
|
|
|
23
|
+
export type UseIsMutatingFilters =
|
|
24
|
+
| VueMutationFilters
|
|
25
|
+
| (() => VueMutationFilters)
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The `useIsMutating` composable returns a `ref` to the `number` of mutations that your application currently
|
|
29
|
+
* has `pending` (useful for app-wide loading indicators).
|
|
30
|
+
*
|
|
31
|
+
* `filters` may be a plain object, `MaybeRefDeep`, or a reactive getter (`() => ({ ... })`) — pass a getter if
|
|
32
|
+
* the filters themselves depend on other reactive state.
|
|
33
|
+
*
|
|
34
|
+
* @param filters - The {@link MutationFilters} to narrow down the matched mutations.
|
|
35
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
36
|
+
* will be used.
|
|
37
|
+
* @returns A `ref` to the `number` of the mutations that your application currently has `pending`.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```vue
|
|
41
|
+
* <script setup lang="ts">
|
|
42
|
+
* import { useIsMutating } from '@tanstack/vue-query'
|
|
43
|
+
*
|
|
44
|
+
* // How many mutations matching the posts prefix are in progress?
|
|
45
|
+
* const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
|
|
46
|
+
* </script>
|
|
47
|
+
*
|
|
48
|
+
* <template>
|
|
49
|
+
* <span v-if="isMutatingPosts">Saving posts...</span>
|
|
50
|
+
* </template>
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
23
53
|
export function useIsMutating(
|
|
24
|
-
filters:
|
|
54
|
+
filters: UseIsMutatingFilters = {},
|
|
25
55
|
queryClient?: QueryClient,
|
|
26
56
|
): Ref<number> {
|
|
27
57
|
if (process.env.NODE_ENV === 'development') {
|
|
@@ -64,7 +94,7 @@ export type MutationStateOptions<
|
|
|
64
94
|
TMutation extends Mutation<any, any, any, any> =
|
|
65
95
|
MutationTypeFromResult<TResult>,
|
|
66
96
|
> = {
|
|
67
|
-
filters?:
|
|
97
|
+
filters?: VueMutationFilters
|
|
68
98
|
select?: (mutation: TMutation) => TResult
|
|
69
99
|
}
|
|
70
100
|
|
|
@@ -86,6 +116,83 @@ function getResult<
|
|
|
86
116
|
)
|
|
87
117
|
}
|
|
88
118
|
|
|
119
|
+
/**
|
|
120
|
+
* `useMutationState` is a composable that gives you access to all mutations in the `MutationCache`. You can
|
|
121
|
+
* pass `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the
|
|
122
|
+
* mutation state.
|
|
123
|
+
*
|
|
124
|
+
* `options` may be a plain object or a reactive getter (`() => ({ ... })`) — pass a getter if the filters
|
|
125
|
+
* themselves depend on other reactive state.
|
|
126
|
+
*
|
|
127
|
+
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
128
|
+
* mutation state.
|
|
129
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
130
|
+
* will be used.
|
|
131
|
+
* @returns A `ref` to an Array of whatever `select` returns for each matching mutation.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* Get all variables of all running mutations:
|
|
135
|
+
* ```vue
|
|
136
|
+
* <script setup lang="ts">
|
|
137
|
+
* import { useMutationState } from '@tanstack/vue-query'
|
|
138
|
+
*
|
|
139
|
+
* const pendingVariables = useMutationState({
|
|
140
|
+
* filters: { status: 'pending' },
|
|
141
|
+
* select: (mutation) => mutation.state.variables,
|
|
142
|
+
* })
|
|
143
|
+
* </script>
|
|
144
|
+
*
|
|
145
|
+
* <template>{{ pendingVariables.length }} posts saving...</template>
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* Get all data for specific mutations via the `mutationKey`:
|
|
150
|
+
* ```vue
|
|
151
|
+
* <script setup lang="ts">
|
|
152
|
+
* import { useMutation, useMutationState } from '@tanstack/vue-query'
|
|
153
|
+
*
|
|
154
|
+
* const mutationKey = ['posts']
|
|
155
|
+
*
|
|
156
|
+
* // Some mutation that we want to get the state for
|
|
157
|
+
* const mutation = useMutation({
|
|
158
|
+
* mutationKey,
|
|
159
|
+
* mutationFn: createPosts,
|
|
160
|
+
* })
|
|
161
|
+
*
|
|
162
|
+
* const savedPosts = useMutationState({
|
|
163
|
+
* // this mutation key needs to match the mutation key of the given mutation (see above)
|
|
164
|
+
* filters: { mutationKey, status: 'success' },
|
|
165
|
+
* select: (mutation) => mutation.state.data,
|
|
166
|
+
* })
|
|
167
|
+
* </script>
|
|
168
|
+
*
|
|
169
|
+
* <template>
|
|
170
|
+
* <button @click="mutation.mutate(['New Post'])">
|
|
171
|
+
* Create post ({{ savedPosts.length }} saved so far)
|
|
172
|
+
* </button>
|
|
173
|
+
* </template>
|
|
174
|
+
* ```
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new
|
|
178
|
+
* entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the
|
|
179
|
+
* last item that `useMutationState` returns to get the latest successful invocation:
|
|
180
|
+
* ```vue
|
|
181
|
+
* <script setup lang="ts">
|
|
182
|
+
* import { computed } from 'vue'
|
|
183
|
+
* import { useMutationState } from '@tanstack/vue-query'
|
|
184
|
+
*
|
|
185
|
+
* const savedPosts = useMutationState({
|
|
186
|
+
* filters: { mutationKey: ['posts'], status: 'success' },
|
|
187
|
+
* select: (mutation) => mutation.state.data,
|
|
188
|
+
* })
|
|
189
|
+
*
|
|
190
|
+
* const latestSavedPost = computed(() => savedPosts.value[savedPosts.value.length - 1])
|
|
191
|
+
* </script>
|
|
192
|
+
*
|
|
193
|
+
* <template>{{ latestSavedPost ? 'Saved' : 'Nothing saved yet' }}</template>
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
89
196
|
export function useMutationState<
|
|
90
197
|
TResult = MutationState,
|
|
91
198
|
TMutation extends Mutation<any, any, any, any> =
|
|
@@ -45,6 +45,52 @@ function isGetter<T>(value: MaybeRefOrGetter<T>): value is () => T {
|
|
|
45
45
|
return typeof value === 'function'
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* `usePrefetchInfiniteQuery` does not return anything — it fires a prefetch as a reactive side effect, useful
|
|
50
|
+
* for kicking off a fetch ahead of the component that will actually render the data with `useInfiniteQuery`.
|
|
51
|
+
* You can pass everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`,
|
|
52
|
+
* though `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required
|
|
53
|
+
* unless a default query function has been defined.
|
|
54
|
+
*
|
|
55
|
+
* `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
|
|
56
|
+
* as well as pageParam information, and should return a single variable that will be passed to your query
|
|
57
|
+
* function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
|
|
58
|
+
*
|
|
59
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
60
|
+
* over from a previous attempt — so it won't refetch data that's already there or already in flight. It
|
|
61
|
+
* re-runs whenever a reactive dependency in `options` (built with `infiniteQueryOptions`, for example) changes.
|
|
62
|
+
*
|
|
63
|
+
* Fire this during render, before a suspense boundary that wraps a component using `useInfiniteQuery`'s
|
|
64
|
+
* `suspense()` — see the {@link https://tanstack.com/query/latest/docs/framework/vue/guides/suspense | Suspense guide}.
|
|
65
|
+
*
|
|
66
|
+
* @param options - A `ref`, plain value, or reactive getter resolving to the
|
|
67
|
+
* {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
68
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
69
|
+
* will be used.
|
|
70
|
+
* @returns `void` — nothing is returned.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```vue
|
|
74
|
+
* <script setup lang="ts">
|
|
75
|
+
* import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/vue-query'
|
|
76
|
+
* import Projects from './Projects.vue'
|
|
77
|
+
*
|
|
78
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
79
|
+
* queryKey: ['projects'],
|
|
80
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
81
|
+
* initialPageParam: 0,
|
|
82
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
83
|
+
* })
|
|
84
|
+
*
|
|
85
|
+
* // Fire the prefetch as soon as this component runs, before `Projects` mounts and calls `useInfiniteQuery`.
|
|
86
|
+
* usePrefetchInfiniteQuery(projectsOptions)
|
|
87
|
+
* </script>
|
|
88
|
+
*
|
|
89
|
+
* <template>
|
|
90
|
+
* <Projects />
|
|
91
|
+
* </template>
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
48
94
|
export function usePrefetchInfiniteQuery<
|
|
49
95
|
TQueryFnData = unknown,
|
|
50
96
|
TError = DefaultError,
|
package/src/usePrefetchQuery.ts
CHANGED
|
@@ -46,6 +46,43 @@ function isGetter<T>(value: MaybeRefOrGetter<T>): value is () => T {
|
|
|
46
46
|
return typeof value === 'function'
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
/**
|
|
50
|
+
* `usePrefetchQuery` does not return anything — it fires a prefetch as a reactive side effect, useful for
|
|
51
|
+
* kicking off a fetch ahead of the component that will actually render the data with `useQuery`. You can pass
|
|
52
|
+
* everything to `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always
|
|
53
|
+
* required, and `queryFn` is required unless a default query function has been defined.
|
|
54
|
+
*
|
|
55
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
56
|
+
* over from a previous attempt — so it won't refetch data that's already there or already in flight. It
|
|
57
|
+
* re-runs whenever a reactive dependency in `options` (built with `queryOptions`, for example) changes.
|
|
58
|
+
*
|
|
59
|
+
* Fire this during render, before a suspense boundary that wraps a component using `useQuery`'s `suspense()`
|
|
60
|
+
* — see the {@link https://tanstack.com/query/latest/docs/framework/vue/guides/suspense | Suspense guide}.
|
|
61
|
+
*
|
|
62
|
+
* @param options - A `ref`, plain value, or reactive getter resolving to the {@link UsePrefetchQueryOptions} to
|
|
63
|
+
* use — everything you can pass to `queryClient.query`.
|
|
64
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
65
|
+
* will be used.
|
|
66
|
+
* @returns `void` — nothing is returned.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```vue
|
|
70
|
+
* <script setup lang="ts">
|
|
71
|
+
* import { usePrefetchQuery } from '@tanstack/vue-query'
|
|
72
|
+
* import Posts from './Posts.vue'
|
|
73
|
+
*
|
|
74
|
+
* // Fire the prefetch as soon as this component runs, before `Posts` mounts and calls `useQuery`.
|
|
75
|
+
* usePrefetchQuery({
|
|
76
|
+
* queryKey: ['posts'],
|
|
77
|
+
* queryFn: fetchPosts,
|
|
78
|
+
* })
|
|
79
|
+
* </script>
|
|
80
|
+
*
|
|
81
|
+
* <template>
|
|
82
|
+
* <Posts />
|
|
83
|
+
* </template>
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
49
86
|
export function usePrefetchQuery<
|
|
50
87
|
TQueryFnData = unknown,
|
|
51
88
|
TError = DefaultError,
|
package/src/useQueries.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
QueryObserverResult,
|
|
23
23
|
ThrowOnError,
|
|
24
24
|
} from '@tanstack/query-core'
|
|
25
|
-
import type { UseQueryOptions } from './
|
|
25
|
+
import type { UseQueryOptions } from './queryOptions'
|
|
26
26
|
import type { QueryClient } from './queryClient'
|
|
27
27
|
import type { DeepUnwrapRef, MaybeRefDeep, ShallowOption } from './types'
|
|
28
28
|
|
|
@@ -231,6 +231,129 @@ type UseQueriesOptionsArg<T extends Array<any>> = readonly [
|
|
|
231
231
|
...UseQueriesOptions<T>,
|
|
232
232
|
]
|
|
233
233
|
|
|
234
|
+
/**
|
|
235
|
+
* The `useQueries` composable can be used to fetch a variable number of queries.
|
|
236
|
+
*
|
|
237
|
+
* The `queries` option accepts an array of query option objects mostly identical to `useQuery`'s. It may be a
|
|
238
|
+
* plain array, a `ref`/reactive array (each entry tracked individually), or a getter function — pass a getter
|
|
239
|
+
* if the array itself (its length, or which queries are in it) depends on other reactive state.
|
|
240
|
+
*
|
|
241
|
+
* Having the same query key more than once in the array of query objects may cause some data to be shared
|
|
242
|
+
* between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
|
|
243
|
+
* structure.
|
|
244
|
+
*
|
|
245
|
+
* The `combine` option can be used to combine the results of the queries into a single value. The result will
|
|
246
|
+
* be structurally shared to be as referentially stable as possible.
|
|
247
|
+
*
|
|
248
|
+
* `placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from
|
|
249
|
+
* previously rendered queries, because the number of queries can differ between renders.
|
|
250
|
+
*
|
|
251
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
252
|
+
* will be used.
|
|
253
|
+
* @returns A `ref` to the combined result. Without `combine`, this is an array with all the query results, in
|
|
254
|
+
* the same order as the input. When `combine` is provided, this is the value returned by `combine` instead.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```vue
|
|
258
|
+
* <script setup lang="ts">
|
|
259
|
+
* import { useQueries } from '@tanstack/vue-query'
|
|
260
|
+
*
|
|
261
|
+
* const props = defineProps<{ ids: Array<number> }>()
|
|
262
|
+
*
|
|
263
|
+
* const postQueries = useQueries({
|
|
264
|
+
* queries: () =>
|
|
265
|
+
* props.ids.map((id) => ({
|
|
266
|
+
* queryKey: ['post', id],
|
|
267
|
+
* queryFn: () => fetchPost(id),
|
|
268
|
+
* staleTime: Infinity,
|
|
269
|
+
* })),
|
|
270
|
+
* })
|
|
271
|
+
* </script>
|
|
272
|
+
*
|
|
273
|
+
* <template>
|
|
274
|
+
* <ul>
|
|
275
|
+
* <li v-for="(query, index) in postQueries" :key="props.ids[index]">
|
|
276
|
+
* <span v-if="query.isPending">Loading...</span>
|
|
277
|
+
* <span v-else-if="query.isError">Error: {{ query.error.message }}</span>
|
|
278
|
+
* <span v-else>{{ query.data.title }}</span>
|
|
279
|
+
* </li>
|
|
280
|
+
* </ul>
|
|
281
|
+
* </template>
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* Combining results into a single value:
|
|
286
|
+
* ```vue
|
|
287
|
+
* <script setup lang="ts">
|
|
288
|
+
* import { useQueries } from '@tanstack/vue-query'
|
|
289
|
+
*
|
|
290
|
+
* const props = defineProps<{ ids: Array<number> }>()
|
|
291
|
+
*
|
|
292
|
+
* const combined = useQueries({
|
|
293
|
+
* queries: () =>
|
|
294
|
+
* props.ids.map((id) => ({
|
|
295
|
+
* queryKey: ['post', id],
|
|
296
|
+
* queryFn: () => fetchPost(id),
|
|
297
|
+
* })),
|
|
298
|
+
* combine: (postQueries) => ({
|
|
299
|
+
* data: postQueries.map((query) => query.data),
|
|
300
|
+
* isPending: postQueries.some((query) => query.isPending),
|
|
301
|
+
* isError: postQueries.some((query) => query.isError),
|
|
302
|
+
* }),
|
|
303
|
+
* })
|
|
304
|
+
* </script>
|
|
305
|
+
*
|
|
306
|
+
* <template>
|
|
307
|
+
* <span v-if="combined.isPending">Loading...</span>
|
|
308
|
+
* <span v-else-if="combined.isError">Error!</span>
|
|
309
|
+
* <ul v-else>
|
|
310
|
+
* <li v-for="post in combined.data" :key="post.id">{{ post.title }}</li>
|
|
311
|
+
* </ul>
|
|
312
|
+
* </template>
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* Typing `select` via {@link queryOptions}. Note that spreading a `queryOptions` result and overriding
|
|
317
|
+
* `select` inline still falls back to `unknown` — wrap the spread in `queryOptions` again so the override is
|
|
318
|
+
* resolved before it reaches `useQueries`:
|
|
319
|
+
* ```vue
|
|
320
|
+
* <script setup lang="ts">
|
|
321
|
+
* import { queryOptions, useQueries } from '@tanstack/vue-query'
|
|
322
|
+
*
|
|
323
|
+
* const props = defineProps<{ id: number }>()
|
|
324
|
+
*
|
|
325
|
+
* const postOptions = (id: number) =>
|
|
326
|
+
* queryOptions({
|
|
327
|
+
* queryKey: ['post', id],
|
|
328
|
+
* queryFn: () => fetchPost(id),
|
|
329
|
+
* })
|
|
330
|
+
*
|
|
331
|
+
* const [{ data: broken }] = useQueries({
|
|
332
|
+
* queries: () => [
|
|
333
|
+
* {
|
|
334
|
+
* ...postOptions(props.id),
|
|
335
|
+
* // ❌ `data` is `unknown` here
|
|
336
|
+
* select: (data) => data.title,
|
|
337
|
+
* },
|
|
338
|
+
* ],
|
|
339
|
+
* })
|
|
340
|
+
*
|
|
341
|
+
* const [{ data: fixed }] = useQueries({
|
|
342
|
+
* queries: () => [
|
|
343
|
+
* queryOptions({
|
|
344
|
+
* ...postOptions(props.id),
|
|
345
|
+
* // ✅ `data` is `Post`
|
|
346
|
+
* select: (data) => data.title,
|
|
347
|
+
* }),
|
|
348
|
+
* ],
|
|
349
|
+
* })
|
|
350
|
+
* </script>
|
|
351
|
+
*
|
|
352
|
+
* <template>
|
|
353
|
+
* <h1>{{ fixed }}</h1>
|
|
354
|
+
* </template>
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
234
357
|
export function useQueries<
|
|
235
358
|
T extends Array<any>,
|
|
236
359
|
TCombinedResult = UseQueriesResults<T>,
|