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 810b490 commit 5ceda2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
30 changes: 9 additions & 21 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +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";
import router from "@/router"

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 @@ -51,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 @@ -85,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 @@ -96,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/order-routing') ? baseURL : `${baseURL}/rest/s1/order-routing/` : `https://${baseURL}.hotwax.io/rest/s1/order-routing/`;
}

Expand All @@ -124,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 @@ -33,8 +33,7 @@ const login = async (token: string): 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
14 changes: 7 additions & 7 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ 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)]
}, omsRedirectionUrl, token);
Expand All @@ -52,19 +49,22 @@ const actions: ActionTree<UserState, RootState> = {
return Promise.reject(new Error(permissionError));
}
}

emitter.emit("presentLoader", { message: "Logging in...", backdropDismiss: false })
const api_key = await UserService.login(token)
const userProfile = await UserService.getUserProfile(api_key);

// TODO: fetch only associated product stores for user, currently api does not support this
userProfile.stores = await UserService.getEComStores(api_key);

if (userProfile.timeZone) {
Settings.defaultZone = userProfile.timeZone;
}

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 5ceda2a

Please sign in to comment.