import { useState } from "react"; import useSWR from "swr"; import type { SWRConfiguration } from "swr"; import type { AxiosRequestConfig } from "axios"; import { get, post } from "@/Utils/ApiClient"; // your axios wrapper // Generic fetcher for GET requests const fetcher = async (url: string, config?: AxiosRequestConfig) => { const res = await get(url, config); return res.data; }; export const useGet = ( url: string, axiosConfig?: AxiosRequestConfig, swrConfig?: SWRConfiguration ) => { const { data, error, isLoading, mutate } = useSWR( url, (url) => fetcher(url, axiosConfig), swrConfig ); return { response: data ?? null, loading: isLoading, error: error?.message ?? null, refetch: mutate, }; }; export const usePost = (endpoint: string) => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const postFn = async (body: B, config?: AxiosRequestConfig): Promise => { setLoading(true); setError(null); try { const res = await post(endpoint, body, config); return res.data; } catch (err: any) { setError(err?.message ?? "Unknown error"); return null; } finally { setLoading(false); } }; return { post: postFn, loading, error }; };