feat: update services all function

This commit is contained in:
hanif salafi 2025-07-03 13:57:17 +07:00
parent 750eae2971
commit de12444e17
10 changed files with 166 additions and 203 deletions

View File

@ -1,8 +1,11 @@
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
env: {
MEDOLS_CLIENT_KEY: process.env.MEDOLS_CLIENT_KEY,
},
images: {
domains: ["kontenhumas.com"],
domains: ["mikulnews.com", "dev.mikulnews.com"],
},
eslint: {
ignoreDuringBuilds: true,

View File

@ -1,13 +1,8 @@
import { PaginationRequest } from "@/types/globals";
import {
httpDeleteInterceptor,
httpGet,
httpPost,
httpPut,
} from "./http-config/axios-base-service";
import Cookies from "js-cookie";
import { httpGet } from "./http-config/http-base-services";
import { httpDeleteInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services";
const token = Cookies.get("access_token");
export async function getListArticle(props: PaginationRequest) {
const {
page,
@ -22,9 +17,6 @@ export async function getListArticle(props: PaginationRequest) {
categorySlug,
isBanner,
} = props;
const headers = {
"content-type": "application/json",
};
return await httpGet(
`/articles?limit=${limit}&page=${page}&isPublish=${
isPublish === undefined ? "" : isPublish
@ -33,7 +25,7 @@ export async function getListArticle(props: PaginationRequest) {
}&categoryId=${category || ""}&sortBy=${sortBy || "created_at"}&sort=${
sort || "asc"
}&category=${categorySlug || ""}&isBanner=${isBanner || ""}`,
headers
null
);
}
@ -54,29 +46,18 @@ export async function getTopArticles(props: PaginationRequest) {
}
export async function createArticle(data: any) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
const pathUrl = `/articles`;
return await httpPost(pathUrl, headers, data);
return await httpPostInterceptor(pathUrl, data);
}
export async function createArticleSchedule(data: any) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
const pathUrl = `/articles/publish-scheduling?id=${data.id}&date=${data.date}`;
return await httpPost(pathUrl, headers, data);
return await httpPostInterceptor(pathUrl, data);
}
export async function updateArticle(id: string, data: any) {
const headers = {
"content-type": "application/json",
};
const pathUrl = `/articles/${id}`;
return await httpPut(pathUrl, headers, data);
return await httpPutInterceptor(pathUrl, data);
}
export async function getArticleById(id: any) {
@ -94,21 +75,11 @@ export async function deleteArticle(id: string) {
}
export async function getArticleByCategory() {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
return await httpGet(`/article-categories?limit=1000`, headers);
return await httpGet(`/article-categories?limit=1000`);
}
export async function getCategoryPagination(data: any) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
return await httpGet(
`/article-categories?limit=${data?.limit}&page=${data?.page}&title=${data?.search}`,
headers
`/article-categories?limit=${data?.limit}&page=${data?.page}&title=${data?.search}`
);
}
@ -116,13 +87,14 @@ export async function uploadArticleFile(id: string, data: any) {
const headers = {
"content-type": "multipart/form-data",
};
return await httpPost(`/article-files/${id}`, headers, data);
return await httpPostInterceptor(`/article-files/${id}`, data, headers);
}
export async function uploadArticleThumbnail(id: string, data: any) {
const headers = {
"content-type": "multipart/form-data",
};
return await httpPost(`/articles/thumbnail/${id}`, headers, data);
return await httpPostInterceptor(`/articles/thumbnail/${id}`, data, headers);
}
export async function deleteArticleFiles(id: number) {
@ -133,35 +105,18 @@ export async function deleteArticleFiles(id: number) {
}
export async function getUserLevelDataStat(startDate: string, endDate: string) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
return await httpGet(
`/articles/statistic/user-levels?startDate=${startDate}&endDate=${endDate}`,
headers
`/articles/statistic/user-levels?startDate=${startDate}&endDate=${endDate}`
);
}
export async function getStatisticMonthly(year: string) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
return await httpGet(`/articles/statistic/monthly?year=${year}`, headers);
return await httpGet(`/articles/statistic/monthly?year=${year}`);
}
export async function getStatisticMonthlyFeedback(year: string) {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
return await httpGet(`/feedbacks/statistic/monthly?year=${year}`, headers);
return await httpGet(`/feedbacks/statistic/monthly?year=${year}`);
}
export async function getStatisticSummary() {
const headers = {
"content-type": "application/json",
Authorization: `Bearer ${token}`,
};
return await httpGet(`/articles/statistic/summary`, headers);
return await httpGet(`/articles/statistic/summary`);
}
export async function submitApproval(data: {
@ -169,11 +124,7 @@ export async function submitApproval(data: {
message: string;
statusId: number;
}) {
const headers = {
"content-type": "multipart/form-data",
Authorization: `Bearer ${token}`,
};
return await httpPost(`/article-approvals`, headers, data);
return await httpPostInterceptor(`/article-approvals`, data);
}
export async function updateIsBannerArticle(id: number, status: boolean) {
@ -181,5 +132,5 @@ export async function updateIsBannerArticle(id: number, status: boolean) {
"content-type": "application/json",
};
const pathUrl = `/articles/banner/${id}?isBanner=${status}`;
return await httpPut(pathUrl, headers);
return await httpPutInterceptor(pathUrl, headers);
}

View File

@ -1,6 +1,6 @@
import axios from "axios";
const baseURL = "http://38.47.180.165:8802";
const baseURL = "https://dev.mikulnews.com/api";
const axiosBaseInstance = axios.create({
baseURL,

View File

@ -4,42 +4,14 @@ import axiosBaseInstance from "./http-base-instance";
import mediahubBaseInstance from "./mediahub-base-service";
import Cookies from "js-cookie";
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;
}
const defaultHeaders = {
"Content-Type": "application/json",
};
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=/";
}
export async function httpPost(pathUrl: any, headers: any, data?: any) {
// 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);
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const defaultHeaders = {
"Content-Type": "application/json",
};
const mergedHeaders = {
...defaultHeaders,
...headers,

View File

@ -1,12 +1,72 @@
import axios from "axios";
import { postSignIn } from "../master-user";
import Cookies from "js-cookie";
const baseURL = "https://kontenhumas.com/api";
const baseURL = "https://dev.mikulnews.com/api";
const refreshToken = Cookies.get("refresh_token");
const axiosInterceptorInstance = axios.create({
baseURL,
headers: {
"content-type": "application/json",
"Content-Type": "application/json",
"X-Client-Key": process.env.MEDOLS_CLIENT_KEY
},
withCredentials: true,
});
// Request interceptor
axiosInterceptorInstance.interceptors.request.use(
(config) => {
console.log("Config interceptor : ", config);
const accessToken = Cookies.get("access_token");
if (accessToken) {
if (config.headers)
config.headers.Authorization = "Bearer " + accessToken;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
// Response interceptor
axiosInterceptorInstance.interceptors.response.use(
(response) => {
console.log("Response interceptor : ", response);
return response;
},
async function (error) {
console.log("Error interceptor : ", error.response.status);
const originalRequest = error.config;
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
const data = {
grantType: "refresh_token",
refreshToken: refreshToken,
clientId: "mediahub-app",
};
console.log("refresh token ", data);
const res = await postSignIn(data);
if (res?.error) {
Object.keys(Cookies.get()).forEach((cookieName) => {
Cookies.remove(cookieName);
});
} else {
const { access_token } = res?.data;
const { refresh_token } = res?.data;
if (access_token) {
Cookies.set("access_token", access_token);
Cookies.set("refresh_token", refresh_token);
}
}
return axiosInterceptorInstance(originalRequest);
}
return Promise.reject(error);
}
);
export default axiosInterceptorInstance;

View File

@ -1,71 +0,0 @@
import axios from "axios";
import { postSignIn } from "../master-user";
import Cookies from "js-cookie";
const baseURL = "https://kontenhumas.com/api";
const refreshToken = Cookies.get("refresh_token");
const axiosInterceptorInstance = axios.create({
baseURL,
headers: {
"content-type": "application/json",
},
withCredentials: true,
});
// Request interceptor
axiosInterceptorInstance.interceptors.request.use(
(config) => {
console.log("Config interceptor : ", config);
const accessToken = Cookies.get("access_token");
if (accessToken) {
if (config.headers)
config.headers.Authorization = "Bearer " + accessToken;
}
return config;
},
(error) => {
return Promise.reject(error);
}
);
// Response interceptor
axiosInterceptorInstance.interceptors.response.use(
(response) => {
console.log("Response interceptor : ", response);
return response;
},
async function (error) {
console.log("Error interceptor : ", error.response.status);
const originalRequest = error.config;
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
const data = {
grantType: "refresh_token",
refreshToken: refreshToken,
clientId: "mediahub-app",
};
console.log("refresh token ", data);
const res = await postSignIn(data);
if (res?.error) {
Object.keys(Cookies.get()).forEach((cookieName) => {
Cookies.remove(cookieName);
});
} else {
const { access_token } = res?.data;
const { refresh_token } = res?.data;
if (access_token) {
Cookies.set("access_token", access_token);
Cookies.set("refresh_token", refresh_token);
}
}
return axiosInterceptorInstance(originalRequest);
}
return Promise.reject(error);
}
);
export default axiosInterceptorInstance;

View File

@ -1,11 +1,12 @@
import axios from "axios";
const baseURL = "https://kontenhumas.com/api";
const baseURL = "https://dev.mikulnews.com/api";
const axiosBaseInstance = axios.create({
baseURL,
headers: {
"content-type": "application/json",
"X-Client-Key": process.env.MEDOLS_CLIENT_KEY
},
withCredentials: true,
});

View File

@ -1,8 +1,21 @@
import axiosBaseInstance from "./axios-base-instance";
import axiosBaseInstance from "./http-base-instance";
const defaultHeaders = {
"Content-Type": "application/json",
"X-Client-Key": process.env.MEDOLS_CLIENT_KEY
};
export async function httpGet(pathUrl: any, headers?: any) {
const mergedHeaders = {
...defaultHeaders,
...headers,
};
console.log("Merged Headers : ", mergedHeaders);
export async function httpGet(pathUrl: any, headers: any) {
const response = await axiosBaseInstance
.get(pathUrl, { headers })
.get(pathUrl, { headers: mergedHeaders })
.catch((error) => error.response);
console.log("Response base svc : ", response);
if (response?.data.success) {

View File

@ -1,6 +1,12 @@
import { useRouter } from "next/navigation";
import axiosInterceptorInstance from "./axios-interceptor-instance";
import Cookies from "js-cookie";
import axiosInterceptorInstance from "./axios-interceptor-instance";
import { getCsrfToken } from "../master-user";
const defaultHeaders = {
"Content-Type": "application/json",
"X-Client-Key": process.env.MEDOLS_CLIENT_KEY
};
export async function httpGetInterceptor(pathUrl: any) {
const response = await axiosInterceptorInstance
@ -29,8 +35,17 @@ export async function httpGetInterceptor(pathUrl: any) {
}
export async function httpPostInterceptor(pathUrl: any, data: any, headers?: any) {
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
};
const response = await axiosInterceptorInstance
.post(pathUrl, data, { headers })
.post(pathUrl, data, { headers: mergedHeaders })
.catch((error) => error.response);
console.log("Response interceptor : ", response);
if (response?.status == 200 || response?.status == 201) {
@ -51,9 +66,49 @@ export async function httpPostInterceptor(pathUrl: any, data: any, headers?: any
}
}
export async function httpDeleteInterceptor(pathUrl: any) {
export async function httpPutInterceptor(pathUrl: any, data: any, headers?: any) {
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
};
const response = await axiosInterceptorInstance
.delete(pathUrl)
.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) {
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, headers: any) {
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.csrf_token;
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}),
};
const response = await axiosInterceptorInstance
.delete(pathUrl, { headers: mergedHeaders })
.catch((error) => error.response);
console.log("Response interceptor : ", response);
if (response?.status == 200 || response?.status == 201) {

View File

@ -160,26 +160,5 @@ export async function getCsrfToken() {
const headers = {
"content-type": "application/json",
};
return httpGetTemp(pathUrl, headers);
}
async function httpGetTemp(pathUrl: any, headers: any) {
const response = await axiosBaseInstance.get(pathUrl, { headers }).catch(function (error: any) {
console.log(error);
return error.response;
});
console.log("Response base svc : ", 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,
};
}
}
return httpGet(pathUrl, headers);
}