Files
Checkmate/client/src/Hooks/UseApi.ts
T

34 lines
730 B
TypeScript
Raw Normal View History

2026-01-23 17:38:30 +00:00
import useSWR from "swr";
import type { SWRConfiguration } from "swr";
import type { AxiosRequestConfig } from "axios";
2026-01-23 18:14:16 +00:00
import { get } from "@/Utils/ApiClient";
2026-01-23 17:38:30 +00:00
interface ApiResponse<T> {
success: boolean;
msg: string;
data: T;
}
const fetcher = async <T>(url: string, config?: AxiosRequestConfig) => {
2026-01-23 18:14:16 +00:00
const res = await get<ApiResponse<T>>(url, config);
2026-01-23 17:38:30 +00:00
return res.data;
};
export const useGet = <T>(
url: string | null,
axiosConfig?: AxiosRequestConfig,
swrConfig?: SWRConfiguration
2026-01-23 18:14:16 +00:00
) => {
2026-01-23 17:38:30 +00:00
const { data, error, isLoading, mutate } = useSWR<ApiResponse<T>>(
url,
(url: string) => fetcher<T>(url, axiosConfig),
swrConfig
);
return {
2026-01-23 18:14:16 +00:00
data: data?.data ?? null,
2026-01-23 17:38:30 +00:00
isLoading,
error,
refetch: mutate,
};
};