use{Service}{Action}.query.ts
import { QueryObserverOptions, useQuery } from '@tanstack/react-query';
import { DataShape } from '@/types/api';
type Params = {
id: number
anything: any
};
/**
* @example:
* getServiceAction
*/
export const getServiceAction = async (
params: Params
): Promise<string> => {
const response = await fetch(`endpoint`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
return response.json() as Promise<DataShape>;
};
/**
* @example:
* useServiceActionQuery
*/
const useServiceActionQuery = (
params: Params,
options: QueryObserverOptions<DataShape['data']>
) => {
return useQuery(
['service-action', params.id],
() => getServiceAction(params),
{
enabled: !!params.id,
...options,
}
);
};
export default useServiceActionQuery;