From 04a1346e89bbce20786fb3ab551d38bebbbfca59 Mon Sep 17 00:00:00 2001 From: Jen Gettings Date: Thu, 2 Jul 2020 19:37:51 -0400 Subject: [PATCH] fix(types): queryCache.getQueries (#688) * fix(types): queryCache.getQueries * fix(formatting) * fix(types): queryCache parameters * fix(formatting) * fix(formatting) Co-authored-by: Jen Gettings --- types/index.d.ts | 32 ++++++++++++++++---------------- types/test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 05eed23645..10d72f4a42 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -540,6 +540,12 @@ export interface CachedQuery { export type QueryKey = TKey | false | null | undefined +export type QueryKeyOrPredicateFn = + | AnyQueryKey + | string + | boolean + | ((query: CachedQuery) => boolean) + export interface QueryCache { prefetchQuery( queryKey: QueryKey, @@ -580,9 +586,9 @@ export interface QueryCache { prefetch?: PrefetchQueryOptions }): Promise - getQueryData(key: AnyQueryKey | string): T | undefined + getQueryData(key: QueryKeyOrPredicateFn): T | undefined setQueryData( - key: AnyQueryKey | string, + queryKeyOrPredicateFn: QueryKeyOrPredicateFn, dataOrUpdater: | TResult | undefined @@ -590,10 +596,7 @@ export interface QueryCache { config?: SetQueryDataQueryOptions ): void invalidateQueries( - queryKeyOrPredicateFn: - | AnyQueryKey - | string - | ((query: CachedQuery) => boolean), + queryKeyOrPredicateFn: QueryKeyOrPredicateFn, { exact, throwOnError, @@ -607,19 +610,16 @@ export interface QueryCache { } ): Promise removeQueries( - queryKeyOrPredicateFn: - | AnyQueryKey - | string - | ((query: CachedQuery) => boolean), + queryKeyOrPredicateFn: QueryKeyOrPredicateFn, { exact }?: { exact?: boolean } ): void - getQuery(queryKey: AnyQueryKey): CachedQuery | undefined - getQueries(queryKey: AnyQueryKey): Array> + getQuery(queryKeyOrPredicateFn: QueryKeyOrPredicateFn): CachedQuery | undefined + getQueries( + queryKeyOrPredicateFn: QueryKeyOrPredicateFn, + { exact }?: { exact?: boolean } + ): Array> cancelQueries( - queryKeyOrPredicateFn: - | AnyQueryKey - | string - | ((query: CachedQuery) => boolean), + queryKeyOrPredicateFn: QueryKeyOrPredicateFn, { exact }?: { exact?: boolean } ): void isFetching: number diff --git a/types/test.ts b/types/test.ts index b870829ea7..78573076a9 100644 --- a/types/test.ts +++ b/types/test.ts @@ -37,6 +37,34 @@ function prefetchQuery() { }) } +function getQueryData() { + queryCache.getQueryData(['queryKey']); + queryCache.getQueryData('queryKey'); + queryCache.getQueryData(true); + queryCache.getQueryData((query) => true); +} + +function setQueryData() { + queryCache.setQueryData(['queryKey'], ['data']); + queryCache.setQueryData('queryKey', ['data']); + queryCache.setQueryData(true, ['data']); + queryCache.setQueryData((query) => true, ['data']); +} + +function getQuery() { + queryCache.getQuery(['queryKey']); + queryCache.getQuery('queryKey'); + queryCache.getQuery(true); + queryCache.getQuery((query) => true); +} + +function getQueries() { + queryCache.getQueries(['queryKey']); + queryCache.getQueries('queryKey'); + queryCache.getQueries(true); + queryCache.getQueries((query) => true); +} + function simpleQuery() { // Query - simple case const querySimple = useQuery('todos', () =>