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

216 lines
5.2 KiB
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-30 19:41:41 +00:00
import { get, patch, post, put, deleteOp } from "@/Utils/ApiClient";
2026-01-23 23:54:58 +00:00
import { useState } from "react";
import { useToast } from "@/Hooks/UseToast";
2026-02-28 01:55:41 +00:00
import { logger } from "@/Utils/logger";
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>(
2026-02-25 19:34:24 +00:00
url: string | null | undefined,
2026-01-23 17:38:30 +00:00
axiosConfig?: AxiosRequestConfig,
swrConfig?: SWRConfiguration
2026-01-23 18:14:16 +00:00
) => {
2026-01-28 18:03:33 +00:00
const { data, error, isLoading, isValidating, mutate } = useSWR<ApiResponse<T>>(
2026-01-23 17:38:30 +00:00
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,
2026-01-28 18:03:33 +00:00
isValidating,
2026-01-23 17:38:30 +00:00
error,
refetch: mutate,
};
};
2026-01-23 23:54:58 +00:00
export const usePost = <B = any, R = any>() => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError, toastSuccess } = useToast();
const postFn = async (
endpoint: string,
body: B,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await post<ApiResponse<R>>(endpoint, body, {
...config,
headers: {
...config?.headers,
},
});
toastSuccess(res.data?.msg || "Operation successful");
return res.data;
} catch (err: any) {
const errMsg = err?.response?.data?.msg || err.message || "An error occurred";
2026-02-28 01:55:41 +00:00
logger.error("POST request failed", err, { endpoint, body });
2026-01-23 23:54:58 +00:00
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { post: postFn, loading, error };
};
export const usePatch = <B = any, R = any>() => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError, toastSuccess } = useToast();
const patchFn = async (
endpoint: string,
body?: B,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await patch<ApiResponse<R>>(endpoint, body, {
...config,
headers: {
...config?.headers,
},
});
toastSuccess(res.data?.msg || "Operation successful");
return res.data;
} catch (err: any) {
2026-02-28 01:55:41 +00:00
logger.error("PATCH request failed", err, { endpoint, body });
2026-01-23 23:54:58 +00:00
const errMsg = err?.response?.data?.msg || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { patch: patchFn, loading, error };
};
2026-01-26 19:36:29 +00:00
2026-01-30 19:41:41 +00:00
export const usePut = <B = any, R = any>() => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError, toastSuccess } = useToast();
const putFn = async (
endpoint: string,
body?: B,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await put<ApiResponse<R>>(endpoint, body, {
...config,
headers: {
...config?.headers,
},
});
toastSuccess(res.data?.msg || "Operation successful");
return res.data;
} catch (err: any) {
2026-02-28 01:55:41 +00:00
logger.error("PUT request failed", err, { endpoint, body });
2026-01-30 19:41:41 +00:00
const errMsg = err?.response?.data?.msg || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { put: putFn, loading, error };
};
2026-01-26 19:36:29 +00:00
export const useDelete = <R = any>() => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError, toastSuccess } = useToast();
const deleteFn = async (
endpoint: string,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await deleteOp<ApiResponse<R>>(endpoint, {
...config,
headers: {
...config?.headers,
},
});
toastSuccess(res.data?.msg || "Operation successful");
return res.data;
} catch (err: any) {
2026-02-28 01:55:41 +00:00
logger.error("DELETE request failed", err, { endpoint });
2026-01-26 19:36:29 +00:00
const errMsg = err?.response?.data?.message || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { deleteFn, loading, error };
};
2026-02-09 23:16:14 +00:00
export const useLazyGet = <R = any>() => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { toastError } = useToast();
const getFn = async (
endpoint: string,
config?: AxiosRequestConfig
): Promise<ApiResponse<R> | null> => {
setLoading(true);
setError(null);
try {
const res = await get<ApiResponse<R>>(endpoint, {
...config,
headers: {
...config?.headers,
},
});
return res.data;
} catch (err: any) {
2026-02-28 01:55:41 +00:00
logger.error("GET request failed", err, { endpoint });
2026-02-09 23:16:14 +00:00
const errMsg = err?.response?.data?.msg || err.message || "An error occurred";
toastError(errMsg);
setError(errMsg);
return null;
} finally {
setLoading(false);
}
};
return { get: getFn, loading, error };
};