2024-12-04 17:28:40 +00:00
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import axiosInterceptorInstance from "./axios-interceptor-instance";
|
|
|
|
|
import Cookies from "js-cookie";
|
2025-01-10 03:30:32 +00:00
|
|
|
import { getCsrfToken } from "../auth";
|
2025-04-07 16:37:19 +00:00
|
|
|
import axiosBaseInstance from "./axios-base-instance";
|
|
|
|
|
import axiosInstanceJson from "./axiosInstanceJson";
|
2024-12-04 17:28:40 +00:00
|
|
|
|
2025-05-06 06:21:11 +00:00
|
|
|
export async function httpGetInterceptorForMetadata(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) {
|
|
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || response?.data || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 17:28:40 +00:00
|
|
|
export async function httpGetInterceptor(pathUrl: any) {
|
2025-05-06 06:21:11 +00:00
|
|
|
const pathname = window?.location.pathname;
|
2024-12-04 17:28:40 +00:00
|
|
|
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) {
|
2025-01-05 00:12:17 +00:00
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
2025-01-15 19:02:28 +00:00
|
|
|
if (
|
|
|
|
|
pathname?.includes("/contributor/") ||
|
|
|
|
|
pathname?.includes("/admin/") ||
|
|
|
|
|
pathname?.includes("/supervisor/")
|
|
|
|
|
) {
|
2025-01-06 03:53:23 +00:00
|
|
|
window.location.href = "/";
|
|
|
|
|
}
|
2024-12-04 17:28:40 +00:00
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || response?.data || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function httpPostInterceptor(
|
|
|
|
|
pathUrl: any,
|
2025-01-03 21:18:22 +00:00
|
|
|
data?: any,
|
2024-12-04 17:28:40 +00:00
|
|
|
headers?: any
|
|
|
|
|
) {
|
2025-01-10 03:30:32 +00:00
|
|
|
const resCsrf = await getCsrfToken();
|
|
|
|
|
const csrfToken = resCsrf?.data?.token;
|
|
|
|
|
|
|
|
|
|
const defaultHeaders = {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
};
|
|
|
|
|
const mergedHeaders = {
|
|
|
|
|
...defaultHeaders,
|
|
|
|
|
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
|
2025-07-08 04:41:50 +00:00
|
|
|
...headers,
|
2025-01-10 03:30:32 +00:00
|
|
|
};
|
|
|
|
|
|
2024-12-04 17:28:40 +00:00
|
|
|
const response = await axiosInterceptorInstance
|
2025-01-10 03:30:32 +00:00
|
|
|
.post(pathUrl, data, { headers: mergedHeaders })
|
2024-12-04 17:28:40 +00:00
|
|
|
.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) {
|
2025-01-05 00:12:17 +00:00
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
|
|
|
|
window.location.href = "/";
|
2024-12-04 17:28:40 +00:00
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || response?.data || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function httpPutInterceptor(
|
|
|
|
|
pathUrl: any,
|
|
|
|
|
data: any,
|
|
|
|
|
headers?: any
|
|
|
|
|
) {
|
2025-01-10 03:55:23 +00:00
|
|
|
const resCsrf = await getCsrfToken();
|
|
|
|
|
const csrfToken = resCsrf?.data?.token;
|
|
|
|
|
|
|
|
|
|
const defaultHeaders = {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
};
|
|
|
|
|
const mergedHeaders = {
|
|
|
|
|
...defaultHeaders,
|
|
|
|
|
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
|
2025-07-08 04:41:50 +00:00
|
|
|
...headers,
|
2025-01-10 03:55:23 +00:00
|
|
|
};
|
|
|
|
|
|
2024-12-04 17:28:40 +00:00
|
|
|
const response = await axiosInterceptorInstance
|
2025-01-10 03:55:23 +00:00
|
|
|
.put(pathUrl, data, { headers: mergedHeaders })
|
2024-12-04 17:28:40 +00:00
|
|
|
.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) {
|
2025-01-05 00:12:17 +00:00
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
|
|
|
|
window.location.href = "/";
|
2024-12-04 17:28:40 +00:00
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || response?.data || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 19:02:28 +00:00
|
|
|
export async function httpDeleteInterceptor(pathUrl: any, data?: any) {
|
2025-01-10 03:55:23 +00:00
|
|
|
const resCsrf = await getCsrfToken();
|
|
|
|
|
const csrfToken = resCsrf?.data?.token;
|
|
|
|
|
|
|
|
|
|
const defaultHeaders = {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
};
|
|
|
|
|
const mergedHeaders = {
|
|
|
|
|
...defaultHeaders,
|
|
|
|
|
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-04 17:28:40 +00:00
|
|
|
const response = await axiosInterceptorInstance
|
2025-01-15 19:02:28 +00:00
|
|
|
.delete(pathUrl, { headers: mergedHeaders, data })
|
2024-12-04 17:28:40 +00:00
|
|
|
.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) {
|
2025-01-05 00:12:17 +00:00
|
|
|
Object.keys(Cookies.get()).forEach((cookieName) => {
|
|
|
|
|
Cookies.remove(cookieName);
|
|
|
|
|
});
|
|
|
|
|
window.location.href = "/";
|
2024-12-04 17:28:40 +00:00
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || response?.data || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function httpGetInterceptorWithToken(pathUrl: any, headers?: any) {
|
|
|
|
|
const response = await axiosInterceptorInstance
|
|
|
|
|
.get(pathUrl, 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 {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || response?.data || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-07 16:37:19 +00:00
|
|
|
|
2025-04-14 06:06:56 +00:00
|
|
|
export async function httpGetArrayBuffer(pathUrl: any, headers: any) {
|
|
|
|
|
const response = await axiosInterceptorInstance
|
2025-05-06 06:21:11 +00:00
|
|
|
.get(pathUrl, { headers, responseType: "arraybuffer" })
|
2025-04-14 06:06:56 +00:00
|
|
|
.catch((error) => error.response);
|
|
|
|
|
console.log("Response base svc : ", response);
|
|
|
|
|
if (response?.status == "200") {
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message || null,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 06:55:30 +00:00
|
|
|
export async function postAPIWithJson(url: any, data: any, token: any) {
|
|
|
|
|
const headers = {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
};
|
2025-04-07 16:37:19 +00:00
|
|
|
const response = await axiosInstanceJson
|
|
|
|
|
.post(url, data, { headers })
|
2025-04-09 06:55:30 +00:00
|
|
|
.catch((error) => error.response);
|
2025-04-07 16:37:19 +00:00
|
|
|
if (response?.status > 300) {
|
|
|
|
|
return {
|
|
|
|
|
error: true,
|
|
|
|
|
message: response?.data?.message,
|
|
|
|
|
data: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
error: false,
|
|
|
|
|
message: "success",
|
|
|
|
|
data: response?.data,
|
|
|
|
|
};
|
|
|
|
|
}
|