@tanstack/vue-query 5.107.0 → 5.107.2
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/README.md +2 -3
- package/build/legacy/devtools/devtools.cjs +1 -1
- package/build/legacy/devtools/devtools.cjs.map +1 -1
- package/build/legacy/devtools/devtools.d.cts +1 -1
- package/build/legacy/devtools/devtools.d.ts +1 -1
- package/build/legacy/devtools/devtools.js +1 -1
- package/build/legacy/devtools/devtools.js.map +1 -1
- package/build/legacy/index.cjs +1 -1
- package/build/legacy/index.d.cts +4 -4
- package/build/legacy/index.d.ts +4 -4
- package/build/legacy/index.js +1 -1
- package/build/legacy/{infiniteQueryOptions-lJ_zR-As.d.cts → infiniteQueryOptions-CLfd_vOE.d.cts} +61 -17
- package/build/legacy/infiniteQueryOptions-CLfd_vOE.d.cts.map +1 -0
- package/build/legacy/{infiniteQueryOptions-CN37iZvc.d.ts → infiniteQueryOptions-vEnw-JaW.d.ts} +61 -17
- package/build/legacy/infiniteQueryOptions-vEnw-JaW.d.ts.map +1 -0
- package/build/legacy/infiniteQueryOptions.d.cts +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +1 -1
- package/build/{modern/queryClient-Ba4HSFnn.d.ts → legacy/queryClient-6AWIHAhP.d.ts} +2 -2
- package/build/legacy/{queryClient-Ba4HSFnn.d.ts.map → queryClient-6AWIHAhP.d.ts.map} +1 -1
- package/build/{modern/queryClient-BGn8uIej.d.cts → legacy/queryClient-yJW8K_VP.d.cts} +2 -2
- package/build/legacy/{queryClient-BGn8uIej.d.cts.map → queryClient-yJW8K_VP.d.cts.map} +1 -1
- package/build/legacy/queryClient.cjs +1 -1
- 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 +1 -1
- package/build/legacy/queryClient.js.map +1 -1
- package/build/{modern/queryOptions-BuZEBLtg.d.cts → legacy/queryOptions-nndmAASX.d.cts} +22 -11
- package/build/legacy/queryOptions-nndmAASX.d.cts.map +1 -0
- package/build/{modern/queryOptions-dXiBZnGk.d.ts → legacy/queryOptions-v0l6M7Eo.d.ts} +22 -11
- package/build/legacy/queryOptions-v0l6M7Eo.d.ts.map +1 -0
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +2 -2
- package/build/legacy/queryOptions.d.ts +2 -2
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/types-u_Tu5Ybs.d.cts.map +1 -1
- package/build/legacy/types-u_Tu5Ybs.d.ts.map +1 -1
- package/build/legacy/useBaseQuery.cjs +11 -5
- 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 +11 -5
- 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.map +1 -1
- package/build/legacy/useIsFetching.d.cts +1 -1
- package/build/legacy/useIsFetching.d.ts +1 -1
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +1 -1
- package/build/legacy/useMutation.d.ts +1 -1
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +1 -1
- package/build/legacy/useMutationState.d.ts +1 -1
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/legacy/usePrefetchQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +1 -1
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +1 -1
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +2 -2
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +2 -2
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +1 -1
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +56 -22
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +56 -22
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useQueryClient.cjs.map +1 -1
- package/build/legacy/useQueryClient.d.cts +1 -1
- package/build/legacy/useQueryClient.d.cts.map +1 -1
- package/build/legacy/useQueryClient.d.ts +1 -1
- package/build/legacy/useQueryClient.d.ts.map +1 -1
- package/build/legacy/useQueryClient.js.map +1 -1
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.js.map +1 -1
- package/build/legacy/vueQueryPlugin.cjs +1 -1
- package/build/legacy/vueQueryPlugin.cjs.map +1 -1
- package/build/legacy/vueQueryPlugin.d.cts +1 -1
- package/build/legacy/vueQueryPlugin.d.cts.map +1 -1
- package/build/legacy/vueQueryPlugin.d.ts +1 -1
- package/build/legacy/vueQueryPlugin.d.ts.map +1 -1
- package/build/legacy/vueQueryPlugin.js +1 -1
- package/build/legacy/vueQueryPlugin.js.map +1 -1
- package/build/modern/devtools/devtools.cjs +1 -1
- package/build/modern/devtools/devtools.cjs.map +1 -1
- package/build/modern/devtools/devtools.d.cts +1 -1
- package/build/modern/devtools/devtools.d.ts +1 -1
- package/build/modern/devtools/devtools.js +1 -1
- package/build/modern/devtools/devtools.js.map +1 -1
- package/build/modern/index.cjs +1 -1
- package/build/modern/index.d.cts +4 -4
- package/build/modern/index.d.ts +4 -4
- package/build/modern/index.js +1 -1
- package/build/modern/{infiniteQueryOptions-lJ_zR-As.d.cts → infiniteQueryOptions-CLfd_vOE.d.cts} +61 -17
- package/build/modern/infiniteQueryOptions-CLfd_vOE.d.cts.map +1 -0
- package/build/modern/{infiniteQueryOptions-CN37iZvc.d.ts → infiniteQueryOptions-vEnw-JaW.d.ts} +61 -17
- package/build/modern/infiniteQueryOptions-vEnw-JaW.d.ts.map +1 -0
- package/build/modern/infiniteQueryOptions.d.cts +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +1 -1
- package/build/{legacy/queryClient-Ba4HSFnn.d.ts → modern/queryClient-6AWIHAhP.d.ts} +2 -2
- package/build/modern/{queryClient-Ba4HSFnn.d.ts.map → queryClient-6AWIHAhP.d.ts.map} +1 -1
- package/build/{legacy/queryClient-BGn8uIej.d.cts → modern/queryClient-yJW8K_VP.d.cts} +2 -2
- package/build/modern/{queryClient-BGn8uIej.d.cts.map → queryClient-yJW8K_VP.d.cts.map} +1 -1
- package/build/modern/queryClient.cjs +1 -1
- 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 +1 -1
- package/build/modern/queryClient.js.map +1 -1
- package/build/{legacy/queryOptions-BuZEBLtg.d.cts → modern/queryOptions-nndmAASX.d.cts} +22 -11
- package/build/modern/queryOptions-nndmAASX.d.cts.map +1 -0
- package/build/{legacy/queryOptions-dXiBZnGk.d.ts → modern/queryOptions-v0l6M7Eo.d.ts} +22 -11
- package/build/modern/queryOptions-v0l6M7Eo.d.ts.map +1 -0
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +2 -2
- package/build/modern/queryOptions.d.ts +2 -2
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/types-u_Tu5Ybs.d.cts.map +1 -1
- package/build/modern/types-u_Tu5Ybs.d.ts.map +1 -1
- package/build/modern/useBaseQuery.cjs +11 -5
- 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 +11 -5
- 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.map +1 -1
- package/build/modern/useIsFetching.d.cts +1 -1
- package/build/modern/useIsFetching.d.ts +1 -1
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +1 -1
- package/build/modern/useMutation.d.ts +1 -1
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +1 -1
- package/build/modern/useMutationState.d.ts +1 -1
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +1 -1
- package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/modern/usePrefetchQuery.cjs.map +1 -1
- package/build/modern/usePrefetchQuery.d.cts +1 -1
- package/build/modern/usePrefetchQuery.d.ts +1 -1
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +1 -1
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +2 -2
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +2 -2
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +1 -1
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +56 -22
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +56 -22
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useQueryClient.cjs.map +1 -1
- package/build/modern/useQueryClient.d.cts +1 -1
- package/build/modern/useQueryClient.d.cts.map +1 -1
- package/build/modern/useQueryClient.d.ts +1 -1
- package/build/modern/useQueryClient.d.ts.map +1 -1
- package/build/modern/useQueryClient.js.map +1 -1
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.js.map +1 -1
- package/build/modern/vueQueryPlugin.cjs +1 -1
- package/build/modern/vueQueryPlugin.cjs.map +1 -1
- package/build/modern/vueQueryPlugin.d.cts +1 -1
- package/build/modern/vueQueryPlugin.d.cts.map +1 -1
- package/build/modern/vueQueryPlugin.d.ts +1 -1
- package/build/modern/vueQueryPlugin.d.ts.map +1 -1
- package/build/modern/vueQueryPlugin.js +1 -1
- package/build/modern/vueQueryPlugin.js.map +1 -1
- package/package.json +12 -2
- package/src/devtools/devtools.ts +2 -2
- package/src/index.ts +2 -0
- package/src/queryClient.ts +12 -12
- package/src/queryOptions.ts +40 -23
- package/src/types.ts +2 -0
- package/src/useBaseQuery.ts +71 -11
- package/src/useInfiniteQuery.ts +32 -28
- package/src/useIsFetching.ts +2 -2
- package/src/useMutation.ts +4 -4
- package/src/useMutationState.ts +2 -2
- package/src/usePrefetchInfiniteQuery.ts +1 -1
- package/src/usePrefetchQuery.ts +1 -1
- package/src/useQueries.ts +9 -5
- package/src/useQuery.ts +55 -21
- package/src/useQueryClient.ts +0 -1
- package/src/utils.ts +2 -2
- package/src/vueQueryPlugin.ts +2 -3
- package/build/legacy/infiniteQueryOptions-CN37iZvc.d.ts.map +0 -1
- package/build/legacy/infiniteQueryOptions-lJ_zR-As.d.cts.map +0 -1
- package/build/legacy/queryOptions-BuZEBLtg.d.cts.map +0 -1
- package/build/legacy/queryOptions-dXiBZnGk.d.ts.map +0 -1
- package/build/modern/infiniteQueryOptions-CN37iZvc.d.ts.map +0 -1
- package/build/modern/infiniteQueryOptions-lJ_zR-As.d.cts.map +0 -1
- package/build/modern/queryOptions-BuZEBLtg.d.cts.map +0 -1
- package/build/modern/queryOptions-dXiBZnGk.d.ts.map +0 -1
package/src/queryOptions.ts
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DeepUnwrapRef,
|
|
3
|
-
MaybeRef,
|
|
4
|
-
MaybeRefDeep,
|
|
5
|
-
MaybeRefOrGetter,
|
|
6
|
-
ShallowOption,
|
|
7
|
-
} from './types'
|
|
8
1
|
import type {
|
|
9
2
|
DefaultError,
|
|
10
3
|
InitialDataFunction,
|
|
@@ -14,6 +7,29 @@ import type {
|
|
|
14
7
|
QueryKeyWithDataTag,
|
|
15
8
|
QueryObserverOptions,
|
|
16
9
|
} from '@tanstack/query-core'
|
|
10
|
+
import type {
|
|
11
|
+
DeepUnwrapRef,
|
|
12
|
+
MaybeRef,
|
|
13
|
+
MaybeRefDeep,
|
|
14
|
+
MaybeRefOrGetter,
|
|
15
|
+
ShallowOption,
|
|
16
|
+
} from './types'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `enabled` for Vue Query. A `ref` or `computed` is reactive, while a plain boolean isn't tracked on its own. A
|
|
20
|
+
* zero-argument getter (`() => ...`) is reactive too: it is called while tracking dependencies. query-core's
|
|
21
|
+
* `(query) => boolean` is kept as that callback and is not tracked by Vue — a `ref` read inside it is current
|
|
22
|
+
* when core calls it, and a `ref` change does not by itself call it again. The two are told apart by the number
|
|
23
|
+
* of declared parameters, so declare `query` as a required parameter.
|
|
24
|
+
*/
|
|
25
|
+
export type VueQueryEnabledOption<
|
|
26
|
+
TQueryFnData,
|
|
27
|
+
TError,
|
|
28
|
+
TQueryData,
|
|
29
|
+
TQueryKey extends QueryKey,
|
|
30
|
+
> =
|
|
31
|
+
| MaybeRefOrGetter<boolean | undefined>
|
|
32
|
+
| QueryBooleanOption<TQueryFnData, TError, TQueryData, TQueryKey>
|
|
17
33
|
|
|
18
34
|
/**
|
|
19
35
|
* The plain, unwrapped options that `queryOptions` hands back, and what `useQuery`, `useQueries`, and the
|
|
@@ -21,7 +37,8 @@ import type {
|
|
|
21
37
|
* {@link UseQueryOptions}, which accepts the same options as `ref`s and `computed`s too.
|
|
22
38
|
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
23
39
|
* @template TError - The type of errors your `queryFn` may throw.
|
|
24
|
-
* @template TData - The type `data` ends up as after `select` runs.
|
|
40
|
+
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
|
|
41
|
+
* `select` is used.
|
|
25
42
|
* @template TQueryData - The type of data stored in the cache, before `select` runs. Defaults to
|
|
26
43
|
* `TQueryFnData` and can be configured independently of it.
|
|
27
44
|
* @template TQueryKey - The type of your `queryKey`.
|
|
@@ -42,14 +59,12 @@ export type QueryOptions<
|
|
|
42
59
|
TQueryKey
|
|
43
60
|
>
|
|
44
61
|
]: Property extends 'enabled'
|
|
45
|
-
?
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
DeepUnwrapRef<TQueryKey>
|
|
52
|
-
>)
|
|
62
|
+
? VueQueryEnabledOption<
|
|
63
|
+
TQueryFnData,
|
|
64
|
+
TError,
|
|
65
|
+
TQueryData,
|
|
66
|
+
DeepUnwrapRef<TQueryKey>
|
|
67
|
+
>
|
|
53
68
|
: Property extends 'queryKey'
|
|
54
69
|
? MaybeRefOrGetter<TQueryKey>
|
|
55
70
|
: QueryObserverOptions<
|
|
@@ -65,12 +80,13 @@ export type QueryOptions<
|
|
|
65
80
|
type SkipTokenForUseQuery = symbol
|
|
66
81
|
|
|
67
82
|
/**
|
|
68
|
-
* The options accepted by `queryOptions`, `useQuery`, and the other query hooks. `enabled`
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* `
|
|
83
|
+
* The options accepted by `queryOptions`, `useQuery`, and the other query hooks. `enabled` accepts a `ref`, a
|
|
84
|
+
* plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is tracked, while a plain boolean
|
|
85
|
+
* isn't tracked on its own. query-core's `(query) => boolean` callback is accepted too and is passed through
|
|
86
|
+
* untracked. `queryKey` reacts through a `ref` or a reactive getter for the array itself, or `ref`s and reactive
|
|
87
|
+
* getters as individual entries. Other options are read once when passed as a plain value, and stay reactive
|
|
88
|
+
* when passed as a `ref` or a `computed`. Only `enabled` and `queryKey` read a function as a getter, so
|
|
89
|
+
* `queryFn` reacts through a `computed`: a function there is the query function itself.
|
|
74
90
|
*
|
|
75
91
|
* If you instead pass a getter for the whole options object (`useQuery(() => ({ ... }))`), every option
|
|
76
92
|
* inside it — including `staleTime`, `retry`, and `select` — is re-evaluated whenever the getter's own
|
|
@@ -83,7 +99,8 @@ type SkipTokenForUseQuery = symbol
|
|
|
83
99
|
* re-evaluates.
|
|
84
100
|
* @template TQueryFnData - The type your `queryFn` resolves to.
|
|
85
101
|
* @template TError - The type of errors your `queryFn` may throw.
|
|
86
|
-
* @template TData - The type `data` ends up as after `select` runs.
|
|
102
|
+
* @template TData - The type `data` ends up as after `select` runs. Defaults to `TQueryFnData` when no
|
|
103
|
+
* `select` is used.
|
|
87
104
|
* @template TQueryData - The type of data stored in the cache, before `select` runs. Defaults to
|
|
88
105
|
* `TQueryFnData` and can be configured independently of it.
|
|
89
106
|
* @template TQueryKey - The type of your `queryKey`.
|
package/src/types.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { ComputedRef, Ref, UnwrapRef } from 'vue-demi'
|
|
|
13
13
|
type Primitive = string | number | boolean | bigint | symbol | undefined | null
|
|
14
14
|
type UnwrapLeaf =
|
|
15
15
|
| Primitive
|
|
16
|
+
// oxlint-disable-next-line typescript/no-unsafe-function-type -- leaves every function-like value, including constructors, unwrapped
|
|
16
17
|
| Function
|
|
17
18
|
| Date
|
|
18
19
|
| Error
|
|
@@ -44,6 +45,7 @@ export type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T)
|
|
|
44
45
|
* object as a whole.
|
|
45
46
|
*/
|
|
46
47
|
export type MaybeRefDeep<T> = MaybeRef<
|
|
48
|
+
// oxlint-disable-next-line typescript/no-unsafe-function-type -- keeps every function-like value, including constructors, as it is
|
|
47
49
|
T extends Function
|
|
48
50
|
? T
|
|
49
51
|
: T extends object
|
package/src/useBaseQuery.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { shouldThrowError } from '@tanstack/query-core'
|
|
1
2
|
import {
|
|
2
3
|
computed,
|
|
3
4
|
getCurrentScope,
|
|
@@ -9,24 +10,31 @@ import {
|
|
|
9
10
|
toRefs,
|
|
10
11
|
watch,
|
|
11
12
|
} from 'vue-demi'
|
|
12
|
-
import { shouldThrowError } from '@tanstack/query-core'
|
|
13
13
|
import { useQueryClient } from './useQueryClient'
|
|
14
14
|
import { cloneDeepUnref, updateState } from './utils'
|
|
15
|
-
import type { Ref } from 'vue-demi'
|
|
16
15
|
import type {
|
|
17
16
|
DefaultedQueryObserverOptions,
|
|
18
17
|
QueryKey,
|
|
19
18
|
QueryObserver,
|
|
20
19
|
QueryObserverResult,
|
|
21
20
|
} from '@tanstack/query-core'
|
|
21
|
+
import type { Ref } from 'vue-demi'
|
|
22
22
|
import type { QueryClient } from './queryClient'
|
|
23
23
|
import type { UseQueryOptions } from './queryOptions'
|
|
24
|
-
import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
|
|
25
24
|
import type { MaybeRefOrGetter } from './types'
|
|
25
|
+
import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Readonly view of a union that stays a union. A single `Readonly<Union>` collapses the members, which
|
|
29
|
+
* drops the `status` / `isSuccess` / `isPending` discriminants.
|
|
30
|
+
*/
|
|
31
|
+
type ReadonlyQueryState<T> = T extends any
|
|
32
|
+
? { readonly [K in keyof T]: T[K] }
|
|
33
|
+
: never
|
|
26
34
|
|
|
27
35
|
/**
|
|
28
36
|
* The result of the query composables: every property of the observer result wrapped in a `Ref`, except
|
|
29
|
-
* the `refetch`, `fetchNextPage`, and `fetchPreviousPage` functions, plus `suspense`.
|
|
37
|
+
* the `refetch`, `fetchNextPage`, and `fetchPreviousPage` functions, plus `suspense` and `state`.
|
|
30
38
|
*/
|
|
31
39
|
export type UseBaseQueryReturnType<
|
|
32
40
|
TData,
|
|
@@ -49,6 +57,37 @@ export type UseBaseQueryReturnType<
|
|
|
49
57
|
* `true`, in which case it rejects.
|
|
50
58
|
*/
|
|
51
59
|
suspense: () => Promise<TResult>
|
|
60
|
+
/**
|
|
61
|
+
* The query result as one readonly reactive object. `data`, `error`, `status`, `isPending`, and the other
|
|
62
|
+
* fields are plain values — read `state.data`, not `state.data.value`.
|
|
63
|
+
*
|
|
64
|
+
* The object is a discriminated union, so a check on `status`, `isSuccess`, `isPending`, or `isError`
|
|
65
|
+
* narrows the other fields. When `status` is `'success'` (or `isSuccess` is `true`), `data` is `TData`.
|
|
66
|
+
* When `status` is `'pending'` (or `isPending` is `true`), `data` is `undefined` and `error` is `null`.
|
|
67
|
+
* @example
|
|
68
|
+
* ```vue
|
|
69
|
+
* <script setup lang="ts">
|
|
70
|
+
* import { useQuery } from '@tanstack/vue-query'
|
|
71
|
+
*
|
|
72
|
+
* const { state } = useQuery({
|
|
73
|
+
* queryKey: ['post'],
|
|
74
|
+
* queryFn: fetchPost,
|
|
75
|
+
* })
|
|
76
|
+
*
|
|
77
|
+
* if (state.status === 'success') {
|
|
78
|
+
* // `state.data` is `Post`, not `Post | undefined`
|
|
79
|
+
* console.log(state.data.title)
|
|
80
|
+
* }
|
|
81
|
+
* </script>
|
|
82
|
+
*
|
|
83
|
+
* <template>
|
|
84
|
+
* <span v-if="state.status === 'pending'">Loading...</span>
|
|
85
|
+
* <span v-else-if="state.status === 'error'">Error: {{ state.error.message }}</span>
|
|
86
|
+
* <h1 v-else>{{ state.data.title }}</h1>
|
|
87
|
+
* </template>
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
state: ReadonlyQueryState<TResult>
|
|
52
91
|
}
|
|
53
92
|
|
|
54
93
|
type UseQueryOptionsGeneric<
|
|
@@ -108,7 +147,12 @@ export function useBaseQuery<
|
|
|
108
147
|
}
|
|
109
148
|
const clonedOptions = cloneDeepUnref(resolvedOptions as any)
|
|
110
149
|
|
|
111
|
-
|
|
150
|
+
// A zero-arg function is a Vue getter and must run here so its refs are tracked.
|
|
151
|
+
// `(query) => boolean` is query-core's callback and is left for core to call.
|
|
152
|
+
if (
|
|
153
|
+
typeof clonedOptions.enabled === 'function' &&
|
|
154
|
+
clonedOptions.enabled.length === 0
|
|
155
|
+
) {
|
|
112
156
|
clonedOptions.enabled = clonedOptions.enabled()
|
|
113
157
|
}
|
|
114
158
|
|
|
@@ -137,6 +181,11 @@ export function useBaseQuery<
|
|
|
137
181
|
// noop
|
|
138
182
|
}
|
|
139
183
|
|
|
184
|
+
// Replaced below with the wrapper. Declared here so the subscriber can put it
|
|
185
|
+
// back after `updateState` copies the observer's own `refetch` onto `state`.
|
|
186
|
+
let refetch: (typeof state)['refetch'] = (...args) =>
|
|
187
|
+
observer.refetch(...args)
|
|
188
|
+
|
|
140
189
|
if (client.isRestoring) {
|
|
141
190
|
watch(
|
|
142
191
|
client.isRestoring,
|
|
@@ -145,6 +194,7 @@ export function useBaseQuery<
|
|
|
145
194
|
unsubscribe()
|
|
146
195
|
unsubscribe = observer.subscribe((result) => {
|
|
147
196
|
updateState(state, result)
|
|
197
|
+
state.refetch = refetch
|
|
148
198
|
})
|
|
149
199
|
}
|
|
150
200
|
},
|
|
@@ -155,6 +205,7 @@ export function useBaseQuery<
|
|
|
155
205
|
const updater = () => {
|
|
156
206
|
observer.setOptions(defaultedOptions.value)
|
|
157
207
|
updateState(state, observer.getCurrentResult())
|
|
208
|
+
state.refetch = refetch
|
|
158
209
|
}
|
|
159
210
|
|
|
160
211
|
watch(defaultedOptions, updater)
|
|
@@ -163,11 +214,13 @@ export function useBaseQuery<
|
|
|
163
214
|
unsubscribe()
|
|
164
215
|
})
|
|
165
216
|
|
|
166
|
-
// fix #5910
|
|
167
|
-
|
|
217
|
+
// fix #5910. Call `observer.refetch`: `state.refetch` is this wrapper, so
|
|
218
|
+
// calling it would recurse.
|
|
219
|
+
refetch = (...args) => {
|
|
168
220
|
updater()
|
|
169
|
-
return
|
|
221
|
+
return observer.refetch(...args)
|
|
170
222
|
}
|
|
223
|
+
state.refetch = refetch
|
|
171
224
|
|
|
172
225
|
const suspense = () => {
|
|
173
226
|
return new Promise<QueryObserverResult<TData, TError>>(
|
|
@@ -176,9 +229,15 @@ export function useBaseQuery<
|
|
|
176
229
|
// noop
|
|
177
230
|
}
|
|
178
231
|
const run = () => {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
232
|
+
// Apply the latest options before resolving `enabled`, so a query callback sees the current query.
|
|
233
|
+
// fix #6133
|
|
234
|
+
observer.setOptions(defaultedOptions.value)
|
|
235
|
+
const { enabled } = defaultedOptions.value
|
|
236
|
+
if (
|
|
237
|
+
(typeof enabled === 'function'
|
|
238
|
+
? enabled(observer.getCurrentQuery())
|
|
239
|
+
: enabled) !== false
|
|
240
|
+
) {
|
|
182
241
|
const optimisticResult = observer.getOptimisticResult(
|
|
183
242
|
defaultedOptions.value,
|
|
184
243
|
)
|
|
@@ -243,6 +302,7 @@ export function useBaseQuery<
|
|
|
243
302
|
|
|
244
303
|
object.suspense = suspense
|
|
245
304
|
object.refetch = refetch
|
|
305
|
+
object.state = readonlyState
|
|
246
306
|
|
|
247
307
|
return object as UseBaseQueryReturnType<TData, TError>
|
|
248
308
|
}
|
package/src/useInfiniteQuery.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { InfiniteQueryObserver } from '@tanstack/query-core'
|
|
2
2
|
import { useBaseQuery } from './useBaseQuery'
|
|
3
|
-
import type {
|
|
4
|
-
DefinedInitialDataInfiniteOptions,
|
|
5
|
-
UndefinedInitialDataInfiniteOptions,
|
|
6
|
-
} from './infiniteQueryOptions'
|
|
7
3
|
import type {
|
|
8
4
|
DefaultError,
|
|
9
5
|
DefinedInfiniteQueryObserverResult,
|
|
@@ -13,9 +9,12 @@ import type {
|
|
|
13
9
|
QueryKey,
|
|
14
10
|
QueryObserver,
|
|
15
11
|
} from '@tanstack/query-core'
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
import type {
|
|
13
|
+
DefinedInitialDataInfiniteOptions,
|
|
14
|
+
UndefinedInitialDataInfiniteOptions,
|
|
15
|
+
} from './infiniteQueryOptions'
|
|
16
|
+
import type { QueryClient } from './queryClient'
|
|
17
|
+
import type { VueQueryEnabledOption } from './queryOptions'
|
|
19
18
|
import type {
|
|
20
19
|
DeepUnwrapRef,
|
|
21
20
|
MaybeRef,
|
|
@@ -23,12 +22,17 @@ import type {
|
|
|
23
22
|
MaybeRefOrGetter,
|
|
24
23
|
ShallowOption,
|
|
25
24
|
} from './types'
|
|
26
|
-
import type {
|
|
25
|
+
import type { UseBaseQueryReturnType } from './useBaseQuery'
|
|
27
26
|
|
|
28
27
|
/**
|
|
29
|
-
* The options accepted by `useInfiniteQuery`.
|
|
30
|
-
* `@tanstack/query-core
|
|
31
|
-
* `enabled` a
|
|
28
|
+
* The options accepted by `useInfiniteQuery`. Extends {@link InfiniteQueryObserverOptions} from
|
|
29
|
+
* `@tanstack/query-core` with the `vue-query`-specific `shallow` option. Each option except `shallow` can also
|
|
30
|
+
* be a `ref` (and `enabled` a zero-argument getter or query-core's `(query) => boolean` callback).
|
|
31
|
+
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
32
|
+
* @template TError - The type of errors your `queryFn` may throw.
|
|
33
|
+
* @template TData - The type `data` ends up as after `select` runs — defaults to `TQueryFnData`.
|
|
34
|
+
* @template TQueryKey - The type of your `queryKey`.
|
|
35
|
+
* @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
|
|
32
36
|
*/
|
|
33
37
|
export type UseInfiniteQueryOptions<
|
|
34
38
|
TQueryFnData = unknown,
|
|
@@ -47,14 +51,11 @@ export type UseInfiniteQueryOptions<
|
|
|
47
51
|
TPageParam
|
|
48
52
|
>
|
|
49
53
|
]: Property extends 'enabled'
|
|
50
|
-
?
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
DeepUnwrapRef<TQueryKey>,
|
|
56
|
-
TPageParam
|
|
57
|
-
>[Property]
|
|
54
|
+
? VueQueryEnabledOption<
|
|
55
|
+
TQueryFnData,
|
|
56
|
+
TError,
|
|
57
|
+
InfiniteData<TQueryFnData, TPageParam>,
|
|
58
|
+
DeepUnwrapRef<TQueryKey>
|
|
58
59
|
>
|
|
59
60
|
: MaybeRefDeep<
|
|
60
61
|
InfiniteQueryObserverOptions<
|
|
@@ -96,9 +97,10 @@ export type UseInfiniteQueryDefinedReturnType<TData, TError> =
|
|
|
96
97
|
* This overload is selected when `initialData` is known to be defined, so the resulting `data` is never
|
|
97
98
|
* `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
98
99
|
*
|
|
99
|
-
* `enabled`
|
|
100
|
-
*
|
|
101
|
-
*
|
|
100
|
+
* `enabled` accepts a `ref`, a plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is
|
|
101
|
+
* tracked, while a plain boolean isn't tracked on its own. query-core's `(query) => boolean` callback is
|
|
102
|
+
* accepted too and is passed through untracked. `queryKey` reacts through a `ref` for the array itself, or
|
|
103
|
+
* `ref`s and reactive getters as individual entries — the array itself can't be a bare getter.
|
|
102
104
|
* @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to
|
|
103
105
|
* `useInfiniteQuery`, with `initialData` known to be defined.
|
|
104
106
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
@@ -159,9 +161,10 @@ export function useInfiniteQuery<
|
|
|
159
161
|
* The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
|
|
160
162
|
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
|
|
161
163
|
*
|
|
162
|
-
* `enabled`
|
|
163
|
-
*
|
|
164
|
-
*
|
|
164
|
+
* `enabled` accepts a `ref`, a plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is
|
|
165
|
+
* tracked, while a plain boolean isn't tracked on its own. query-core's `(query) => boolean` callback is
|
|
166
|
+
* accepted too and is passed through untracked. `queryKey` reacts through a `ref` for the array itself, or
|
|
167
|
+
* `ref`s and reactive getters as individual entries — the array itself can't be a bare getter.
|
|
165
168
|
* @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to
|
|
166
169
|
* `useInfiniteQuery`.
|
|
167
170
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
@@ -291,9 +294,10 @@ export function useInfiniteQuery<
|
|
|
291
294
|
* `ref`/reactive object built up conditionally, rather than a plain object literal. Prefer one of the other
|
|
292
295
|
* overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.
|
|
293
296
|
*
|
|
294
|
-
* `enabled`
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
+
* `enabled` accepts a `ref`, a plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is
|
|
298
|
+
* tracked, while a plain boolean isn't tracked on its own. query-core's `(query) => boolean` callback is
|
|
299
|
+
* accepted too and is passed through untracked. `queryKey` reacts through a `ref` for the array itself, or
|
|
300
|
+
* `ref`s and reactive getters as individual entries — the array itself can't be a bare getter.
|
|
297
301
|
* @param options - A `ref`, plain value, or reactive getter resolving to the {@link UseInfiniteQueryOptions} to
|
|
298
302
|
* use.
|
|
299
303
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
package/src/useIsFetching.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { getCurrentScope, onScopeDispose, ref, watchEffect } from 'vue-demi'
|
|
2
2
|
import { useQueryClient } from './useQueryClient'
|
|
3
3
|
import { cloneDeepUnref } from './utils'
|
|
4
|
-
import type { Ref } from 'vue-demi'
|
|
5
4
|
import type { QueryFilters } from '@tanstack/query-core'
|
|
6
|
-
import type {
|
|
5
|
+
import type { Ref } from 'vue-demi'
|
|
7
6
|
import type { QueryClient } from './queryClient'
|
|
7
|
+
import type { MaybeRefDeep } from './types'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The filters accepted by `useIsFetching`: {@link QueryFilters} as a plain object, a `ref`, or a reactive
|
package/src/useMutation.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MutationObserver, shouldThrowError } from '@tanstack/query-core'
|
|
1
2
|
import {
|
|
2
3
|
computed,
|
|
3
4
|
getCurrentScope,
|
|
@@ -9,18 +10,17 @@ import {
|
|
|
9
10
|
toRefs,
|
|
10
11
|
watch,
|
|
11
12
|
} from 'vue-demi'
|
|
12
|
-
import { MutationObserver, shouldThrowError } from '@tanstack/query-core'
|
|
13
|
-
import { cloneDeepUnref, updateState } from './utils'
|
|
14
13
|
import { useQueryClient } from './useQueryClient'
|
|
15
|
-
import
|
|
14
|
+
import { cloneDeepUnref, updateState } from './utils'
|
|
16
15
|
import type {
|
|
17
16
|
DefaultError,
|
|
18
17
|
DistributiveOmit,
|
|
19
18
|
MutateFunction,
|
|
20
19
|
MutationObserverResult,
|
|
21
20
|
} from '@tanstack/query-core'
|
|
22
|
-
import type {
|
|
21
|
+
import type { ToRefs } from 'vue-demi'
|
|
23
22
|
import type { QueryClient } from './queryClient'
|
|
23
|
+
import type { MaybeRefDeep, MutationOptions } from './types'
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
26
|
* The {@link MutationObserverResult} properties without `mutate` and `reset`, which `useMutation` replaces with
|
package/src/useMutationState.ts
CHANGED
|
@@ -8,15 +8,15 @@ import {
|
|
|
8
8
|
} from 'vue-demi'
|
|
9
9
|
import { useQueryClient } from './useQueryClient'
|
|
10
10
|
import { cloneDeepUnref } from './utils'
|
|
11
|
-
import type { Ref } from 'vue-demi'
|
|
12
11
|
import type {
|
|
13
12
|
Mutation,
|
|
14
13
|
MutationFilters,
|
|
15
14
|
MutationState,
|
|
16
15
|
} from '@tanstack/query-core'
|
|
16
|
+
import type { Ref } from 'vue-demi'
|
|
17
|
+
import type { MutationCache } from './mutationCache'
|
|
17
18
|
import type { QueryClient } from './queryClient'
|
|
18
19
|
import type { MaybeRefDeep } from './types'
|
|
19
|
-
import type { MutationCache } from './mutationCache'
|
|
20
20
|
|
|
21
21
|
type VueMutationFilters = MaybeRefDeep<MutationFilters>
|
|
22
22
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getCurrentScope, unref, watchEffect } from 'vue-demi'
|
|
2
1
|
import { noop } from '@tanstack/query-core'
|
|
2
|
+
import { getCurrentScope, unref, watchEffect } from 'vue-demi'
|
|
3
3
|
import { useQueryClient } from './useQueryClient'
|
|
4
4
|
import { cloneDeepUnref } from './utils'
|
|
5
5
|
import type {
|
package/src/usePrefetchQuery.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { getCurrentScope, unref, watchEffect } from 'vue-demi'
|
|
2
1
|
import { noop } from '@tanstack/query-core'
|
|
2
|
+
import { getCurrentScope, unref, watchEffect } from 'vue-demi'
|
|
3
3
|
import { useQueryClient } from './useQueryClient'
|
|
4
4
|
import { cloneDeepUnref } from './utils'
|
|
5
5
|
import type {
|
package/src/useQueries.ts
CHANGED
|
@@ -9,10 +9,8 @@ import {
|
|
|
9
9
|
unref,
|
|
10
10
|
watch,
|
|
11
11
|
} from 'vue-demi'
|
|
12
|
-
|
|
13
12
|
import { useQueryClient } from './useQueryClient'
|
|
14
13
|
import { cloneDeepUnref } from './utils'
|
|
15
|
-
import type { Ref } from 'vue-demi'
|
|
16
14
|
import type {
|
|
17
15
|
DefaultError,
|
|
18
16
|
DefinedQueryObserverResult,
|
|
@@ -22,8 +20,9 @@ import type {
|
|
|
22
20
|
QueryObserverResult,
|
|
23
21
|
ThrowOnError,
|
|
24
22
|
} from '@tanstack/query-core'
|
|
25
|
-
import type {
|
|
23
|
+
import type { Ref } from 'vue-demi'
|
|
26
24
|
import type { QueryClient } from './queryClient'
|
|
25
|
+
import type { UseQueryOptions } from './queryOptions'
|
|
27
26
|
import type { DeepUnwrapRef, MaybeRefDeep, ShallowOption } from './types'
|
|
28
27
|
|
|
29
28
|
// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.
|
|
@@ -393,8 +392,13 @@ export function useQueries<
|
|
|
393
392
|
return queriesRaw.map((queryOptions) => {
|
|
394
393
|
const clonedOptions = cloneDeepUnref(queryOptions)
|
|
395
394
|
|
|
396
|
-
|
|
397
|
-
|
|
395
|
+
// A zero-arg function is a Vue getter and must run here so its refs are tracked.
|
|
396
|
+
// `(query) => boolean` is query-core's callback and is left for core to call.
|
|
397
|
+
if (
|
|
398
|
+
typeof clonedOptions.enabled === 'function' &&
|
|
399
|
+
clonedOptions.enabled.length === 0
|
|
400
|
+
) {
|
|
401
|
+
clonedOptions.enabled = clonedOptions.enabled()
|
|
398
402
|
}
|
|
399
403
|
|
|
400
404
|
const defaulted = client.defaultQueryOptions(clonedOptions)
|
package/src/useQuery.ts
CHANGED
|
@@ -5,14 +5,14 @@ import type {
|
|
|
5
5
|
DefinedQueryObserverResult,
|
|
6
6
|
QueryKey,
|
|
7
7
|
} from '@tanstack/query-core'
|
|
8
|
-
import type { UseBaseQueryReturnType } from './useBaseQuery'
|
|
9
|
-
import type { MaybeRefOrGetter } from './types'
|
|
10
8
|
import type { QueryClient } from './queryClient'
|
|
11
9
|
import type {
|
|
12
10
|
DefinedInitialQueryOptions,
|
|
13
11
|
UndefinedInitialQueryOptions,
|
|
14
12
|
UseQueryOptions,
|
|
15
13
|
} from './queryOptions'
|
|
14
|
+
import type { MaybeRefOrGetter } from './types'
|
|
15
|
+
import type { UseBaseQueryReturnType } from './useBaseQuery'
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* The result of `useQuery` when `initialData` is omitted, may be `undefined`, or is a function that may
|
|
@@ -35,20 +35,24 @@ export type UseQueryDefinedReturnType<TData, TError> = UseBaseQueryReturnType<
|
|
|
35
35
|
>
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* This overload is selected when `initialData` is known to be defined, so the resulting `data` is never
|
|
39
|
-
* `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
38
|
+
* This overload is selected when `initialData` is known to be defined, so the resulting `data` is typed as never
|
|
39
|
+
* `undefined` (unless a `select` changes `TData` to include `undefined`). At runtime, `data` is still `undefined`
|
|
40
|
+
* if `select` throws on its first call.
|
|
40
41
|
*
|
|
41
|
-
* `enabled`
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
42
|
+
* `enabled` accepts a `ref`, a plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is
|
|
43
|
+
* tracked, while a plain boolean isn't tracked on its own. query-core's `(query) => boolean` callback is
|
|
44
|
+
* accepted too and is passed through untracked. `queryKey` reacts through a `ref` or a reactive getter for the
|
|
45
|
+
* array itself, or `ref`s and reactive getters as individual entries. Other options are read once when passed as
|
|
46
|
+
* a plain value, and stay reactive when passed as a `ref` or a `computed`.
|
|
45
47
|
* @param options - The {@link DefinedInitialQueryOptions} to use — everything you can pass to `useQuery`,
|
|
46
48
|
* with `initialData` known to be defined.
|
|
47
49
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
48
50
|
* will be used.
|
|
49
|
-
* @returns The current query result, typed so that `
|
|
50
|
-
* `
|
|
51
|
-
* `
|
|
51
|
+
* @returns The current query result, typed so that `status` is `success` or `error` (`status` never resolves
|
|
52
|
+
* to `pending` in this overload's type, since `initialData` guarantees data upfront). `status` is `error` if
|
|
53
|
+
* the query has an error from a fetch that failed after its retries or if `select` threw, and `success`
|
|
54
|
+
* otherwise. `isSuccess`/`isError` are derived boolean `ref`s for convenience. `state` is that result as one
|
|
55
|
+
* readonly reactive object — `state.data`, `state.isPending`, and the rest are plain values.
|
|
52
56
|
* @example
|
|
53
57
|
* ```vue
|
|
54
58
|
* <script setup lang="ts">
|
|
@@ -82,15 +86,20 @@ export function useQuery<
|
|
|
82
86
|
): UseQueryDefinedReturnType<TData, TError>
|
|
83
87
|
|
|
84
88
|
/**
|
|
85
|
-
* `enabled`
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
+
* `enabled` accepts a `ref`, a plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is
|
|
90
|
+
* tracked, while a plain boolean isn't tracked on its own. query-core's `(query) => boolean` callback is
|
|
91
|
+
* accepted too and is passed through untracked. `queryKey` reacts through a `ref` or a reactive getter for the
|
|
92
|
+
* array itself, or `ref`s and reactive getters as individual entries. Other options are read once when passed as
|
|
93
|
+
* a plain value, and stay reactive when passed as a `ref` or a `computed`.
|
|
89
94
|
* @param options - The {@link UndefinedInitialQueryOptions} to use — everything you can pass to `useQuery`.
|
|
90
95
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
91
96
|
* will be used.
|
|
92
|
-
* @returns The current query result. `status` is `
|
|
93
|
-
*
|
|
97
|
+
* @returns The current query result. `status` is `error` if the query has an error from a fetch that failed
|
|
98
|
+
* after its retries or if `select` threw, `success` if the query has cached data (even if `select` returns
|
|
99
|
+
* `undefined`) or data from `placeholderData`, and `pending` otherwise. `isPending`/`isSuccess`/`isError` are
|
|
100
|
+
* derived boolean `ref`s for convenience. `state` is that result as one readonly reactive object —
|
|
101
|
+
* `state.data`, `state.isPending`, and the rest are plain values, and `state.status === 'success'` narrows
|
|
102
|
+
* `state.data` to `TData`.
|
|
94
103
|
* @example
|
|
95
104
|
* A query key built from a reactive `ref` — the query refetches whenever `postId` changes:
|
|
96
105
|
* ```vue
|
|
@@ -209,6 +218,25 @@ export function useQuery<
|
|
|
209
218
|
* </template>
|
|
210
219
|
* </template>
|
|
211
220
|
* ```
|
|
221
|
+
* @example
|
|
222
|
+
* `state` keeps `data`, `error`, `status`, and `isPending` on one reactive object, so a `status` check
|
|
223
|
+
* narrows `data` and you never read `.value`:
|
|
224
|
+
* ```vue
|
|
225
|
+
* <script setup lang="ts">
|
|
226
|
+
* import { useQuery } from '@tanstack/vue-query'
|
|
227
|
+
*
|
|
228
|
+
* const { state } = useQuery({
|
|
229
|
+
* queryKey: ['post'],
|
|
230
|
+
* queryFn: fetchPost,
|
|
231
|
+
* })
|
|
232
|
+
* </script>
|
|
233
|
+
*
|
|
234
|
+
* <template>
|
|
235
|
+
* <span v-if="state.status === 'pending'">Loading...</span>
|
|
236
|
+
* <span v-else-if="state.status === 'error'">Error: {{ state.error.message }}</span>
|
|
237
|
+
* <h1 v-else>{{ state.data.title }}</h1>
|
|
238
|
+
* </template>
|
|
239
|
+
* ```
|
|
212
240
|
*/
|
|
213
241
|
export function useQuery<
|
|
214
242
|
TQueryFnData = unknown,
|
|
@@ -225,16 +253,22 @@ export function useQuery<
|
|
|
225
253
|
* `ref`/reactive object built up conditionally, rather than a plain object literal. Prefer one of the other
|
|
226
254
|
* overloads when possible, since they infer whether `data` can be `undefined` from `initialData` directly.
|
|
227
255
|
*
|
|
228
|
-
* `enabled`
|
|
229
|
-
*
|
|
230
|
-
*
|
|
256
|
+
* `enabled` accepts a `ref`, a plain boolean, or a zero-argument getter (`() => ...`): a `ref` or a getter is
|
|
257
|
+
* tracked, while a plain boolean isn't tracked on its own. query-core's `(query) => boolean` callback is
|
|
258
|
+
* accepted too and is passed through untracked. `queryKey` reacts through a `ref` or a reactive getter for the
|
|
259
|
+
* array itself, or `ref`s and reactive getters as individual entries.
|
|
231
260
|
*
|
|
232
261
|
* When `options` itself is a reactive getter, the whole object is re-evaluated on every change to its
|
|
233
262
|
* dependencies, so any option inside it — not just `queryKey` and `enabled` — can change over time.
|
|
234
263
|
* @param options - A `ref`, plain value, or reactive getter resolving to the {@link UseQueryOptions} to use.
|
|
235
264
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one provided by `VueQueryPlugin`
|
|
236
265
|
* will be used.
|
|
237
|
-
* @returns The current query result
|
|
266
|
+
* @returns The current query result. `status` is `error` if the query has an error from a fetch that failed
|
|
267
|
+
* after its retries or if `select` threw, `success` if the query has cached data (even if `select` returns
|
|
268
|
+
* `undefined`) or data from `placeholderData`, and `pending` otherwise. `isPending`/`isSuccess`/`isError` are
|
|
269
|
+
* derived boolean `ref`s for convenience. `state` is that result as one readonly reactive object —
|
|
270
|
+
* `state.data`, `state.isPending`, and the rest are plain values, and `state.status === 'success'` narrows
|
|
271
|
+
* `state.data` to `TData`.
|
|
238
272
|
* @example
|
|
239
273
|
* Passing a whole-options getter so `staleTime` reacts to a setting stored elsewhere, not just `queryKey`:
|
|
240
274
|
* ```vue
|