2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2018-05-25 15:25:35 +00:00
|
|
|
import HttpError from './http-error';
|
2017-06-05 07:01:21 +00:00
|
|
|
|
2018-08-02 07:49:00 +00:00
|
|
|
interceptor.$inject = ['$q', 'vnApp', '$cookies', '$translate'];
|
|
|
|
function interceptor($q, vnApp, $cookies, $translate) {
|
2017-06-05 07:01:21 +00:00
|
|
|
return {
|
|
|
|
request: function(config) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.pushLoader();
|
2017-06-05 07:01:21 +00:00
|
|
|
let token = $cookies.get('vnToken');
|
|
|
|
|
|
|
|
if (token)
|
|
|
|
config.headers.Authorization = token;
|
|
|
|
|
2018-08-02 07:49:00 +00:00
|
|
|
if ($translate.use())
|
|
|
|
config.headers['Accept-Language'] = $translate.use();
|
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
return config;
|
|
|
|
},
|
|
|
|
requestError: function(rejection) {
|
|
|
|
return $q.reject(rejection);
|
|
|
|
},
|
|
|
|
response: function(response) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.popLoader();
|
2017-06-05 07:01:21 +00:00
|
|
|
return response;
|
|
|
|
},
|
|
|
|
responseError: function(rejection) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.popLoader();
|
2018-05-25 15:25:35 +00:00
|
|
|
let err = new HttpError(rejection.statusText);
|
|
|
|
Object.assign(err, rejection);
|
|
|
|
return $q.reject(err);
|
2017-06-05 07:01:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.factory('vnInterceptor', interceptor);
|