salix/front/core/services/interceptor.js

33 lines
1003 B
JavaScript

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) {
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) {
vnApp.popLoader();
return response;
},
responseError: function(rejection) {
vnApp.popLoader();
let err = new HttpError(rejection.statusText);
Object.assign(err, rejection);
return $q.reject(err);
}
};
}
ngModule.factory('vnInterceptor', interceptor);