93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import { AxiosError } from 'axios';
|
|
import Axios from 'axios';
|
|
import eventHub from './services/eventhub';
|
|
import { open as openOverlay } from './services/overlay';
|
|
//import Auth from 'zero/helpers/auth.js';
|
|
import Qs from 'qs';
|
|
|
|
//if (!__zero || !__zero.apiPath)
|
|
//{
|
|
// throw Exception('window.zero and zero.apiPath (= base path to the backoffice API) have to be configured');
|
|
//}
|
|
|
|
//Axios.defaults.baseURL = '/zero/api/';
|
|
Axios.defaults.withCredentials = true;
|
|
|
|
Axios.defaults.paramsSerializer = (params) =>
|
|
{
|
|
return Qs.stringify(params, { allowDots: true });
|
|
};
|
|
|
|
Axios.interceptors.response.use(
|
|
response => response,
|
|
(error: AxiosError) =>
|
|
{
|
|
const isApi = error.response && error.response.headers['x-variant'] == 'api-response';
|
|
|
|
if (isApi && error.response && error.response.status >= 500)
|
|
{
|
|
const errorData = error.response.data && error.response.data.errors ? error.response.data.errors[0] : {
|
|
category: 'server',
|
|
code: 'server.error.axios',
|
|
message: 'Unknown server error',
|
|
propery: null
|
|
};
|
|
|
|
openOverlay({
|
|
alias: 'servererror',
|
|
model: errorData,
|
|
autoclose: true,
|
|
softdismiss: true,
|
|
component: () => import('./ui/overlays/servererror.vue')
|
|
});
|
|
}
|
|
|
|
if (isApi)
|
|
{
|
|
return Promise.resolve(error.response);
|
|
}
|
|
|
|
if (error.response && error.response.status === 401)
|
|
{
|
|
console.error('[zero.axios] Auth failed. Please login again.');
|
|
eventHub.emit('zero.rejectauth');
|
|
return Promise.reject(error.response);
|
|
//Auth.rejectUser("@login.rejectReasons.inactive");
|
|
//Notification.error('Authentication failed. Please login again.', 3);
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
Axios.interceptors.request.use(config =>
|
|
{
|
|
if (!config.params)
|
|
{
|
|
config.params = {};
|
|
}
|
|
|
|
let locationQuery = Qs.parse(location.search.substring(1));
|
|
let appKey = 'system';
|
|
|
|
// set app key to system when required
|
|
if (config.params['zero.system'] === true || locationQuery['zero.shared'] === "true")
|
|
{
|
|
delete config.params['zero.system'];
|
|
appKey = 'system';
|
|
}
|
|
|
|
// rewrite URLs to replace {app} placeholder
|
|
if (config.url != null && config.url.indexOf('{app}') > -1)
|
|
{
|
|
config.url = config.url.replace('{app}', appKey);
|
|
}
|
|
if (config.baseURL != null && config.baseURL.indexOf('{app}') > -1)
|
|
{
|
|
config.baseURL = config.baseURL.replace('{app}', appKey);
|
|
}
|
|
|
|
return config;
|
|
}, error => Promise.reject(error));
|
|
|
|
export default Axios; |