|
| 1 | +import { AxiosError, AxiosResponse } from "axios"; |
| 2 | + |
| 3 | +import { axiosInstance } from "@/utils/axiosInstance"; |
| 4 | + |
| 5 | +import { QueryKeys } from "./queryKeys"; |
| 6 | + |
| 7 | +import { ApplicationListResponse } from "@/types/application"; |
| 8 | + |
| 9 | +// UseQueryResult는 useQuery의 반환 타입을 명시할 때 유용합니다. |
| 10 | +import { UseQueryOptions, UseQueryResult, useQuery } from "@tanstack/react-query"; |
| 11 | + |
| 12 | +export const getCompetitorsApplicationList = (): Promise<AxiosResponse<ApplicationListResponse>> => |
| 13 | + axiosInstance.get("/applications"); |
| 14 | + |
| 15 | +// 커스텀 훅의 props 타입 정의를 개선하여 queryKey와 queryFn을 제외시킵니다. |
| 16 | +type UseGetCompetitorsApplicationListOptions = Omit< |
| 17 | + UseQueryOptions< |
| 18 | + AxiosResponse<ApplicationListResponse>, // queryFn이 반환하는 원본 데이터 타입 |
| 19 | + AxiosError<{ message: string }>, // 에러 타입 |
| 20 | + ApplicationListResponse // select를 통해 최종적으로 반환될 데이터 타입 |
| 21 | + >, |
| 22 | + "queryKey" | "queryFn" // 훅 내부에서 지정하므로 props에서는 제외 |
| 23 | +>; |
| 24 | + |
| 25 | +const useGetApplicationsList = ( |
| 26 | + props?: UseGetCompetitorsApplicationListOptions, |
| 27 | +): UseQueryResult<ApplicationListResponse, AxiosError<{ message: string }>> => { |
| 28 | + // 반환 타입 명시 |
| 29 | + return useQuery({ |
| 30 | + queryKey: [QueryKeys.competitorsApplicationList], |
| 31 | + queryFn: getCompetitorsApplicationList, |
| 32 | + staleTime: 1000 * 60 * 5, |
| 33 | + select: (response) => response.data, |
| 34 | + ...props, |
| 35 | + }); |
| 36 | +}; |
| 37 | + |
| 38 | +export default useGetApplicationsList; |
0 commit comments