@tanstack/vue-query 5.40.0 → 5.40.1

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.
@@ -1,106 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { reactive } from 'vue-demi'
3
- import { useInfiniteQuery } from '../useInfiniteQuery'
4
- import { doNotExecute, simpleFetcher } from './test-utils'
5
- import type { Equal, Expect } from './test-utils'
6
- import type { InfiniteData } from '@tanstack/query-core'
7
-
8
- describe('Discriminated union return type', () => {
9
- it('data should be possibly undefined by default', () => {
10
- doNotExecute(() => {
11
- const query = reactive(
12
- useInfiniteQuery({
13
- queryKey: ['infiniteQuery'],
14
- queryFn: simpleFetcher,
15
- getNextPageParam: () => undefined,
16
- initialPageParam: 0,
17
- }),
18
- )
19
-
20
- // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
21
- const result: Expect<
22
- Equal<InfiniteData<string, unknown> | undefined, typeof query.data>
23
- > = true
24
- return result
25
- })
26
- })
27
-
28
- it('data should be defined when query is success', () => {
29
- doNotExecute(() => {
30
- const query = reactive(
31
- useInfiniteQuery({
32
- queryKey: ['infiniteQuery'],
33
- queryFn: simpleFetcher,
34
- getNextPageParam: () => undefined,
35
- initialPageParam: 0,
36
- }),
37
- )
38
-
39
- if (query.isSuccess) {
40
- // TODO: Order of generics prevents pageParams to be typed correctly. Using `unknown` for now
41
- const result: Expect<
42
- Equal<InfiniteData<string, unknown>, typeof query.data>
43
- > = true
44
- return result
45
- }
46
- return
47
- })
48
- })
49
-
50
- it('error should be null when query is success', () => {
51
- doNotExecute(() => {
52
- const query = reactive(
53
- useInfiniteQuery({
54
- queryKey: ['infiniteQuery'],
55
- queryFn: simpleFetcher,
56
- getNextPageParam: () => undefined,
57
- initialPageParam: 0,
58
- }),
59
- )
60
-
61
- if (query.isSuccess) {
62
- const result: Expect<Equal<null, typeof query.error>> = true
63
- return result
64
- }
65
- return
66
- })
67
- })
68
-
69
- it('data should be undefined when query is pending', () => {
70
- doNotExecute(() => {
71
- const query = reactive(
72
- useInfiniteQuery({
73
- queryKey: ['infiniteQuery'],
74
- queryFn: simpleFetcher,
75
- getNextPageParam: () => undefined,
76
- initialPageParam: 0,
77
- }),
78
- )
79
-
80
- if (query.isPending) {
81
- const result: Expect<Equal<undefined, typeof query.data>> = true
82
- return result
83
- }
84
- return
85
- })
86
- })
87
-
88
- it('error should be defined when query is error', () => {
89
- doNotExecute(() => {
90
- const query = reactive(
91
- useInfiniteQuery({
92
- queryKey: ['infiniteQuery'],
93
- queryFn: simpleFetcher,
94
- getNextPageParam: () => undefined,
95
- initialPageParam: 0,
96
- }),
97
- )
98
-
99
- if (query.isError) {
100
- const result: Expect<Equal<Error, typeof query.error>> = true
101
- return result
102
- }
103
- return
104
- })
105
- })
106
- })
@@ -1,98 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { reactive } from 'vue-demi'
3
- import { useMutation } from '../useMutation'
4
- import { doNotExecute, successMutator } from './test-utils'
5
- import type { Equal, Expect } from './test-utils'
6
-
7
- describe('Discriminated union return type', () => {
8
- it('data should be possibly undefined by default', () => {
9
- doNotExecute(() => {
10
- const mutation = reactive(
11
- useMutation({ mutationFn: successMutator<string> }),
12
- )
13
-
14
- const result: Expect<Equal<string | undefined, typeof mutation.data>> =
15
- true
16
- return result
17
- })
18
- })
19
-
20
- it('data should be defined when mutation is success', () => {
21
- doNotExecute(() => {
22
- const mutation = reactive(
23
- useMutation({ mutationFn: successMutator<string> }),
24
- )
25
-
26
- if (mutation.isSuccess) {
27
- const result: Expect<Equal<string, typeof mutation.data>> = true
28
- return result
29
- }
30
- return
31
- })
32
- })
33
-
34
- it('error should be null when mutation is success', () => {
35
- doNotExecute(() => {
36
- const mutation = reactive(
37
- useMutation({ mutationFn: successMutator<string> }),
38
- )
39
-
40
- if (mutation.isSuccess) {
41
- const result: Expect<Equal<null, typeof mutation.error>> = true
42
- return result
43
- }
44
- return
45
- })
46
- })
47
-
48
- it('data should be undefined when mutation is pending', () => {
49
- doNotExecute(() => {
50
- const mutation = reactive(
51
- useMutation({ mutationFn: successMutator<string> }),
52
- )
53
-
54
- if (mutation.isPending) {
55
- const result: Expect<Equal<undefined, typeof mutation.data>> = true
56
- return result
57
- }
58
- return
59
- })
60
- })
61
-
62
- it('error should be defined when mutation is error', () => {
63
- doNotExecute(() => {
64
- const mutation = reactive(
65
- useMutation({ mutationFn: successMutator<string> }),
66
- )
67
-
68
- if (mutation.isError) {
69
- const result: Expect<Equal<Error, typeof mutation.error>> = true
70
- return result
71
- }
72
- return
73
- })
74
- })
75
-
76
- it('should narrow variables', () => {
77
- doNotExecute(() => {
78
- const mutation = reactive(
79
- useMutation({ mutationFn: successMutator<string> }),
80
- )
81
-
82
- if (mutation.isIdle) {
83
- const result: Expect<Equal<undefined, typeof mutation.variables>> = true
84
- return result
85
- }
86
- if (mutation.isPending) {
87
- const result: Expect<Equal<string, typeof mutation.variables>> = true
88
- return result
89
- }
90
- if (mutation.isSuccess) {
91
- const result: Expect<Equal<string, typeof mutation.variables>> = true
92
- return result
93
- }
94
- const result: Expect<Equal<string, typeof mutation.variables>> = true
95
- return result
96
- })
97
- })
98
- })
@@ -1,176 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { reactive } from 'vue'
3
- import { skipToken, useQueries } from '..'
4
- import { queryOptions } from '../queryOptions'
5
- import { doNotExecute } from './test-utils'
6
- import type { OmitKeyof } from '..'
7
- import type { UseQueryOptions } from '../useQuery'
8
- import type { Equal, Expect } from './test-utils'
9
-
10
- describe('UseQueries config object overload', () => {
11
- it('TData should always be defined when initialData is provided as an object', () => {
12
- const query1 = {
13
- queryKey: ['key1'],
14
- queryFn: () => {
15
- return {
16
- wow: true,
17
- }
18
- },
19
- initialData: {
20
- wow: false,
21
- },
22
- }
23
-
24
- const query2 = queryOptions({
25
- queryKey: ['key2'],
26
- queryFn: () => 'Query Data',
27
- initialData: 'initial data',
28
- })
29
-
30
- const query3 = {
31
- queryKey: ['key2'],
32
- queryFn: () => 'Query Data',
33
- }
34
-
35
- doNotExecute(() => {
36
- const { value: queriesState } = useQueries({
37
- queries: [query1, query2, query3],
38
- })
39
-
40
- const query1Data = queriesState[0].data
41
- const query2Data = queriesState[1].data
42
- const query3Data = queriesState[2].data
43
-
44
- const result1: Expect<Equal<{ wow: boolean }, typeof query1Data>> = true
45
-
46
- const result2: Expect<Equal<string, typeof query2Data>> = true
47
-
48
- const result3: Expect<Equal<string | undefined, typeof query3Data>> = true
49
-
50
- return result1 && result2 && result3
51
- })
52
- })
53
-
54
- it('TData should be defined when passed through queryOptions', () => {
55
- doNotExecute(() => {
56
- const options = queryOptions({
57
- queryKey: ['key'],
58
- queryFn: () => {
59
- return {
60
- wow: true,
61
- }
62
- },
63
- initialData: {
64
- wow: true,
65
- },
66
- })
67
-
68
- const { value: queriesState } = useQueries({ queries: [options] })
69
-
70
- const data = queriesState[0].data
71
-
72
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
73
- return result
74
- })
75
- })
76
-
77
- it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQueries', () => {
78
- doNotExecute(() => {
79
- const query1 = queryOptions({
80
- queryKey: ['key'],
81
- queryFn: () => Promise.resolve(1),
82
- select: (data) => data > 1,
83
- })
84
-
85
- const query2 = {
86
- queryKey: ['key'],
87
- queryFn: () => Promise.resolve(1),
88
- select: (data: any) => data > 1,
89
- }
90
-
91
- const queriesState = reactive(useQueries({ queries: [query1, query2] }))
92
- const query1Data = queriesState.value[0].data
93
- const query2Data = queriesState.value[1].data
94
-
95
- const result1: Expect<Equal<boolean | undefined, typeof query1Data>> =
96
- true
97
- const result2: Expect<Equal<boolean | undefined, typeof query2Data>> =
98
- true
99
- return result1 && result2
100
- })
101
- })
102
-
103
- it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
104
- doNotExecute(() => {
105
- const { value: queriesState } = useQueries({
106
- queries: [
107
- {
108
- queryKey: ['key'],
109
- queryFn: () => {
110
- return {
111
- wow: true,
112
- }
113
- },
114
- initialData: () => undefined as { wow: boolean } | undefined,
115
- },
116
- ],
117
- })
118
-
119
- const data = queriesState[0].data
120
-
121
- const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
122
- true
123
- return result
124
- })
125
- })
126
-
127
- it('TData should have correct type when conditional skipToken is passed', () => {
128
- doNotExecute(() => {
129
- const { value: queriesState } = useQueries({
130
- queries: [
131
- {
132
- queryKey: ['key'],
133
- queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
134
- },
135
- ],
136
- })
137
-
138
- const data = queriesState[0].data
139
-
140
- const result: Expect<Equal<number | undefined, typeof data>> = true
141
- return result
142
- })
143
- })
144
-
145
- describe('custom hook', () => {
146
- it('should allow custom hooks using UseQueryOptions', () => {
147
- doNotExecute(() => {
148
- type Data = string
149
-
150
- const useCustomQueries = (
151
- options?: OmitKeyof<
152
- UseQueryOptions<Data>,
153
- 'queryKey' | 'queryFn',
154
- 'safely'
155
- >,
156
- ) => {
157
- return useQueries({
158
- queries: [
159
- {
160
- ...options,
161
- queryKey: ['todos-key'],
162
- queryFn: () => Promise.resolve('data'),
163
- },
164
- ],
165
- })
166
- }
167
-
168
- const { value: queriesState } = useCustomQueries()
169
- const data = queriesState[0].data
170
-
171
- const result: Expect<Equal<Data | undefined, typeof data>> = true
172
- return result
173
- })
174
- })
175
- })
176
- })
@@ -1,282 +0,0 @@
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 { OmitKeyof } from '..'
7
- import type { UseQueryOptions } from '../useQuery'
8
- import type { Equal, Expect } from './test-utils'
9
-
10
- describe('initialData', () => {
11
- describe('Config object overload', () => {
12
- it('TData should always be defined when initialData is provided as an object', () => {
13
- doNotExecute(() => {
14
- const { data } = reactive(
15
- useQuery({
16
- queryKey: ['key'],
17
- queryFn: () => {
18
- return {
19
- wow: true,
20
- }
21
- },
22
- initialData: {
23
- wow: true,
24
- },
25
- }),
26
- )
27
-
28
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
29
-
30
- return result
31
- })
32
- })
33
-
34
- it('TData should be defined when passed through queryOptions', () => {
35
- doNotExecute(() => {
36
- const options = queryOptions({
37
- queryKey: ['key'],
38
- queryFn: () => {
39
- return {
40
- wow: true,
41
- }
42
- },
43
- initialData: {
44
- wow: true,
45
- },
46
- })
47
- const { data } = reactive(useQuery(options))
48
-
49
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
50
- return result
51
- })
52
- })
53
-
54
- it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
55
- doNotExecute(() => {
56
- const options = queryOptions({
57
- queryKey: ['key'],
58
- queryFn: () => Promise.resolve(1),
59
- })
60
-
61
- const query = reactive(
62
- useQuery({
63
- ...options,
64
- select: (data) => data > 1,
65
- }),
66
- )
67
-
68
- const result: Expect<Equal<boolean | undefined, typeof query.data>> =
69
- true
70
- return result
71
- })
72
- })
73
-
74
- it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
75
- doNotExecute(() => {
76
- const { data } = reactive(
77
- useQuery({
78
- queryKey: ['key'],
79
- queryFn: () => {
80
- return {
81
- wow: true,
82
- }
83
- },
84
- initialData: () => ({
85
- wow: true,
86
- }),
87
- }),
88
- )
89
-
90
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
91
- return result
92
- })
93
- })
94
-
95
- it('TData should have undefined in the union when initialData is NOT provided', () => {
96
- doNotExecute(() => {
97
- const { data } = reactive(
98
- useQuery({
99
- queryKey: ['key'],
100
- queryFn: () => {
101
- return {
102
- wow: true,
103
- }
104
- },
105
- }),
106
- )
107
-
108
- const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
109
- true
110
- return result
111
- })
112
- })
113
-
114
- it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
115
- doNotExecute(() => {
116
- const { data } = reactive(
117
- useQuery({
118
- queryKey: ['key'],
119
- queryFn: () => {
120
- return {
121
- wow: true,
122
- }
123
- },
124
- initialData: () => undefined as { wow: boolean } | undefined,
125
- }),
126
- )
127
-
128
- const result: Expect<Equal<{ wow: boolean } | undefined, typeof data>> =
129
- true
130
- return result
131
- })
132
- })
133
-
134
- it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
135
- doNotExecute(() => {
136
- const { data, isSuccess } = reactive(
137
- useQuery({
138
- queryKey: ['key'],
139
- queryFn: () => {
140
- return {
141
- wow: true,
142
- }
143
- },
144
- initialData: () => undefined as { wow: boolean } | undefined,
145
- }),
146
- )
147
-
148
- if (isSuccess) {
149
- const result: Expect<Equal<{ wow: boolean }, typeof data>> = true
150
- return result
151
- }
152
- return false
153
- })
154
- })
155
- })
156
-
157
- describe('custom composable', () => {
158
- it('should allow custom composable using UseQueryOptions', () => {
159
- doNotExecute(() => {
160
- type Data = string
161
-
162
- const useCustomQuery = (
163
- options?: OmitKeyof<
164
- UseQueryOptions<Data>,
165
- 'queryKey' | 'queryFn',
166
- 'safely'
167
- >,
168
- ) => {
169
- return useQuery({
170
- ...options,
171
- queryKey: ['todos-key'],
172
- queryFn: () => Promise.resolve('data'),
173
- })
174
- }
175
-
176
- const { data } = reactive(useCustomQuery())
177
-
178
- const result: Expect<Equal<Data | undefined, typeof data>> = true
179
- return result
180
- })
181
- })
182
- })
183
-
184
- describe('structuralSharing', () => {
185
- it('should restrict to same types', () => {
186
- doNotExecute(() => {
187
- useQuery({
188
- queryKey: ['key'],
189
- queryFn: () => 5,
190
- structuralSharing: (_oldData, newData) => {
191
- return newData
192
- },
193
- })
194
- })
195
- })
196
- })
197
-
198
- describe('Discriminated union return type', () => {
199
- it('data should be possibly undefined by default', () => {
200
- doNotExecute(() => {
201
- const query = reactive(
202
- useQuery({
203
- queryKey: ['key'],
204
- queryFn: simpleFetcher,
205
- }),
206
- )
207
-
208
- const result: Expect<Equal<string | undefined, typeof query.data>> =
209
- true
210
- return result
211
- })
212
- })
213
-
214
- it('data should be defined when query is success', () => {
215
- doNotExecute(() => {
216
- const query = reactive(
217
- useQuery({
218
- queryKey: ['key'],
219
- queryFn: simpleFetcher,
220
- }),
221
- )
222
-
223
- if (query.isSuccess) {
224
- const result: Expect<Equal<string, typeof query.data>> = true
225
- return result
226
- }
227
- return
228
- })
229
- })
230
-
231
- it('error should be null when query is success', () => {
232
- doNotExecute(() => {
233
- const query = reactive(
234
- useQuery({
235
- queryKey: ['key'],
236
- queryFn: simpleFetcher,
237
- }),
238
- )
239
-
240
- if (query.isSuccess) {
241
- const result: Expect<Equal<null, typeof query.error>> = true
242
- return result
243
- }
244
- return
245
- })
246
- })
247
-
248
- it('data should be undefined when query is pending', () => {
249
- doNotExecute(() => {
250
- const query = reactive(
251
- useQuery({
252
- queryKey: ['key'],
253
- queryFn: simpleFetcher,
254
- }),
255
- )
256
-
257
- if (query.isPending) {
258
- const result: Expect<Equal<undefined, typeof query.data>> = true
259
- return result
260
- }
261
- return
262
- })
263
- })
264
-
265
- it('error should be defined when query is error', () => {
266
- doNotExecute(() => {
267
- const query = reactive(
268
- useQuery({
269
- queryKey: ['key'],
270
- queryFn: simpleFetcher,
271
- }),
272
- )
273
-
274
- if (query.isError) {
275
- const result: Expect<Equal<Error, typeof query.error>> = true
276
- return result
277
- }
278
- return
279
- })
280
- })
281
- })
282
- })