web-humas-fe/services/http-config/http-interceptor-services.ts

80 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-04-23 05:17:21 +00:00
import { useRouter } from "next/navigation";
import Cookies from "js-cookie";
import axiosInterceptorInstance from "./axios-interceptor-service";
2024-04-23 05:17:21 +00:00
export async function httpGetInterceptor(pathUrl: any) {
const response = await axiosInterceptorInstance
.get(pathUrl)
.catch((error) => error.response);
console.log("Response interceptor : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else if (response?.status == 401) {
Cookies.set("is_logout", "true");
window.location.href = "/";
return {
error: true,
};
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
export async function httpPostInterceptor(
pathUrl: any,
data: any,
headers?: any
) {
2024-04-23 05:17:21 +00:00
const response = await axiosInterceptorInstance
.post(pathUrl, data, { headers })
.catch((error) => error.response);
console.log("Response interceptor : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else if (response?.status == 401) {
Cookies.set("is_logout", "true");
window.location.href = "/";
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}
export async function httpDeleteInterceptor(pathUrl: any) {
const response = await axiosInterceptorInstance
.delete(pathUrl)
.catch((error) => error.response);
console.log("Response interceptor : ", response);
if (response?.status == 200 || response?.status == 201) {
return {
error: false,
message: "success",
data: response?.data,
};
} else if (response?.status == 401) {
Cookies.set("is_logout", "true");
window.location.href = "/";
} else {
return {
error: true,
message: response?.data?.message || response?.data || null,
data: null,
};
}
}