From b76f661b809b95dc12cd6ccf842ec63d7494aa2d Mon Sep 17 00:00:00 2001 From: hanif salafi Date: Thu, 3 Jul 2025 21:55:53 +0700 Subject: [PATCH] feat: update fixing user services --- service/http-config/http-base-services.ts | 8 +- .../http-config/http-interceptor-services.ts | 2 +- service/master-user.ts | 94 ++++++------------- 3 files changed, 34 insertions(+), 70 deletions(-) diff --git a/service/http-config/http-base-services.ts b/service/http-config/http-base-services.ts index f958b02..9a87ffc 100644 --- a/service/http-config/http-base-services.ts +++ b/service/http-config/http-base-services.ts @@ -33,9 +33,13 @@ export async function httpGet(pathUrl: any, headers?: any) { } } -export async function httpPost(pathUrl: any, headers: any, data: any) { +export async function httpPost(pathUrl: any, data: any, headers?: any) { + const mergedHeaders = { + ...defaultHeaders, + ...headers, + }; const response = await axiosBaseInstance - .post(pathUrl, data, { headers }) + .post(pathUrl, data, { headers: mergedHeaders }) .catch(function (error) { console.log(error); return error.response; diff --git a/service/http-config/http-interceptor-services.ts b/service/http-config/http-interceptor-services.ts index 4dbe657..416b738 100644 --- a/service/http-config/http-interceptor-services.ts +++ b/service/http-config/http-interceptor-services.ts @@ -98,7 +98,7 @@ export async function httpPutInterceptor(pathUrl: any, data: any, headers?: any) } } -export async function httpDeleteInterceptor(pathUrl: any, headers: any) { +export async function httpDeleteInterceptor(pathUrl: any, headers?: any) { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.csrf_token; diff --git a/service/master-user.ts b/service/master-user.ts index cf875e1..f92f9db 100644 --- a/service/master-user.ts +++ b/service/master-user.ts @@ -1,6 +1,6 @@ -import { httpDeleteInterceptor, httpGet, httpPost, httpPut } from "./http-config/axios-base-service"; import Cookies from "js-cookie"; -import axiosBaseInstance from "./http-config/http-base-instance"; +import { httpGet, httpPost } from "./http-config/http-base-services"; +import { httpDeleteInterceptor, httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; const token = Cookies.get("access_token"); const id = Cookies.get("uie"); @@ -13,46 +13,32 @@ export async function listMasterUsers(data: any) { } export async function createMasterUser(data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/users`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } + export async function emailValidation(data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/users/email-validation`; - return await httpPost(pathUrl, headers, data); + return await httpPost(pathUrl, data); } export async function setupEmail(data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/users/setup-email`; - return await httpPost(pathUrl, headers, data); + return await httpPost(pathUrl, data); } export async function getDetailMasterUsers(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`/users/detail/${id}`, headers); + const pathUrl = `/users/detail/${id}`; + return await httpGetInterceptor(pathUrl); } export async function editMasterUsers(data: any, id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpPut(`/users/${id}`, headers, data); + const pathUrl = `/users/${id}` + return await httpPutInterceptor(pathUrl, data); } export async function deleteMasterUser(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpDeleteInterceptor(`/users/${id}`, headers); + const pathUrl = `/users/${id}` + return await httpDeleteInterceptor(pathUrl); } export async function postSignIn(data: any) { @@ -73,18 +59,12 @@ export async function getProfile(code?: string) { } export async function updateProfile(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpPut(`/users/${id}`, headers, data); + const pathUrl = `/users/${id}`; + return await httpPutInterceptor(pathUrl, data); } export async function savePassword(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpPost(`/users/save-password`, headers, data); + const pathUrl = `/users/save-password` + return await httpPostInterceptor(pathUrl, data); } export async function resetPassword(data: any) { @@ -102,23 +82,13 @@ export async function checkUsernames(username: string) { } export async function otpRequest(email: string, name: string) { - const headers = { - "content-type": "application/json", - }; - return await httpPost(`/users/otp-request`, headers, { email, name }); + const pathUrl = `/users/otp-request`; + return await httpPost(pathUrl, { email, name }); } export async function otpValidation(email: string, otpCode: string) { - const headers = { - "content-type": "application/json", - }; - return await httpPost(`/users/otp-validation`, headers, { email, otpCode }); -} -export async function otpValidationLogin(data: any) { - const headers = { - "content-type": "application/json", - }; - return await httpPost(`/users/otp-validation`, headers, data); + const pathUrl = `/users/otp-validation` + return await httpPost(pathUrl, { email, otpCode }); } export async function postArticleComment(data: any) { @@ -134,31 +104,21 @@ export async function postArticleComment(data: any) { } export async function editArticleComment(data: any, id: number) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpPut(`/article-comments/${id}`, headers, data); + const pathUrl = `/article-comments/${id}`; + return await httpPutInterceptor(pathUrl, data); } export async function getArticleComment(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`/article-comments?isPublic=true&articleId=${id}`, headers); + const pathUrl = `/article-comments?isPublic=true&articleId=${id}`; + return await httpGet(pathUrl); } export async function deleteArticleComment(id: number) { - const headers = { - "content-type": "application/json", - }; - return await httpDeleteInterceptor(`/article-comments/${id}`, headers); + const pathUrl = `/article-comments/${id}` + return await httpDeleteInterceptor(pathUrl); } export async function getCsrfToken() { const pathUrl = "csrf-token"; - const headers = { - "content-type": "application/json", - }; - return httpGet(pathUrl, headers); + return httpGet(pathUrl); } \ No newline at end of file