XHR is configurable based on axios ⏲ 🚀
# npm
npm install axhr axios --save
# yarn
yarn add axhr axios
xhr({
url: '/add',
type: 'GET',
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
baseUrl: 'https://some-domain.com/api/',
data: {},
config: {
...others
},
cancelMsg: '',
success: (res, response) => {},
error: res => {},
cancel: err => {},
});
- url:
url [required]
is the server URL that will be used for the request - type:
type [required]
is the request method to be used when making the request, defaultGET
- header:
header
are custom headers to be sent, default'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
- baseUrl:
baseURL
will be prepended tourl
unlessurl
is absolute. - data:
data
is the data to be sent as the request body - success: Callback after successful request and
xhr.success
intercept returns true - error: Callback after failed request or
xhr.success
intercept returns false - cancel: Callback after cancel request
- cancelMsg: cancel request message
- config: refer to https://github.com/axios/axios#request-config
- cancelToken:
false
, don't cancel request; can also set unique token - noRepeat:
false
, can repeat request, custom prop,cancelToken
isn'tfalse
;iftrue
cancel before pending request
- cancelToken:
global definition method
Global configuration, will merge to
config
xhr.defaultConfig = {
timeout: 10000,
withCredentials: true // cookie
};
// or
xhr.defaultConfig = () => {
return {
timeout: 10000,
withCredentials: true // cookie
}
}
Global basic params, will merge to
config.data
xhr.baseData = {
t: Date.now() // IE catch
};
global baseUrl, Priority less than
xhr.getUrl
Implementing dynamic url, @params{config}
const apiBaseUrl = '/oapi';
xhr.getUrl = option => {
if (option.baseUrl) {
return {
baseUrl: option.baseUrl,
url: option.url
};
}
return {
baseUrl: apiBaseUrl,
url: option.url
};
};
Implement dynamic interception configuration when the request is successful
You can do some global logic
xhr.success = res => boolean
Implement dynamic interception configuration when the request is failed
xhr.error = (err, [isCancel]) => {}
cancel request
Can specify a URL to cancel some request; no hanve urls, all requests will be cancelled
xhr.cancelXhr(message, urls?: []);
Execute before request,
xhr.before = () => {}
Execute end request
xhr.end = (res) => {}
import xhr from 'axhr';
import {message} from 'antd';
import auth from './auth';
let apiUrl = '';
const apiBaseUrl = '/';
xhr.getUrl = option => {
if (option.baseUrl) {
apiUrl = option.baseUrl + option.url;
return {
baseUrl: option.baseUrl,
url: option.url
};
}
apiUrl = apiBaseUrl + option.url;
return {
baseUrl: apiBaseUrl,
url: option.url
};
};
xhr.baseData = {
t: Date.now()
};
xhr.defaultConfig = {
timeout: 10000,
withCredentials: true
};
xhr.success = (res, response) => {
let isSuccess = true;
if (typeof res !== 'object') {
message.error(apiUrl + ': response data should be JSON');
isSuccess = false;
}
switch (res.code + '') {
case '000000':
isSuccess = true;
break;
case '100006':
auth.toOutLogin();
isSuccess = false;
break;
case '100013':
auth.toOutLogin();
isSuccess = false;
break;
default:
message.error(res.message || 'unknown error');
isSuccess = false;
}
return isSuccess;
};
xhr.error = (err) => {
message.error('The server strayed!');
};