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