AxiosSugar
An axios wrapper, which supports resending, store and so on
Usage
import axiosSugar from 'axiosSugar';
import axios from 'axios';
import axiosSugar from 'axiosSugar';
const ins = axios.create();
axiosSugar.axiosInstance = ins;
axios.get();
ins.get();
Callback
You can overrid the callback to achieve some goals, e.g.:
axiosSugar.onExisted = function (error) {
console.log(error.reason);
}
The base properties of the error argument is reason and message.
onExisted
The argument of this callback is an error.
onSaved
The argument of this callback is an error, with an additional property res: AxiosResponse
Config
You can use setConfig
to change the configure.
axiosSugar.setConfig(conf);
The conf is:
const defaultConfig = {
resend: false,
resendDelay: 200,
resendNum: 3,
compareRule: 1,
store: false
};
Compare Rule
1: use the JSON.stringify(AxiosRequestConfig)
as symbol
2: use the rid
as symbol, e.g.:
axios.request({
url: '',
method: '',
reqConf: {
rid: ''
}
});
reqConf
this is a property injected in each AxiosRequestConfig
:
{
reqConf: {
rid: '',
resendCount: 0
}
}
You can change the key name
axiosSugar.injectProp = 'injectProp';
{
injectProp: {
rid: '',
resendCount: 0
}
}
Store
In default, Store will save the response in an object variable:
{
symbol: response
}
The Store is an single object, you can override this Store's function(save, get, contains)
to customize:
{
data: {},
save (symbol: csymbol, res: AxiosResponse) {
this.data[symbol] = res;
},
get (symbol: csymbol): any {
return this.data[symbol];
},
contains (key): boolean {
return typeof this.data[key] !== 'undefined';
}
}