import axiosInterceptorInstance from "./axios-interceptor-instance"; import Cookies from "js-cookie"; import { getCsrfToken } from "../auth"; import axiosInstanceJson from "./axiosInstanceJson"; 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, }; } } // export async function httpGetInterceptor(pathUrl: any) { // const pathname = window?.location.pathname; // 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); // }); // if ( // pathname?.includes("/contributor/") || // pathname?.includes("/admin/") || // pathname?.includes("/supervisor/") // ) { // window.location.href = "/"; // } // } else { // return { // error: true, // message: response?.data?.message || response?.data || null, // data: null, // }; // } // } export async function httpGetInterceptor(pathUrl: any) { const pathname = window?.location.pathname; 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); }); if ( pathname?.includes("/contributor/") || pathname?.includes("/admin/") || pathname?.includes("/supervisor/") ) { window.location.href = "/"; } } return { error: true, message: response?.data?.message || response?.data || "Unknown error", data: null, }; } // export async function httpPostInterceptor( // pathUrl: any, // data?: any, // headers?: any // ) { // const resCsrf = await getCsrfToken(); // const csrfToken = resCsrf?.data?.token; // const defaultHeaders = { // "Content-Type": "application/json", // }; // const mergedHeaders = { // ...defaultHeaders, // ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), // ...headers, // }; // const response = await axiosInterceptorInstance // .post(pathUrl, data, { headers: mergedHeaders }) // .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); // }); // window.location.href = "/"; // } else { // return { // error: true, // message: response?.data?.message || response?.data || null, // data: null, // }; // } // } export async function httpPostInterceptor( pathUrl: any, data?: any, headers?: any ) { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; const token = Cookies.get("token"); const clientKey = process.env.NEXT_PUBLIC_CLIENT_KEY; const defaultHeaders = { "Content-Type": "application/json", ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), ...(token ? { Authorization: `Bearer ${token}` } : {}), ...(clientKey ? { "X-Client-Key": clientKey } : {}), }; const mergedHeaders = { ...defaultHeaders, ...headers, }; const response = await axiosInterceptorInstance .post(pathUrl, data, { headers: mergedHeaders }) .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); }); window.location.href = "/"; } else { return { error: true, message: response?.data?.message || response?.data || null, data: null, }; } } export async function httpPutInterceptor( pathUrl: any, data: any, headers?: any ) { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; const defaultHeaders = { "Content-Type": "application/json", }; const mergedHeaders = { ...defaultHeaders, ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), ...headers, }; const response = await axiosInterceptorInstance .put(pathUrl, data, { headers: mergedHeaders }) .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); }); window.location.href = "/"; } else { return { error: true, message: response?.data?.message || response?.data || null, data: null, }; } } export async function httpDeleteInterceptor(pathUrl: any, data?: any) { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; const defaultHeaders = { "Content-Type": "application/json", }; const mergedHeaders = { ...defaultHeaders, ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), }; const response = await axiosInterceptorInstance .delete(pathUrl, { headers: mergedHeaders, data }) .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); }); window.location.href = "/"; } 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, }; } } export async function httpGetArrayBuffer(pathUrl: any, headers: any) { const response = await axiosInterceptorInstance .get(pathUrl, { headers, responseType: "arraybuffer" }) .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, }; } } export async function postAPIWithJson(url: any, data: any, token: any) { const headers = { Authorization: `Bearer ${token}`, }; const response = await axiosInstanceJson .post(url, data, { headers }) .catch((error) => error.response); if (response?.status > 300) { return { error: true, message: response?.data?.message, data: null, }; } return { error: false, message: "success", data: response?.data, }; }