salix/front/core/lib/interceptor.js

35 lines
1.0 KiB
JavaScript

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) {
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) {
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);