web-humas-fe/services/http-config/axios-base-service.ts

178 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-04-11 06:38:38 +00:00
import axiosInterceptorInstance from "@/services/http-config/axios-interceptor-instance";
import { getCsrfToken } from "../master-user";
2025-05-04 07:14:12 +00:00
import axiosBaseInstance from "./http-base-instance";
2024-04-24 04:14:06 +00:00
import mediahubBaseInstance from "./mediahub-base-service";
2025-04-17 09:25:59 +00:00
import Cookies from "js-cookie";
2024-04-04 10:30:07 +00:00
2025-04-17 09:25:59 +00:00
function getCookie(name: string) {
const value = `; ${document.cookie}`;
console.log("val", value);
const parts: any = value.split(`; ${name}=`);
if (parts.length === 2) {
return parts.pop().split(";").shift();
}
return undefined;
}
function removeCookies() {
console.log("run");
Cookies.remove("csrf_");
Cookies.remove("session_id");
document.cookie = "csrf_=; Max-Age=0;path=/";
document.cookie = "session_id=; Max-Age=0;path=/";
}
2024-04-04 10:30:07 +00:00
export async function httpPost(pathUrl: any, headers: any, data?: any) {
2025-04-17 09:25:59 +00:00
// const aCookie = getCookie("session_id");
// if (aCookie === undefined) {
// console.log("kosong");
// } else {
// console.log("cookie ada", aCookie);
// }
// removeCookies();
// const csrfNow = Cookies.get("csrf_");
// const sessionNow = Cookies.get("session_id");
// console.log("now", csrfNow, sessionNow);
2025-04-11 06:38:38 +00:00
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const defaultHeaders = {
"Content-Type": "application/json",
};
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
};
2025-03-18 08:30:18 +00:00
const response = await axiosBaseInstance
2025-04-11 06:38:38 +00:00
.post(pathUrl, data, { headers: mergedHeaders })
2025-03-18 08:30:18 +00:00
.catch(function (error: any) {
console.log(error);
return error.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,
};
}
2024-04-04 10:30:07 +00:00
}
export async function httpGet(pathUrl: any, headers: any) {
2025-04-11 06:38:38 +00:00
const response = await axiosInterceptorInstance
2025-03-18 08:30:18 +00:00
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error);
return error.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,
};
}
2024-04-04 10:30:07 +00:00
}
2024-04-24 04:14:06 +00:00
export async function httpPut(pathUrl: any, headers: any, data?: any) {
2025-04-11 06:38:38 +00:00
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const defaultHeaders = {
"Content-Type": "application/json",
};
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
};
2025-03-18 08:30:18 +00:00
const response = await axiosBaseInstance
2025-04-11 06:38:38 +00:00
.put(pathUrl, data, { headers: mergedHeaders })
2025-03-18 08:30:18 +00:00
.catch(function (error: any) {
console.log(error);
return error.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,
};
}
2024-04-24 04:14:06 +00:00
}
2025-03-18 08:30:18 +00:00
export async function httpDeleteInterceptor(pathUrl: any, headers: any) {
2025-04-11 06:38:38 +00:00
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const defaultHeaders = {
"Content-Type": "application/json",
};
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
};
2025-03-18 08:30:18 +00:00
const response = await axiosBaseInstance
2025-04-11 06:38:38 +00:00
.delete(pathUrl, { headers: mergedHeaders })
2025-03-18 08:30:18 +00:00
.catch((error) => error.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,
};
}
2024-04-24 04:14:06 +00:00
}
export async function mediahubGet(pathUrl: any, headers: any) {
2025-03-18 08:30:18 +00:00
const response = await mediahubBaseInstance
.get(pathUrl, { headers })
.catch(function (error: any) {
console.log(error);
return error.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,
};
}
2024-04-24 04:14:06 +00:00
}