salix/front/core/lib/interceptor.js

35 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
import HttpError from './http-error';
interceptor.$inject = ['$q', 'vnApp', '$cookies', '$translate'];
function interceptor($q, vnApp, $cookies, $translate) {
return {
request: function(config) {
2017-11-16 12:47:18 +00:00
vnApp.pushLoader();
let token = $cookies.get('vnToken');
if (token)
config.headers.Authorization = 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);