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 | 'remove'
33 ? Result[K]
34 : ToRef<Readonly<Result>[K]>
35} & {
36 suspense: () => Promise<Result>
37}
38β
39type UseQueryOptionsGeneric<
40 TQueryFnData,
41 TError,
42 TData,
43 TQueryData,
44 TQueryKey extends QueryKey = QueryKey,
45 TPageParam = unknown,
46> =
47 | UseQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
48 | UseInfiniteQueryOptions<
49 TQueryFnData,
50 TError,
51 TData,
52 TQueryData,
53 TQueryKey,
54 TPageParam
55 >
56β
57export function useBaseQuery<
58 TQueryFnData,
59 TError,
60 TData,
61 TQueryData,
62 TQueryKey extends QueryKey,
63 TPageParam,
64>(
65 Observer: typeof QueryObserver,
66 options: UseQueryOptionsGeneric<
67 TQueryFnData,
68 TError,
69 TData,
70 TQueryData,
71 TQueryKey,
72 TPageParam
73 >,
74 queryClient?: QueryClient,
75): UseBaseQueryReturnType<TData, TError> {
76 if (process.env.NODE_ENV === 'development') {
77 if (!getCurrentScope()) {
78 console.warn(
79 '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.',
80 )
81 }
82 }
83β
84 const client = queryClient || useQueryClient()
85β
86 const defaultedOptions = computed(() => {
87 const clonedOptions = cloneDeepUnref(options as any)
88β
89 if (typeof clonedOptions.enabled === 'function') {
90 clonedOptions.enabled = clonedOptions.enabled()
91 }
92β
93 const defaulted: DefaultedQueryObserverOptions<
94 TQueryFnData,
95 TError,
96 TData,
97 TQueryData,
98 TQueryKey
99 > = client.defaultQueryOptions(clonedOptions)
100β
101 defaulted._optimisticResults = client.isRestoring.value
102 ? 'isRestoring'
103 : 'optimistic'
104β
105 return defaulted
106 })
107β
108 const observer = new Observer(client, defaultedOptions.value)
109 const state = reactive(observer.getCurrentResult())
110β
111 let unsubscribe = () => {
112 // noop
113 }
114β
115 watch(
116 client.isRestoring,
117 (isRestoring) => {
118 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
119 if (!isRestoring) {
120 unsubscribe()
121 unsubscribe = observer.subscribe((result) => {
122 updateState(state, result)
123 })
124 }
125 },
126 { immediate: true },
127 )
128β
129 const updater = () => {
130 observer.setOptions(defaultedOptions.value)
131 updateState(state, observer.getCurrentResult())
132 }
133β
134 watch(defaultedOptions, updater)
135β
136 onScopeDispose(() => {
137 unsubscribe()
138 })
139β
140 // fix #5910
141 const refetch = (...args: Parameters<(typeof state)['refetch']>) => {
142 updater()
143 return state.refetch(...args)
144 }
145β
146 const suspense = () => {
147 return new Promise<QueryObserverResult<TData, TError>>(
148 (resolve, reject) => {
149 let stopWatch = () => {
150 //noop
151 }
152 const run = () => {
153 if (defaultedOptions.value.enabled !== false) {
154 // fix #6133
155 observer.setOptions(defaultedOptions.value)
156 const optimisticResult = observer.getOptimisticResult(
157 defaultedOptions.value,
158 )
159 if (optimisticResult.isStale) {
160 stopWatch()
161 observer
162 .fetchOptimistic(defaultedOptions.value)
163 .then(resolve, reject)
164 } else {
165 stopWatch()
166 resolve(optimisticResult)
167 }
168 }
169 }
170β
171 run()
172β
173 stopWatch = watch(defaultedOptions, run)
174 },
175 )
176 }
177β
178 // Handle error boundary
179 watch(
180 () => state.error,
181 (error) => {
182 if (
183 state.isError &&
184 !state.isFetching &&
185 shouldThrowError(defaultedOptions.value.throwOnError, [
186 error as TError,
187 observer.getCurrentQuery(),
188 ])
189 ) {
190 throw error
191 }
192 },
193 )
194β
195 const object: any = toRefs(readonly(state))
196 for (const key in state) {
197 if (typeof state[key as keyof typeof state] === 'function') {
198 object[key] = state[key as keyof typeof state]
199 }
200 }
201β
202 object.suspense = suspense
203 object.refetch = refetch
204β
205 return object as UseBaseQueryReturnType<TData, TError>
206}