Use LocalStorage as Cache when read an external service or API
Install package with NPM
npm install --save localstorage-cache-api
import LocalStorageCacheApi from 'localstorage-cache-api';
const LS = new LocalStorageCacheApi({
url: 'http://www.example.com/service',
key: 'myLocalStoragekey'
});
LS.getData().then((response) => {
// your code goes here
});
Option | Type | Description |
---|---|---|
url |
String | (Required) Url |
key |
String | (Required) Key to use in the LocalStore |
expiration |
Integer | Expiration time (in miliseconds) if is necesary |
callback |
Function | Callback to run before save the data in localStorage |
LocalStorageApi use the fetch API, so you can modify the headers through a second parameter in the constructor.
import LocalStorageCacheApi from 'localstorage-cache-api';
const LS = new LocalStorageCacheApi({
url: 'http://www.example.com/service',
key: 'mykey',
expiration: 60 * 60 * 1000,
callback: function(value) {
return `new value is ${value}`;
}
});
LS.getData().then((response) => {
// your code goes here
});
import LocalStorageCacheApi from 'localstorage-cache-api';
const LS = new LocalStorageCacheApi({
url: 'http://www.example.com/service',
key: 'mykey',
expiration: 60 * 60 * 1000
});
LS.getData().then((response) => {
// your code goes here
});
import LocalStorageCacheApi from 'localstorage-cache-api';
const LS = new LocalStorageCacheApi({
url: 'http://www.example.com/service',
key: 'mykey'
}, {
mode: 'cors'
});
LS.getData().then((response) => {
// your code goes here
});