@tanstack/vue-query 5.0.4 → 5.4.0
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/index.cjs +3 -0
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +2 -1
- package/build/legacy/index.d.ts +2 -1
- package/build/legacy/index.js +2 -0
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/queryOptions.cjs +33 -0
- package/build/legacy/queryOptions.cjs.map +1 -0
- package/build/legacy/queryOptions.d.cts +14 -0
- package/build/legacy/queryOptions.d.ts +14 -0
- package/build/legacy/queryOptions.js +8 -0
- package/build/legacy/queryOptions.js.map +1 -0
- package/build/legacy/useBaseQuery.d.cts +1 -1
- package/build/legacy/useBaseQuery.d.ts +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +1 -1
- package/build/legacy/useQueries.d.cts +1 -1
- package/build/legacy/useQueries.d.ts +1 -1
- package/build/legacy/{useQuery-ee0d2f3e.d.ts → useQuery-45f53b2f.d.ts} +1 -1
- package/build/legacy/{useQuery-83bce497.d.ts → useQuery-eb363283.d.ts} +1 -1
- package/build/legacy/useQuery.d.cts +1 -1
- package/build/legacy/useQuery.d.ts +1 -1
- package/build/modern/index.cjs +3 -0
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +2 -1
- package/build/modern/index.d.ts +2 -1
- package/build/modern/index.js +2 -0
- package/build/modern/index.js.map +1 -1
- package/build/modern/queryOptions.cjs +33 -0
- package/build/modern/queryOptions.cjs.map +1 -0
- package/build/modern/queryOptions.d.cts +14 -0
- package/build/modern/queryOptions.d.ts +14 -0
- package/build/modern/queryOptions.js +8 -0
- package/build/modern/queryOptions.js.map +1 -0
- package/build/modern/useBaseQuery.d.cts +1 -1
- package/build/modern/useBaseQuery.d.ts +1 -1
- package/build/modern/useInfiniteQuery.d.cts +1 -1
- package/build/modern/useInfiniteQuery.d.ts +1 -1
- package/build/modern/useQueries.d.cts +1 -1
- package/build/modern/useQueries.d.ts +1 -1
- package/build/modern/{useQuery-ee0d2f3e.d.ts → useQuery-45f53b2f.d.ts} +1 -1
- package/build/modern/{useQuery-83bce497.d.ts → useQuery-eb363283.d.ts} +1 -1
- package/build/modern/useQuery.d.cts +1 -1
- package/build/modern/useQuery.d.ts +1 -1
- package/package.json +2 -2
- package/src/__tests__/mutationCache.test.ts +1 -2
- package/src/__tests__/queryCache.test.ts +1 -2
- package/src/__tests__/queryClient.test.ts +1 -2
- package/src/__tests__/queryOptions.types.test.ts +142 -0
- package/src/__tests__/useInfiniteQuery.test.ts +1 -1
- package/src/__tests__/useInfiniteQuery.types.test.tsx +2 -1
- package/src/__tests__/useIsFetching.test.ts +1 -2
- package/src/__tests__/useIsMutating.test.ts +1 -2
- package/src/__tests__/useMutation.test.ts +1 -1
- package/src/__tests__/useMutation.types.test.tsx +2 -1
- package/src/__tests__/useQueries.test.ts +1 -2
- package/src/__tests__/useQuery.test.ts +1 -2
- package/src/__tests__/useQuery.types.test.ts +277 -0
- package/src/__tests__/useQueryClient.test.ts +1 -1
- package/src/__tests__/utils.test.ts +1 -0
- package/src/__tests__/vueQueryPlugin.test.ts +1 -2
- package/src/index.ts +1 -0
- package/src/queryOptions.ts +31 -0
- package/src/__tests__/useQuery.types.test.tsx +0 -88
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { describe, it } from 'vitest'
|
|
2
|
+
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
+
import { reactive } from 'vue-demi'
|
|
4
|
+
import { queryOptions } from '../queryOptions'
|
|
5
|
+
import { useQuery } from '../useQuery'
|
|
6
|
+
import { doNotExecute } from './test-utils'
|
|
7
|
+
import type { dataTagSymbol } from '@tanstack/query-core'
|
|
8
|
+
import type { Equal, Expect } from './test-utils'
|
|
9
|
+
|
|
10
|
+
describe('queryOptions', () => {
|
|
11
|
+
it('should not allow excess properties', () => {
|
|
12
|
+
doNotExecute(() => {
|
|
13
|
+
return queryOptions({
|
|
14
|
+
queryKey: ['key'],
|
|
15
|
+
queryFn: () => Promise.resolve(5),
|
|
16
|
+
// @ts-expect-error this is a good error, because stallTime does not exist!
|
|
17
|
+
stallTime: 1000,
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
it('should infer types for callbacks', () => {
|
|
22
|
+
doNotExecute(() => {
|
|
23
|
+
return queryOptions({
|
|
24
|
+
queryKey: ['key'],
|
|
25
|
+
queryFn: () => Promise.resolve(5),
|
|
26
|
+
staleTime: 1000,
|
|
27
|
+
select: (data) => {
|
|
28
|
+
const result: Expect<Equal<number, typeof data>> = true
|
|
29
|
+
return result
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
it('should work when passed to useQuery', () => {
|
|
35
|
+
doNotExecute(() => {
|
|
36
|
+
const options = queryOptions({
|
|
37
|
+
queryKey: ['key'],
|
|
38
|
+
queryFn: () => Promise.resolve(5),
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const { data } = reactive(useQuery(options))
|
|
42
|
+
|
|
43
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
44
|
+
|
|
45
|
+
return result
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
it('should tag the queryKey with the result type of the QueryFn', () => {
|
|
49
|
+
doNotExecute(() => {
|
|
50
|
+
const { queryKey } = queryOptions({
|
|
51
|
+
queryKey: ['key'],
|
|
52
|
+
queryFn: () => Promise.resolve(5),
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const result: Expect<
|
|
56
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], number>
|
|
57
|
+
> = true
|
|
58
|
+
return result
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should tag the queryKey even if no promise is returned', () => {
|
|
62
|
+
doNotExecute(() => {
|
|
63
|
+
const { queryKey } = queryOptions({
|
|
64
|
+
queryKey: ['key'],
|
|
65
|
+
queryFn: () => 5,
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
const result: Expect<
|
|
69
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], number>
|
|
70
|
+
> = true
|
|
71
|
+
return result
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should tag the queryKey with unknown if there is no queryFn', () => {
|
|
76
|
+
doNotExecute(() => {
|
|
77
|
+
const { queryKey } = queryOptions({
|
|
78
|
+
queryKey: ['key'],
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const result: Expect<
|
|
82
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], unknown>
|
|
83
|
+
> = true
|
|
84
|
+
return result
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('should return the proper type when passed to getQueryData', () => {
|
|
89
|
+
doNotExecute(() => {
|
|
90
|
+
const { queryKey } = queryOptions({
|
|
91
|
+
queryKey: ['key'],
|
|
92
|
+
queryFn: () => Promise.resolve(5),
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const queryClient = new QueryClient()
|
|
96
|
+
const data = queryClient.getQueryData(queryKey)
|
|
97
|
+
|
|
98
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
99
|
+
return result
|
|
100
|
+
})
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('should properly type updaterFn when passed to setQueryData', () => {
|
|
104
|
+
doNotExecute(() => {
|
|
105
|
+
const { queryKey } = queryOptions({
|
|
106
|
+
queryKey: ['key'],
|
|
107
|
+
queryFn: () => Promise.resolve(5),
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
const queryClient = new QueryClient()
|
|
111
|
+
const data = queryClient.setQueryData(queryKey, (prev) => {
|
|
112
|
+
const result: Expect<Equal<typeof prev, number | undefined>> = true
|
|
113
|
+
return result ? prev : 1
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
117
|
+
return result
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it('should properly type value when passed to setQueryData', () => {
|
|
122
|
+
doNotExecute(() => {
|
|
123
|
+
const { queryKey } = queryOptions({
|
|
124
|
+
queryKey: ['key'],
|
|
125
|
+
queryFn: () => Promise.resolve(5),
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const queryClient = new QueryClient()
|
|
129
|
+
|
|
130
|
+
// @ts-expect-error value should be a number
|
|
131
|
+
queryClient.setQueryData(queryKey, '5')
|
|
132
|
+
// @ts-expect-error value should be a number
|
|
133
|
+
queryClient.setQueryData(queryKey, () => '5')
|
|
134
|
+
|
|
135
|
+
const data = queryClient.setQueryData(queryKey, 5)
|
|
136
|
+
|
|
137
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
138
|
+
return result
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, it } from 'vitest'
|
|
2
|
+
import { reactive } from 'vue-demi'
|
|
2
3
|
import { useInfiniteQuery } from '../useInfiniteQuery'
|
|
3
4
|
import { doNotExecute, simpleFetcher } from './test-utils'
|
|
4
5
|
import type { Equal, Expect } from './test-utils'
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
1
2
|
import { onScopeDispose, reactive } from 'vue-demi'
|
|
2
|
-
|
|
3
|
-
import { vi } from 'vitest'
|
|
4
3
|
import { useQuery } from '../useQuery'
|
|
5
4
|
import { useIsFetching } from '../useIsFetching'
|
|
6
5
|
import { flushPromises, simpleFetcher } from './test-utils'
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { describe, expect, it, test, vi } from 'vitest'
|
|
1
2
|
import { onScopeDispose, reactive } from 'vue-demi'
|
|
2
|
-
|
|
3
|
-
import { vi } from 'vitest'
|
|
4
3
|
import { useMutation } from '../useMutation'
|
|
5
4
|
import { useIsMutating, useMutationState } from '../useMutationState'
|
|
6
5
|
import { useQueryClient } from '../useQueryClient'
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
1
2
|
import { reactive, ref } from 'vue-demi'
|
|
2
|
-
import { vi } from 'vitest'
|
|
3
3
|
import { useMutation } from '../useMutation'
|
|
4
4
|
import { useQueryClient } from '../useQueryClient'
|
|
5
5
|
import { errorMutator, flushPromises, successMutator } from './test-utils'
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
1
2
|
import { onScopeDispose, ref } from 'vue-demi'
|
|
2
|
-
|
|
3
|
-
import { vi } from 'vitest'
|
|
4
3
|
import { useQueries } from '../useQueries'
|
|
5
4
|
import { useQueryClient } from '../useQueryClient'
|
|
6
5
|
import { QueryClient } from '../queryClient'
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
1
2
|
import {
|
|
2
3
|
computed,
|
|
3
4
|
getCurrentInstance,
|
|
@@ -6,8 +7,6 @@ import {
|
|
|
6
7
|
ref,
|
|
7
8
|
} from 'vue-demi'
|
|
8
9
|
import { QueryObserver } from '@tanstack/query-core'
|
|
9
|
-
|
|
10
|
-
import { vi } from 'vitest'
|
|
11
10
|
import { useQuery } from '../useQuery'
|
|
12
11
|
import { useBaseQuery } from '../useBaseQuery'
|
|
13
12
|
import {
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { describe, it } from 'vitest'
|
|
2
|
+
import { reactive } from 'vue-demi'
|
|
3
|
+
import { useQuery } from '../useQuery'
|
|
4
|
+
import { queryOptions } from '../queryOptions'
|
|
5
|
+
import { doNotExecute, simpleFetcher } from './test-utils'
|
|
6
|
+
import type { UseQueryOptions } from '../useQuery'
|
|
7
|
+
import type { Equal, Expect } from './test-utils'
|
|
8
|
+
|
|
9
|
+
describe('initialData', () => {
|
|
10
|
+
describe('Config object overload', () => {
|
|
11
|
+
it('TData should always be defined when initialData is provided as an object', () => {
|
|
12
|
+
doNotExecute(() => {
|
|
13
|
+
const { data } = reactive(
|
|
14
|
+
useQuery({
|
|
15
|
+
queryKey: ['key'],
|
|
16
|
+
queryFn: () => {
|
|
17
|
+
return {
|
|
18
|
+
wow: true,
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
initialData: {
|
|
22
|
+
wow: true,
|
|
23
|
+
},
|
|
24
|
+
}),
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
|
|
28
|
+
|
|
29
|
+
return result
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('TData should be defined when passed through queryOptions', () => {
|
|
34
|
+
doNotExecute(() => {
|
|
35
|
+
const options = queryOptions({
|
|
36
|
+
queryKey: ['key'],
|
|
37
|
+
queryFn: () => {
|
|
38
|
+
return {
|
|
39
|
+
wow: true,
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
initialData: {
|
|
43
|
+
wow: true,
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
const { data } = reactive(useQuery(options))
|
|
47
|
+
|
|
48
|
+
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
|
|
49
|
+
return result
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
|
|
54
|
+
doNotExecute(() => {
|
|
55
|
+
const options = queryOptions({
|
|
56
|
+
queryKey: ['key'],
|
|
57
|
+
queryFn: () => Promise.resolve(1),
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
const query = reactive(
|
|
61
|
+
useQuery({
|
|
62
|
+
...options,
|
|
63
|
+
select: (data) => data > 1,
|
|
64
|
+
}),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const result: Expect<Equal<boolean | undefined, typeof query.data>> =
|
|
68
|
+
true
|
|
69
|
+
return result
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
|
|
74
|
+
doNotExecute(() => {
|
|
75
|
+
const { data } = reactive(
|
|
76
|
+
useQuery({
|
|
77
|
+
queryKey: ['key'],
|
|
78
|
+
queryFn: () => {
|
|
79
|
+
return {
|
|
80
|
+
wow: true,
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
initialData: () => ({
|
|
84
|
+
wow: true,
|
|
85
|
+
}),
|
|
86
|
+
}),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
|
|
90
|
+
return result
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('TData should have undefined in the union when initialData is NOT provided', () => {
|
|
95
|
+
doNotExecute(() => {
|
|
96
|
+
const { data } = reactive(
|
|
97
|
+
useQuery({
|
|
98
|
+
queryKey: ['key'],
|
|
99
|
+
queryFn: () => {
|
|
100
|
+
return {
|
|
101
|
+
wow: true,
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
|
|
108
|
+
true
|
|
109
|
+
return result
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
|
|
114
|
+
doNotExecute(() => {
|
|
115
|
+
const { data } = reactive(
|
|
116
|
+
useQuery({
|
|
117
|
+
queryKey: ['key'],
|
|
118
|
+
queryFn: () => {
|
|
119
|
+
return {
|
|
120
|
+
wow: true,
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
initialData: () => undefined as { wow: boolean } | undefined,
|
|
124
|
+
}),
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
|
|
128
|
+
true
|
|
129
|
+
return result
|
|
130
|
+
})
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
|
|
134
|
+
doNotExecute(() => {
|
|
135
|
+
const { data, isSuccess } = reactive(
|
|
136
|
+
useQuery({
|
|
137
|
+
queryKey: ['key'],
|
|
138
|
+
queryFn: () => {
|
|
139
|
+
return {
|
|
140
|
+
wow: true,
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
initialData: () => undefined as { wow: boolean } | undefined,
|
|
144
|
+
}),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
if (isSuccess) {
|
|
148
|
+
const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
|
|
149
|
+
return result
|
|
150
|
+
}
|
|
151
|
+
return false
|
|
152
|
+
})
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
describe('custom composables', () => {
|
|
157
|
+
it('should allow custom composables using UseQueryOptions', () => {
|
|
158
|
+
doNotExecute(() => {
|
|
159
|
+
type Data = string
|
|
160
|
+
|
|
161
|
+
const useCustomQuery = (
|
|
162
|
+
options?: Omit<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
|
|
163
|
+
) => {
|
|
164
|
+
return useQuery({
|
|
165
|
+
...options,
|
|
166
|
+
queryKey: ['todos-key'],
|
|
167
|
+
queryFn: () => Promise.resolve('data'),
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const { data } = reactive(useCustomQuery())
|
|
172
|
+
|
|
173
|
+
const result: Expect<Equal<Data | undefined, typeof data>> = true
|
|
174
|
+
return result
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
describe('structuralSharing', () => {
|
|
180
|
+
it('should restrict to same types', () => {
|
|
181
|
+
doNotExecute(() => {
|
|
182
|
+
useQuery({
|
|
183
|
+
queryKey: ['key'],
|
|
184
|
+
queryFn: () => 5,
|
|
185
|
+
structuralSharing: (_oldData, newData) => {
|
|
186
|
+
return newData
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
describe('Discriminated union return type', () => {
|
|
194
|
+
it('data should be possibly undefined by default', () => {
|
|
195
|
+
doNotExecute(() => {
|
|
196
|
+
const query = reactive(
|
|
197
|
+
useQuery({
|
|
198
|
+
queryKey: ['key'],
|
|
199
|
+
queryFn: simpleFetcher,
|
|
200
|
+
}),
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
const result: Expect<Equal<string | undefined, typeof query.data>> =
|
|
204
|
+
true
|
|
205
|
+
return result
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it('data should be defined when query is success', () => {
|
|
210
|
+
doNotExecute(() => {
|
|
211
|
+
const query = reactive(
|
|
212
|
+
useQuery({
|
|
213
|
+
queryKey: ['key'],
|
|
214
|
+
queryFn: simpleFetcher,
|
|
215
|
+
}),
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
if (query.isSuccess) {
|
|
219
|
+
const result: Expect<Equal<string, typeof query.data>> = true
|
|
220
|
+
return result
|
|
221
|
+
}
|
|
222
|
+
return
|
|
223
|
+
})
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
it('error should be null when query is success', () => {
|
|
227
|
+
doNotExecute(() => {
|
|
228
|
+
const query = reactive(
|
|
229
|
+
useQuery({
|
|
230
|
+
queryKey: ['key'],
|
|
231
|
+
queryFn: simpleFetcher,
|
|
232
|
+
}),
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
if (query.isSuccess) {
|
|
236
|
+
const result: Expect<Equal<null, typeof query.error>> = true
|
|
237
|
+
return result
|
|
238
|
+
}
|
|
239
|
+
return
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it('data should be undefined when query is pending', () => {
|
|
244
|
+
doNotExecute(() => {
|
|
245
|
+
const query = reactive(
|
|
246
|
+
useQuery({
|
|
247
|
+
queryKey: ['key'],
|
|
248
|
+
queryFn: simpleFetcher,
|
|
249
|
+
}),
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
if (query.isPending) {
|
|
253
|
+
const result: Expect<Equal<undefined, typeof query.data>> = true
|
|
254
|
+
return result
|
|
255
|
+
}
|
|
256
|
+
return
|
|
257
|
+
})
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
it('error should be defined when query is error', () => {
|
|
261
|
+
doNotExecute(() => {
|
|
262
|
+
const query = reactive(
|
|
263
|
+
useQuery({
|
|
264
|
+
queryKey: ['key'],
|
|
265
|
+
queryFn: simpleFetcher,
|
|
266
|
+
}),
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
if (query.isError) {
|
|
270
|
+
const result: Expect<Equal<Error, typeof query.error>> = true
|
|
271
|
+
return result
|
|
272
|
+
}
|
|
273
|
+
return
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
})
|
|
277
|
+
})
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
1
2
|
import { hasInjectionContext, inject } from 'vue-demi'
|
|
2
|
-
import { vi } from 'vitest'
|
|
3
3
|
import { useQueryClient } from '../useQueryClient'
|
|
4
4
|
import { VUE_QUERY_CLIENT } from '../utils'
|
|
5
5
|
import type { Mock } from 'vitest'
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
1
2
|
import { isVue2, isVue3, ref } from 'vue-demi'
|
|
2
|
-
|
|
3
|
-
import { vi } from 'vitest'
|
|
4
3
|
import { QueryClient } from '../queryClient'
|
|
5
4
|
import { VueQueryPlugin } from '../vueQueryPlugin'
|
|
6
5
|
import { VUE_QUERY_CLIENT } from '../utils'
|
package/src/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ export { VueQueryPlugin } from './vueQueryPlugin'
|
|
|
5
5
|
|
|
6
6
|
export { QueryClient } from './queryClient'
|
|
7
7
|
export { QueryCache } from './queryCache'
|
|
8
|
+
export { queryOptions } from './queryOptions'
|
|
8
9
|
export { MutationCache } from './mutationCache'
|
|
9
10
|
export { useQuery } from './useQuery'
|
|
10
11
|
export { useQueries } from './useQueries'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
|
|
2
|
+
import type {
|
|
3
|
+
DefinedInitialQueryOptions,
|
|
4
|
+
UndefinedInitialQueryOptions,
|
|
5
|
+
} from './useQuery'
|
|
6
|
+
|
|
7
|
+
export function queryOptions<
|
|
8
|
+
TQueryFnData = unknown,
|
|
9
|
+
TError = DefaultError,
|
|
10
|
+
TData = TQueryFnData,
|
|
11
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
12
|
+
>(
|
|
13
|
+
options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
14
|
+
): UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
15
|
+
queryKey: DataTag<TQueryKey, TData>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function queryOptions<
|
|
19
|
+
TQueryFnData = unknown,
|
|
20
|
+
TError = DefaultError,
|
|
21
|
+
TData = TQueryFnData,
|
|
22
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
23
|
+
>(
|
|
24
|
+
options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
25
|
+
): DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
26
|
+
queryKey: DataTag<TQueryKey, TData>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function queryOptions(options: unknown) {
|
|
30
|
+
return options
|
|
31
|
+
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { reactive } from 'vue'
|
|
2
|
-
import { useQuery } from '../useQuery'
|
|
3
|
-
import { doNotExecute, simpleFetcher } from './test-utils'
|
|
4
|
-
import type { Equal, Expect } from './test-utils'
|
|
5
|
-
|
|
6
|
-
describe('Discriminated union return type', () => {
|
|
7
|
-
it('data should be possibly undefined by default', () => {
|
|
8
|
-
doNotExecute(() => {
|
|
9
|
-
const query = reactive(
|
|
10
|
-
useQuery({
|
|
11
|
-
queryKey: ['key'],
|
|
12
|
-
queryFn: simpleFetcher,
|
|
13
|
-
}),
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
const result: Expect<Equal<string | undefined, typeof query.data>> = true
|
|
17
|
-
return result
|
|
18
|
-
})
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
it('data should be defined when query is success', () => {
|
|
22
|
-
doNotExecute(() => {
|
|
23
|
-
const query = reactive(
|
|
24
|
-
useQuery({
|
|
25
|
-
queryKey: ['key'],
|
|
26
|
-
queryFn: simpleFetcher,
|
|
27
|
-
}),
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
if (query.isSuccess) {
|
|
31
|
-
const result: Expect<Equal<string, typeof query.data>> = true
|
|
32
|
-
return result
|
|
33
|
-
}
|
|
34
|
-
return
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
it('error should be null when query is success', () => {
|
|
39
|
-
doNotExecute(() => {
|
|
40
|
-
const query = reactive(
|
|
41
|
-
useQuery({
|
|
42
|
-
queryKey: ['key'],
|
|
43
|
-
queryFn: simpleFetcher,
|
|
44
|
-
}),
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
if (query.isSuccess) {
|
|
48
|
-
const result: Expect<Equal<null, typeof query.error>> = true
|
|
49
|
-
return result
|
|
50
|
-
}
|
|
51
|
-
return
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
it('data should be undefined when query is pending', () => {
|
|
56
|
-
doNotExecute(() => {
|
|
57
|
-
const query = reactive(
|
|
58
|
-
useQuery({
|
|
59
|
-
queryKey: ['key'],
|
|
60
|
-
queryFn: simpleFetcher,
|
|
61
|
-
}),
|
|
62
|
-
)
|
|
63
|
-
|
|
64
|
-
if (query.isPending) {
|
|
65
|
-
const result: Expect<Equal<undefined, typeof query.data>> = true
|
|
66
|
-
return result
|
|
67
|
-
}
|
|
68
|
-
return
|
|
69
|
-
})
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('error should be defined when query is error', () => {
|
|
73
|
-
doNotExecute(() => {
|
|
74
|
-
const query = reactive(
|
|
75
|
-
useQuery({
|
|
76
|
-
queryKey: ['key'],
|
|
77
|
-
queryFn: simpleFetcher,
|
|
78
|
-
}),
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
if (query.isError) {
|
|
82
|
-
const result: Expect<Equal<Error, typeof query.error>> = true
|
|
83
|
-
return result
|
|
84
|
-
}
|
|
85
|
-
return
|
|
86
|
-
})
|
|
87
|
-
})
|
|
88
|
-
})
|