From 8beec9f332ed96a4d46cf725c723125fee992cab Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Mon, 3 Mar 2025 11:22:36 +0700 Subject: [PATCH] feat: update interceptor prod --- .../http-config/axios-base-prod-instance.ts | 13 ++ .../http-interceptor-prod-service.ts | 174 ++++++++++++++++++ service/tableau/tableau-service.ts | 2 +- 3 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 service/http-config/axios-base-prod-instance.ts create mode 100644 service/http-config/http-interceptor-prod-service.ts diff --git a/service/http-config/axios-base-prod-instance.ts b/service/http-config/axios-base-prod-instance.ts new file mode 100644 index 00000000..abec8be4 --- /dev/null +++ b/service/http-config/axios-base-prod-instance.ts @@ -0,0 +1,13 @@ +import axios from "axios"; + +const baseURL = "https://mediahub.polri.go.id/api/"; + +const axiosBaseProdInstance = axios.create({ + baseURL, + headers: { + "content-type": "application/json", + }, + withCredentials: true, +}); + +export default axiosBaseProdInstance; diff --git a/service/http-config/http-interceptor-prod-service.ts b/service/http-config/http-interceptor-prod-service.ts new file mode 100644 index 00000000..0f6c0086 --- /dev/null +++ b/service/http-config/http-interceptor-prod-service.ts @@ -0,0 +1,174 @@ +import { useRouter } from "next/navigation"; +import Cookies from "js-cookie"; +import { getCsrfToken } from "../auth"; +import axiosBaseProdInstance from "./axios-base-prod-instance"; + +export async function httpGetInterceptor(pathUrl: any) { + const pathname = window.location.pathname; + const response = await axiosBaseProdInstance + .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 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, + ...headers, + ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), + }; + + const response = await axiosBaseProdInstance + .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, + ...headers, + ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), + }; + + const response = await axiosBaseProdInstance + .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 axiosBaseProdInstance + .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 axiosBaseProdInstance + .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, + }; + } +} diff --git a/service/tableau/tableau-service.ts b/service/tableau/tableau-service.ts index a81649b0..0c1851a5 100644 --- a/service/tableau/tableau-service.ts +++ b/service/tableau/tableau-service.ts @@ -1,4 +1,4 @@ -import { httpPostInterceptor } from "../http-config/http-interceptor-service"; +import { httpPostInterceptor } from "../http-config/http-interceptor-prod-service"; export async function generateTicket() { const url = "/admin/tableau-ticket";