salix/front/core/services/interceptor.js

47 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
import HttpError from 'core/lib/http-error';
interceptor.$inject = ['$q', 'vnApp', 'vnToken', '$translate'];
function interceptor($q, vnApp, vnToken, $translate) {
let apiPath = 'api/';
return {
setApiPath(path) {
apiPath = path;
},
request(config) {
2017-11-16 12:47:18 +00:00
vnApp.pushLoader();
if (config.url.charAt(0) !== '/' && apiPath)
2019-11-10 10:08:44 +00:00
config.url = `${apiPath}${config.url}`;
if (vnToken.token)
config.headers.Authorization = vnToken.token;
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;
}
return config;
},
requestError(rejection) {
return $q.reject(rejection);
},
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);
return response;
},
responseError(rejection) {
2017-11-16 12:47:18 +00:00
vnApp.popLoader();
const err = new HttpError(rejection.statusText);
Object.assign(err, rejection);
return $q.reject(err);
2022-06-14 08:16:41 +00:00
},
};
}
2018-02-10 15:18:01 +00:00
ngModule.factory('vnInterceptor', interceptor);