
AxiosSugar
An axios wrapper, which supports resending, storage, cancel repeat request automatically; and so on.
Usage
import axios from 'axios'
import factory from 'axios-sugar'
factory(axios);
const ins = axios.create();
factory(ins);
import axios from 'axios'
import { AxiosSugar } from 'axios-sugar'
const axiosSugar = new AxiosSugar(axios);
axios.get();
ins.get();
Config
import axios from 'axios'
import factory, { AxiosSugarConfig } from 'axios-sugar'
let options = {
isResend: true,
isSave: true
}
factory(axios, {
config: new AxiosSugarConfig(options)
});
AxiosSugarConfigOptions
{
isResend?: boolean;
resendDelay?: number;
resendTimes?: number;
isSave?: boolean;
prop?: string;
}
prop is a property injected in the config of axios:
import axios from 'axios'
import factory, { AxiosSugarConfig } from 'axios-sugar'
let options = {
isResend: true,
isSave: true
}
factory(axios, {
config: new AxiosSugarConfig(options)
});
axios.get('/path', {
custom: {
isSave: false
}
})
axios.get('/path')
Storage
This library supports two way to save the response data(res.data in axios.then)
.
InnerStorage
This storage will be save the data in a object variable.
import axios from 'axios'
import factory, { AxiosSugarInnerStorage } from 'axios-sugar'
let options = {
isSave: true
}
factory(axios, {
config: new AxiosSugarConfig(options),
storage: new AxiosSugarInnerStorage()
});
InnerReleaseStorage
Unlike InnerStorage, it can automatically free up memory.
import axios from 'axios'
import factory, { AxiosSugarInnerReleaseStorage } from 'axios-sugar'
let options = {
isSave: true
}
factory(axios, {
config: new AxiosSugarConfig(options),
storage: new AxiosSugarInnerReleaseStorage()
});
LocalStorage
This storage will be save the data in localStorage.
import axios from 'axios'
import factory, { AxiosSugarLocalStorage } from 'axios-sugar'
let options = {
isSave: true
}
factory(axios, {
config: new AxiosSugarConfig(options),
storage: new AxiosSugarLocalStorage()
});
Custom Storage
You can implements the interface AxiosSugarStorage to customize your storage.
// The interface is:
interface AxiosSugarStorage {
set (symbol: string, res: any): void;
get (symbol: string): any;
contains (symbol: string): boolean;
}
// custom:
import axios from 'axios'
import factory from 'axios-sugar'
let options = {
isSave: true
}
class CustomStorage {
set (symbol: string, res: any);
get (symbol: string);
contains (symbol: string);
}
factory(axios, {
config: new AxiosSugarConfig(options),
storage: new CustomStorage()
});
Lifecycle
The lifecycle is some callback.
import axios from 'axios'
import factory{ AxiosSugarLifeCycle } from 'axios-sugar'
let cycle = new AxiosSugarLifeCycle();
cycle.beforeRequest = (conf) => {
return false
}
factory(axios, {
lifecycle: cycle
})
callback
The callback of lifecycle is:
beforeRequest(conf: AxiosRequestConfig): AxiosSugarLifeCycleResult;
beforeResponse(res: AxiosResponse): AxiosSugarLifeCycleResult;
All callback should return an AxiosSugarLifeCycleResult, it is a object:
{
state: false,
message: '',
}
AxiosSugarError
The AxiosSugarError is:
{
reason: string;
message?: any;
data?: any;
}
AxiosSugarError Handler
You can catch the errors in axios.catch
axios.get()
.catch(err => {
if (err.readon) {
console.log(err)
}
else {
}
})
Test
Node test:
npm test
Some browser test(e.g. AxiosSugarLocalStorage)
:
open the corresponding index.html
File in /test
which built by mocha.
TODO
- Broken network retransmission
- Broken network lifecycle
Change Log
v1.1.3-0 export default a factory instead of exporting an AxiosSugar class