1- import { QueryKey , UseQueryResult , UseQueryOptions , useQuery as useRQQuery } from '@tanstack/react-query' ;
1+ import { QueryKey , UseQueryOptions , useQuery as useRQQuery , useQueryClient } from '@tanstack/react-query' ;
2+ import { useCallback } from 'react' ;
23
34import { AxiosQueriesType , queries } from 'api/actions' ;
45import { DataForQuery , GetQueryParams } from 'api/types/types' ;
@@ -8,18 +9,25 @@ import { parseQueryKey } from 'utils/parseQueryKey';
89export const useQuery = < Key extends keyof AxiosQueriesType , TError = unknown > (
910 query : Key ,
1011 args : GetQueryParams < Key > ,
11- options ?: UseQueryOptions < DataForQuery < Key > , TError > ,
12+ options ?: Omit < UseQueryOptions < DataForQuery < Key > , TError > , 'queryKey' | 'queryFn' > ,
1213) => {
1314 const { client } = useApiClient ( ) ;
15+ const queryClient = useQueryClient ( ) ;
1416 const queryFn = queries [ query ] ( client ) ;
1517 const queryKey : QueryKey = parseQueryKey ( query , args ) ;
1618
17- const result = useRQQuery (
19+ const result = useRQQuery < DataForQuery < Key > , TError > ( {
1820 queryKey,
19- async ( ) => await queryFn ( args ) ,
21+ queryFn : async ( ) => await queryFn ( args ) ,
2022 // eslint-disable-next-line @typescript-eslint/no-explicit-any
21- options as any ,
22- ) as UseQueryResult < DataForQuery < Key > , TError > ;
23+ ... ( options as any ) ,
24+ } ) ;
2325
24- return { ...result , isLoadingAndEnabled : result . isLoading && result . fetchStatus !== 'idle' } ;
26+ const remove = useCallback ( ( ) => queryClient . removeQueries ( { queryKey } ) , [ queryClient , queryKey ] ) ;
27+
28+ return {
29+ ...result ,
30+ isLoadingAndEnabled : result . isLoading && result . fetchStatus !== 'idle' ,
31+ remove,
32+ } ;
2533} ;
0 commit comments