Caching adapter for axios.
- Support
localStorage
、sessionStorage
、memory
mode - Support each request to be configured
- Rerequest when the request parameter is inconsistent with the last request parameter
Using npm:
npm install axios-storage --save
Using cdn:
<script src="https://unpkg.com/axios-storage/dist/axios-storage.js"></script>
You can use the axios-storage directly
import axios from 'axios';
import AxiosStorage from 'axios-storage';
// set global config
AxiosStorage.config({
storagePrefix: 'axios-storage',
storageMode: 'sessionStorage',
maxAge: 120 * 60 * 1000
});
const api = axios.create({
adapter: AxiosStorage.adapter
});
api({
method: 'get',
url: '/data',
cacheConfig: {
maxAge: 60 * 60 * 1000,
storageMode: 'sessionStorage'
}
})
.then(function(res){
console.log(res);
})
api({
method: 'get',
url: '/data/other',
cacheConfig: {
maxAge: 60 * 60 * 1000,
storageMode: 'localStorage'
}
})
.then(function(res){
console.log(res);
})
// or use global config
api({
method: 'get',
url: '/data/other',
cacheConfig: true
})
.then(function(res){
console.log(res);
})
#------------include------------#
cd example && npm install
node app.js
after that,browser open http://localhost:3000/