salix/client/core/src/lib/interceptor.js

32 lines
901 B
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'];
function interceptor($q, vnApp, $cookies) {
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;
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);