From 49a3e728fb240edc4c37b269cfb36bbe1ef864b7 Mon Sep 17 00:00:00 2001 From: R-Sourabh Date: Mon, 14 Oct 2024 16:46:17 +0530 Subject: [PATCH] Improved: client method to not honor the interceptors and removed logic to use api method for making ofbiz calls --- src/api/index.ts | 29 +++++++++-------------------- src/services/UserService.ts | 3 +-- src/store/modules/user/actions.ts | 6 +++--- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 77d0d0a..434b48d 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,22 +3,15 @@ import { setupCache } from "axios-cache-adapter" import OfflineHelper from "@/offline-helper" import emitter from "@/event-bus" import store from "@/store"; -import { StatusCodes } from "http-status-codes"; -let apiConfig = {} as any axios.interceptors.request.use((config: any) => { // TODO: pass csrf token const token = store.getters["user/getUserToken"]; - if (token && !apiConfig.useOmsRedirection) { + if (token) { config.headers["api_key"] = token; config.headers["Content-Type"] = "application/json"; } - const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"] - if (apiConfig.useOmsRedirection && omsRedirectionInfo.token) { - config.headers["Authorization"] = `Bearer ${omsRedirectionInfo.token}`; - config.headers["Content-Type"] = "application/json"; - } return config; }); @@ -50,12 +43,12 @@ axios.interceptors.response.use(function (response) { if (error.response) { // TODO Handle case for failed queue request const { status } = error.response; - if (status === StatusCodes.UNAUTHORIZED) { - store.dispatch("user/logout"); - const redirectUrl = window.location.origin + '/login'; - // Explicitly passing isLoggedOut as in case of maarg apps we need to call the logout api in launchpad - window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}&isLoggedOut=true`; - } + // if (status === StatusCodes.UNAUTHORIZED) { + // store.dispatch("user/logout"); + // const redirectUrl = window.location.origin + '/login'; + // // Explicitly passing isLoggedOut as in case of maarg apps we need to call the logout api in launchpad + // window.location.href = `${process.env.VUE_APP_LOGIN_URL}?redirectUrl=${redirectUrl}&isLoggedOut=true`; + // } } // Any status codes that falls outside the range of 2xx cause this function to trigger // Do something with response error @@ -84,7 +77,6 @@ const axiosCache = setupCache({ * @return {Promise} Response from API as returned by Axios */ const api = async (customConfig: any) => { - apiConfig = customConfig // Prepare configuration const config: any = { url: customConfig.url, @@ -95,11 +87,8 @@ const api = async (customConfig: any) => { } const baseURL = store.getters["user/getInstanceUrl"]; - const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"] - if(customConfig.useOmsRedirection) { - config.baseURL = omsRedirectionInfo.url.startsWith('http') ? omsRedirectionInfo.url.includes('/api') ? omsRedirectionInfo.url : `${omsRedirectionInfo.url}/api/` : `https://${omsRedirectionInfo.url}.hotwax.io/api/`; - } else if (baseURL) { + if (baseURL) { config.baseURL = baseURL.startsWith('http') ? baseURL.includes('/rest/s1/admin') ? baseURL : `${baseURL}/rest/s1/admin/` : `https://${baseURL}.hotwax.io/rest/s1/admin/`; } @@ -123,7 +112,7 @@ const api = async (customConfig: any) => { * @return {Promise} Response from API as returned by Axios */ const client = (config: any) => { - return axios.request(config); + return axios.create().request(config); } export { api as default, client, axios }; \ No newline at end of file diff --git a/src/services/UserService.ts b/src/services/UserService.ts index 6cb3168..438279a 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -71,8 +71,7 @@ const setUserTimeZone = async (payload: any): Promise => { const getUserPermissions = async (payload: any, url: string, token: any): Promise => { // Currently, making this request in ofbiz - const omsRedirectionInfo = store.getters["user/getOmsRedirectionInfo"] - const baseURL = omsRedirectionInfo.url.startsWith('http') ? omsRedirectionInfo.url.includes('/api') ? omsRedirectionInfo.url : `${omsRedirectionInfo.url}/api/` : `https://${omsRedirectionInfo.url}.hotwax.io/api/`; + const baseURL = url.startsWith('http') ? url.includes('/api') ? url : `${url}/api/` : `https://${url}.hotwax.io/api/`; let serverPermissions = [] as any; // If the server specific permission list doesn't exist, getting server permissions will be of no use diff --git a/src/store/modules/user/actions.ts b/src/store/modules/user/actions.ts index ab42e96..62c6945 100644 --- a/src/store/modules/user/actions.ts +++ b/src/store/modules/user/actions.ts @@ -30,9 +30,6 @@ const actions: ActionTree = { // Prepare permissions list const serverPermissionsFromRules = getServerPermissionsFromRules(); if (permissionId) serverPermissionsFromRules.push(permissionId); - if(omsRedirectionUrl && token) { - dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token }) - } const serverPermissions: Array = await UserService.getUserPermissions({ permissionIds: [...new Set(serverPermissionsFromRules)] @@ -65,6 +62,9 @@ const actions: ActionTree = { } setPermissions(appPermissions); + if(omsRedirectionUrl && token) { + dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token }) + } commit(types.USER_TOKEN_CHANGED, { newToken: api_key }) commit(types.USER_INFO_UPDATED, userProfile); commit(types.USER_PERMISSIONS_UPDATED, appPermissions);