@tanstack/query-core 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.
Files changed (99) hide show
  1. package/build/legacy/hydration.cjs +13 -11
  2. package/build/legacy/hydration.cjs.map +1 -1
  3. package/build/legacy/hydration.d.cts +1 -1
  4. package/build/legacy/hydration.d.ts +1 -1
  5. package/build/legacy/hydration.js +13 -11
  6. package/build/legacy/hydration.js.map +1 -1
  7. package/build/legacy/index.d.cts +1 -1
  8. package/build/legacy/index.d.ts +1 -1
  9. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  10. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  11. package/build/legacy/infiniteQueryObserver.d.cts +1 -1
  12. package/build/legacy/infiniteQueryObserver.d.ts +1 -1
  13. package/build/legacy/mutation.d.cts +1 -1
  14. package/build/legacy/mutation.d.ts +1 -1
  15. package/build/legacy/mutationCache.d.cts +1 -1
  16. package/build/legacy/mutationCache.d.ts +1 -1
  17. package/build/legacy/mutationObserver.d.cts +1 -1
  18. package/build/legacy/mutationObserver.d.ts +1 -1
  19. package/build/legacy/queriesObserver.d.cts +1 -1
  20. package/build/legacy/queriesObserver.d.ts +1 -1
  21. package/build/legacy/query.d.cts +1 -1
  22. package/build/legacy/query.d.ts +1 -1
  23. package/build/legacy/queryCache.d.cts +1 -1
  24. package/build/legacy/queryCache.d.ts +1 -1
  25. package/build/legacy/{queryClient-230e991e.d.ts → queryClient-5b892aba.d.ts} +10 -8
  26. package/build/legacy/{queryClient-64e47985.d.ts → queryClient-c9f8057a.d.ts} +10 -8
  27. package/build/legacy/queryClient.cjs.map +1 -1
  28. package/build/legacy/queryClient.d.cts +1 -1
  29. package/build/legacy/queryClient.d.ts +1 -1
  30. package/build/legacy/queryClient.js.map +1 -1
  31. package/build/legacy/queryObserver.cjs +1 -4
  32. package/build/legacy/queryObserver.cjs.map +1 -1
  33. package/build/legacy/queryObserver.d.cts +1 -1
  34. package/build/legacy/queryObserver.d.ts +1 -1
  35. package/build/legacy/queryObserver.js +1 -4
  36. package/build/legacy/queryObserver.js.map +1 -1
  37. package/build/legacy/retryer.d.cts +1 -1
  38. package/build/legacy/retryer.d.ts +1 -1
  39. package/build/legacy/types.cjs.map +1 -1
  40. package/build/legacy/types.d.cts +1 -1
  41. package/build/legacy/types.d.ts +1 -1
  42. package/build/legacy/utils.cjs.map +1 -1
  43. package/build/legacy/utils.d.cts +1 -1
  44. package/build/legacy/utils.d.ts +1 -1
  45. package/build/legacy/utils.js.map +1 -1
  46. package/build/modern/hydration.cjs +13 -11
  47. package/build/modern/hydration.cjs.map +1 -1
  48. package/build/modern/hydration.d.cts +1 -1
  49. package/build/modern/hydration.d.ts +1 -1
  50. package/build/modern/hydration.js +13 -11
  51. package/build/modern/hydration.js.map +1 -1
  52. package/build/modern/index.d.cts +1 -1
  53. package/build/modern/index.d.ts +1 -1
  54. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  55. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  56. package/build/modern/infiniteQueryObserver.d.cts +1 -1
  57. package/build/modern/infiniteQueryObserver.d.ts +1 -1
  58. package/build/modern/mutation.d.cts +1 -1
  59. package/build/modern/mutation.d.ts +1 -1
  60. package/build/modern/mutationCache.d.cts +1 -1
  61. package/build/modern/mutationCache.d.ts +1 -1
  62. package/build/modern/mutationObserver.d.cts +1 -1
  63. package/build/modern/mutationObserver.d.ts +1 -1
  64. package/build/modern/queriesObserver.d.cts +1 -1
  65. package/build/modern/queriesObserver.d.ts +1 -1
  66. package/build/modern/query.d.cts +1 -1
  67. package/build/modern/query.d.ts +1 -1
  68. package/build/modern/queryCache.d.cts +1 -1
  69. package/build/modern/queryCache.d.ts +1 -1
  70. package/build/modern/{queryClient-230e991e.d.ts → queryClient-5b892aba.d.ts} +10 -8
  71. package/build/modern/{queryClient-64e47985.d.ts → queryClient-c9f8057a.d.ts} +10 -8
  72. package/build/modern/queryClient.cjs.map +1 -1
  73. package/build/modern/queryClient.d.cts +1 -1
  74. package/build/modern/queryClient.d.ts +1 -1
  75. package/build/modern/queryClient.js.map +1 -1
  76. package/build/modern/queryObserver.cjs +1 -4
  77. package/build/modern/queryObserver.cjs.map +1 -1
  78. package/build/modern/queryObserver.d.cts +1 -1
  79. package/build/modern/queryObserver.d.ts +1 -1
  80. package/build/modern/queryObserver.js +1 -4
  81. package/build/modern/queryObserver.js.map +1 -1
  82. package/build/modern/retryer.d.cts +1 -1
  83. package/build/modern/retryer.d.ts +1 -1
  84. package/build/modern/types.cjs.map +1 -1
  85. package/build/modern/types.d.cts +1 -1
  86. package/build/modern/types.d.ts +1 -1
  87. package/build/modern/utils.cjs.map +1 -1
  88. package/build/modern/utils.d.cts +1 -1
  89. package/build/modern/utils.d.ts +1 -1
  90. package/build/modern/utils.js.map +1 -1
  91. package/package.json +1 -1
  92. package/src/hydration.ts +15 -14
  93. package/src/queryClient.ts +30 -9
  94. package/src/queryObserver.ts +1 -4
  95. package/src/tests/hydration.test.tsx +37 -1
  96. package/src/tests/queryClient.types.test.tsx +173 -0
  97. package/src/tests/utils.ts +10 -0
  98. package/src/types.ts +11 -6
  99. package/src/utils.ts +2 -6
@@ -514,7 +514,7 @@ describe('dehydration and rehydration', () => {
514
514
  queryClient.clear()
515
515
  })
516
516
 
517
- test('should set the fetchStatus to idle in all cases when dehydrating', async () => {
517
+ test('should set the fetchStatus to idle when creating a query with dehydrate', async () => {
518
518
  const queryCache = new QueryCache()
519
519
  const queryClient = createQueryClient({ queryCache })
520
520
 
@@ -668,4 +668,40 @@ describe('dehydration and rehydration', () => {
668
668
  undefined,
669
669
  )
670
670
  })
671
+
672
+ test('should not change fetchStatus when updating a query with dehydrate', async () => {
673
+ const queryClient = createQueryClient()
674
+
675
+ const options = {
676
+ queryKey: ['string'],
677
+ queryFn: async () => {
678
+ await sleep(10)
679
+ return 'string'
680
+ },
681
+ } as const
682
+
683
+ await queryClient.prefetchQuery(options)
684
+
685
+ const dehydrated = dehydrate(queryClient)
686
+ expect(
687
+ dehydrated.queries.find((q) => q.queryHash === '["string"]')?.state
688
+ .fetchStatus,
689
+ ).toBe('idle')
690
+ const stringified = JSON.stringify(dehydrated)
691
+
692
+ // ---
693
+ const parsed = JSON.parse(stringified)
694
+ const hydrationCache = new QueryCache()
695
+ const hydrationClient = createQueryClient({ queryCache: hydrationCache })
696
+
697
+ const promise = hydrationClient.prefetchQuery(options)
698
+ hydrate(hydrationClient, parsed)
699
+ expect(
700
+ hydrationCache.find({ queryKey: ['string'] })?.state.fetchStatus,
701
+ ).toBe('fetching')
702
+ await promise
703
+ expect(
704
+ hydrationCache.find({ queryKey: ['string'] })?.state.fetchStatus,
705
+ ).toBe('idle')
706
+ })
671
707
  })
@@ -0,0 +1,173 @@
1
+ import { QueryClient } from '../queryClient'
2
+ import { doNotExecute } from './utils'
3
+ import type { Equal, Expect } from './utils'
4
+ import type { DataTag, InfiniteData } from '../types'
5
+
6
+ describe('getQueryData', () => {
7
+ it('should be typed if key is tagged', () => {
8
+ doNotExecute(() => {
9
+ const queryKey = ['key'] as DataTag<Array<string>, number>
10
+ const queryClient = new QueryClient()
11
+ const data = queryClient.getQueryData(queryKey)
12
+
13
+ const result: Expect<Equal<typeof data, number | undefined>> = true
14
+ return result
15
+ })
16
+ })
17
+
18
+ it('should infer unknown if key is not tagged', () => {
19
+ doNotExecute(() => {
20
+ const queryKey = ['key'] as const
21
+ const queryClient = new QueryClient()
22
+ const data = queryClient.getQueryData(queryKey)
23
+
24
+ const result: Expect<Equal<typeof data, unknown>> = true
25
+ return result
26
+ })
27
+ })
28
+
29
+ it('should infer passed generic if passed', () => {
30
+ doNotExecute(() => {
31
+ const queryKey = ['key'] as const
32
+ const queryClient = new QueryClient()
33
+ const data = queryClient.getQueryData<number>(queryKey)
34
+
35
+ const result: Expect<Equal<typeof data, number | undefined>> = true
36
+ return result
37
+ })
38
+ })
39
+
40
+ it('should only allow Arrays to be passed', () => {
41
+ doNotExecute(() => {
42
+ const queryKey = 'key' as const
43
+ const queryClient = new QueryClient()
44
+ // @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey'
45
+ return queryClient.getQueryData(queryKey)
46
+ })
47
+ })
48
+ })
49
+
50
+ describe('setQueryData', () => {
51
+ it('updater should be typed if key is tagged', () => {
52
+ doNotExecute(() => {
53
+ const queryKey = ['key'] as DataTag<Array<string>, number>
54
+ const queryClient = new QueryClient()
55
+ const data = queryClient.setQueryData(queryKey, (prev) => {
56
+ const result: Expect<Equal<typeof prev, number | undefined>> = true
57
+ return result ? prev : 1
58
+ })
59
+
60
+ const result: Expect<Equal<typeof data, number | undefined>> = true
61
+ return result
62
+ })
63
+ })
64
+
65
+ it('value should be typed if key is tagged', () => {
66
+ doNotExecute(() => {
67
+ const queryKey = ['key'] as DataTag<Array<string>, number>
68
+ const queryClient = new QueryClient()
69
+
70
+ // @ts-expect-error value should be a number
71
+ queryClient.setQueryData(queryKey, '1')
72
+
73
+ // @ts-expect-error value should be a number
74
+ queryClient.setQueryData(queryKey, () => '1')
75
+
76
+ const data = queryClient.setQueryData(queryKey, 1)
77
+
78
+ const result: Expect<Equal<typeof data, number | undefined>> = true
79
+ return result
80
+ })
81
+ })
82
+
83
+ it('should infer unknown for updater if key is not tagged', () => {
84
+ doNotExecute(() => {
85
+ const queryKey = ['key'] as const
86
+ const queryClient = new QueryClient()
87
+ const data = queryClient.setQueryData(queryKey, (prev) => {
88
+ const result: Expect<Equal<typeof prev, unknown>> = true
89
+ return result ? prev : 1
90
+ })
91
+
92
+ const result: Expect<Equal<typeof data, unknown>> = true
93
+ return result
94
+ })
95
+ })
96
+
97
+ it('should infer unknown for value if key is not tagged', () => {
98
+ doNotExecute(() => {
99
+ const queryKey = ['key'] as const
100
+ const queryClient = new QueryClient()
101
+ const data = queryClient.setQueryData(queryKey, 'foo')
102
+
103
+ const result: Expect<Equal<typeof data, unknown>> = true
104
+ return result
105
+ })
106
+ })
107
+
108
+ it('should infer passed generic if passed', () => {
109
+ doNotExecute(() => {
110
+ const queryKey = ['key'] as const
111
+ const queryClient = new QueryClient()
112
+ const data = queryClient.setQueryData<string>(queryKey, (prev) => {
113
+ const result: Expect<Equal<typeof prev, string | undefined>> = true
114
+ return result ? prev : '1'
115
+ })
116
+
117
+ const result: Expect<Equal<typeof data, string | undefined>> = true
118
+ return result
119
+ })
120
+ })
121
+
122
+ it('should infer passed generic for value', () => {
123
+ doNotExecute(() => {
124
+ const queryKey = ['key'] as const
125
+ const queryClient = new QueryClient()
126
+ const data = queryClient.setQueryData<string>(queryKey, 'foo')
127
+
128
+ const result: Expect<Equal<typeof data, string | undefined>> = true
129
+ return result
130
+ })
131
+ })
132
+ })
133
+
134
+ describe('fetchInfiniteQuery', () => {
135
+ it('should allow passing pages', () => {
136
+ doNotExecute(async () => {
137
+ const data = await new QueryClient().fetchInfiniteQuery({
138
+ queryKey: ['key'],
139
+ queryFn: () => Promise.resolve('string'),
140
+ getNextPageParam: () => 1,
141
+ initialPageParam: 1,
142
+ pages: 5,
143
+ })
144
+
145
+ const result: Expect<Equal<typeof data, InfiniteData<string, number>>> =
146
+ true
147
+ return result
148
+ })
149
+ })
150
+
151
+ it('should not allow passing getNextPageParam without pages', () => {
152
+ doNotExecute(async () => {
153
+ return new QueryClient().fetchInfiniteQuery({
154
+ queryKey: ['key'],
155
+ queryFn: () => Promise.resolve('string'),
156
+ initialPageParam: 1,
157
+ getNextPageParam: () => 1,
158
+ })
159
+ })
160
+ })
161
+
162
+ it('should not allow passing pages without getNextPageParam', () => {
163
+ doNotExecute(async () => {
164
+ // @ts-expect-error Property 'getNextPageParam' is missing
165
+ return new QueryClient().fetchInfiniteQuery({
166
+ queryKey: ['key'],
167
+ queryFn: () => Promise.resolve('string'),
168
+ initialPageParam: 1,
169
+ pages: 5,
170
+ })
171
+ })
172
+ })
173
+ })
@@ -66,3 +66,13 @@ export function setIsServer(isServer: boolean) {
66
66
  })
67
67
  }
68
68
  }
69
+
70
+ export const doNotExecute = (_func: () => void) => true
71
+
72
+ export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
73
+ T,
74
+ >() => T extends Y ? 1 : 2
75
+ ? true
76
+ : false
77
+
78
+ export type Expect<T extends true> = T
package/src/types.ts CHANGED
@@ -7,7 +7,7 @@ import type { QueryFilters, QueryTypeFilter } from './utils'
7
7
  import type { QueryCache } from './queryCache'
8
8
  import type { MutationCache } from './mutationCache'
9
9
 
10
- type NoInfer<T> = [T][T extends any ? 0 : never]
10
+ export type NoInfer<T> = [T][T extends any ? 0 : never]
11
11
 
12
12
  export interface Register {
13
13
  // defaultError: Error
@@ -23,6 +23,11 @@ export type DefaultError = Register extends {
23
23
 
24
24
  export type QueryKey = ReadonlyArray<unknown>
25
25
 
26
+ export declare const dataTagSymbol: unique symbol
27
+ export type DataTag<Type, Value> = Type & {
28
+ [dataTagSymbol]: Value
29
+ }
30
+
26
31
  export type QueryFunction<
27
32
  T = unknown,
28
33
  TQueryKey extends QueryKey = QueryKey,
@@ -76,9 +81,10 @@ export type PlaceholderDataFunction<
76
81
  previousQuery: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined,
77
82
  ) => TQueryData | undefined
78
83
 
79
- export type QueriesPlaceholderDataFunction<TQueryData> = () =>
80
- | TQueryData
81
- | undefined
84
+ export type QueriesPlaceholderDataFunction<TQueryData> = (
85
+ previousData: undefined,
86
+ previousQuery: undefined,
87
+ ) => TQueryData | undefined
82
88
 
83
89
  export type QueryKeyHashFunction<TQueryKey extends QueryKey> = (
84
90
  queryKey: TQueryKey,
@@ -231,7 +237,6 @@ export interface QueryObserverOptions<
231
237
  | number
232
238
  | false
233
239
  | ((
234
- data: TData | undefined,
235
240
  query: Query<TQueryFnData, TError, TQueryData, TQueryKey>,
236
241
  ) => number | false | undefined)
237
242
  /**
@@ -386,7 +391,7 @@ export interface FetchQueryOptions<
386
391
  }
387
392
 
388
393
  type FetchInfiniteQueryPages<TQueryFnData = unknown, TPageParam = unknown> =
389
- | { pages?: never; getNextPageParam?: never }
394
+ | { pages?: never }
390
395
  | {
391
396
  pages: number
392
397
  getNextPageParam: GetNextPageParamFunction<TPageParam, TQueryFnData>
package/src/utils.ts CHANGED
@@ -56,11 +56,7 @@ export interface MutationFilters {
56
56
  status?: MutationStatus
57
57
  }
58
58
 
59
- export type DataUpdateFunction<TInput, TOutput> = (input: TInput) => TOutput
60
-
61
- export type Updater<TInput, TOutput> =
62
- | TOutput
63
- | DataUpdateFunction<TInput, TOutput>
59
+ export type Updater<TInput, TOutput> = TOutput | ((input: TInput) => TOutput)
64
60
 
65
61
  export type QueryTypeFilter = 'all' | 'active' | 'inactive'
66
62
 
@@ -77,7 +73,7 @@ export function functionalUpdate<TInput, TOutput>(
77
73
  input: TInput,
78
74
  ): TOutput {
79
75
  return typeof updater === 'function'
80
- ? (updater as DataUpdateFunction<TInput, TOutput>)(input)
76
+ ? (updater as (_: TInput) => TOutput)(input)
81
77
  : updater
82
78
  }
83
79