Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/**
* 基础服务类封装
* BaseService
*/
import fetchIntercept from 'fetch-intercept'
import constants from '../common/constants';
import ServiceError from '../common/service-error';
import code from '../common/code';
const fetchApi = fetch; // eslint-disable-line
fetchIntercept.register({
request: function (url, config) {
return [url, config];
},
requestError: function (error) {
return Promise.reject(error);
},
response: function (res) {
return res;
},
responseError: function (error) {
return Promise.reject(error);
}
});
export default class BaseService {
constructor(props){
export function registerInterceptor(next = null) {
nprogress.configure({ showSpinner: false });
const authService = container.get(cid.IAuthService);
fetchIntercept.register({
request: function (url, config) {
nprogress.start();
config = config || {};
const headers = config.headers || {};
config.headers = {
...headers,
'Content-Type': 'application/json',
'x-api-version': '2.0',
Authorization: `Bearer ${authService.user.access_token}`
};
return [url, config];
},
requestError: function (error) {
}
return _fetch.call(this, ...[localUrl, ...args]);
};
fetch.prototype = Object.create(_fetch.prototype);
fetch.prototype.constructor = fetch;
global['fetch'] = fetch;
} else {
require('whatwg-fetch');
}
const fetchIntercept = require('fetch-intercept');
fetchIntercept.register({
request: function (url, defaultConfig) {
if (!url.includes || !url.includes(store.state.config.settings.apiUrl)) {
return [url, defaultConfig];
}
const config = defaultConfig || {};
config.headers = config.headers || {};
config.headers['Accept-Language'] = app.i18n.locale || 'en';
return [url, config];
},
requestError: (error) => {
return Promise.reject(error);
},
export function initInterceptor(store: Store): VoidFunction {
return fetchIntercept.register({
request(url, config: RequestConfig = {}) {
const { toolName, context, endpoint } = extrapolateFromUrl(url);
const { credentials } = store.getState();
if (config.body) {
config.body = JSON.stringify(config.body);
}
const baseHeaders = getHeaders(toolName, credentials);
if (config.headers) {
config.headers = {
...config.headers,
...baseHeaders,
};
} else {