2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2019-01-23 12:11:44 +00:00
|
|
|
import HttpError from 'core/lib/http-error';
|
2017-06-05 07:01:21 +00:00
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
interceptor.$inject = ['$q', 'vnApp', 'vnToken', '$translate'];
|
|
|
|
function interceptor($q, vnApp, vnToken, $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
|
|
|
|
2019-01-23 12:11:44 +00:00
|
|
|
if (vnToken.token)
|
|
|
|
config.headers.Authorization = vnToken.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);
|