useQuery,
} from '@tanstack/react-query';
+const isFnWithoutParams = <T>(fn: ((data: any) => T) | (() => T)): fn is () => T => fn.length === 0;
+
export function createQueryHook<
T = unknown,
TQueryData = unknown,
- TError = Error,
+ TError extends Error = Error,
TData = TQueryData,
TQueryKey extends QueryKey = QueryKey,
>(
- fn: (data: T) => UseQueryOptions<TQueryData, TError, TData, TQueryKey>,
-): <SelectType = TQueryData>(
- data: T,
- options?: Omit<
- UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
- 'queryKey' | 'queryFn'
- >,
-) => UseQueryResult<SelectType, TError>;
+ fn:
+ | ((data: T) => UseQueryOptions<TQueryData, TError, TData, TQueryKey>)
+ | (() => UseQueryOptions<TQueryData, TError, TData, TQueryKey>),
+): unknown extends T
+ ? <SelectType = TQueryData>(
+ options?: Omit<
+ UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
+ 'queryKey' | 'queryFn'
+ >,
+ ) => UseQueryResult<SelectType, TError>
+ : <SelectType = TQueryData>(
+ data: T,
+ options?: Omit<
+ UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
+ 'queryKey' | 'queryFn'
+ >,
+ ) => UseQueryResult<SelectType, TError>;
-export function createQueryHook(fn: (data: any) => UseQueryOptions) {
+export function createQueryHook(fn: ((data: any) => UseQueryOptions) | (() => UseQueryOptions)) {
+ if (isFnWithoutParams(fn)) {
+ return (options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>) =>
+ useQuery({ ...fn(), ...options });
+ }
return (data: any, options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>) =>
useQuery({ ...fn(data), ...options });
}
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
- fn: (
- data: T,
- ) => UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>,
-): <SelectType = TData>(
- data: T,
- options?: Omit<
- UseInfiniteQueryOptions<TQueryFnData, TError, SelectType, TQueryData, TQueryKey, TPageParam>,
- 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
- >,
-) => UseInfiniteQueryResult<SelectType, TError>;
+ fn:
+ | ((
+ data: T,
+ ) => UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>)
+ | (() => UseInfiniteQueryOptions<
+ TQueryFnData,
+ TError,
+ TData,
+ TQueryData,
+ TQueryKey,
+ TPageParam
+ >),
+): unknown extends T
+ ? <SelectType = TData>(
+ options?: Omit<
+ UseInfiniteQueryOptions<
+ TQueryFnData,
+ TError,
+ SelectType,
+ TQueryData,
+ TQueryKey,
+ TPageParam
+ >,
+ 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+ >,
+ ) => UseInfiniteQueryResult<SelectType, TError>
+ : <SelectType = TData>(
+ data: T,
+ options?: Omit<
+ UseInfiniteQueryOptions<
+ TQueryFnData,
+ TError,
+ SelectType,
+ TQueryData,
+ TQueryKey,
+ TPageParam
+ >,
+ 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+ >,
+ ) => UseInfiniteQueryResult<SelectType, TError>;
-export function createInfiniteQueryHook(fn: (data: any) => UseInfiniteQueryOptions) {
+export function createInfiniteQueryHook(
+ fn: ((data?: any) => UseInfiniteQueryOptions) | (() => UseInfiniteQueryOptions),
+) {
+ if (isFnWithoutParams(fn)) {
+ return (
+ options?: Omit<
+ UseInfiniteQueryOptions,
+ 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+ >,
+ ) => useInfiniteQuery({ ...fn(), ...options });
+ }
return (
data: any,
options?: Omit<
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { UseQueryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
+import { queryOptions, useMutation, useQueryClient } from '@tanstack/react-query';
import { useLocation } from 'react-router-dom';
import {
deleteProjectAlmBinding,
} from '../api/alm-settings';
import { HttpStatus } from '../helpers/request';
import { AlmKeys, ProjectAlmBindingParams, ProjectAlmBindingResponse } from '../types/alm-settings';
+import { createQueryHook } from './common';
function useProjectKeyFromLocation() {
const location = useLocation();
return id as string;
}
-export function useProjectBindingQuery<T = ProjectAlmBindingResponse>(
- project?: string,
- options?: Omit<
- UseQueryOptions<
- ProjectAlmBindingResponse | null,
- Error,
- T,
- ['devops_integration', string, 'binding']
- >,
- 'queryKey' | 'queryFn'
- >,
-) {
+export const useProjectBindingQuery = createQueryHook((project?: string) => {
const keyFromUrl = useProjectKeyFromLocation();
const projectKey = project ?? keyFromUrl;
- return useQuery({
+ return queryOptions({
queryKey: ['devops_integration', projectKey ?? '_blank_', 'binding'],
queryFn: ({ queryKey: [_, key] }) =>
getProjectAlmBinding(key).catch((e: Response) => {
if (e.status === HttpStatus.NotFound) {
return null;
}
- return e;
+ return e as unknown as ProjectAlmBindingResponse;
}),
staleTime: 60_000,
enabled: projectKey !== null,
- ...options,
});
-}
+});
export function useIsGitHubProjectQuery(project?: string) {
- return useProjectBindingQuery<boolean>(project, {
+ return useProjectBindingQuery(project, {
select: (data) => data?.alm === AlmKeys.GitHub,
});
}
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { UseQueryOptions, useQuery } from '@tanstack/react-query';
+import { queryOptions } from '@tanstack/react-query';
import { getSystemUpgrades } from '../api/system';
+import { createQueryHook } from './common';
-export function useSystemUpgrades<T = Awaited<ReturnType<typeof getSystemUpgrades>>>(
- options?: Omit<
- UseQueryOptions<Awaited<ReturnType<typeof getSystemUpgrades>>, Error, T>,
- 'queryKey' | 'queryFn'
- >,
-) {
- return useQuery({
+export const useSystemUpgrades = createQueryHook(() => {
+ return queryOptions({
queryKey: ['system', 'upgrades'],
queryFn: () => getSystemUpgrades(),
staleTime: Infinity,
- ...options,
});
-}
+});