first commit
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
import axios, {
|
||||
AxiosError,
|
||||
AxiosInstance,
|
||||
AxiosResponse,
|
||||
InternalAxiosRequestConfig,
|
||||
} from "axios";
|
||||
|
||||
import { delay } from "@/common/funcs/delay";
|
||||
import { getKeyCert } from "@/common/funcs/optionConvert";
|
||||
import { toastInfo, toastSuccess, toastWarn } from "@/common/funcs/toast";
|
||||
|
||||
import { logout } from "@/redux/reducer/auth";
|
||||
import { setInfoUser } from "@/redux/reducer/user";
|
||||
import { store } from "@/redux/store";
|
||||
|
||||
interface ApiError {
|
||||
code: number;
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface ApiResponse<T = unknown> {
|
||||
error: ApiError;
|
||||
data?: T;
|
||||
items?: T[];
|
||||
pagination?: unknown;
|
||||
}
|
||||
|
||||
interface HttpRequestOptions<T> {
|
||||
http: Promise<ApiResponse<T>>;
|
||||
|
||||
setLoading?: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
|
||||
onError?: () => void;
|
||||
|
||||
showMessageSuccess?: boolean;
|
||||
showMessageFailed?: boolean;
|
||||
|
||||
msgSuccess?: string;
|
||||
|
||||
isDropdown?: boolean;
|
||||
isPagination?: boolean;
|
||||
isData?: boolean;
|
||||
}
|
||||
|
||||
const axiosClient = axios.create({
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
|
||||
baseURL: "/api-proxy",
|
||||
|
||||
timeout: 15000,
|
||||
|
||||
timeoutErrorMessage: "Timeout error request",
|
||||
}) as AxiosInstance & {
|
||||
get<T = unknown>(url: string, config?: any): Promise<ApiResponse<T>>;
|
||||
|
||||
post<T = unknown>(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: any,
|
||||
): Promise<ApiResponse<T>>;
|
||||
|
||||
put<T = unknown>(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: any,
|
||||
): Promise<ApiResponse<T>>;
|
||||
|
||||
delete<T = unknown>(url: string, config?: any): Promise<ApiResponse<T>>;
|
||||
};
|
||||
|
||||
axiosClient.interceptors.request.use(
|
||||
async (
|
||||
config: InternalAxiosRequestConfig,
|
||||
): Promise<InternalAxiosRequestConfig> => {
|
||||
const token = store.getState().auth.token;
|
||||
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
if (!(config.data instanceof FormData)) {
|
||||
config.data = {
|
||||
...getKeyCert(),
|
||||
...(config.data || {}),
|
||||
};
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
);
|
||||
|
||||
axiosClient.interceptors.response.use(
|
||||
(response) => {
|
||||
return response.data;
|
||||
},
|
||||
|
||||
(error: AxiosError<ApiError>) => {
|
||||
return Promise.reject(error.response?.data || error);
|
||||
},
|
||||
);
|
||||
|
||||
export default axiosClient;
|
||||
|
||||
export const httpRequest = async <T>({
|
||||
http,
|
||||
|
||||
setLoading,
|
||||
|
||||
msgSuccess = "Thành công",
|
||||
|
||||
showMessageSuccess = false,
|
||||
showMessageFailed = false,
|
||||
|
||||
onError,
|
||||
|
||||
isDropdown = false,
|
||||
isPagination = false,
|
||||
isData = false,
|
||||
}: HttpRequestOptions<T>) => {
|
||||
try {
|
||||
setLoading?.(true);
|
||||
|
||||
await delay(500);
|
||||
|
||||
const res = await http;
|
||||
|
||||
if (res.error.code === 0) {
|
||||
if (showMessageSuccess) {
|
||||
toastSuccess({
|
||||
msg: msgSuccess || res.error.message,
|
||||
});
|
||||
}
|
||||
|
||||
if (isDropdown) {
|
||||
return res.items || [];
|
||||
}
|
||||
|
||||
if (isPagination) {
|
||||
return {
|
||||
...res.data,
|
||||
items: res.items || [],
|
||||
pagination: res.pagination,
|
||||
};
|
||||
}
|
||||
|
||||
if (isData) {
|
||||
return res;
|
||||
}
|
||||
|
||||
return res.data || true;
|
||||
}
|
||||
|
||||
onError?.();
|
||||
|
||||
throw new Error(res.error.message);
|
||||
} catch (error: unknown) {
|
||||
const err = error as AxiosError<ApiResponse> | Error;
|
||||
|
||||
const errorData = (err as AxiosError<ApiResponse>)?.response?.data;
|
||||
|
||||
if (errorData?.error?.code === 401 || errorData?.error?.code === 403) {
|
||||
store.dispatch(logout());
|
||||
|
||||
store.dispatch(setInfoUser(null));
|
||||
|
||||
if (showMessageFailed) {
|
||||
toastWarn({
|
||||
msg: errorData?.error?.message || "Có lỗi đã xảy ra!",
|
||||
});
|
||||
}
|
||||
} else if (err instanceof Error) {
|
||||
if (err.message === "ERR_NETWORK" || err.message === "ECONNABORTED") {
|
||||
if (showMessageFailed) {
|
||||
toastInfo({
|
||||
msg: "Kiểm tra kết nối internet",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (showMessageFailed) {
|
||||
toastWarn({
|
||||
msg: err.message || "Có lỗi đã xảy ra!",
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (showMessageFailed) {
|
||||
toastWarn({
|
||||
msg: "Có lỗi đã xảy ra!",
|
||||
});
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
setLoading?.(false);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user