2022-05-03 15:27:09 +05:30
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
|
import getLocalStorageApi from 'api/browser/localstorage/get';
|
|
|
|
|
import loginApi from 'api/user/login';
|
|
|
|
|
import afterLogin from 'AppRoutes/utils';
|
|
|
|
|
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
|
2021-08-26 11:50:47 +05:30
|
|
|
import { ENVIRONMENT } from 'constants/env';
|
2022-05-03 15:27:09 +05:30
|
|
|
import { LOCALSTORAGE } from 'constants/localStorage';
|
|
|
|
|
import store from 'store';
|
2021-08-26 11:50:47 +05:30
|
|
|
|
2022-05-03 15:41:40 +05:30
|
|
|
import apiV1, { apiAlertManager, apiV2 } from './apiV1';
|
2022-05-03 15:27:09 +05:30
|
|
|
import { Logout } from './utils';
|
2021-04-24 01:51:45 +05:30
|
|
|
|
2022-05-03 15:27:09 +05:30
|
|
|
const interceptorsResponse = (
|
|
|
|
|
value: AxiosResponse<any>,
|
|
|
|
|
): Promise<AxiosResponse<any>> => Promise.resolve(value);
|
|
|
|
|
|
|
|
|
|
const interceptorsRequestResponse = (
|
|
|
|
|
value: AxiosRequestConfig,
|
|
|
|
|
): AxiosRequestConfig => {
|
|
|
|
|
const token =
|
|
|
|
|
store.getState().app.user?.accessJwt ||
|
|
|
|
|
getLocalStorageApi(LOCALSTORAGE.AUTH_TOKEN) ||
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
value.headers.Authorization = token ? `Bearer ${token}` : '';
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const interceptorRejected = async (
|
|
|
|
|
value: AxiosResponse<any>,
|
|
|
|
|
): Promise<AxiosResponse<any>> => {
|
|
|
|
|
if (axios.isAxiosError(value) && value.response) {
|
|
|
|
|
const { response } = value;
|
|
|
|
|
console.log(response);
|
|
|
|
|
// reject the refresh token error
|
|
|
|
|
if (response.status === 401 && response.config.url !== '/login') {
|
|
|
|
|
const response = await loginApi({
|
|
|
|
|
refreshToken: store.getState().app.user?.accessJwt,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
|
await afterLogin(
|
|
|
|
|
response.payload.userId,
|
|
|
|
|
response.payload.accessJwt,
|
|
|
|
|
response.payload.refreshJwt,
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
Logout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// when refresh token is expired
|
|
|
|
|
if (response.status === 401 && response.config.url === '/login') {
|
|
|
|
|
Logout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const instance = axios.create({
|
2021-09-23 15:43:43 +05:30
|
|
|
baseURL: `${ENVIRONMENT.baseURL}${apiV1}`,
|
2021-04-24 01:51:45 +05:30
|
|
|
});
|
|
|
|
|
|
2022-05-03 15:27:09 +05:30
|
|
|
instance.interceptors.response.use(interceptorsResponse, interceptorRejected);
|
|
|
|
|
instance.interceptors.request.use(interceptorsRequestResponse);
|
|
|
|
|
|
2021-11-22 11:49:09 +05:30
|
|
|
export const AxiosAlertManagerInstance = axios.create({
|
2022-05-03 15:41:40 +05:30
|
|
|
baseURL: `${ENVIRONMENT.baseURL}${apiAlertManager}`,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const ApiV2Instance = axios.create({
|
2021-11-22 11:49:09 +05:30
|
|
|
baseURL: `${ENVIRONMENT.baseURL}${apiV2}`,
|
|
|
|
|
});
|
|
|
|
|
|
2022-05-03 15:27:09 +05:30
|
|
|
AxiosAlertManagerInstance.interceptors.response.use(
|
|
|
|
|
interceptorsResponse,
|
|
|
|
|
interceptorRejected,
|
|
|
|
|
);
|
|
|
|
|
AxiosAlertManagerInstance.interceptors.request.use(interceptorsRequestResponse);
|
|
|
|
|
|
2021-04-24 01:51:45 +05:30
|
|
|
export { apiV1 };
|
2022-05-03 15:27:09 +05:30
|
|
|
export default instance;
|