@tanstack/react-query 5.0.0-rc.0 → 5.0.0-rc.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/README.md +1 -1
- package/build/codemods/coverage/clover.xml +2 -2
- package/build/codemods/coverage/index.html +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +11 -6
- package/build/legacy/infiniteQueryOptions.d.ts +11 -6
- package/build/legacy/infiniteQueryOptions.js.map +1 -1
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +6 -5
- package/build/legacy/queryOptions.d.ts +6 -5
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +3 -3
- package/build/legacy/types.d.ts +3 -3
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +2 -2
- package/build/legacy/useInfiniteQuery.d.ts +2 -2
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +2 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +2 -1
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +11 -6
- package/build/modern/infiniteQueryOptions.d.ts +11 -6
- package/build/modern/infiniteQueryOptions.js.map +1 -1
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +6 -5
- package/build/modern/queryOptions.d.ts +6 -5
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +3 -3
- package/build/modern/types.d.ts +3 -3
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +2 -2
- package/build/modern/useInfiniteQuery.d.ts +2 -2
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +2 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +2 -1
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/infiniteQueryOptions.types.test.tsx +163 -0
- package/src/__tests__/queryOptions.types.test.tsx +154 -3
- package/src/__tests__/suspense.types.test.tsx +146 -0
- package/src/__tests__/useQuery.test.tsx +3 -3
- package/src/__tests__/useQuery.types.test.tsx +7 -1
- package/src/infiniteQueryOptions.ts +21 -22
- package/src/queryOptions.ts +11 -19
- package/src/types.ts +2 -4
- package/src/useInfiniteQuery.ts +0 -2
- package/src/useQueries.ts +1 -1
- package/src/useSuspenseInfiniteQuery.ts +2 -1
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/query-core'
|
|
2
|
+
import { infiniteQueryOptions } from '../infiniteQueryOptions'
|
|
3
|
+
import { useInfiniteQuery } from '../useInfiniteQuery'
|
|
4
|
+
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
|
|
5
|
+
import { doNotExecute } from './utils'
|
|
6
|
+
import type { InfiniteData, dataTagSymbol } from '@tanstack/query-core'
|
|
7
|
+
import type { Equal, Expect } from './utils'
|
|
8
|
+
|
|
9
|
+
describe('queryOptions', () => {
|
|
10
|
+
it('should not allow excess properties', () => {
|
|
11
|
+
doNotExecute(() => {
|
|
12
|
+
return infiniteQueryOptions({
|
|
13
|
+
queryKey: ['key'],
|
|
14
|
+
queryFn: () => Promise.resolve('data'),
|
|
15
|
+
getNextPageParam: () => 1,
|
|
16
|
+
initialPageParam: 1,
|
|
17
|
+
// @ts-expect-error this is a good error, because stallTime does not exist!
|
|
18
|
+
stallTime: 1000,
|
|
19
|
+
})
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
it('should infer types for callbacks', () => {
|
|
23
|
+
doNotExecute(() => {
|
|
24
|
+
return infiniteQueryOptions({
|
|
25
|
+
queryKey: ['key'],
|
|
26
|
+
queryFn: () => Promise.resolve('data'),
|
|
27
|
+
staleTime: 1000,
|
|
28
|
+
getNextPageParam: () => 1,
|
|
29
|
+
initialPageParam: 1,
|
|
30
|
+
select: (data) => {
|
|
31
|
+
const result: Expect<
|
|
32
|
+
Equal<InfiniteData<string, number>, typeof data>
|
|
33
|
+
> = true
|
|
34
|
+
return result
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
})
|
|
39
|
+
it('should work when passed to useInfiniteQuery', () => {
|
|
40
|
+
doNotExecute(() => {
|
|
41
|
+
const options = infiniteQueryOptions({
|
|
42
|
+
queryKey: ['key'],
|
|
43
|
+
queryFn: () => Promise.resolve('string'),
|
|
44
|
+
getNextPageParam: () => 1,
|
|
45
|
+
initialPageParam: 1,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const { data } = useInfiniteQuery(options)
|
|
49
|
+
|
|
50
|
+
// known issue: type of pageParams is unknown when returned from useInfiniteQuery
|
|
51
|
+
const result: Expect<
|
|
52
|
+
Equal<typeof data, InfiniteData<string, unknown> | undefined>
|
|
53
|
+
> = true
|
|
54
|
+
return result
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
it('should work when passed to useSuspenseInfiniteQuery', () => {
|
|
58
|
+
doNotExecute(() => {
|
|
59
|
+
const options = infiniteQueryOptions({
|
|
60
|
+
queryKey: ['key'],
|
|
61
|
+
queryFn: () => Promise.resolve('string'),
|
|
62
|
+
getNextPageParam: () => 1,
|
|
63
|
+
initialPageParam: 1,
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const { data } = useSuspenseInfiniteQuery(options)
|
|
67
|
+
|
|
68
|
+
const result: Expect<Equal<typeof data, InfiniteData<string, unknown>>> =
|
|
69
|
+
true
|
|
70
|
+
return result
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
it('should work when passed to fetchInfiniteQuery', () => {
|
|
74
|
+
doNotExecute(async () => {
|
|
75
|
+
const options = infiniteQueryOptions({
|
|
76
|
+
queryKey: ['key'],
|
|
77
|
+
queryFn: () => Promise.resolve('string'),
|
|
78
|
+
getNextPageParam: () => 1,
|
|
79
|
+
initialPageParam: 1,
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
const data = await new QueryClient().fetchInfiniteQuery(options)
|
|
83
|
+
|
|
84
|
+
const result: Expect<Equal<typeof data, InfiniteData<string, number>>> =
|
|
85
|
+
true
|
|
86
|
+
return result
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
it('should tag the queryKey with the result type of the QueryFn', () => {
|
|
90
|
+
doNotExecute(() => {
|
|
91
|
+
const { queryKey } = infiniteQueryOptions({
|
|
92
|
+
queryKey: ['key'],
|
|
93
|
+
queryFn: () => Promise.resolve('string'),
|
|
94
|
+
getNextPageParam: () => 1,
|
|
95
|
+
initialPageParam: 1,
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
const result: Expect<
|
|
99
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], InfiniteData<string>>
|
|
100
|
+
> = true
|
|
101
|
+
return result
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('should tag the queryKey even if no promise is returned', () => {
|
|
105
|
+
doNotExecute(() => {
|
|
106
|
+
const { queryKey } = infiniteQueryOptions({
|
|
107
|
+
queryKey: ['key'],
|
|
108
|
+
queryFn: () => 'string',
|
|
109
|
+
getNextPageParam: () => 1,
|
|
110
|
+
initialPageParam: 1,
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
const result: Expect<
|
|
114
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], InfiniteData<string>>
|
|
115
|
+
> = true
|
|
116
|
+
return result
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('should return the proper type when passed to getQueryData', () => {
|
|
121
|
+
doNotExecute(() => {
|
|
122
|
+
const { queryKey } = infiniteQueryOptions({
|
|
123
|
+
queryKey: ['key'],
|
|
124
|
+
queryFn: () => Promise.resolve('string'),
|
|
125
|
+
getNextPageParam: () => 1,
|
|
126
|
+
initialPageParam: 1,
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
const queryClient = new QueryClient()
|
|
130
|
+
const data = queryClient.getQueryData(queryKey)
|
|
131
|
+
|
|
132
|
+
const result: Expect<
|
|
133
|
+
Equal<typeof data, InfiniteData<string, unknown> | undefined>
|
|
134
|
+
> = true
|
|
135
|
+
return result
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('should properly type when passed to setQueryData', () => {
|
|
140
|
+
doNotExecute(() => {
|
|
141
|
+
const { queryKey } = infiniteQueryOptions({
|
|
142
|
+
queryKey: ['key'],
|
|
143
|
+
queryFn: () => Promise.resolve('string'),
|
|
144
|
+
getNextPageParam: () => 1,
|
|
145
|
+
initialPageParam: 1,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const queryClient = new QueryClient()
|
|
149
|
+
const data = queryClient.setQueryData(queryKey, (prev) => {
|
|
150
|
+
const result: Expect<
|
|
151
|
+
Equal<typeof prev, InfiniteData<string, unknown> | undefined>
|
|
152
|
+
> = true
|
|
153
|
+
return result ? prev : { pages: ['foo'], pageParams: [1] }
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
const result: Expect<
|
|
157
|
+
Equal<typeof data, InfiniteData<string, unknown> | undefined>
|
|
158
|
+
> = true
|
|
159
|
+
return result
|
|
160
|
+
})
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
})
|
|
@@ -1,14 +1,19 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/query-core'
|
|
1
2
|
import { queryOptions } from '../queryOptions'
|
|
3
|
+
import { useQuery } from '../useQuery'
|
|
4
|
+
import { useQueries } from '../useQueries'
|
|
5
|
+
import { useSuspenseQuery } from '../useSuspenseQuery'
|
|
2
6
|
import { doNotExecute } from './utils'
|
|
7
|
+
import type { dataTagSymbol } from '@tanstack/query-core'
|
|
3
8
|
import type { Equal, Expect } from './utils'
|
|
4
9
|
|
|
5
10
|
describe('queryOptions', () => {
|
|
6
11
|
it('should not allow excess properties', () => {
|
|
7
12
|
doNotExecute(() => {
|
|
8
|
-
// @ts-expect-error this is a good error, because stallTime does not exist!
|
|
9
13
|
return queryOptions({
|
|
10
14
|
queryKey: ['key'],
|
|
11
15
|
queryFn: () => Promise.resolve(5),
|
|
16
|
+
// @ts-expect-error this is a good error, because stallTime does not exist!
|
|
12
17
|
stallTime: 1000,
|
|
13
18
|
})
|
|
14
19
|
})
|
|
@@ -20,12 +25,158 @@ describe('queryOptions', () => {
|
|
|
20
25
|
queryFn: () => Promise.resolve(5),
|
|
21
26
|
staleTime: 1000,
|
|
22
27
|
select: (data) => {
|
|
23
|
-
// @ts-expect-error data is in fact inferred as unknown
|
|
24
|
-
// see https://github.com/microsoft/TypeScript/issues/47599
|
|
25
28
|
const result: Expect<Equal<number, typeof data>> = true
|
|
26
29
|
return result
|
|
27
30
|
},
|
|
28
31
|
})
|
|
29
32
|
})
|
|
30
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 } = useQuery(options)
|
|
42
|
+
|
|
43
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
44
|
+
return result
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
it('should work when passed to useSuspenseQuery', () => {
|
|
48
|
+
doNotExecute(() => {
|
|
49
|
+
const options = queryOptions({
|
|
50
|
+
queryKey: ['key'],
|
|
51
|
+
queryFn: () => Promise.resolve(5),
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const { data } = useSuspenseQuery(options)
|
|
55
|
+
|
|
56
|
+
const result: Expect<Equal<typeof data, number>> = true
|
|
57
|
+
return result
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
it('should work when passed to fetchQuery', () => {
|
|
61
|
+
doNotExecute(async () => {
|
|
62
|
+
const options = queryOptions({
|
|
63
|
+
queryKey: ['key'],
|
|
64
|
+
queryFn: () => Promise.resolve(5),
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const data = await new QueryClient().fetchQuery(options)
|
|
68
|
+
|
|
69
|
+
const result: Expect<Equal<typeof data, number>> = true
|
|
70
|
+
return result
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
it('should work when passed to useQueries', () => {
|
|
74
|
+
doNotExecute(() => {
|
|
75
|
+
const options = queryOptions({
|
|
76
|
+
queryKey: ['key'],
|
|
77
|
+
queryFn: () => Promise.resolve(5),
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const [{ data }] = useQueries({
|
|
81
|
+
queries: [options],
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
85
|
+
return result
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
it('should tag the queryKey with the result type of the QueryFn', () => {
|
|
89
|
+
doNotExecute(() => {
|
|
90
|
+
const { queryKey } = queryOptions({
|
|
91
|
+
queryKey: ['key'],
|
|
92
|
+
queryFn: () => Promise.resolve(5),
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const result: Expect<
|
|
96
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], number>
|
|
97
|
+
> = true
|
|
98
|
+
return result
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should tag the queryKey even if no promise is returned', () => {
|
|
102
|
+
doNotExecute(() => {
|
|
103
|
+
const { queryKey } = queryOptions({
|
|
104
|
+
queryKey: ['key'],
|
|
105
|
+
queryFn: () => 5,
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
const result: Expect<
|
|
109
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], number>
|
|
110
|
+
> = true
|
|
111
|
+
return result
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('should tag the queryKey with unknown if there is no queryFn', () => {
|
|
116
|
+
doNotExecute(() => {
|
|
117
|
+
const { queryKey } = queryOptions({
|
|
118
|
+
queryKey: ['key'],
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const result: Expect<
|
|
122
|
+
Equal<(typeof queryKey)[typeof dataTagSymbol], unknown>
|
|
123
|
+
> = true
|
|
124
|
+
return result
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('should return the proper type when passed to getQueryData', () => {
|
|
129
|
+
doNotExecute(() => {
|
|
130
|
+
const { queryKey } = queryOptions({
|
|
131
|
+
queryKey: ['key'],
|
|
132
|
+
queryFn: () => Promise.resolve(5),
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
const queryClient = new QueryClient()
|
|
136
|
+
const data = queryClient.getQueryData(queryKey)
|
|
137
|
+
|
|
138
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
139
|
+
return result
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('should properly type updaterFn when passed to setQueryData', () => {
|
|
144
|
+
doNotExecute(() => {
|
|
145
|
+
const { queryKey } = queryOptions({
|
|
146
|
+
queryKey: ['key'],
|
|
147
|
+
queryFn: () => Promise.resolve(5),
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const queryClient = new QueryClient()
|
|
151
|
+
const data = queryClient.setQueryData(queryKey, (prev) => {
|
|
152
|
+
const result: Expect<Equal<typeof prev, number | undefined>> = true
|
|
153
|
+
return result ? prev : 1
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
157
|
+
return result
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('should properly type value when passed to setQueryData', () => {
|
|
162
|
+
doNotExecute(() => {
|
|
163
|
+
const { queryKey } = queryOptions({
|
|
164
|
+
queryKey: ['key'],
|
|
165
|
+
queryFn: () => Promise.resolve(5),
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
const queryClient = new QueryClient()
|
|
169
|
+
|
|
170
|
+
// @ts-expect-error value should be a number
|
|
171
|
+
queryClient.setQueryData(queryKey, '5')
|
|
172
|
+
// @ts-expect-error value should be a number
|
|
173
|
+
queryClient.setQueryData(queryKey, () => '5')
|
|
174
|
+
|
|
175
|
+
const data = queryClient.setQueryData(queryKey, 5)
|
|
176
|
+
|
|
177
|
+
const result: Expect<Equal<typeof data, number | undefined>> = true
|
|
178
|
+
return result
|
|
179
|
+
})
|
|
180
|
+
})
|
|
181
|
+
})
|
|
31
182
|
})
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { useSuspenseQuery } from '../useSuspenseQuery'
|
|
2
|
+
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
|
|
3
|
+
import { doNotExecute } from './utils'
|
|
4
|
+
import type { InfiniteData } from '@tanstack/query-core'
|
|
5
|
+
import type { Equal, Expect } from './utils'
|
|
6
|
+
|
|
7
|
+
describe('useSuspenseQuery', () => {
|
|
8
|
+
it('should always have data defined', () => {
|
|
9
|
+
doNotExecute(() => {
|
|
10
|
+
const { data } = useSuspenseQuery({
|
|
11
|
+
queryKey: ['key'],
|
|
12
|
+
queryFn: () => Promise.resolve(5),
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const result: Expect<Equal<typeof data, number>> = true
|
|
16
|
+
return result
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should not have pending status', () => {
|
|
21
|
+
doNotExecute(() => {
|
|
22
|
+
const { status } = useSuspenseQuery({
|
|
23
|
+
queryKey: ['key'],
|
|
24
|
+
queryFn: () => Promise.resolve(5),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const result: Expect<Equal<typeof status, 'error' | 'success'>> = true
|
|
28
|
+
return result
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should not allow placeholderData, enabled or throwOnError props', () => {
|
|
33
|
+
doNotExecute(() => {
|
|
34
|
+
useSuspenseQuery({
|
|
35
|
+
queryKey: ['key'],
|
|
36
|
+
queryFn: () => Promise.resolve(5),
|
|
37
|
+
// @ts-expect-error TS2345
|
|
38
|
+
placeholderData: 5,
|
|
39
|
+
enabled: true,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
useSuspenseQuery({
|
|
43
|
+
queryKey: ['key'],
|
|
44
|
+
queryFn: () => Promise.resolve(5),
|
|
45
|
+
// @ts-expect-error TS2345
|
|
46
|
+
enabled: true,
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
useSuspenseQuery({
|
|
50
|
+
queryKey: ['key'],
|
|
51
|
+
queryFn: () => Promise.resolve(5),
|
|
52
|
+
// @ts-expect-error TS2345
|
|
53
|
+
throwOnError: true,
|
|
54
|
+
})
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('should not return isPlaceholderData', () => {
|
|
59
|
+
doNotExecute(() => {
|
|
60
|
+
const query = useSuspenseQuery({
|
|
61
|
+
queryKey: ['key'],
|
|
62
|
+
queryFn: () => Promise.resolve(5),
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// @ts-expect-error TS2339
|
|
66
|
+
void query.isPlaceholderData
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
describe('useSuspenseInfiniteQuery', () => {
|
|
72
|
+
it('should always have data defined', () => {
|
|
73
|
+
doNotExecute(() => {
|
|
74
|
+
const { data } = useSuspenseInfiniteQuery({
|
|
75
|
+
queryKey: ['key'],
|
|
76
|
+
queryFn: () => Promise.resolve(5),
|
|
77
|
+
initialPageParam: 1,
|
|
78
|
+
getNextPageParam: () => 1,
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const result: Expect<Equal<typeof data, InfiniteData<number, unknown>>> =
|
|
82
|
+
true
|
|
83
|
+
return result
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it('should not have pending status', () => {
|
|
88
|
+
doNotExecute(() => {
|
|
89
|
+
const { status } = useSuspenseInfiniteQuery({
|
|
90
|
+
queryKey: ['key'],
|
|
91
|
+
queryFn: () => Promise.resolve(5),
|
|
92
|
+
initialPageParam: 1,
|
|
93
|
+
getNextPageParam: () => 1,
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
const result: Expect<Equal<typeof status, 'error' | 'success'>> = true
|
|
97
|
+
return result
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should not allow placeholderData, enabled or throwOnError props', () => {
|
|
102
|
+
doNotExecute(() => {
|
|
103
|
+
useSuspenseInfiniteQuery({
|
|
104
|
+
queryKey: ['key'],
|
|
105
|
+
queryFn: () => Promise.resolve(5),
|
|
106
|
+
initialPageParam: 1,
|
|
107
|
+
getNextPageParam: () => 1,
|
|
108
|
+
// @ts-expect-error TS2345
|
|
109
|
+
placeholderData: 5,
|
|
110
|
+
enabled: true,
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
useSuspenseInfiniteQuery({
|
|
114
|
+
queryKey: ['key'],
|
|
115
|
+
queryFn: () => Promise.resolve(5),
|
|
116
|
+
initialPageParam: 1,
|
|
117
|
+
getNextPageParam: () => 1,
|
|
118
|
+
// @ts-expect-error TS2345
|
|
119
|
+
enabled: true,
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
useSuspenseInfiniteQuery({
|
|
123
|
+
queryKey: ['key'],
|
|
124
|
+
queryFn: () => Promise.resolve(5),
|
|
125
|
+
initialPageParam: 1,
|
|
126
|
+
getNextPageParam: () => 1,
|
|
127
|
+
// @ts-expect-error TS2345
|
|
128
|
+
throwOnError: true,
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('should not return isPlaceholderData', () => {
|
|
134
|
+
doNotExecute(() => {
|
|
135
|
+
const query = useSuspenseInfiniteQuery({
|
|
136
|
+
queryKey: ['key'],
|
|
137
|
+
queryFn: () => Promise.resolve(5),
|
|
138
|
+
initialPageParam: 1,
|
|
139
|
+
getNextPageParam: () => 1,
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
// @ts-expect-error TS2339
|
|
143
|
+
void query.isPlaceholderData
|
|
144
|
+
})
|
|
145
|
+
})
|
|
146
|
+
})
|
|
@@ -4146,7 +4146,7 @@ describe('useQuery', () => {
|
|
|
4146
4146
|
await sleep(10)
|
|
4147
4147
|
return count++
|
|
4148
4148
|
},
|
|
4149
|
-
refetchInterval: (data = 0) => (data < 2 ? 10 : false),
|
|
4149
|
+
refetchInterval: ({ state: { data = 0 } }) => (data < 2 ? 10 : false),
|
|
4150
4150
|
})
|
|
4151
4151
|
|
|
4152
4152
|
states.push(queryInfo)
|
|
@@ -5975,11 +5975,11 @@ describe('useQuery', () => {
|
|
|
5975
5975
|
<div>data: {state.data}</div>
|
|
5976
5976
|
<div>dataUpdatedAt: {state.dataUpdatedAt}</div>
|
|
5977
5977
|
<button
|
|
5978
|
-
onClick={() =>
|
|
5978
|
+
onClick={() => {
|
|
5979
5979
|
queryClient.setQueryData(key, 'newData', {
|
|
5980
5980
|
updatedAt: 100,
|
|
5981
5981
|
})
|
|
5982
|
-
}
|
|
5982
|
+
}}
|
|
5983
5983
|
>
|
|
5984
5984
|
setQueryData
|
|
5985
5985
|
</button>
|
|
@@ -51,10 +51,16 @@ describe('initialData', () => {
|
|
|
51
51
|
queryKey: ['key'],
|
|
52
52
|
queryFn: () => Promise.resolve(1),
|
|
53
53
|
})
|
|
54
|
-
|
|
54
|
+
|
|
55
|
+
const query = useQuery({
|
|
55
56
|
...options,
|
|
56
57
|
select: (data) => data > 1,
|
|
57
58
|
})
|
|
59
|
+
|
|
60
|
+
const result: Expect<
|
|
61
|
+
Equal<boolean | undefined, (typeof query)['data']>
|
|
62
|
+
> = true
|
|
63
|
+
return result
|
|
58
64
|
})
|
|
59
65
|
})
|
|
60
66
|
|
|
@@ -1,50 +1,50 @@
|
|
|
1
|
+
import type { DataTag } from '@tanstack/query-core'
|
|
1
2
|
import type { InfiniteData } from '@tanstack/query-core'
|
|
2
3
|
import type { UseInfiniteQueryOptions } from './types'
|
|
3
4
|
import type { DefaultError, QueryKey } from '@tanstack/query-core'
|
|
4
5
|
|
|
5
6
|
export type UndefinedInitialDataInfiniteOptions<
|
|
6
|
-
TQueryFnData
|
|
7
|
+
TQueryFnData,
|
|
7
8
|
TError = DefaultError,
|
|
8
|
-
TData = TQueryFnData
|
|
9
|
-
TQueryData = TQueryFnData,
|
|
9
|
+
TData = InfiniteData<TQueryFnData>,
|
|
10
10
|
TQueryKey extends QueryKey = QueryKey,
|
|
11
11
|
TPageParam = unknown,
|
|
12
12
|
> = UseInfiniteQueryOptions<
|
|
13
13
|
TQueryFnData,
|
|
14
14
|
TError,
|
|
15
15
|
TData,
|
|
16
|
-
|
|
16
|
+
TQueryFnData,
|
|
17
17
|
TQueryKey,
|
|
18
18
|
TPageParam
|
|
19
19
|
> & {
|
|
20
20
|
initialData?: undefined
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
type NonUndefinedGuard<T> = T extends undefined ? never : T
|
|
24
|
+
|
|
23
25
|
export type DefinedInitialDataInfiniteOptions<
|
|
24
|
-
TQueryFnData
|
|
26
|
+
TQueryFnData,
|
|
25
27
|
TError = DefaultError,
|
|
26
|
-
TData = TQueryFnData
|
|
27
|
-
TQueryData = TQueryFnData,
|
|
28
|
+
TData = InfiniteData<TQueryFnData>,
|
|
28
29
|
TQueryKey extends QueryKey = QueryKey,
|
|
29
30
|
TPageParam = unknown,
|
|
30
31
|
> = UseInfiniteQueryOptions<
|
|
31
32
|
TQueryFnData,
|
|
32
33
|
TError,
|
|
33
34
|
TData,
|
|
34
|
-
|
|
35
|
+
TQueryFnData,
|
|
35
36
|
TQueryKey,
|
|
36
37
|
TPageParam
|
|
37
38
|
> & {
|
|
38
39
|
initialData:
|
|
39
|
-
| InfiniteData<
|
|
40
|
-
| (() => InfiniteData<
|
|
40
|
+
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
|
|
41
|
+
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
export function infiniteQueryOptions<
|
|
44
|
-
TQueryFnData
|
|
45
|
+
TQueryFnData,
|
|
45
46
|
TError = DefaultError,
|
|
46
|
-
TData = TQueryFnData
|
|
47
|
-
TQueryData = TQueryFnData,
|
|
47
|
+
TData = InfiniteData<TQueryFnData>,
|
|
48
48
|
TQueryKey extends QueryKey = QueryKey,
|
|
49
49
|
TPageParam = unknown,
|
|
50
50
|
>(
|
|
@@ -52,7 +52,6 @@ export function infiniteQueryOptions<
|
|
|
52
52
|
TQueryFnData,
|
|
53
53
|
TError,
|
|
54
54
|
TData,
|
|
55
|
-
TQueryFnData,
|
|
56
55
|
TQueryKey,
|
|
57
56
|
TPageParam
|
|
58
57
|
>,
|
|
@@ -60,16 +59,16 @@ export function infiniteQueryOptions<
|
|
|
60
59
|
TQueryFnData,
|
|
61
60
|
TError,
|
|
62
61
|
TData,
|
|
63
|
-
TQueryFnData,
|
|
64
62
|
TQueryKey,
|
|
65
63
|
TPageParam
|
|
66
|
-
>
|
|
64
|
+
> & {
|
|
65
|
+
queryKey: DataTag<TQueryKey, TData>
|
|
66
|
+
}
|
|
67
67
|
|
|
68
68
|
export function infiniteQueryOptions<
|
|
69
|
-
TQueryFnData
|
|
69
|
+
TQueryFnData,
|
|
70
70
|
TError = DefaultError,
|
|
71
|
-
TData = TQueryFnData
|
|
72
|
-
TQueryData = TQueryFnData,
|
|
71
|
+
TData = InfiniteData<TQueryFnData>,
|
|
73
72
|
TQueryKey extends QueryKey = QueryKey,
|
|
74
73
|
TPageParam = unknown,
|
|
75
74
|
>(
|
|
@@ -77,7 +76,6 @@ export function infiniteQueryOptions<
|
|
|
77
76
|
TQueryFnData,
|
|
78
77
|
TError,
|
|
79
78
|
TData,
|
|
80
|
-
TQueryFnData,
|
|
81
79
|
TQueryKey,
|
|
82
80
|
TPageParam
|
|
83
81
|
>,
|
|
@@ -85,10 +83,11 @@ export function infiniteQueryOptions<
|
|
|
85
83
|
TQueryFnData,
|
|
86
84
|
TError,
|
|
87
85
|
TData,
|
|
88
|
-
TQueryFnData,
|
|
89
86
|
TQueryKey,
|
|
90
87
|
TPageParam
|
|
91
|
-
>
|
|
88
|
+
> & {
|
|
89
|
+
queryKey: DataTag<TQueryKey, TData>
|
|
90
|
+
}
|
|
92
91
|
|
|
93
92
|
export function infiniteQueryOptions(options: unknown) {
|
|
94
93
|
return options
|
package/src/queryOptions.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DefaultError, QueryKey } from '@tanstack/query-core'
|
|
1
|
+
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
|
|
2
2
|
import type { UseQueryOptions } from './types'
|
|
3
3
|
|
|
4
4
|
export type UndefinedInitialDataOptions<
|
|
@@ -23,35 +23,27 @@ export type DefinedInitialDataOptions<
|
|
|
23
23
|
| (() => NonUndefinedGuard<TQueryFnData>)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
type ValidateQueryOptions<T> = {
|
|
27
|
-
[K in keyof T]: K extends keyof UseQueryOptions ? T[K] : never
|
|
28
|
-
}
|
|
29
|
-
|
|
30
26
|
export function queryOptions<
|
|
31
27
|
TQueryFnData = unknown,
|
|
32
28
|
TError = DefaultError,
|
|
33
29
|
TData = TQueryFnData,
|
|
34
30
|
TQueryKey extends QueryKey = QueryKey,
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
> = UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
41
|
-
>(options: ValidateQueryOptions<TOptions>): TOptions
|
|
31
|
+
>(
|
|
32
|
+
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
33
|
+
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
34
|
+
queryKey: DataTag<TQueryKey, TData>
|
|
35
|
+
}
|
|
42
36
|
|
|
43
37
|
export function queryOptions<
|
|
44
38
|
TQueryFnData = unknown,
|
|
45
39
|
TError = DefaultError,
|
|
46
40
|
TData = TQueryFnData,
|
|
47
41
|
TQueryKey extends QueryKey = QueryKey,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
> = DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
54
|
-
>(options: ValidateQueryOptions<TOptions>): TOptions
|
|
42
|
+
>(
|
|
43
|
+
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
|
|
44
|
+
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & {
|
|
45
|
+
queryKey: DataTag<TQueryKey, TData>
|
|
46
|
+
}
|
|
55
47
|
|
|
56
48
|
export function queryOptions(options: unknown) {
|
|
57
49
|
return options
|