salix/front/core/services/interceptor.js

51 lines
1.5 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', '$translate'];
function interceptor($q, vnApp, $translate) {
let apiPath = 'api/';
let token;
return {
setToken(newToken) {
token = newToken;
},
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 (token)
config.headers.Authorization = 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();
2022-06-14 08:16:41 +00:00
const newVersion = response.headers('salix-version');
2022-06-16 06:25:38 +00:00
vnApp.setVersion(newVersion);
return response;
},
responseError(rejection) {
2017-11-16 12:47:18 +00:00
vnApp.popLoader();
const err = new HttpError(rejection.statusText);
Object.assign(err, rejection);
return $q.reject(err);
2022-06-14 08:16:41 +00:00
},
};
}
2018-02-10 15:18:01 +00:00
ngModule.factory('vnInterceptor', interceptor);