Skip to content

Use LocalStorage as Cache when read an external service or API

Notifications You must be signed in to change notification settings

dientuki/localstorage-cache-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

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

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

About

Use LocalStorage as Cache when read an external service or API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published