yarn add react-native-xfetch
init XFetch:
1) import {XFetch, XFetchConfig} from "react-native-xfetch";
2)
//set all request heads
const commonHeader = {
'Content-Type': 'application/json',
'platform': 'android',
'deviceId': '6f580xxxxxx-e7aaaaaaaa0'
};
XFetchConfig.getInstance()
.setBaseUrl('Your host')
.setCommonHeaders(commonHeader)
.setCommonTimeOut(30000)
//here, you can monitor the response results of all requests.
.setResponseConfig(this.handleResponse)
//param 1: isTokenExpired? , param 2: refreshToken http , param 3: refreshToken response
.setRefreshTokenConfig(this.checkTokenExpired, this.refreshToken, (promise) => {
...
})
...
handleResponse = (isResponseSuccess, url, resolve, reject, data) =>{
if (isResponseSuccess) {
}else {
}
};
checkTokenExpired = () => isTokenExpired;
refreshToken = () => new XFetch().post('refresh_token') //please do not use the do() here.
normal use:
import {XFetch} from "react-native-xfetch";
let promise = new XFetch().get('your address').do().then((res) => {
...
}).catch((error) => {
...
});