salix/front/core/services/interceptor.js

33 lines
1003 B
JavaScript
Raw 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) {
return {
request: function(config) {
2017-11-16 12:47:18 +00:00
vnApp.pushLoader();
if (vnToken.token)
config.headers.Authorization = vnToken.token;
if ($translate.use())
config.headers['Accept-Language'] = $translate.use();
return config;
},
requestError: function(rejection) {
return $q.reject(rejection);
},
response: function(response) {
2017-11-16 12:47:18 +00:00
vnApp.popLoader();
return response;
},
responseError: function(rejection) {
2017-11-16 12:47:18 +00:00
vnApp.popLoader();
let err = new HttpError(rejection.statusText);
Object.assign(err, rejection);
return $q.reject(err);
}
};
}
2018-02-10 15:18:01 +00:00
ngModule.factory('vnInterceptor', interceptor);