1.3.8 • Published 7 years ago

axios-storage v1.3.8

Weekly downloads
125
License
MIT
Repository
github
Last release
7 years ago

axios-storage

NPM

Language License

English | 中文

Caching adapter for axios.

Feature

  • Support localStoragesessionStoragememory mode
  • Support each request to be configured
  • Rerequest when the request parameter is inconsistent with the last request parameter

Install

Using npm:

npm install axios-storage --save

Using cdn:

<script src="https://unpkg.com/axios-storage/dist/axios-storage.js"></script>

Usage

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);
})

API

AxiosStorage.config(options)

global config options, see all options

ParamTypeDefaultDescription
optionsobject
options.storagePrefixstring"axios-storage"thhe prefix of storage
options.storageModestring"sessionStorage"the mode of storage,support localStoragesessionStoragememory
options.deleteOnExpirestring"aggressive"how to handler expired storage

Example

import axios from 'axios';
import AxiosStorage from 'axios-storage';

AxiosStorage.config({
  storagePrefix: 'axios-storage-example:',
  storageMode: 'sessionStorage'
});

AxiosStorage.adapter()

adapter

Example

import axios from 'axios';
import AxiosStorage from 'axios-storage';

const api = axios.create({
  adapter: AxiosStorage.adapter
});

api.get(...)
api.post(...)

AxiosStorage.getCache(options) ⇒ object

Cache Object

Returns: object - Cache,see detail Cache

ParamTypeDefaultDescription
optionsobject | string
options.storageModestring"sessionStorage"storage mode

Example

let oCache = AxiosStorage.getCache('localStorage');

oCache.put('foo', 'bar');
oCache.get('foo'); // "bar"
...

// request data with cacheConfig
api({
  method: 'GET',
  url: '/data/other',
  cacheConfig: {
    maxAge: 60 * 60 * 1000,
    storageMode: 'localStorage'
  }
})
.then((res) => {
   console.log(res)
})

// get this request cache
let res = oCache.get('GET./data/other') // `res` is the same as above

oCache.get('[method].[url]') // `method` is uppercase, GET、POST etc.

Example

example

cd example && npm install
node app.js

after that,browser open http://localhost:3000/

Thanks

cachefactory

License

license

1.3.8

7 years ago

1.3.7

7 years ago

1.3.6

7 years ago

1.3.5

7 years ago

1.3.4

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago