0.0.6 • Published 5 years ago

localstorage-cache-api v0.0.6

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

localStorage-cache-api

Use LocalStorage as Cache when read an external service or API

Installation

Install package with NPM

npm install --save localstorage-cache-api

Basic Usage

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

Configuration

Options

OptionTypeDescription
urlString(Required) Url
keyString(Required) Key to use in the LocalStore
expirationIntegerExpiration time (in miliseconds) if is necesary
callbackFunctionCallback to run before save the data in localStorage

Headers

LocalStorageApi use the fetch API, so you can modify the headers through a second parameter in the constructor.

Recipes

Adding a callback to modify the response before save it to LocalStorage

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

Adding an expiration time

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

Adding a header to the Fetch API

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

Disclaimers