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) { vnApp.pushLoader(); if (config.url.charAt(0) !== '/' && apiPath) config.url = `${apiPath}${config.url}`; if (vnToken.token) config.headers.Authorization = vnToken.token; if ($translate.use()) config.headers['Accept-Language'] = $translate.use(); if (config.filter) { if (!config.params) config.params = {}; config.params.filter = config.filter; } return config; }, requestError(rejection) { return $q.reject(rejection); }, response(response) { vnApp.popLoader(); return response; }, responseError(rejection) { vnApp.popLoader(); let err = new HttpError(rejection.statusText); Object.assign(err, rejection); return $q.reject(err); } }; } ngModule.factory('vnInterceptor', interceptor);