@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.
Files changed (44) hide show
  1. package/build/legacy/queryClient.cjs.map +1 -1
  2. package/build/legacy/queryClient.d.cts +1 -1
  3. package/build/legacy/queryClient.d.ts +1 -1
  4. package/build/legacy/queryClient.js.map +1 -1
  5. package/build/legacy/useBaseQuery.cjs +32 -19
  6. package/build/legacy/useBaseQuery.cjs.map +1 -1
  7. package/build/legacy/useBaseQuery.js +33 -20
  8. package/build/legacy/useBaseQuery.js.map +1 -1
  9. package/build/legacy/useMutation.cjs +8 -0
  10. package/build/legacy/useMutation.cjs.map +1 -1
  11. package/build/legacy/useMutation.js +9 -1
  12. package/build/legacy/useMutation.js.map +1 -1
  13. package/build/legacy/utils.cjs +8 -0
  14. package/build/legacy/utils.cjs.map +1 -1
  15. package/build/legacy/utils.d.cts +2 -1
  16. package/build/legacy/utils.d.ts +2 -1
  17. package/build/legacy/utils.js +7 -0
  18. package/build/legacy/utils.js.map +1 -1
  19. package/build/modern/queryClient.cjs.map +1 -1
  20. package/build/modern/queryClient.d.cts +1 -1
  21. package/build/modern/queryClient.d.ts +1 -1
  22. package/build/modern/queryClient.js.map +1 -1
  23. package/build/modern/useBaseQuery.cjs +32 -19
  24. package/build/modern/useBaseQuery.cjs.map +1 -1
  25. package/build/modern/useBaseQuery.js +33 -20
  26. package/build/modern/useBaseQuery.js.map +1 -1
  27. package/build/modern/useMutation.cjs +8 -0
  28. package/build/modern/useMutation.cjs.map +1 -1
  29. package/build/modern/useMutation.js +9 -1
  30. package/build/modern/useMutation.js.map +1 -1
  31. package/build/modern/utils.cjs +8 -0
  32. package/build/modern/utils.cjs.map +1 -1
  33. package/build/modern/utils.d.cts +2 -1
  34. package/build/modern/utils.d.ts +2 -1
  35. package/build/modern/utils.js +7 -0
  36. package/build/modern/utils.js.map +1 -1
  37. package/package.json +3 -3
  38. package/src/__tests__/useInfiniteQuery.types.test.tsx +6 -3
  39. package/src/__tests__/useMutation.test.ts +20 -0
  40. package/src/__tests__/useQuery.test.ts +22 -0
  41. package/src/queryClient.ts +2 -2
  42. package/src/useBaseQuery.ts +41 -20
  43. package/src/useMutation.ts +13 -1
  44. package/src/utils.ts +12 -0
@@ -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>>((resolve) => {
121
- let stopWatch = () => {
122
- //noop
123
- }
124
- const run = () => {
125
- if (defaultedOptions.value.enabled !== false) {
126
- const optimisticResult = observer.getOptimisticResult(
127
- defaultedOptions.value,
128
- )
129
- if (optimisticResult.isStale) {
130
- stopWatch()
131
- resolve(observer.fetchOptimistic(defaultedOptions.value))
132
- } else {
133
- stopWatch()
134
- resolve(optimisticResult)
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
- run()
142
+ run()
140
143
 
141
- stopWatch = watch(defaultedOptions, run, { deep: true })
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>>
@@ -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
+ }