@tanstack/solid-query 5.24.8 → 5.26.3
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/index.d.cts +3 -3
- package/build/index.d.ts +3 -3
- package/package.json +2 -2
- package/src/__tests__/createQueries.test.tsx +99 -82
- package/src/__tests__/createQuery.test.tsx +28 -24
- package/src/__tests__/useMutationState.test-d.tsx +23 -0
- package/src/__tests__/useMutationState.test.tsx +2 -24
- package/src/__tests__/utils.tsx +0 -9
- package/src/createQueries.ts +7 -5
package/build/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DefaultError, QueryKey, QueryObserverOptions as QueryObserverOptions$1, InfiniteQueryObserverOptions as InfiniteQueryObserverOptions$1, DefaultOptions as DefaultOptions$1, QueryClientConfig as QueryClientConfig$1, QueryClient as QueryClient$1, QueryObserverResult, DefinedQueryObserverResult, InfiniteQueryObserverResult, MutationObserverOptions, MutateFunction, MutationObserverResult, QueryFilters, InfiniteData, MutationFilters, MutationState, Mutation, QueriesPlaceholderDataFunction, QueryFunction, ThrowOnError } from '@tanstack/query-core';
|
|
1
|
+
import { DefaultError, QueryKey, QueryObserverOptions as QueryObserverOptions$1, InfiniteQueryObserverOptions as InfiniteQueryObserverOptions$1, DefaultOptions as DefaultOptions$1, QueryClientConfig as QueryClientConfig$1, QueryClient as QueryClient$1, QueryObserverResult, DefinedQueryObserverResult, InfiniteQueryObserverResult, MutationObserverOptions, MutateFunction, MutationObserverResult, QueryFilters, InfiniteData, MutationFilters, MutationState, Mutation, QueriesPlaceholderDataFunction, QueryFunction, SkipToken, ThrowOnError } from '@tanstack/query-core';
|
|
2
2
|
export * from '@tanstack/query-core';
|
|
3
3
|
import * as solid_js from 'solid-js';
|
|
4
4
|
import { JSX, Accessor } from 'solid-js';
|
|
@@ -111,11 +111,11 @@ type GetOptions<T> = T extends {
|
|
|
111
111
|
data: infer TData;
|
|
112
112
|
error?: infer TError;
|
|
113
113
|
} ? CreateQueryOptionsForCreateQueries<unknown, TError, TData> : T extends [infer TQueryFnData, infer TError, infer TData] ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData> : T extends [infer TQueryFnData, infer TError] ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError> : T extends [infer TQueryFnData] ? CreateQueryOptionsForCreateQueries<TQueryFnData> : T extends {
|
|
114
|
-
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey
|
|
114
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipToken;
|
|
115
115
|
select?: (data: any) => infer TData;
|
|
116
116
|
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
117
117
|
} ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData, TQueryKey> : T extends {
|
|
118
|
-
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey
|
|
118
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipToken;
|
|
119
119
|
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
120
120
|
} ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TQueryFnData, TQueryKey> : CreateQueryOptionsForCreateQueries;
|
|
121
121
|
type GetResults<T> = T extends {
|
package/build/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DefaultError, QueryKey, QueryObserverOptions as QueryObserverOptions$1, InfiniteQueryObserverOptions as InfiniteQueryObserverOptions$1, DefaultOptions as DefaultOptions$1, QueryClientConfig as QueryClientConfig$1, QueryClient as QueryClient$1, QueryObserverResult, DefinedQueryObserverResult, InfiniteQueryObserverResult, MutationObserverOptions, MutateFunction, MutationObserverResult, QueryFilters, InfiniteData, MutationFilters, MutationState, Mutation, QueriesPlaceholderDataFunction, QueryFunction, ThrowOnError } from '@tanstack/query-core';
|
|
1
|
+
import { DefaultError, QueryKey, QueryObserverOptions as QueryObserverOptions$1, InfiniteQueryObserverOptions as InfiniteQueryObserverOptions$1, DefaultOptions as DefaultOptions$1, QueryClientConfig as QueryClientConfig$1, QueryClient as QueryClient$1, QueryObserverResult, DefinedQueryObserverResult, InfiniteQueryObserverResult, MutationObserverOptions, MutateFunction, MutationObserverResult, QueryFilters, InfiniteData, MutationFilters, MutationState, Mutation, QueriesPlaceholderDataFunction, QueryFunction, SkipToken, ThrowOnError } from '@tanstack/query-core';
|
|
2
2
|
export * from '@tanstack/query-core';
|
|
3
3
|
import * as solid_js from 'solid-js';
|
|
4
4
|
import { JSX, Accessor } from 'solid-js';
|
|
@@ -111,11 +111,11 @@ type GetOptions<T> = T extends {
|
|
|
111
111
|
data: infer TData;
|
|
112
112
|
error?: infer TError;
|
|
113
113
|
} ? CreateQueryOptionsForCreateQueries<unknown, TError, TData> : T extends [infer TQueryFnData, infer TError, infer TData] ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData> : T extends [infer TQueryFnData, infer TError] ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError> : T extends [infer TQueryFnData] ? CreateQueryOptionsForCreateQueries<TQueryFnData> : T extends {
|
|
114
|
-
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey
|
|
114
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipToken;
|
|
115
115
|
select?: (data: any) => infer TData;
|
|
116
116
|
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
117
117
|
} ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TData, TQueryKey> : T extends {
|
|
118
|
-
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey
|
|
118
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipToken;
|
|
119
119
|
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
120
120
|
} ? CreateQueryOptionsForCreateQueries<TQueryFnData, TError, TQueryFnData, TQueryKey> : CreateQueryOptionsForCreateQueries;
|
|
121
121
|
type GetResults<T> = T extends {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-query",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.26.3",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Solid",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
],
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"solid-js": "^1.8.14",
|
|
51
|
-
"@tanstack/query-core": "5.
|
|
51
|
+
"@tanstack/query-core": "5.26.3"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"tsup-preset-solid": "^2.2.0",
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { describe, expect, expectTypeOf, it, vi } from 'vitest'
|
|
2
2
|
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
|
|
3
3
|
import * as QueryCore from '@tanstack/query-core'
|
|
4
|
-
|
|
5
4
|
import { createRenderEffect, createSignal } from 'solid-js'
|
|
6
5
|
import {
|
|
7
6
|
QueriesObserver,
|
|
@@ -9,14 +8,9 @@ import {
|
|
|
9
8
|
QueryClientProvider,
|
|
10
9
|
createQueries,
|
|
11
10
|
} from '..'
|
|
12
|
-
import { createQueryClient,
|
|
11
|
+
import { createQueryClient, queryKey, sleep } from './utils'
|
|
13
12
|
import type { QueryFunctionContext, QueryKey } from '@tanstack/query-core'
|
|
14
|
-
import type {
|
|
15
|
-
CreateQueryResult,
|
|
16
|
-
QueryFunction,
|
|
17
|
-
QueryObserverResult,
|
|
18
|
-
SolidQueryOptions,
|
|
19
|
-
} from '..'
|
|
13
|
+
import type { CreateQueryResult, QueryFunction, SolidQueryOptions } from '..'
|
|
20
14
|
|
|
21
15
|
describe('useQueries', () => {
|
|
22
16
|
const queryCache = new QueryCache()
|
|
@@ -100,13 +94,15 @@ describe('useQueries', () => {
|
|
|
100
94
|
},
|
|
101
95
|
],
|
|
102
96
|
}))
|
|
103
|
-
expectTypeOf
|
|
104
|
-
expectTypeOf
|
|
105
|
-
expectTypeOf
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
expectTypeOf<
|
|
109
|
-
expectTypeOf<
|
|
97
|
+
expectTypeOf(result1[0]).toEqualTypeOf<CreateQueryResult<number>>()
|
|
98
|
+
expectTypeOf(result1[1]).toEqualTypeOf<CreateQueryResult<string>>()
|
|
99
|
+
expectTypeOf(result1[2]).toEqualTypeOf<
|
|
100
|
+
CreateQueryResult<Array<string>, boolean>
|
|
101
|
+
>()
|
|
102
|
+
expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
|
|
103
|
+
expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
|
|
104
|
+
expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
|
|
105
|
+
expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
|
|
110
106
|
|
|
111
107
|
// TData (3rd element) takes precedence over TQueryFnData (1st element)
|
|
112
108
|
const result2 = createQueries<
|
|
@@ -117,8 +113,7 @@ describe('useQueries', () => {
|
|
|
117
113
|
queryKey: key1,
|
|
118
114
|
queryFn: () => 'string',
|
|
119
115
|
select: (a) => {
|
|
120
|
-
expectTypeOf<string>(
|
|
121
|
-
expectTypeNotAny(a)
|
|
116
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
122
117
|
return a.toLowerCase()
|
|
123
118
|
},
|
|
124
119
|
},
|
|
@@ -126,17 +121,20 @@ describe('useQueries', () => {
|
|
|
126
121
|
queryKey: key2,
|
|
127
122
|
queryFn: () => 'string',
|
|
128
123
|
select: (a) => {
|
|
129
|
-
expectTypeOf<string>(
|
|
130
|
-
expectTypeNotAny(a)
|
|
124
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
131
125
|
return parseInt(a)
|
|
132
126
|
},
|
|
133
127
|
},
|
|
134
128
|
],
|
|
135
129
|
}))
|
|
136
|
-
expectTypeOf
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
expectTypeOf
|
|
130
|
+
expectTypeOf(result2[0]).toEqualTypeOf<
|
|
131
|
+
CreateQueryResult<string, unknown>
|
|
132
|
+
>()
|
|
133
|
+
expectTypeOf(result2[1]).toEqualTypeOf<
|
|
134
|
+
CreateQueryResult<number, unknown>
|
|
135
|
+
>()
|
|
136
|
+
expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
|
|
137
|
+
expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
|
|
140
138
|
|
|
141
139
|
// types should be enforced
|
|
142
140
|
createQueries<[[string, unknown, string], [string, boolean, number]]>(
|
|
@@ -146,8 +144,7 @@ describe('useQueries', () => {
|
|
|
146
144
|
queryKey: key1,
|
|
147
145
|
queryFn: () => 'string',
|
|
148
146
|
select: (a) => {
|
|
149
|
-
expectTypeOf<string>(
|
|
150
|
-
expectTypeNotAny(a)
|
|
147
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
151
148
|
return a.toLowerCase()
|
|
152
149
|
},
|
|
153
150
|
placeholderData: 'string',
|
|
@@ -158,8 +155,7 @@ describe('useQueries', () => {
|
|
|
158
155
|
queryKey: key2,
|
|
159
156
|
queryFn: () => 'string',
|
|
160
157
|
select: (a) => {
|
|
161
|
-
expectTypeOf<string>(
|
|
162
|
-
expectTypeNotAny(a)
|
|
158
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
163
159
|
return parseInt(a)
|
|
164
160
|
},
|
|
165
161
|
placeholderData: 'string',
|
|
@@ -211,13 +207,19 @@ describe('useQueries', () => {
|
|
|
211
207
|
},
|
|
212
208
|
],
|
|
213
209
|
}))
|
|
214
|
-
expectTypeOf
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
expectTypeOf
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
expectTypeOf
|
|
210
|
+
expectTypeOf(result1[0]).toEqualTypeOf<
|
|
211
|
+
CreateQueryResult<number, unknown>
|
|
212
|
+
>()
|
|
213
|
+
expectTypeOf(result1[1]).toEqualTypeOf<
|
|
214
|
+
CreateQueryResult<string, unknown>
|
|
215
|
+
>()
|
|
216
|
+
expectTypeOf(result1[2]).toEqualTypeOf<
|
|
217
|
+
CreateQueryResult<Array<string>, boolean>
|
|
218
|
+
>()
|
|
219
|
+
expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
|
|
220
|
+
expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
|
|
221
|
+
expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
|
|
222
|
+
expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
|
|
221
223
|
|
|
222
224
|
// TData (data prop) takes precedence over TQueryFnData (queryFnData prop)
|
|
223
225
|
const result2 = createQueries<
|
|
@@ -231,8 +233,7 @@ describe('useQueries', () => {
|
|
|
231
233
|
queryKey: key1,
|
|
232
234
|
queryFn: () => 'string',
|
|
233
235
|
select: (a) => {
|
|
234
|
-
expectTypeOf<string>(
|
|
235
|
-
expectTypeNotAny(a)
|
|
236
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
236
237
|
return a.toLowerCase()
|
|
237
238
|
},
|
|
238
239
|
},
|
|
@@ -240,17 +241,20 @@ describe('useQueries', () => {
|
|
|
240
241
|
queryKey: key2,
|
|
241
242
|
queryFn: () => 'string',
|
|
242
243
|
select: (a) => {
|
|
243
|
-
expectTypeOf<string>(
|
|
244
|
-
expectTypeNotAny(a)
|
|
244
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
245
245
|
return parseInt(a)
|
|
246
246
|
},
|
|
247
247
|
},
|
|
248
248
|
],
|
|
249
249
|
}))
|
|
250
|
-
expectTypeOf
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
expectTypeOf
|
|
250
|
+
expectTypeOf(result2[0]).toEqualTypeOf<
|
|
251
|
+
CreateQueryResult<string, unknown>
|
|
252
|
+
>()
|
|
253
|
+
expectTypeOf(result2[1]).toEqualTypeOf<
|
|
254
|
+
CreateQueryResult<number, unknown>
|
|
255
|
+
>()
|
|
256
|
+
expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
|
|
257
|
+
expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
|
|
254
258
|
|
|
255
259
|
// can pass only TData (data prop) although TQueryFnData will be left unknown
|
|
256
260
|
const result3 = createQueries<[{ data: string }, { data: number }]>(
|
|
@@ -260,8 +264,7 @@ describe('useQueries', () => {
|
|
|
260
264
|
queryKey: key1,
|
|
261
265
|
queryFn: () => 'string',
|
|
262
266
|
select: (a) => {
|
|
263
|
-
expectTypeOf<unknown>(
|
|
264
|
-
expectTypeNotAny(a)
|
|
267
|
+
expectTypeOf(a).toEqualTypeOf<unknown>()
|
|
265
268
|
return a as string
|
|
266
269
|
},
|
|
267
270
|
},
|
|
@@ -269,18 +272,21 @@ describe('useQueries', () => {
|
|
|
269
272
|
queryKey: key2,
|
|
270
273
|
queryFn: () => 'string',
|
|
271
274
|
select: (a) => {
|
|
272
|
-
expectTypeOf<unknown>(
|
|
273
|
-
expectTypeNotAny(a)
|
|
275
|
+
expectTypeOf(a).toEqualTypeOf<unknown>()
|
|
274
276
|
return a as number
|
|
275
277
|
},
|
|
276
278
|
},
|
|
277
279
|
],
|
|
278
280
|
}),
|
|
279
281
|
)
|
|
280
|
-
expectTypeOf
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
expectTypeOf
|
|
282
|
+
expectTypeOf(result3[0]).toEqualTypeOf<
|
|
283
|
+
CreateQueryResult<string, unknown>
|
|
284
|
+
>()
|
|
285
|
+
expectTypeOf(result3[1]).toEqualTypeOf<
|
|
286
|
+
CreateQueryResult<number, unknown>
|
|
287
|
+
>()
|
|
288
|
+
expectTypeOf(result3[0].data).toEqualTypeOf<string | undefined>()
|
|
289
|
+
expectTypeOf(result3[1].data).toEqualTypeOf<number | undefined>()
|
|
284
290
|
|
|
285
291
|
// types should be enforced
|
|
286
292
|
createQueries<
|
|
@@ -294,8 +300,7 @@ describe('useQueries', () => {
|
|
|
294
300
|
queryKey: key1,
|
|
295
301
|
queryFn: () => 'string',
|
|
296
302
|
select: (a) => {
|
|
297
|
-
expectTypeOf<string>(
|
|
298
|
-
expectTypeNotAny(a)
|
|
303
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
299
304
|
return a.toLowerCase()
|
|
300
305
|
},
|
|
301
306
|
placeholderData: 'string',
|
|
@@ -306,8 +311,7 @@ describe('useQueries', () => {
|
|
|
306
311
|
queryKey: key2,
|
|
307
312
|
queryFn: () => 'string',
|
|
308
313
|
select: (a) => {
|
|
309
|
-
expectTypeOf<string>(
|
|
310
|
-
expectTypeNotAny(a)
|
|
314
|
+
expectTypeOf(a).toEqualTypeOf<string>()
|
|
311
315
|
return parseInt(a)
|
|
312
316
|
},
|
|
313
317
|
placeholderData: 'string',
|
|
@@ -344,8 +348,12 @@ describe('useQueries', () => {
|
|
|
344
348
|
queryFn: () => i + 10,
|
|
345
349
|
})),
|
|
346
350
|
}))
|
|
347
|
-
expectTypeOf
|
|
348
|
-
|
|
351
|
+
expectTypeOf(result1).toEqualTypeOf<
|
|
352
|
+
Array<CreateQueryResult<number, Error>>
|
|
353
|
+
>()
|
|
354
|
+
if (result1[0]) {
|
|
355
|
+
expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
|
|
356
|
+
}
|
|
349
357
|
|
|
350
358
|
// Array.map preserves TData
|
|
351
359
|
const result2 = createQueries(() => ({
|
|
@@ -355,7 +363,9 @@ describe('useQueries', () => {
|
|
|
355
363
|
select: (data: number) => data.toString(),
|
|
356
364
|
})),
|
|
357
365
|
}))
|
|
358
|
-
expectTypeOf
|
|
366
|
+
expectTypeOf(result2).toEqualTypeOf<
|
|
367
|
+
Array<CreateQueryResult<string, Error>>
|
|
368
|
+
>()
|
|
359
369
|
|
|
360
370
|
const result3 = createQueries(() => ({
|
|
361
371
|
queries: [
|
|
@@ -374,13 +384,13 @@ describe('useQueries', () => {
|
|
|
374
384
|
},
|
|
375
385
|
],
|
|
376
386
|
}))
|
|
377
|
-
expectTypeOf<
|
|
378
|
-
expectTypeOf<
|
|
379
|
-
expectTypeOf<
|
|
380
|
-
expectTypeOf<number | undefined>(
|
|
381
|
-
expectTypeOf<string | undefined>(
|
|
387
|
+
expectTypeOf(result3[0]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
388
|
+
expectTypeOf(result3[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
389
|
+
expectTypeOf(result3[2]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
390
|
+
expectTypeOf(result3[0].data).toEqualTypeOf<number | undefined>()
|
|
391
|
+
expectTypeOf(result3[1].data).toEqualTypeOf<string | undefined>()
|
|
382
392
|
// select takes precedence over queryFn
|
|
383
|
-
expectTypeOf<number | undefined>(
|
|
393
|
+
expectTypeOf(result3[2].data).toEqualTypeOf<number | undefined>()
|
|
384
394
|
|
|
385
395
|
// initialData/placeholderData are enforced
|
|
386
396
|
createQueries(() => ({
|
|
@@ -463,9 +473,9 @@ describe('useQueries', () => {
|
|
|
463
473
|
},
|
|
464
474
|
],
|
|
465
475
|
}))
|
|
466
|
-
expectTypeOf<
|
|
467
|
-
expectTypeOf<
|
|
468
|
-
expectTypeOf<
|
|
476
|
+
expectTypeOf(result4[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
477
|
+
expectTypeOf(result4[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
478
|
+
expectTypeOf(result4[2]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
469
479
|
|
|
470
480
|
// handles when queryFn returns a Promise
|
|
471
481
|
const result5 = createQueries(() => ({
|
|
@@ -476,7 +486,7 @@ describe('useQueries', () => {
|
|
|
476
486
|
},
|
|
477
487
|
],
|
|
478
488
|
}))
|
|
479
|
-
expectTypeOf<
|
|
489
|
+
expectTypeOf(result5[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
480
490
|
|
|
481
491
|
// Array as const does not throw error
|
|
482
492
|
const result6 = createQueries(
|
|
@@ -494,8 +504,8 @@ describe('useQueries', () => {
|
|
|
494
504
|
],
|
|
495
505
|
}) as const,
|
|
496
506
|
)
|
|
497
|
-
expectTypeOf<
|
|
498
|
-
expectTypeOf<
|
|
507
|
+
expectTypeOf(result6[0]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
508
|
+
expectTypeOf(result6[1]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
499
509
|
|
|
500
510
|
// field names should be enforced - array literal
|
|
501
511
|
createQueries(() => ({
|
|
@@ -581,13 +591,20 @@ describe('useQueries', () => {
|
|
|
581
591
|
// no need to type the mapped query
|
|
582
592
|
(query) => {
|
|
583
593
|
const { queryFn: fn, queryKey: key } = query
|
|
584
|
-
expectTypeOf
|
|
594
|
+
expectTypeOf(fn).toEqualTypeOf<
|
|
595
|
+
| typeof QueryCore.skipToken
|
|
596
|
+
| QueryCore.QueryFunction<TQueryFnData, TQueryKey, never>
|
|
597
|
+
| undefined
|
|
598
|
+
>()
|
|
585
599
|
return {
|
|
586
600
|
queryKey: key,
|
|
587
601
|
queryFn: fn
|
|
588
602
|
? (ctx: QueryFunctionContext<TQueryKey>) => {
|
|
589
603
|
expectTypeOf<TQueryKey>(ctx.queryKey)
|
|
590
|
-
return fn.call(
|
|
604
|
+
return (fn as QueryFunction<TQueryFnData, TQueryKey>).call(
|
|
605
|
+
{},
|
|
606
|
+
ctx,
|
|
607
|
+
)
|
|
591
608
|
}
|
|
592
609
|
: undefined,
|
|
593
610
|
}
|
|
@@ -610,8 +627,8 @@ describe('useQueries', () => {
|
|
|
610
627
|
},
|
|
611
628
|
],
|
|
612
629
|
}))
|
|
613
|
-
expectTypeOf<
|
|
614
|
-
expectTypeOf<
|
|
630
|
+
expectTypeOf(result[0]).toEqualTypeOf<CreateQueryResult<number, Error>>()
|
|
631
|
+
expectTypeOf(result[1]).toEqualTypeOf<CreateQueryResult<string, Error>>()
|
|
615
632
|
|
|
616
633
|
const withSelector = createQueries(() => ({
|
|
617
634
|
queries: [
|
|
@@ -627,12 +644,12 @@ describe('useQueries', () => {
|
|
|
627
644
|
},
|
|
628
645
|
],
|
|
629
646
|
}))
|
|
630
|
-
expectTypeOf
|
|
631
|
-
|
|
632
|
-
)
|
|
633
|
-
expectTypeOf
|
|
634
|
-
|
|
635
|
-
)
|
|
647
|
+
expectTypeOf(withSelector[0]).toEqualTypeOf<
|
|
648
|
+
CreateQueryResult<[number, string], Error>
|
|
649
|
+
>()
|
|
650
|
+
expectTypeOf(withSelector[1]).toEqualTypeOf<
|
|
651
|
+
CreateQueryResult<[string, number], Error>
|
|
652
|
+
>()
|
|
636
653
|
|
|
637
654
|
const withWrappedQueries = useWrappedQueries(
|
|
638
655
|
Array(10).map(() => ({
|
|
@@ -642,9 +659,9 @@ describe('useQueries', () => {
|
|
|
642
659
|
})),
|
|
643
660
|
)
|
|
644
661
|
|
|
645
|
-
expectTypeOf<
|
|
646
|
-
|
|
647
|
-
)
|
|
662
|
+
expectTypeOf(withWrappedQueries).toEqualTypeOf<
|
|
663
|
+
Array<CreateQueryResult<number, Error>>
|
|
664
|
+
>()
|
|
648
665
|
}
|
|
649
666
|
})
|
|
650
667
|
|
|
@@ -46,32 +46,32 @@ describe('createQuery', () => {
|
|
|
46
46
|
function Page() {
|
|
47
47
|
// unspecified query function should default to unknown
|
|
48
48
|
const noQueryFn = createQuery(() => ({ queryKey: key }))
|
|
49
|
-
expectTypeOf
|
|
50
|
-
expectTypeOf
|
|
49
|
+
expectTypeOf(noQueryFn.data).toEqualTypeOf<unknown>()
|
|
50
|
+
expectTypeOf(noQueryFn.error).toEqualTypeOf<Error | null>()
|
|
51
51
|
|
|
52
52
|
// it should infer the result type from the query function
|
|
53
53
|
const fromQueryFn = createQuery(() => ({
|
|
54
54
|
queryKey: key,
|
|
55
55
|
queryFn: () => 'test',
|
|
56
56
|
}))
|
|
57
|
-
expectTypeOf<string | undefined>(
|
|
58
|
-
expectTypeOf
|
|
57
|
+
expectTypeOf(fromQueryFn.data).toEqualTypeOf<string | undefined>()
|
|
58
|
+
expectTypeOf(fromQueryFn.error).toEqualTypeOf<Error | null>()
|
|
59
59
|
|
|
60
60
|
// it should be possible to specify the result type
|
|
61
61
|
const withResult = createQuery<string>(() => ({
|
|
62
62
|
queryKey: key,
|
|
63
63
|
queryFn: () => 'test',
|
|
64
64
|
}))
|
|
65
|
-
expectTypeOf<string | undefined>(
|
|
66
|
-
expectTypeOf<
|
|
65
|
+
expectTypeOf(withResult.data).toEqualTypeOf<string | undefined>()
|
|
66
|
+
expectTypeOf(withResult.error).toEqualTypeOf<Error | null>()
|
|
67
67
|
|
|
68
68
|
// it should be possible to specify the error type
|
|
69
69
|
const withError = createQuery<string, Error>(() => ({
|
|
70
70
|
queryKey: key,
|
|
71
71
|
queryFn: () => 'test',
|
|
72
72
|
}))
|
|
73
|
-
expectTypeOf<string | undefined>(
|
|
74
|
-
expectTypeOf<Error | null>(
|
|
73
|
+
expectTypeOf(withError.data).toEqualTypeOf<string | undefined>()
|
|
74
|
+
expectTypeOf(withError.error).toEqualTypeOf<Error | null>()
|
|
75
75
|
|
|
76
76
|
// it should provide the result type in the configuration
|
|
77
77
|
createQuery(() => ({
|
|
@@ -82,14 +82,14 @@ describe('createQuery', () => {
|
|
|
82
82
|
// it should be possible to specify a union type as result type
|
|
83
83
|
const unionTypeSync = createQuery(() => ({
|
|
84
84
|
queryKey: key,
|
|
85
|
-
queryFn: () => (Math.random() > 0.5 ? 'a' : 'b'),
|
|
85
|
+
queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)),
|
|
86
86
|
}))
|
|
87
|
-
expectTypeOf<'a' | 'b' | undefined>(
|
|
87
|
+
expectTypeOf(unionTypeSync.data).toEqualTypeOf<'a' | 'b' | undefined>()
|
|
88
88
|
const unionTypeAsync = createQuery<'a' | 'b'>(() => ({
|
|
89
89
|
queryKey: key,
|
|
90
90
|
queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'),
|
|
91
91
|
}))
|
|
92
|
-
expectTypeOf<'a' | 'b' | undefined>(
|
|
92
|
+
expectTypeOf(unionTypeAsync.data).toEqualTypeOf<'a' | 'b' | undefined>()
|
|
93
93
|
|
|
94
94
|
// should error when the query function result does not match with the specified type
|
|
95
95
|
// @ts-expect-error
|
|
@@ -104,15 +104,19 @@ describe('createQuery', () => {
|
|
|
104
104
|
queryKey: key,
|
|
105
105
|
queryFn: () => queryFn(),
|
|
106
106
|
}))
|
|
107
|
-
expectTypeOf<string | undefined>(
|
|
108
|
-
expectTypeOf
|
|
107
|
+
expectTypeOf(fromGenericQueryFn.data).toEqualTypeOf<string | undefined>()
|
|
108
|
+
expectTypeOf(fromGenericQueryFn.error).toEqualTypeOf<Error | null>()
|
|
109
109
|
|
|
110
110
|
const fromGenericOptionsQueryFn = createQuery(() => ({
|
|
111
111
|
queryKey: key,
|
|
112
112
|
queryFn: () => queryFn(),
|
|
113
113
|
}))
|
|
114
|
-
expectTypeOf
|
|
115
|
-
|
|
114
|
+
expectTypeOf(fromGenericOptionsQueryFn.data).toEqualTypeOf<
|
|
115
|
+
string | undefined
|
|
116
|
+
>()
|
|
117
|
+
expectTypeOf(
|
|
118
|
+
fromGenericOptionsQueryFn.error,
|
|
119
|
+
).toEqualTypeOf<Error | null>()
|
|
116
120
|
|
|
117
121
|
type MyData = number
|
|
118
122
|
type MyQueryKey = readonly ['my-data', number]
|
|
@@ -131,7 +135,7 @@ describe('createQuery', () => {
|
|
|
131
135
|
const getMyDataStringKey: QueryFunction<MyData, ['1']> = async (
|
|
132
136
|
context,
|
|
133
137
|
) => {
|
|
134
|
-
expectTypeOf<['1']>(
|
|
138
|
+
expectTypeOf(context.queryKey).toEqualTypeOf<['1']>()
|
|
135
139
|
return Number(context.queryKey[0]) + 42
|
|
136
140
|
}
|
|
137
141
|
|
|
@@ -170,7 +174,7 @@ describe('createQuery', () => {
|
|
|
170
174
|
...options,
|
|
171
175
|
}))
|
|
172
176
|
const test = useWrappedQuery([''], async () => '1')
|
|
173
|
-
expectTypeOf<string | undefined>(
|
|
177
|
+
expectTypeOf(test.data).toEqualTypeOf<string | undefined>()
|
|
174
178
|
|
|
175
179
|
// handles wrapped queries with custom fetcher passed directly to createQuery
|
|
176
180
|
const useWrappedFuncStyleQuery = <
|
|
@@ -187,7 +191,7 @@ describe('createQuery', () => {
|
|
|
187
191
|
>,
|
|
188
192
|
) => createQuery(() => ({ queryKey: qk, queryFn: fetcher, ...options }))
|
|
189
193
|
const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true)
|
|
190
|
-
expectTypeOf<boolean | undefined>(
|
|
194
|
+
expectTypeOf(testFuncStyle.data).toEqualTypeOf<boolean | undefined>()
|
|
191
195
|
}
|
|
192
196
|
})
|
|
193
197
|
|
|
@@ -240,14 +244,14 @@ describe('createQuery', () => {
|
|
|
240
244
|
})
|
|
241
245
|
|
|
242
246
|
if (state.isPending) {
|
|
243
|
-
expectTypeOf
|
|
244
|
-
expectTypeOf
|
|
247
|
+
expectTypeOf(state.data).toEqualTypeOf<undefined>()
|
|
248
|
+
expectTypeOf(state.error).toEqualTypeOf<null>()
|
|
245
249
|
} else if (state.isLoadingError) {
|
|
246
|
-
expectTypeOf
|
|
247
|
-
expectTypeOf
|
|
250
|
+
expectTypeOf(state.data).toEqualTypeOf<undefined>()
|
|
251
|
+
expectTypeOf(state.error).toEqualTypeOf<Error>()
|
|
248
252
|
} else {
|
|
249
|
-
expectTypeOf
|
|
250
|
-
expectTypeOf<Error | null>(
|
|
253
|
+
expectTypeOf(state.data).toEqualTypeOf<string>()
|
|
254
|
+
expectTypeOf(state.error).toEqualTypeOf<Error | null>()
|
|
251
255
|
}
|
|
252
256
|
|
|
253
257
|
return (
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from 'vitest'
|
|
2
|
+
import { useMutationState } from '../useMutationState'
|
|
3
|
+
import type { MutationState, MutationStatus } from '@tanstack/query-core'
|
|
4
|
+
|
|
5
|
+
describe('useMutationState', () => {
|
|
6
|
+
it('should default to QueryState', () => {
|
|
7
|
+
const result = useMutationState(() => ({
|
|
8
|
+
filters: { status: 'pending' },
|
|
9
|
+
}))
|
|
10
|
+
|
|
11
|
+
expectTypeOf(result()).toEqualTypeOf<
|
|
12
|
+
Array<MutationState<unknown, Error, void, unknown>>
|
|
13
|
+
>()
|
|
14
|
+
})
|
|
15
|
+
it('should infer with select', () => {
|
|
16
|
+
const result = useMutationState(() => ({
|
|
17
|
+
filters: { status: 'pending' },
|
|
18
|
+
select: (mutation) => mutation.state.status,
|
|
19
|
+
}))
|
|
20
|
+
|
|
21
|
+
expectTypeOf(result()).toEqualTypeOf<Array<MutationStatus>>()
|
|
22
|
+
})
|
|
23
|
+
})
|
|
@@ -1,34 +1,12 @@
|
|
|
1
|
-
import { describe, expect,
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { fireEvent, render, waitFor } from '@solidjs/testing-library'
|
|
3
3
|
import { createEffect } from 'solid-js'
|
|
4
4
|
import { useMutationState } from '../useMutationState'
|
|
5
5
|
import { createMutation } from '../createMutation'
|
|
6
6
|
import { QueryClientProvider } from '../QueryClientProvider'
|
|
7
|
-
import { createQueryClient,
|
|
8
|
-
import type { MutationState, MutationStatus } from '@tanstack/query-core'
|
|
7
|
+
import { createQueryClient, sleep } from './utils'
|
|
9
8
|
|
|
10
9
|
describe('useMutationState', () => {
|
|
11
|
-
describe('types', () => {
|
|
12
|
-
it('should default to QueryState', () => {
|
|
13
|
-
doNotExecute(() => {
|
|
14
|
-
const result = useMutationState(() => ({
|
|
15
|
-
filters: { status: 'pending' },
|
|
16
|
-
}))
|
|
17
|
-
|
|
18
|
-
expectTypeOf<Array<MutationState>>(result())
|
|
19
|
-
})
|
|
20
|
-
})
|
|
21
|
-
it('should infer with select', () => {
|
|
22
|
-
doNotExecute(() => {
|
|
23
|
-
const result = useMutationState(() => ({
|
|
24
|
-
filters: { status: 'pending' },
|
|
25
|
-
select: (mutation) => mutation.state.status,
|
|
26
|
-
}))
|
|
27
|
-
|
|
28
|
-
expectTypeOf<Array<MutationStatus>>(result())
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
})
|
|
32
10
|
it('should return variables after calling mutate', async () => {
|
|
33
11
|
const queryClient = createQueryClient()
|
|
34
12
|
const variables: Array<Array<unknown>> = []
|
package/src/__tests__/utils.tsx
CHANGED
|
@@ -59,12 +59,3 @@ export function setActTimeout(fn: () => void, ms?: number) {
|
|
|
59
59
|
fn()
|
|
60
60
|
}, ms)
|
|
61
61
|
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Assert the parameter is not typed as `any`
|
|
65
|
-
*/
|
|
66
|
-
export function expectTypeNotAny<T>(_: 0 extends 1 & T ? never : T): void {
|
|
67
|
-
return undefined
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export const doNotExecute = (_func: () => void) => true
|
package/src/createQueries.ts
CHANGED
|
@@ -23,6 +23,7 @@ import type {
|
|
|
23
23
|
QueryFunction,
|
|
24
24
|
QueryKey,
|
|
25
25
|
QueryObserverResult,
|
|
26
|
+
SkipToken,
|
|
26
27
|
ThrowOnError,
|
|
27
28
|
} from '@tanstack/query-core'
|
|
28
29
|
|
|
@@ -64,7 +65,9 @@ type GetOptions<T> =
|
|
|
64
65
|
? CreateQueryOptionsForCreateQueries<TQueryFnData>
|
|
65
66
|
: // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided
|
|
66
67
|
T extends {
|
|
67
|
-
queryFn?:
|
|
68
|
+
queryFn?:
|
|
69
|
+
| QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
70
|
+
| SkipToken
|
|
68
71
|
select?: (data: any) => infer TData
|
|
69
72
|
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
70
73
|
}
|
|
@@ -75,10 +78,9 @@ type GetOptions<T> =
|
|
|
75
78
|
TQueryKey
|
|
76
79
|
>
|
|
77
80
|
: T extends {
|
|
78
|
-
queryFn?:
|
|
79
|
-
infer TQueryFnData,
|
|
80
|
-
|
|
81
|
-
>
|
|
81
|
+
queryFn?:
|
|
82
|
+
| QueryFunction<infer TQueryFnData, infer TQueryKey>
|
|
83
|
+
| SkipToken
|
|
82
84
|
throwOnError?: ThrowOnError<any, infer TError, any, any>
|
|
83
85
|
}
|
|
84
86
|
? CreateQueryOptionsForCreateQueries<
|