Open History to see the last commit
1import {
2 computed,
3 getCurrentScope,
4 onScopeDispose,
5 reactive,
6 readonly,
7 toRefs,
8 watch,
9} from 'vue-demi'
10import { useQueryClient } from './useQueryClient'
11import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
12import type { Ref } from 'vue-demi'
13import type {
14 DefaultedQueryObserverOptions,
15 QueryKey,
16 QueryObserver,
17 QueryObserverResult,
18} from '@tanstack/query-core'
19import type { QueryClient } from './queryClient'
20import type { UseQueryOptions } from './useQuery'
21import type { UseInfiniteQueryOptions } from './useInfiniteQuery'
22β
23export type UseBaseQueryReturnType<
24 TData,
25 TError,
26 TResult = QueryObserverResult<TData, TError>,
27> = {
28 [K in keyof TResult]: K extends
29 | 'fetchNextPage'
30 | 'fetchPreviousPage'
31 | 'refetch'
32 ? TResult[K]
33 : Ref<Readonly<TResult>[K]>
34} & {
35 suspense: () => Promise<TResult>
36}
37β
38type UseQueryOptionsGeneric<
39 TQueryFnData,
40 TError,
41 TData,
42 TQueryData,
43 TQueryKey extends QueryKey = QueryKey,
44 TPageParam = unknown,
45> =
46 | UseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
47 | UseInfiniteQueryOptions<
48 TQueryFnData,
49 TError,
50 TData,
51 TQueryData,
52 TQueryKey,
53 TPageParam
54 >
55β
56export function useBaseQuery<
57 TQueryFnData,
58 TError,
59 TData,
60 TQueryData,
61 TQueryKey extends QueryKey,
62 TPageParam,
63>(
64 Observer: typeof QueryObserver,
65 options: UseQueryOptionsGeneric<
66 TQueryFnData,
67 TError,
68 TData,
69 TQueryData,
70 TQueryKey,
71 TPageParam
72 >,
73 queryClient?: QueryClient,
74): UseBaseQueryReturnType<TData, TError> {
75 if (process.env.NODE_ENV === 'development') {
76 if (!getCurrentScope()) {
77 console.warn(
78 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
79 )
80 }
81 }
82β
83 const client = queryClient || useQueryClient()
84β
85 const defaultedOptions = computed(() => {
86 const clonedOptions = cloneDeepUnref(options as any)
87β
88 if (typeof clonedOptions.enabled === 'function') {
89 clonedOptions.enabled = clonedOptions.enabled()
90 }
91β
92 const defaulted: DefaultedQueryObserverOptions<
93 TQueryFnData,
94 TError,
95 TData,
96 TQueryData,
97 TQueryKey
98 > = client.defaultQueryOptions(clonedOptions)
99β
100 defaulted._optimisticResults = client.isRestoring.value
101 ? 'isRestoring'
102 : 'optimistic'
103β
104 return defaulted
105 })
106β
107 const observer = new Observer(client, defaultedOptions.value)
108 const state = reactive(observer.getCurrentResult())
109β
110 let unsubscribe = () => {
111 // noop
112 }
113β
114 watch(
115 client.isRestoring,
116 (isRestoring) => {
117 if (!isRestoring) {
118 unsubscribe()
119 unsubscribe = observer.subscribe((result) => {
120 updateState(state, result)
121 })
122 }
123 },
124 { immediate: true },
125 )
126β
127 const updater = () => {
128 observer.setOptions(defaultedOptions.value)
129 updateState(state, observer.getCurrentResult())
130 }
131β
132 watch(defaultedOptions, updater)
133β
134 onScopeDispose(() => {
135 unsubscribe()
136 })
137β
138 // fix #5910
139 const refetch = (...args: Parameters<(typeof state)['refetch']>) => {
140 updater()
141 return state.refetch(...args)
142 }
143β
144 const suspense = () => {
145 return new Promise<QueryObserverResult<TData, TError>>(
146 (resolve, reject) => {
147 let stopWatch = () => {
148 // noop
149 }
150 const run = () => {
151 if (defaultedOptions.value.enabled !== false) {
152 // fix #6133
153 observer.setOptions(defaultedOptions.value)
154 const optimisticResult = observer.getOptimisticResult(
155 defaultedOptions.value,
156 )
157 if (optimisticResult.isStale) {
158 stopWatch()
159 observer
160 .fetchOptimistic(defaultedOptions.value)
161 .then(resolve, (error: TError) => {
162 if (
163 shouldThrowError(defaultedOptions.value.throwOnError, [
164 error,
165 observer.getCurrentQuery(),
166 ])
167 ) {
168 reject(error)
169 } else {
170 resolve(observer.getCurrentResult())
171 }
172 })
173 } else {
174 stopWatch()
175 resolve(optimisticResult)
176 }
177 }
178 }
179β
180 run()
181β
182 stopWatch = watch(defaultedOptions, run)
183 },
184 )
185 }
186β
187 // Handle error boundary
188 watch(
189 () => state.error,
190 (error) => {
191 if (
192 state.isError &&
193 !state.isFetching &&
194 shouldThrowError(defaultedOptions.value.throwOnError, [
195 error as TError,
196 observer.getCurrentQuery(),
197 ])
198 ) {
199 throw error
200 }
201 },
202 )
203β
204 const object: any = toRefs(readonly(state))
205 for (const key in state) {
206 if (typeof state[key as keyof typeof state] === 'function') {
207 object[key] = state[key as keyof typeof state]
208 }
209 }
210β
211 object.suspense = suspense
212 object.refetch = refetch
213β
214 return object as UseBaseQueryReturnType<TData, TError>
215}