Skip to content

Commit

Permalink
ref: move type definition of createQueryToolkit (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 authored Apr 30, 2022
1 parent 10ee66f commit a997189
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 93 deletions.
104 changes: 11 additions & 93 deletions src/createQueryToolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,10 @@ import {
} from "react-query";
import { generateKey } from "./internal/generateKey";
import { returnByCondition } from "./internal/returnByCondition";
import {
QueryDefaultOption,
QueryType,
TQueryFunction,
UseInfiniteQueryDefaultOption,
UseQueryDefaultOption,
} from "./types/query";
import {
QueryToolkit,
QueryToolkitInfiniteQueryType,
QueryToolkitQueryType,
} from "./types/queryToolkit";
import { QueryCreator, QueryToolkit } from "./types/queryToolkit";

export function createQueryToolkit(queryClient: QueryClient) {
function createQuery<
TQueryFnArgs extends unknown[],
TQueryFnReturn,
TData = TQueryFnReturn,
>(
queryKey: QueryKey,
queryFn: TQueryFunction<TQueryFnArgs, TQueryFnReturn>,
options?: {
passArgsToQueryKey?: boolean;
queryType?: "query";
defaultOptions?: QueryDefaultOption<TQueryFnReturn, TData> &
UseQueryDefaultOption<TQueryFnReturn, TData>;
},
): Omit<
QueryToolkitQueryType<TQueryFnArgs, TQueryFnReturn, Error, TData>,
"useInfiniteQuery" | "fetchInfiniteQuery" | "prefetchInfiniteQuery"
>;
function createQuery<
TQueryFnArgs extends unknown[],
TQueryFnReturn,
TData = TQueryFnReturn,
>(
queryKey: QueryKey,
queryFn: TQueryFunction<TQueryFnArgs, TQueryFnReturn>,
options?: {
passArgsToQueryKey?: boolean;
queryType?: "infiniteQuery";
defaultOptions?: QueryDefaultOption<TQueryFnReturn, TData> &
UseInfiniteQueryDefaultOption<TQueryFnReturn, TData>;
},
): Omit<
QueryToolkitInfiniteQueryType<TQueryFnArgs, TQueryFnReturn, Error, TData>,
"useQuery" | "fetchQuery" | "prefetchQuery"
>;
function createQuery<
TQueryFnArgs extends unknown[],
TQueryFnReturn,
TData = TQueryFnReturn,
>(
queryKey: QueryKey,
queryFn: TQueryFunction<TQueryFnArgs, TQueryFnReturn>,
options: {
passArgsToQueryKey?: boolean;
queryType?: QueryType;
defaultOptions?: QueryDefaultOption<TQueryFnReturn, TData> &
UseQueryDefaultOption<TQueryFnReturn, TData> &
UseInfiniteQueryDefaultOption<TQueryFnReturn, TData>;
} = {},
) {
export function createQueryToolkit(queryClient: QueryClient): QueryCreator {
return (queryKey, queryFn, options = {}) => {
const {
passArgsToQueryKey = true,
queryType = "query",
Expand All @@ -87,19 +28,14 @@ export function createQueryToolkit(queryClient: QueryClient) {
...(passArgsToQueryKey && args ? args : []),
]);

const handleHooks =
(hook: any) => (args?: TQueryFnArgs, queryOptions?: any) =>
hook(
getKey(queryOptions?.queryKey, args),
queryFn(...((args || []) as TQueryFnArgs)),
{ ...defaultOptions, ...queryOptions },
);
const handleHooks = (hook: any) => (args?: any, queryOptions?: any) =>
hook(getKey(queryOptions?.queryKey, args), queryFn(...(args || [])), {
...defaultOptions,
...queryOptions,
});

const hooks: Partial<
Pick<
QueryToolkit<TQueryFnArgs>,
"useQuery" | "useInfiniteQuery" | "useIsFetching"
>
Pick<QueryToolkit, "useQuery" | "useInfiniteQuery" | "useIsFetching">
> = {
useQuery: returnOnQuery(handleHooks(useQuery)),
useInfiniteQuery: returnOnInfiniteQuery(handleHooks(useInfiniteQuery)),
Expand All @@ -119,7 +55,7 @@ export function createQueryToolkit(queryClient: QueryClient) {
),
);

const handler = new Proxy(hooks, {
return new Proxy(hooks, {
get(target: any, path: keyof QueryToolkit) {
if (target[path]) return target[path];
switch (path) {
Expand All @@ -144,23 +80,5 @@ export function createQueryToolkit(queryClient: QueryClient) {
}
},
});

if (isInfiniteQuery)
return handler as Omit<
QueryToolkitInfiniteQueryType<
TQueryFnArgs,
TQueryFnReturn,
Error,
TData
>,
"useQuery" | "fetchQuery" | "prefetchQuery"
>;

return handler as Omit<
QueryToolkitQueryType<TQueryFnArgs, TQueryFnReturn, Error, TData>,
"useInfiniteQuery" | "fetchInfiniteQuery" | "prefetchInfiniteQuery"
>;
}

return createQuery;
};
}
35 changes: 35 additions & 0 deletions src/types/queryToolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import {
} from "react-query";
import { QueryState } from "react-query/types/core/query";
import { QueryFilters, Updater } from "react-query/types/core/utils";
import {
QueryDefaultOption,
TQueryFunction,
UseInfiniteQueryDefaultOption,
UseQueryDefaultOption,
} from "./query";

interface QueryToolkitBase<
TQueryFnData = unknown,
Expand Down Expand Up @@ -120,3 +126,32 @@ export type QueryToolkit<
> =
| QueryToolkitQueryType<TQueryFnArgs, TQueryFnData, TError, TData>
| QueryToolkitInfiniteQueryType<TQueryFnArgs, TQueryFnData, TError, TData>;

export interface QueryCreator {
<TQueryFnArgs extends unknown[], TQueryFnReturn, TData = TQueryFnReturn>(
queryKey: QueryKey,
queryFn: TQueryFunction<TQueryFnArgs, TQueryFnReturn>,
options?: {
passArgsToQueryKey?: boolean;
queryType?: "query";
defaultOptions?: QueryDefaultOption<TQueryFnReturn, TData> &
UseQueryDefaultOption<TQueryFnReturn, TData>;
},
): Omit<
QueryToolkitQueryType<TQueryFnArgs, TQueryFnReturn, Error, TData>,
"useInfiniteQuery" | "fetchInfiniteQuery" | "prefetchInfiniteQuery"
>;
<TQueryFnArgs extends unknown[], TQueryFnReturn, TData = TQueryFnReturn>(
queryKey: QueryKey,
queryFn: TQueryFunction<TQueryFnArgs, TQueryFnReturn>,
options?: {
passArgsToQueryKey?: boolean;
queryType?: "infiniteQuery";
defaultOptions?: QueryDefaultOption<TQueryFnReturn, TData> &
UseInfiniteQueryDefaultOption<TQueryFnReturn, TData>;
},
): Omit<
QueryToolkitInfiniteQueryType<TQueryFnArgs, TQueryFnReturn, Error, TData>,
"useQuery" | "fetchQuery" | "prefetchQuery"
>;
}

0 comments on commit a997189

Please sign in to comment.