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) {
|
2019-10-24 22:53:53 +00:00
|
|
|
let apiPath = 'api/';
|
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
return {
|
2019-10-24 22:53:53 +00:00
|
|
|
setApiPath(path) {
|
|
|
|
apiPath = path;
|
|
|
|
},
|
|
|
|
request(config) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.pushLoader();
|
2017-06-05 07:01:21 +00:00
|
|
|
|
2019-10-24 22:53:53 +00:00
|
|
|
if (config.url.charAt(0) !== '/' && apiPath)
|
2019-11-10 10:08:44 +00:00
|
|
|
config.url = `${apiPath}${config.url}`;
|
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();
|
2019-11-10 10:08:44 +00:00
|
|
|
if (config.filter) {
|
|
|
|
if (!config.params) config.params = {};
|
|
|
|
config.params.filter = config.filter;
|
|
|
|
}
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2017-06-05 07:01:21 +00:00
|
|
|
return config;
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
requestError(rejection) {
|
2017-06-05 07:01:21 +00:00
|
|
|
return $q.reject(rejection);
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
response(response) {
|
2017-11-16 12:47:18 +00:00
|
|
|
vnApp.popLoader();
|
2017-06-05 07:01:21 +00:00
|
|
|
return response;
|
|
|
|
},
|
2019-10-24 22:53:53 +00:00
|
|
|
responseError(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);
|