Skip to content

Commit

Permalink
Improved: client method to not honor the interceptors and removed log…
Browse files Browse the repository at this point in the history
…ic to use api method for making ofbiz calls
  • Loading branch information
R-Sourabh committed Oct 14, 2024
1 parent 9b7a465 commit 49a3e72
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
29 changes: 9 additions & 20 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -50,12 +43,12 @@ axios.interceptors.response.use(function (response) {
if (error.response) {
// TODO Handle case for failed queue request
const { status } = error.response;

Check warning on line 45 in src/api/index.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (18.x)

'status' is assigned a value but never used

Check warning on line 45 in src/api/index.ts

View workflow job for this annotation

GitHub Actions / call-workflow-in-another-repo / reusable_workflow_job (20.x)

'status' is assigned a value but never used
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
Expand Down Expand Up @@ -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,
Expand All @@ -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/`;
}

Expand All @@ -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 };
3 changes: 1 addition & 2 deletions src/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ const setUserTimeZone = async (payload: any): Promise <any> => {

const getUserPermissions = async (payload: any, url: string, token: any): Promise<any> => {
// 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
Expand Down
6 changes: 3 additions & 3 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ const actions: ActionTree<UserState, RootState> = {
// Prepare permissions list
const serverPermissionsFromRules = getServerPermissionsFromRules();
if (permissionId) serverPermissionsFromRules.push(permissionId);
if(omsRedirectionUrl && token) {
dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token })
}

const serverPermissions: Array<string> = await UserService.getUserPermissions({
permissionIds: [...new Set(serverPermissionsFromRules)]
Expand Down Expand Up @@ -65,6 +62,9 @@ const actions: ActionTree<UserState, RootState> = {
}

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);
Expand Down

0 comments on commit 49a3e72

Please sign in to comment.