@tanstack/vue-query 5.40.0 → 5.41.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.
Files changed (52) hide show
  1. package/build/legacy/index.cjs +3 -0
  2. package/build/legacy/index.cjs.map +1 -1
  3. package/build/legacy/index.d.cts +1 -0
  4. package/build/legacy/index.d.ts +1 -0
  5. package/build/legacy/index.js +2 -0
  6. package/build/legacy/index.js.map +1 -1
  7. package/build/legacy/infiniteQueryOptions.cjs +33 -0
  8. package/build/legacy/infiniteQueryOptions.cjs.map +1 -0
  9. package/build/legacy/infiniteQueryOptions.d.cts +21 -0
  10. package/build/legacy/infiniteQueryOptions.d.ts +21 -0
  11. package/build/legacy/infiniteQueryOptions.js +8 -0
  12. package/build/legacy/infiniteQueryOptions.js.map +1 -0
  13. package/build/legacy/useQueries.cjs.map +1 -1
  14. package/build/legacy/useQueries.d.cts +9 -14
  15. package/build/legacy/useQueries.d.ts +9 -14
  16. package/build/legacy/useQueries.js.map +1 -1
  17. package/build/modern/index.cjs +3 -0
  18. package/build/modern/index.cjs.map +1 -1
  19. package/build/modern/index.d.cts +1 -0
  20. package/build/modern/index.d.ts +1 -0
  21. package/build/modern/index.js +2 -0
  22. package/build/modern/index.js.map +1 -1
  23. package/build/modern/infiniteQueryOptions.cjs +33 -0
  24. package/build/modern/infiniteQueryOptions.cjs.map +1 -0
  25. package/build/modern/infiniteQueryOptions.d.cts +21 -0
  26. package/build/modern/infiniteQueryOptions.d.ts +21 -0
  27. package/build/modern/infiniteQueryOptions.js +8 -0
  28. package/build/modern/infiniteQueryOptions.js.map +1 -0
  29. package/build/modern/useQueries.cjs.map +1 -1
  30. package/build/modern/useQueries.d.cts +9 -14
  31. package/build/modern/useQueries.d.ts +9 -14
  32. package/build/modern/useQueries.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/__tests__/infiniteQueryOptions.test-d.ts +111 -0
  35. package/src/__tests__/queryClient.test-d.ts +132 -0
  36. package/src/__tests__/queryClient.test.ts +17 -0
  37. package/src/__tests__/queryOptions.test-d.ts +125 -0
  38. package/src/__tests__/test-utils.ts +0 -10
  39. package/src/__tests__/useInfiniteQuery.test-d.tsx +84 -0
  40. package/src/__tests__/useInfiniteQuery.test.ts +32 -0
  41. package/src/__tests__/useMutation.test-d.tsx +74 -0
  42. package/src/__tests__/useQueries.test-d.ts +145 -0
  43. package/src/__tests__/useQuery.test-d.ts +228 -0
  44. package/src/index.ts +5 -0
  45. package/src/infiniteQueryOptions.ts +94 -0
  46. package/src/useQueries.ts +18 -36
  47. package/src/__tests__/queryClient.type.test.ts +0 -174
  48. package/src/__tests__/queryOptions.types.test.ts +0 -170
  49. package/src/__tests__/useInfiniteQuery.types.test.tsx +0 -106
  50. package/src/__tests__/useMutation.types.test.tsx +0 -98
  51. package/src/__tests__/useQueries.types.test.ts +0 -176
  52. package/src/__tests__/useQuery.types.test.ts +0 -282
@@ -1,170 +0,0 @@
1
- import { describe, it } from 'vitest'
2
- import { reactive, ref } from 'vue-demi'
3
- import { QueryClient } from '../queryClient'
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
- it('should tag the queryKey with unknown if there is no queryFn', () => {
75
- doNotExecute(() => {
76
- const { queryKey } = queryOptions({
77
- queryKey: ['key'],
78
- })
79
-
80
- const result: Expect<
81
- Equal<(typeof queryKey)[typeof dataTagSymbol], unknown>
82
- > = true
83
- return result
84
- })
85
- })
86
- it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
87
- doNotExecute(() => {
88
- const { queryKey } = queryOptions({
89
- queryKey: ['key'],
90
- queryFn: () => Promise.resolve(5),
91
- select: (data) => data.toString(),
92
- })
93
-
94
- const result: Expect<
95
- Equal<(typeof queryKey)[typeof dataTagSymbol], number>
96
- > = true
97
- return result
98
- })
99
- })
100
- it('should return the proper type when passed to getQueryData', () => {
101
- doNotExecute(() => {
102
- const { queryKey } = queryOptions({
103
- queryKey: ['key'],
104
- queryFn: () => Promise.resolve(5),
105
- })
106
-
107
- const queryClient = new QueryClient()
108
- const data = queryClient.getQueryData(queryKey)
109
-
110
- const result: Expect<Equal<typeof data, number | undefined>> = true
111
- return result
112
- })
113
- })
114
- it('should properly type updaterFn when passed to setQueryData', () => {
115
- doNotExecute(() => {
116
- const { queryKey } = queryOptions({
117
- queryKey: ['key'],
118
- queryFn: () => Promise.resolve(5),
119
- })
120
-
121
- const queryClient = new QueryClient()
122
- const data = queryClient.setQueryData(queryKey, (prev) => {
123
- const result: Expect<Equal<typeof prev, number | undefined>> = true
124
- return result ? prev : 1
125
- })
126
-
127
- const result: Expect<Equal<typeof data, number | undefined>> = true
128
- return result
129
- })
130
- })
131
- it('should properly type value when passed to setQueryData', () => {
132
- doNotExecute(() => {
133
- const { queryKey } = queryOptions({
134
- queryKey: ['key'],
135
- queryFn: () => Promise.resolve(5),
136
- })
137
-
138
- const queryClient = new QueryClient()
139
-
140
- // @ts-expect-error value should be a number
141
- queryClient.setQueryData(queryKey, '5')
142
- // @ts-expect-error value should be a number
143
- queryClient.setQueryData(queryKey, () => '5')
144
-
145
- const data = queryClient.setQueryData(queryKey, 5)
146
-
147
- const result: Expect<Equal<typeof data, number | undefined>> = true
148
- return result
149
- })
150
- })
151
- it('should allow to be passed to QueryClient methods while containing ref in queryKey', () => {
152
- doNotExecute(() => {
153
- const options = queryOptions({
154
- queryKey: ['key', ref(1), { nested: ref(2) }],
155
- queryFn: () => Promise.resolve(5),
156
- })
157
-
158
- const queryClient = new QueryClient()
159
-
160
- // Should not error
161
- const data = queryClient.invalidateQueries(options)
162
- // Should not error
163
- const data2 = queryClient.fetchQuery(options)
164
-
165
- const result: Expect<Equal<typeof data, Promise<void>>> = true
166
- const result2: Expect<Equal<typeof data2, Promise<number>>> = true
167
- return result || result2
168
- })
169
- })
170
- })
@@ -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
- })