salix/front/core/services/interceptor.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-02-10 15:18:01 +00:00
import ngModule from '../module';
import HttpError from 'core/lib/http-error';
interceptor.$inject = ['$q', 'vnApp', 'vnToken', '$translate'];
function interceptor($q, vnApp, vnToken, $translate) {
let apiPath = 'api/';
return {
setApiPath(path) {
apiPath = path;
},
request(config) {
2017-11-16 12:47:18 +00:00
vnApp.pushLoader();
if (config.url.charAt(0) !== '/' && apiPath)
2019-11-10 10:08:44 +00:00
config.url = `${apiPath}${config.url}`;
if (vnToken.token)
config.headers.Authorization = vnToken.token;
if ($translate.use())
config.headers['Accept-Language'] = $translate.use();
2019-11-10 10:08:44 +00:00
if (config.filter) {
if (!config.params) config.params = {};
config.params.filter = config.filter;
}
return config;
},
requestError(rejection) {
return $q.reject(rejection);
},
response(response) {
2017-11-16 12:47:18 +00:00
vnApp.popLoader();
return response;
},
responseError(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);