2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2019-01-23 12:11:44 +00:00
|
|
|
import HttpError from 'core/lib/http-error';
|
2017-06-05 07:01:21 +00:00
|
|
|
|
2023-06-29 10:11:16 +00:00
|
|
|
interceptor.$inject = ['$q', 'vnApp', '$translate'];
|
|
|
|
function interceptor($q, vnApp, $translate) {
|
2019-10-24 22:53:53 +00:00
|
|
|
let apiPath = 'api/';
|
2023-06-29 10:15:32 +00:00
|
|
|
let token = sessionStorage.getItem('vnToken')
|
|
|
|
?? localStorage.getItem('vnToken');
|
2019-10-24 22:53:53 +00:00
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
return {
|
2023-06-29 10:11:16 +00:00
|
|
|
setToken(newToken) {
|
|
|
|
token = newToken;
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
setApiPath(path) {
|
|
|
|
apiPath = path;
|
|
|
|
},
|
|
|
|
request(config) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.pushLoader();
|
2017-06-05 07:01:21 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
if (config.url.charAt(0) !== '/' && apiPath)
|
2019-11-10 10:08:44 +00:00
|
|
|
config.url = `${apiPath}${config.url}`;
|
2023-06-29 10:11:16 +00:00
|
|
|
if (token)
|
|
|
|
config.headers.Authorization = token;
|
2018-08-02 07:49:00 +00:00
|
|
|
if ($translate.use())
|
|
|
|
config.headers['Accept-Language'] = $translate.use();
|
2019-11-10 10:08:44 +00:00
|
|
|
if (config.filter) {
|
|
|
|
if (!config.params) config.params = {};
|
|
|
|
config.params.filter = config.filter;
|
|
|
|
}
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
return config;
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
requestError(rejection) {
|
2017-06-05 07:01:21 +00:00
|
|
|
return $q.reject(rejection);
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
response(response) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.popLoader();
|
2022-06-14 08:16:41 +00:00
|
|
|
const newVersion = response.headers('salix-version');
|
2022-06-16 06:25:38 +00:00
|
|
|
vnApp.setVersion(newVersion);
|
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
return response;
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
responseError(rejection) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.popLoader();
|
2022-06-13 11:36:05 +00:00
|
|
|
const err = new HttpError(rejection.statusText);
|
2018-05-25 15:25:35 +00:00
|
|
|
Object.assign(err, rejection);
|
|
|
|
return $q.reject(err);
|
2022-06-14 08:16:41 +00:00
|
|
|
},
|
2017-06-05 07:01:21 +00:00
|
|
|
};
|
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.factory('vnInterceptor', interceptor);
|