From 1b745cc164cb2db26b14f8e7971042627ca92714 Mon Sep 17 00:00:00 2001 From: Nima Shoghi Date: Mon, 19 Jul 2021 16:10:05 +0000 Subject: [PATCH] Updated useQuery types to support "select" prop --- packages/core/src/use-query-hooks.ts | 105 +++++++++++++++++++-------- 1 file changed, 76 insertions(+), 29 deletions(-) diff --git a/packages/core/src/use-query-hooks.ts b/packages/core/src/use-query-hooks.ts index 4e7d9fbed0..fd4982d30e 100644 --- a/packages/core/src/use-query-hooks.ts +++ b/packages/core/src/use-query-hooks.ts @@ -29,22 +29,38 @@ type QueryNonLazyOptions = // ------------------------- // useQuery // ------------------------- -type RestQueryResult = Omit, "data"> & QueryCacheFunctions +type RestQueryResult = Omit, "data"> & + QueryCacheFunctions -export function useQuery>( +export function useQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, params: FirstParam, - options?: UseQueryOptions & QueryNonLazyOptions, -): [TResult, RestQueryResult] -export function useQuery>( + options?: UseQueryOptions & QueryNonLazyOptions, +): [TSelectedData, RestQueryResult] +export function useQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, params: FirstParam, - options: UseQueryOptions & QueryLazyOptions, -): [TResult | undefined, RestQueryResult] -export function useQuery>( + options: UseQueryOptions & QueryLazyOptions, +): [TSelectedData | undefined, RestQueryResult] +export function useQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, params: FirstParam, - options: UseQueryOptions = {}, + options: UseQueryOptions = {}, ) { if (typeof queryFn === "undefined") { throw new Error("useQuery is missing the first argument - it must be a query function") @@ -94,23 +110,38 @@ export function useQuery>( // ------------------------- // usePaginatedQuery // ------------------------- -type RestPaginatedResult = Omit, "data"> & +type RestPaginatedResult = Omit, "data"> & QueryCacheFunctions -export function usePaginatedQuery>( +export function usePaginatedQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, params: FirstParam, - options?: UseQueryOptions & QueryNonLazyOptions, -): [TResult, RestPaginatedResult] -export function usePaginatedQuery>( + options?: UseQueryOptions & QueryNonLazyOptions, +): [TSelectedData, RestPaginatedResult] +export function usePaginatedQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, params: FirstParam, - options: UseQueryOptions & QueryLazyOptions, -): [TResult | undefined, RestPaginatedResult] -export function usePaginatedQuery>( + options: UseQueryOptions & QueryLazyOptions, +): [TSelectedData | undefined, RestPaginatedResult] +export function usePaginatedQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, params: FirstParam, - options: UseQueryOptions = {}, + options: UseQueryOptions = {}, ) { if (typeof queryFn === "undefined") { throw new Error("usePaginatedQuery is missing the first argument - it must be a query function") @@ -148,31 +179,47 @@ export function usePaginatedQuery - extends Omit, "data">, +interface RestInfiniteResult + extends Omit, "data">, QueryCacheFunctions { pageParams: any } -interface InfiniteQueryConfig extends UseInfiniteQueryOptions { +interface InfiniteQueryConfig + extends UseInfiniteQueryOptions { // getPreviousPageParam?: (lastPage: TResult, allPages: TResult[]) => TGetPageParamResult // getNextPageParam?: (lastPage: TResult, allPages: TResult[]) => TGetPageParamResult } -export function useInfiniteQuery>( +export function useInfiniteQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, getQueryParams: (pageParam: any) => FirstParam, - options: InfiniteQueryConfig & QueryNonLazyOptions, -): [TResult[], RestInfiniteResult] -export function useInfiniteQuery>( + options: InfiniteQueryConfig & QueryNonLazyOptions, +): [TSelectedData[], RestInfiniteResult] +export function useInfiniteQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, getQueryParams: (pageParam: any) => FirstParam, - options: InfiniteQueryConfig & QueryLazyOptions, -): [TResult[] | undefined, RestInfiniteResult] -export function useInfiniteQuery>( + options: InfiniteQueryConfig & QueryLazyOptions, +): [TSelectedData[] | undefined, RestInfiniteResult] +export function useInfiniteQuery< + T extends QueryFn, + TResult = PromiseReturnType, + TError = unknown, + TSelectedData = TResult +>( queryFn: T, getQueryParams: (pageParam: any) => FirstParam, - options: InfiniteQueryConfig, + options: InfiniteQueryConfig, ) { if (typeof queryFn === "undefined") { throw new Error("useInfiniteQuery is missing the first argument - it must be a query function")