2020-10-15 14:10:23 +02:00
|
|
|
import Axios from 'axios';
|
2021-12-07 15:59:29 +01:00
|
|
|
//import Auth from 'zero/helpers/auth.js';
|
2020-05-18 12:31:58 +02:00
|
|
|
import Qs from 'qs';
|
2020-04-09 10:38:27 +02:00
|
|
|
|
2021-12-07 15:59:29 +01:00
|
|
|
//if (!__zero || !__zero.apiPath)
|
|
|
|
|
//{
|
|
|
|
|
// throw Exception('window.zero and zero.apiPath (= base path to the backoffice API) have to be configured');
|
|
|
|
|
//}
|
2020-04-09 10:38:27 +02:00
|
|
|
|
2021-12-13 14:28:25 +01:00
|
|
|
//Axios.defaults.baseURL = '/zero/api/';
|
2020-04-09 10:38:27 +02:00
|
|
|
Axios.defaults.withCredentials = true;
|
|
|
|
|
|
2020-05-18 12:31:58 +02:00
|
|
|
Axios.defaults.paramsSerializer = (params) =>
|
|
|
|
|
{
|
2020-10-15 14:10:23 +02:00
|
|
|
return Qs.stringify(params, { allowDots: true });
|
2020-05-18 12:31:58 +02:00
|
|
|
};
|
|
|
|
|
|
2021-12-14 11:54:36 +01:00
|
|
|
Axios.interceptors.response.use(
|
2021-12-15 15:34:05 +01:00
|
|
|
response => response,
|
2021-12-14 11:54:36 +01:00
|
|
|
error =>
|
|
|
|
|
{
|
|
|
|
|
if (error.response && error.response.headers['x-variant'] == 'api-response')
|
|
|
|
|
{
|
2021-12-15 14:36:45 +01:00
|
|
|
return Promise.resolve(error.response);
|
2021-12-14 11:54:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error.response && error.response.status === 401)
|
2020-04-09 10:38:27 +02:00
|
|
|
{
|
2021-12-07 15:59:29 +01:00
|
|
|
console.error('[zero.axios] Auth failed. Please login again.');
|
|
|
|
|
//Auth.rejectUser("@login.rejectReasons.inactive");
|
2020-04-09 10:38:27 +02:00
|
|
|
//Notification.error('Authentication failed. Please login again.', 3);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 11:54:36 +01:00
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
2020-04-09 10:38:27 +02:00
|
|
|
|
2020-11-22 22:31:45 +01:00
|
|
|
Axios.interceptors.request.use(config =>
|
|
|
|
|
{
|
2021-12-15 15:34:05 +01:00
|
|
|
if (!config.params)
|
2020-11-22 22:31:45 +01:00
|
|
|
{
|
2021-12-15 15:34:05 +01:00
|
|
|
config.params = {};
|
2020-11-22 22:31:45 +01:00
|
|
|
}
|
2021-12-15 15:34:05 +01:00
|
|
|
|
|
|
|
|
let locationQuery = Qs.parse(location.search.substring(1));
|
|
|
|
|
let appKey = 'hofbauer';
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 22:31:45 +01:00
|
|
|
return config;
|
|
|
|
|
}, error => Promise.reject(error));
|
|
|
|
|
|
2020-04-09 10:38:27 +02:00
|
|
|
export default Axios;
|