@tanstack/vue-query 5.0.0-beta.0 → 5.0.0-beta.12
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/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.map +1 -1
- package/build/legacy/useBaseQuery.cjs +32 -19
- package/build/legacy/useBaseQuery.cjs.map +1 -1
- package/build/legacy/useBaseQuery.js +33 -20
- package/build/legacy/useBaseQuery.js.map +1 -1
- package/build/legacy/useMutation.cjs +8 -0
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.js +9 -1
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/utils.cjs +8 -0
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +2 -1
- package/build/legacy/utils.d.ts +2 -1
- package/build/legacy/utils.js +7 -0
- package/build/legacy/utils.js.map +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.map +1 -1
- package/build/modern/useBaseQuery.cjs +32 -19
- package/build/modern/useBaseQuery.cjs.map +1 -1
- package/build/modern/useBaseQuery.js +33 -20
- package/build/modern/useBaseQuery.js.map +1 -1
- package/build/modern/useMutation.cjs +8 -0
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.js +9 -1
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/utils.cjs +8 -0
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +2 -1
- package/build/modern/utils.d.ts +2 -1
- package/build/modern/utils.js +7 -0
- package/build/modern/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/useInfiniteQuery.types.test.tsx +6 -3
- package/src/__tests__/useMutation.test.ts +20 -0
- package/src/__tests__/useQuery.test.ts +22 -0
- package/src/queryClient.ts +2 -2
- package/src/useBaseQuery.ts +41 -20
- package/src/useMutation.ts +13 -1
- package/src/utils.ts +12 -0
package/src/useBaseQuery.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
watch,
|
|
8
8
|
} from 'vue-demi'
|
|
9
9
|
import { useQueryClient } from './useQueryClient'
|
|
10
|
-
import { cloneDeepUnref, updateState } from './utils'
|
|
10
|
+
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
|
|
11
11
|
import type { ToRefs } from 'vue-demi'
|
|
12
12
|
import type {
|
|
13
13
|
DefaultedQueryObserverOptions,
|
|
@@ -117,31 +117,52 @@ export function useBaseQuery<
|
|
|
117
117
|
})
|
|
118
118
|
|
|
119
119
|
const suspense = () => {
|
|
120
|
-
return new Promise<QueryObserverResult<TData, TError>>(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
120
|
+
return new Promise<QueryObserverResult<TData, TError>>(
|
|
121
|
+
(resolve, reject) => {
|
|
122
|
+
let stopWatch = () => {
|
|
123
|
+
//noop
|
|
124
|
+
}
|
|
125
|
+
const run = () => {
|
|
126
|
+
if (defaultedOptions.value.enabled !== false) {
|
|
127
|
+
const optimisticResult = observer.getOptimisticResult(
|
|
128
|
+
defaultedOptions.value,
|
|
129
|
+
)
|
|
130
|
+
if (optimisticResult.isStale) {
|
|
131
|
+
stopWatch()
|
|
132
|
+
observer
|
|
133
|
+
.fetchOptimistic(defaultedOptions.value)
|
|
134
|
+
.then(resolve, reject)
|
|
135
|
+
} else {
|
|
136
|
+
stopWatch()
|
|
137
|
+
resolve(optimisticResult)
|
|
138
|
+
}
|
|
135
139
|
}
|
|
136
140
|
}
|
|
137
|
-
}
|
|
138
141
|
|
|
139
|
-
|
|
142
|
+
run()
|
|
140
143
|
|
|
141
|
-
|
|
142
|
-
|
|
144
|
+
stopWatch = watch(defaultedOptions, run, { deep: true })
|
|
145
|
+
},
|
|
146
|
+
)
|
|
143
147
|
}
|
|
144
148
|
|
|
149
|
+
// Handle error boundary
|
|
150
|
+
watch(
|
|
151
|
+
() => state.error,
|
|
152
|
+
(error) => {
|
|
153
|
+
if (
|
|
154
|
+
state.isError &&
|
|
155
|
+
!state.isFetching &&
|
|
156
|
+
shouldThrowError(defaultedOptions.value.throwOnError, [
|
|
157
|
+
error as TError,
|
|
158
|
+
observer.getCurrentQuery(),
|
|
159
|
+
])
|
|
160
|
+
) {
|
|
161
|
+
throw error
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
)
|
|
165
|
+
|
|
145
166
|
return {
|
|
146
167
|
...(toRefs(readonly(state)) as ToRefs<
|
|
147
168
|
Readonly<QueryObserverResult<TData, TError>>
|
package/src/useMutation.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
watch,
|
|
8
8
|
} from 'vue-demi'
|
|
9
9
|
import { MutationObserver } from '@tanstack/query-core'
|
|
10
|
-
import { cloneDeepUnref, updateState } from './utils'
|
|
10
|
+
import { cloneDeepUnref, shouldThrowError, updateState } from './utils'
|
|
11
11
|
import { useQueryClient } from './useQueryClient'
|
|
12
12
|
import type { ToRefs } from 'vue-demi'
|
|
13
13
|
import type {
|
|
@@ -100,6 +100,18 @@ export function useMutation<
|
|
|
100
100
|
Readonly<MutationResult<TData, TError, TVariables, TContext>>
|
|
101
101
|
>
|
|
102
102
|
|
|
103
|
+
watch(
|
|
104
|
+
() => state.error,
|
|
105
|
+
(error) => {
|
|
106
|
+
if (
|
|
107
|
+
error &&
|
|
108
|
+
shouldThrowError(options.value.throwOnError, [error as TError])
|
|
109
|
+
) {
|
|
110
|
+
throw error
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
)
|
|
114
|
+
|
|
103
115
|
return {
|
|
104
116
|
...resultRefs,
|
|
105
117
|
mutate,
|
package/src/utils.ts
CHANGED
|
@@ -65,3 +65,15 @@ function isPlainObject(value: unknown): value is Object {
|
|
|
65
65
|
const prototype = Object.getPrototypeOf(value)
|
|
66
66
|
return prototype === null || prototype === Object.prototype
|
|
67
67
|
}
|
|
68
|
+
|
|
69
|
+
export function shouldThrowError<T extends (...args: any[]) => boolean>(
|
|
70
|
+
throwOnError: boolean | T | undefined,
|
|
71
|
+
params: Parameters<T>,
|
|
72
|
+
): boolean {
|
|
73
|
+
// Allow throwOnError function to override throwing behavior on a per-error basis
|
|
74
|
+
if (typeof throwOnError === 'function') {
|
|
75
|
+
return throwOnError(...params)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return !!throwOnError
|
|
79
|
+
}
|