forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
694 lines (620 loc) · 19.3 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// Definitions by: Lukasz Fiszer <https://github.com/lukaszfiszer>
// Jace Hensley <https://github.com/jacehensley>
// Matteo Frana <https://github.com/matteofrana>
// Igor Oleinikov <https://github.com/igorbek>
// Minimum TypeScript Version: 3.9
import * as React from 'react'
import * as _ from 'ts-toolbelt'
// overloaded useQuery function
export type UseQueryRest<TResult, TKey extends AnyQueryKey, TError> =
| []
| [QueryFunction<TResult, TKey>]
| [QueryFunction<TResult, TKey>, QueryOptions<TResult, TError> | undefined]
| [QueryOptions<TResult, TError>]
// Object Syntax
export function useQuery<TResult, TKey extends AnyQueryKey, TError = Error>({
queryKey,
queryFn,
config,
}: {
queryKey: TKey
queryFn?: QueryFunction<TResult, TKey>
config?: QueryOptions<TResult, TError>
}): QueryResult<TResult, TError>
export function useQuery<TResult, TKey extends string, TError = Error>({
queryKey,
queryFn,
config,
}: {
queryKey: TKey
queryFn?: QueryFunction<TResult, [TKey]>
config?: QueryOptions<TResult, TError>
}): QueryResult<TResult, TError>
// Parameters Syntax
export function useQuery<TResult, TKey extends AnyQueryKey, TError = Error>(
queryKey: TKey | false | null | undefined,
...rest: UseQueryRest<TResult, TKey, TError>
): QueryResult<TResult, TError>
export function useQuery<TResult, TKey extends string, TError = Error>(
queryKey: TKey | false | null | undefined,
...rest: UseQueryRest<TResult, [TKey], TError>
): QueryResult<TResult, TError>
// usePaginatedQuery
export function usePaginatedQuery<
TResult,
TKey extends AnyQueryKey,
TError = Error
>({
queryKey,
queryFn,
config,
}: {
queryKey: TKey | false | null | undefined
queryFn: QueryFunction<TResult, TKey>
config?: QueryOptions<TResult, TError>
}): PaginatedQueryResult<TResult, TError>
export function usePaginatedQuery<
TResult,
TSingleKey extends string,
TError = Error
>({
queryKey,
queryFn,
config,
}: {
queryKey: TSingleKey | false | null | undefined
queryFn: QueryFunction<TResult, [TSingleKey]>
config?: QueryOptions<TResult, TError>
}): PaginatedQueryResult<TResult, TError>
export function usePaginatedQuery<
TResult,
TKey extends AnyQueryKey,
TError = Error
>(
queryKey: TKey | false | null | undefined,
queryFn: QueryFunction<TResult, TKey>,
config?: QueryOptions<TResult, TError>
): PaginatedQueryResult<TResult, TError>
export function usePaginatedQuery<TResult, TKey extends string, TError = Error>(
queryKey: TKey | false | null | undefined,
queryFn: QueryFunction<TResult, [TKey]>,
config?: QueryOptions<TResult, TError>
): PaginatedQueryResult<TResult, TError>
export function usePaginatedQuery<
TResult,
TKey extends AnyQueryKey,
TError = Error
>(
queryKey: TKey | false | null | undefined,
queryFn: QueryFunction<TResult, TKey>,
config?: QueryOptions<TResult, TError>
): PaginatedQueryResult<TResult, TError>
export function usePaginatedQuery<TResult, TKey extends string, TError = Error>(
queryKey: TKey | false | null | undefined,
queryFn: QueryFunction<TResult, [TKey]>,
config?: QueryOptions<TResult, TError>
): PaginatedQueryResult<TResult, TError>
// useInfiniteQuery
export type InfiniteQueryKey<T> = T | false | null | undefined
export function useInfiniteQuery<
TResult,
TKey extends AnyQueryKey,
TMoreVariable,
TError = Error
>({
queryKey,
queryFn,
config,
}: {
queryKey: InfiniteQueryKey<TKey>
queryFn?: InfiniteQueryFunction<TResult, TKey, TMoreVariable>
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
}): InfiniteQueryResult<TResult, TMoreVariable, TError>
export function useInfiniteQuery<
TResult,
TSingleKey extends string,
TMoreVariable,
TError = Error
>({
queryKey,
queryFn,
config,
}: {
queryKey: InfiniteQueryKey<TSingleKey>
queryFn?: InfiniteQueryFunction<TResult, [TSingleKey], TMoreVariable>
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
}): InfiniteQueryResult<TResult, TMoreVariable, TError>
export function useInfiniteQuery<
TResult,
TKey extends AnyQueryKey,
TMoreVariable,
TError = Error
>(
queryKey: InfiniteQueryKey<TKey>,
queryFn: InfiniteQueryFunction<TResult, TKey, TMoreVariable>,
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
): InfiniteQueryResult<TResult, TMoreVariable, TError>
export function useInfiniteQuery<
TResult,
TKey extends string,
TMoreVariable,
TError = Error
>(
queryKey: InfiniteQueryKey<TKey>,
queryFn: InfiniteQueryFunction<TResult, [TKey], TMoreVariable>,
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
): InfiniteQueryResult<TResult, TMoreVariable, TError>
export function useInfiniteQuery<
TResult,
TKey extends AnyQueryKey,
TMoreVariable,
TError = Error
>(
queryKey: InfiniteQueryKey<TKey>,
queryFn: InfiniteQueryFunction<TResult, TKey, TMoreVariable>,
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
): InfiniteQueryResult<TResult, TMoreVariable, TError>
export function useInfiniteQuery<
TResult,
TKey extends string,
TMoreVariable,
TError = Error
>(
queryKey: InfiniteQueryKey<TKey>,
queryFn: InfiniteQueryFunction<TResult, [TKey], TMoreVariable>,
config?: InfiniteQueryOptions<TResult, TMoreVariable, TError>
): InfiniteQueryResult<TResult, TMoreVariable, TError>
export type DefinedQueryKeyPart =
| string
| object
| boolean
| number
| readonly QueryKeyPart[]
export type QueryKeyPart =
| string
| object
| boolean
| number
| readonly QueryKeyPart[]
| null
| undefined
export type AnyQueryKey = readonly [DefinedQueryKeyPart, ...QueryKeyPart[]] // this forces the key to be inferred as a tuple
export type QueryFunction<TResult, TKey extends AnyQueryKey> = (
...key: Readonly<TKey>
) => Promise<TResult>
export type InfiniteQueryFunction<
TResult,
TKey extends AnyQueryKey,
TMoreVariable
> = (
...keysAndMore: _.List.Append<TKey, TMoreVariable> | TKey
) => Promise<TResult>
export interface BaseSharedOptions {
suspense: boolean
}
export interface BaseQueryOptions<TResult = unknown, TError = Error> {
/**
* Set this to `false` to disable automatic refetching when the query mounts or changes query keys.
* To refetch the query, use the `refetch` method returned from the `useQuery` instance.
*/
enabled?: boolean | unknown
/**
* If `false`, failed queries will not retry by default.
* If `true`, failed queries will retry infinitely., failureCount: num
* If set to an integer number, e.g. 3, failed queries will retry until the failed query count meets that number.
* If set to a function `(failureCount, error) => boolean` failed queries will retry until the function returns false.
*/
retry?: boolean | number | ((failureCount: number, error: TError) => boolean)
retryDelay?: (retryAttempt: number) => number
staleTime?: number
cacheTime?: number
refetchInterval?: false | number
refetchIntervalInBackground?: boolean
refetchOnWindowFocus?: boolean
refetchOnMount?: boolean
onSuccess?: (data: TResult) => void
onError?: (err: TError) => void
onSettled?: (data: TResult | undefined, error: TError | null) => void
isDataEqual?: (oldData: unknown, newData: unknown) => boolean
useErrorBoundary?: boolean
queryFnParamsFilter?: (args: any[]) => any[]
}
export interface QueryOptions<TResult, TError = Error>
extends BaseQueryOptions<TResult, TError> {
suspense?: boolean
initialData?: TResult | (() => TResult | undefined)
initialStale?: boolean | (() => boolean | undefined)
}
export interface PrefetchQueryOptions {
force?: boolean
throwOnError?: boolean
}
export interface SetQueryDataQueryOptions<TResult, TError = Error>
extends QueryOptions<TResult, TError> {
exact?: boolean
}
export interface InfiniteQueryOptions<TResult, TMoreVariable, TError = Error>
extends QueryOptions<TResult[], TError> {
getFetchMore: (
lastPage: TResult,
allPages: TResult[]
) => TMoreVariable | false
}
export type QueryStatus = 'idle' | 'loading' | 'error' | 'success'
export interface QueryResultBase<TResult, TError = Error> {
status: QueryStatus
error: null | TError
isLoading: boolean
isSuccess: boolean
isError: boolean
isFetching: boolean
isStale: boolean
failureCount: number
canFetchMore?: boolean
markedForGarbageCollection: boolean
query: object
updatedAt: number
refetch: ({ throwOnError }?: { throwOnError?: boolean }) => Promise<TResult>
clear: () => void
}
export interface QueryIdleResult<TResult, TError = Error>
extends QueryResultBase<TResult, TError> {
status: 'idle'
isIdle: true
isLoading: false
isSuccess: false
isError: false
data: undefined
error: null
}
export interface QueryLoadingResult<TResult, TError = Error>
extends QueryResultBase<TResult, TError> {
status: 'loading'
isIdle: false
isLoading: true
isSuccess: false
isError: false
data: TResult | undefined // even when error, data can have stale data
error: TError | null // it still can be error
}
export interface QueryErrorResult<TResult, TError = Error>
extends QueryResultBase<TResult, TError> {
status: 'error'
isIdle: false
isError: true
isLoading: false
isSuccess: false
data: TResult | undefined // even when error, data can have stale data
error: TError
}
export interface QuerySuccessResult<TResult> extends QueryResultBase<TResult> {
status: 'success'
isIdle: false
isSuccess: true
isLoading: false
isError: false
data: TResult
error: null
}
export type QueryResult<TResult, TError = Error> =
| QueryIdleResult<TResult, TError>
| QueryLoadingResult<TResult, TError>
| QueryErrorResult<TResult, TError>
| QuerySuccessResult<TResult>
export interface PaginatedQueryLoadingResult<TResult, TError = Error>
extends QueryResultBase<TResult, TError> {
status: 'loading'
isLoading: true
isError: false
isSuccess: false
resolvedData: undefined | TResult // even when error, data can have stale data
latestData: undefined | TResult // even when error, data can have stale data
error: null | TError // it still can be error
}
export interface PaginatedQueryErrorResult<TResult, TError = Error>
extends QueryResultBase<TResult, TError> {
status: 'error'
isError: true
isLoading: false
isSuccess: false
resolvedData: undefined | TResult // even when error, data can have stale data
latestData: undefined | TResult // even when error, data can have stale data
error: TError
}
export interface PaginatedQuerySuccessResult<TResult>
extends QueryResultBase<TResult> {
status: 'success'
isSuccess: true
isError: false
isLoading: false
resolvedData: TResult
latestData: TResult
error: null
}
export type PaginatedQueryResult<TResult, TError = Error> =
| PaginatedQueryLoadingResult<TResult, TError>
| PaginatedQueryErrorResult<TResult, TError>
| PaginatedQuerySuccessResult<TResult>
export interface InfiniteQueryResult<TResult, TMoreVariable, TError = Error>
extends QueryResultBase<TResult[], TError> {
data: TResult[] | undefined
isFetchingMore: false | 'previous' | 'next'
canFetchMore: boolean | undefined
fetchMore: (
moreVariable?: TMoreVariable | false,
options?: { previous: boolean }
) => Promise<TResult[]> | undefined
}
export function useMutation<
TResult,
TVariables = undefined,
TError = Error,
TSnapshot = unknown
>(
mutationFn: MutationFunction<TResult, TVariables, TError, TSnapshot>,
mutationOptions?: MutationOptions<TResult, TVariables, TError, TSnapshot>
): MutationResultPair<TResult, TVariables, TError>
export type MutationResultPair<TResult, TVariables, TError> = [
MutateFunction<TResult, TVariables, TError>,
MutationResult<TResult, TError>
]
export type MutationFunction<
TResult,
TVariables,
TError = Error,
TSnapshot = unknown
> = (
variables: TVariables,
mutateOptions?: MutateOptions<TResult, TVariables, TError, TSnapshot>
) => Promise<TResult>
export interface MutateOptions<
TResult,
TVariables,
TError = Error,
TSnapshot = unknown
> {
onSuccess?: (data: TResult, variables: TVariables) => Promise<void> | void
onError?: (
error: TError,
variables: TVariables,
snapshotValue: TSnapshot
) => Promise<void> | void
onSettled?: (
data: undefined | TResult,
error: TError | null,
variables: TVariables,
snapshotValue?: TSnapshot
) => Promise<void> | void
throwOnError?: boolean
}
export interface MutationOptions<
TResult,
TVariables,
TError = Error,
TSnapshot = unknown
> extends MutateOptions<TResult, TVariables, TError, TSnapshot> {
onMutate?: (variables: TVariables) => Promise<TSnapshot> | TSnapshot
useErrorBoundary?: boolean
}
export type MutateFunction<
TResult,
TVariables,
TError = Error
> = TVariables extends undefined
? (options?: MutateOptions<TResult, TVariables, TError>) => Promise<TResult>
: (
variables: TVariables,
options?: MutateOptions<TResult, TVariables, TError>
) => Promise<TResult>
export interface MutationResultBase<TResult, TError = Error> {
status: 'idle' | 'loading' | 'error' | 'success'
data: undefined | TResult
error: undefined | null | TError
isIdle: boolean
isLoading: boolean
isSuccess: boolean
isError: boolean
reset: () => void
}
export interface IdleMutationResult<TResult, TError = Error>
extends MutationResultBase<TResult, TError> {
status: 'idle'
data: undefined
error: null
isIdle: true
isLoading: false
isSuccess: false
isError: false
}
export interface LoadingMutationResult<TResult, TError = Error>
extends MutationResultBase<TResult, TError> {
status: 'loading'
data: undefined
error: undefined
isIdle: false
isLoading: true
isSuccess: false
isError: false
}
export interface ErrorMutationResult<TResult, TError = Error>
extends MutationResultBase<TResult, TError> {
status: 'error'
data: undefined
error: TError
isIdle: false
isLoading: false
isSuccess: false
isError: true
}
export interface SuccessMutationResult<TResult>
extends MutationResultBase<TResult> {
status: 'success'
data: TResult
error: undefined
isIdle: false
isLoading: false
isSuccess: true
isError: false
}
export type MutationResult<TResult, TError = Error> =
| IdleMutationResult<TResult, TError>
| LoadingMutationResult<TResult, TError>
| ErrorMutationResult<TResult, TError>
| SuccessMutationResult<TResult>
export interface CachedQueryState<T, TError = Error> {
data?: T
error?: TError | null
failureCount: number
isFetching: boolean
canFetchMore?: boolean
isStale: boolean
status: 'loading' | 'error' | 'success'
updatedAt: number
}
export interface CachedQuery<T, TError = unknown> {
queryKey: AnyQueryKey
queryFn: (...args: any[]) => unknown
config: QueryOptions<unknown, TError>
state: CachedQueryState<T, TError>
setData(
dataOrUpdater:
| unknown
| undefined
| ((oldData: unknown | undefined) => unknown | undefined)
): void
clear(): void
}
export type QueryKey<TKey> = TKey | false | null | undefined
export type QueryKeyOrPredicateFn =
| AnyQueryKey
| string
| boolean
| ((query: CachedQuery<unknown>) => boolean)
export interface QueryCache {
prefetchQuery<TResult, TKey extends AnyQueryKey, TError = Error>(
queryKey: QueryKey<TKey>,
queryFn: QueryFunction<TResult, TKey>,
config?: QueryOptions<TResult, TError>,
prefetch?: PrefetchQueryOptions
): Promise<TResult>
prefetchQuery<TResult, TKey extends string, TError = Error>(
queryKey: QueryKey<TKey>,
queryFn: QueryFunction<TResult, [TKey]>,
config?: QueryOptions<TResult, TError>,
prefetch?: PrefetchQueryOptions
): Promise<TResult>
prefetchQuery<TResult, TKey extends AnyQueryKey, TError = Error>(
queryKey: QueryKey<TKey>,
queryFn: QueryFunction<TResult, TKey>,
prefetch?: PrefetchQueryOptions,
config?: QueryOptions<TResult, TError>
): Promise<TResult>
prefetchQuery<TResult, TKey extends string, TError = Error>(
queryKey: QueryKey<TKey>,
queryFn: QueryFunction<TResult, [TKey]>,
prefetch?: PrefetchQueryOptions,
config?: QueryOptions<TResult, TError>
): Promise<TResult>
prefetchQuery<TResult, TKey extends AnyQueryKey, TError = Error>({
queryKey,
queryFn,
config,
}: {
queryKey: QueryKey<TKey>
queryFn: QueryFunction<TResult, TKey>
config?: QueryOptions<TResult, TError>
prefetch?: PrefetchQueryOptions
}): Promise<TResult>
getQueryData<T = unknown>(key: QueryKeyOrPredicateFn): T | undefined
setQueryData<TResult, TError = Error>(
queryKeyOrPredicateFn: QueryKeyOrPredicateFn,
dataOrUpdater:
| TResult
| undefined
| ((oldData: TResult | undefined) => TResult | undefined),
config?: SetQueryDataQueryOptions<TResult, TError>
): void
invalidateQueries<TResult>(
queryKeyOrPredicateFn: QueryKeyOrPredicateFn,
{
exact,
throwOnError,
refetchActive,
refetchInactive,
}?: {
exact?: boolean
throwOnError?: boolean
refetchActive?: boolean
refetchInactive?: boolean
}
): Promise<TResult>
removeQueries(
queryKeyOrPredicateFn: QueryKeyOrPredicateFn,
{ exact }?: { exact?: boolean }
): void
getQuery(
queryKeyOrPredicateFn: QueryKeyOrPredicateFn
): CachedQuery<unknown> | undefined
getQueries(
queryKeyOrPredicateFn: QueryKeyOrPredicateFn,
{ exact }?: { exact?: boolean }
): Array<CachedQuery<unknown>>
cancelQueries(
queryKeyOrPredicateFn: QueryKeyOrPredicateFn,
{ exact }?: { exact?: boolean }
): void
isFetching: number
subscribe(callback: (queryCache: QueryCache, query?: CachedQuery<unknown>) => void): () => void
clear(options?: { notify?: boolean }): void
resetErrorBoundaries: () => void
}
export const queryCache: QueryCache
export const queryCaches: QueryCache[]
export interface MakeQueryCacheOptions<TError = Error> {
frozen?: boolean
defaultConfig?: ReactQueryProviderConfig<TError>
}
/**
* a factory that creates a new query cache
*/
export function makeQueryCache<TError = Error>(
makeQueryCacheOptions?: MakeQueryCacheOptions<TError>
): QueryCache
/**
* A hook that uses the query cache context
*/
export function useQueryCache(): QueryCache
export const ReactQueryCacheProvider: React.ComponentType<{
queryCache?: QueryCache
}>
/**
* A hook that returns the number of the queries that your application is loading or fetching in the background
* (useful for app-wide loading indicators).
* @returns the number of the queries that your application is currently loading or fetching in the background.
*/
export function useIsFetching(): number
export function ReactQueryConfigProvider<TError = Error>(props: {
config?: ReactQueryProviderConfig<TError>
children?: React.ReactNode
}): React.ReactElement
export interface ReactQueryProviderConfig<TError = Error> {
queries?: BaseQueryOptions<unknown, TError> & {
/** Defaults to the value of `suspense` if not defined otherwise */
useErrorBoundary?: boolean
refetchOnWindowFocus?: boolean
queryFn?: QueryFunction<unknown, AnyQueryKey>
queryKeySerializerFn?: (
queryKey: QueryKeyPart[] | string | false | undefined
) => [string, QueryKeyPart[]] | []
}
shared?: BaseSharedOptions
mutations?: MutationOptions<unknown, unknown, TError>
}
export type ConsoleFunction = (...args: any[]) => void
export interface ConsoleObject {
log: ConsoleFunction
warn: ConsoleFunction
error: ConsoleFunction
}
export function setConsole(consoleObject: ConsoleObject): void
export function deepIncludes(haystack: unknown, needle: unknown): boolean