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 { ToRef } 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 Result = QueryObserverResult<TData, TError>,
27> = {
28 [K in keyof Result]: K extends
29 | 'fetchNextPage'
30 | 'fetchPreviousPage'
31 | 'refetch'
32 ? Result[K]
33 : ToRef<Readonly<Result>[K]>
34} & {
35 suspense: () => Promise<Result>
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 composables 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 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
118 if (!isRestoring) {
119 unsubscribe()
120 unsubscribe = observer.subscribe((result) => {
121 updateState(state, result)
122 })
123 }
124 },
125 { immediate: true },
126 )
127β
128 const updater = () => {
129 observer.setOptions(defaultedOptions.value)
130 updateState(state, observer.getCurrentResult())
131 }
132β
133 watch(defaultedOptions, updater)
134β
135 onScopeDispose(() => {
136 unsubscribe()
137 })
138β
139 // fix #5910
140 const refetch = (...args: Parameters<(typeof state)['refetch']>) => {
141 updater()
142 return state.refetch(...args)
143 }
144β
145 const suspense = () => {
146 return new Promise<QueryObserverResult<TData, TError>>(
147 (resolve, reject) => {
148 let stopWatch = () => {
149 //noop
150 }
151 const run = () => {
152 if (defaultedOptions.value.enabled !== false) {
153 // fix #6133
154 observer.setOptions(defaultedOptions.value)
155 const optimisticResult = observer.getOptimisticResult(
156 defaultedOptions.value,
157 )
158 if (optimisticResult.isStale) {
159 stopWatch()
160 observer
161 .fetchOptimistic(defaultedOptions.value)
162 .then(resolve, reject)
163 } else {
164 stopWatch()
165 resolve(optimisticResult)
166 }
167 }
168 }
169β
170 run()
171β
172 stopWatch = watch(defaultedOptions, run)
173 },
174 )
175 }
176β
177 // Handle error boundary
178 watch(
179 () => state.error,
180 (error) => {
181 if (
182 state.isError &&
183 !state.isFetching &&
184 shouldThrowError(defaultedOptions.value.throwOnError, [
185 error as TError,
186 observer.getCurrentQuery(),
187 ])
188 ) {
189 throw error
190 }
191 },
192 )
193β
194 const object: any = toRefs(readonly(state))
195 for (const key in state) {
196 if (typeof state[key as keyof typeof state] === 'function') {
197 object[key] = state[key as keyof typeof state]
198 }
199 }
200β
201 object.suspense = suspense
202 object.refetch = refetch
203β
204 return object as UseBaseQueryReturnType<TData, TError>
205}