// Type definitions for react-query 1.1 // Project: https://github.com/tannerlinsley/react-query // Definitions by: Lukasz Fiszer // Jace Hensley // Matteo Frana // Igor Oleinikov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // Minimum TypeScript Version: 3.7 import * as React from 'react'; import * as _ from 'ts-toolbelt'; // overloaded useQuery function export function useQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, config?: QueryOptions, ): QueryResult; export function useQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, config?: QueryOptions, ): QueryResult; export function useQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, config?: QueryOptions, ): QueryResult; export function useQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, config?: QueryOptions, ): QueryResult; export function useQuery({ queryKey, variables, queryFn, config, }: { queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined); variables?: TVariables; queryFn: QueryFunctionWithVariables; config?: QueryOptions; }): QueryResult; // usePaginatedQuery export function usePaginatedQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, config?: QueryOptions, ): PaginatedQueryResult; export function usePaginatedQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, config?: QueryOptions, ): PaginatedQueryResult; export function usePaginatedQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, config?: QueryOptions, ): PaginatedQueryResult; export function usePaginatedQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, config?: QueryOptions, ): PaginatedQueryResult; export function usePaginatedQuery({ queryKey, variables, queryFn, config, }: { queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined); variables?: TVariables; queryFn: QueryFunctionWithVariables; config?: QueryOptions; }): PaginatedQueryResult; // useInfiniteQuery export function useInfiniteQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: InfiniteQueryFunction, config?: InfiniteQueryOptions, ): InfiniteQueryResult; export function useInfiniteQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: InfiniteQueryFunction, config?: InfiniteQueryOptions, ): InfiniteQueryResult; export function useInfiniteQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: InfiniteQueryFunctionWithVariables, config?: InfiniteQueryOptions, ): InfiniteQueryResult; export function useInfiniteQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: InfiniteQueryFunctionWithVariables, config?: InfiniteQueryOptions, ): InfiniteQueryResult; export function useInfiniteQuery< TResult, TKey extends AnyQueryKey, TMoreVariable, TVariables extends AnyVariables = [] >({ queryKey, variables, queryFn, config, }: { queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined); variables?: TVariables; queryFn: InfiniteQueryFunctionWithVariables; config?: InfiniteQueryOptions; }): InfiniteQueryResult; export type QueryKeyPart = string | object | boolean | number | null | readonly QueryKeyPart[] | null | undefined; export type AnyQueryKey = readonly [string, ...QueryKeyPart[]]; // this forces the key to be inferred as a tuple export type AnyVariables = readonly [] | readonly [any, ...any[]]; // this forces the variables to be inferred as a tuple export type QueryFunction = (...key: TKey) => Promise; export type QueryFunctionWithVariables = ( ...key: _.List.Concat ) => Promise; export type InfiniteQueryFunction = ( ...keysAndMore: _.List.Append | TKey ) => Promise; export type InfiniteQueryFunctionWithVariables< TResult, TKey extends AnyQueryKey, TVariables extends AnyVariables, TMoreVariable > = ( ...keysAndVariablesAndMore: | _.List.Append<_.List.Concat, TMoreVariable> | _.List.Concat ) => Promise; export interface BaseQueryOptions { /** * Set this to `true` 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. */ manual?: boolean; /** * If `false`, failed queries will not retry by default. * If `true`, failed queries will retry infinitely. * If set to an integer number, e.g. 3, failed queries will retry until the failed query count meets that number. */ retry?: boolean | number; retryDelay?: (retryAttempt: number) => number; staleTime?: number; cacheTime?: number; refetchInterval?: false | number; refetchIntervalInBackground?: boolean; refetchOnWindowFocus?: boolean; refetchOnMount?: boolean; onError?: (err: unknown) => void; suspense?: boolean; } export interface QueryOptions extends BaseQueryOptions { onSuccess?: (data: TResult) => void; onSettled?: (data: TResult | undefined, error: unknown | null) => void; initialData?: TResult; } export interface InfiniteQueryOptions extends QueryOptions { getFetchMore: (lastPage: TResult, allPages: TResult[]) => TMoreVariable | false; } export interface QueryResultBase { status: 'loading' | 'error' | 'success'; error: null | unknown; isFetching: boolean; failureCount: number; refetch: ({ force, throwOnError }?: { force?: boolean; throwOnError?: boolean }) => Promise; } export interface QueryLoadingResult extends QueryResultBase { status: 'loading'; data: TResult | undefined; // even when error, data can have stale data error: unknown | null; // it still can be error } export interface QueryErrorResult extends QueryResultBase { status: 'error'; data: TResult | undefined; // even when error, data can have stale data error: unknown; } export interface QuerySuccessResult extends QueryResultBase { status: 'success'; data: TResult; error: null; } export type QueryResult = | QueryLoadingResult | QueryErrorResult | QuerySuccessResult; export interface PaginatedQueryLoadingResult extends QueryResultBase { status: 'loading'; resolvedData: undefined | TResult; // even when error, data can have stale data latestData: undefined | TResult; // even when error, data can have stale data error: unknown | null; // it still can be error } export interface PaginatedQueryErrorResult extends QueryResultBase { status: 'error'; resolvedData: undefined | TResult; // even when error, data can have stale data latestData: undefined | TResult; // even when error, data can have stale data error: unknown; } export interface PaginatedQuerySuccessResult extends QueryResultBase { status: 'success'; resolvedData: TResult; latestData: TResult; error: null; } export type PaginatedQueryResult = | PaginatedQueryLoadingResult | PaginatedQueryErrorResult | PaginatedQuerySuccessResult; export interface InfiniteQueryResult extends QueryResultBase { data: TResult[]; isFetchingMore: boolean; fetchMore: (moreVariable?: TMoreVariable | false) => Promise | undefined; } export function useMutation( mutationFn: MutationFunction, mutationOptions?: MutationOptions, ): [MutateFunction, MutationResult]; export type MutationFunction = (variables: TVariables) => Promise; export interface MutateOptions { onSuccess?: (data: TResult, variables: TVariables) => Promise | void; onError?: (error: unknown, variables: TVariables, snapshotValue: unknown) => Promise | void; onSettled?: ( data: undefined | TResult, error: unknown | null, variables: TVariables, snapshotValue?: unknown, ) => Promise | void; throwOnError?: boolean; } export interface MutationOptions extends MutateOptions { onMutate?: (variables: TVariables) => Promise | unknown; useErrorBoundary?: boolean; } export type MutateFunction = undefined extends TVariables ? (variables?: TVariables, options?: MutateOptions) => Promise : (variables: TVariables, options?: MutateOptions) => Promise; export interface MutationResultBase { status: 'idle' | 'loading' | 'error' | 'success'; data: undefined | TResult; error: null | unknown; promise: Promise; reset: () => void; } export interface IdleMutationResult extends MutationResultBase { status: 'idle'; data: undefined; error: null; } export interface LoadingMutationResult extends MutationResultBase { status: 'loading'; data: undefined; error: undefined; } export interface ErrorMutationResult extends MutationResultBase { status: 'error'; data: undefined; error: unknown; } export interface SuccessMutationResult extends MutationResultBase { status: 'success'; data: TResult; error: undefined; } export type MutationResult = | IdleMutationResult | LoadingMutationResult | ErrorMutationResult | SuccessMutationResult; export interface CachedQuery { queryKey: AnyQueryKey; queryVariables: AnyVariables; queryFn: (...args: any[]) => unknown; config: QueryOptions; state: unknown; setData(dataOrUpdater: unknown | ((oldData: unknown | undefined) => unknown)): void; } export interface QueryCache { prefetchQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, config?: QueryOptions, ): Promise; prefetchQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), queryFn: QueryFunction, config?: QueryOptions, ): Promise; prefetchQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, config?: QueryOptions, ): Promise; prefetchQuery( queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined), variables: TVariables, queryFn: QueryFunctionWithVariables, config?: QueryOptions, ): Promise; prefetchQuery({ queryKey, variables, queryFn, config, }: { queryKey: TKey | false | null | undefined | (() => TKey | false | null | undefined); variables?: TVariables; queryFn: QueryFunctionWithVariables; config?: QueryOptions; }): Promise; getQueryData(key: AnyQueryKey | string): unknown | undefined; setQueryData(key: AnyQueryKey | string, dataOrUpdater: unknown | ((oldData: unknown | undefined) => unknown)): void; refetchQueries( queryKeyOrPredicateFn: AnyQueryKey | string | ((query: CachedQuery) => boolean), { exact, throwOnError, force }?: { exact?: boolean; throwOnError?: boolean; force?: boolean }, ): Promise; removeQueries( queryKeyOrPredicateFn: AnyQueryKey | string | ((query: CachedQuery) => boolean), { exact }?: { exact?: boolean }, ): Promise; getQuery(queryKey: AnyQueryKey): CachedQuery | undefined; getQueries(queryKey: AnyQueryKey): CachedQuery[]; isFetching: number; subscribe(callback: (queryCache: QueryCache) => void): () => void; clear(): CachedQuery[]; } export const queryCache: QueryCache; /** * A hook that returns the number of the quiries that your application is loading or fetching in the background * (useful for app-wide loading indicators). * @returns the number of the quiries that your application is currently loading or fetching in the background. */ export function useIsFetching(): number; export const ReactQueryConfigProvider: React.ComponentType<{ config?: ReactQueryProviderConfig; }>; export interface ReactQueryProviderConfig extends BaseQueryOptions { /** Defaults to the value of `suspense` if not defined otherwise */ useErrorBoundary?: boolean; throwOnError?: boolean; refetchAllOnWindowFocus?: boolean; queryKeySerializerFn?: ( queryKey: QueryKeyPart[] | string | false | undefined | (() => QueryKeyPart[] | string | false | undefined), ) => [string, QueryKeyPart[]] | []; onMutate?: (variables: unknown) => Promise | unknown; onSuccess?: (data: unknown, variables?: unknown) => void; onError?: (err: unknown, snapshotValue?: unknown) => void; onSettled?: (data: unknown | undefined, error: unknown | null, snapshotValue?: unknown) => void; } export type ConsoleFunction = (...args: any[]) => void; export interface ConsoleObject { log: ConsoleFunction; warn: ConsoleFunction; error: ConsoleFunction; } export function setConsole(consoleObject: ConsoleObject): void;