2.0.2 • Published 6 years ago

fetch-unless-cached v2.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

fetch-unless-cached

Store fetch JSON responses in localStorage with expire timers! And fetch only if the timer has expired.

npm.io

npm npm

What is it really?

  • A wrapper on top isomorphic-fetch for JSON responses
  • When data is fetched, it's stored in localStorage with an expire timer
  • When data is reqeusted, it checks in the storage and only fetches if needed, otherwise it resolves the cached data.
  • Optionally, the api call can be made when the browser is idle and the timer is udpated.

Installation

npm i --save fetch-unless-cached

Usage

  1. Use the inbuilt cached fetch which caches response for 600 minutes.

    import cachedFetch from "fetch-unless-cached";
  2. Or create a custom cached fetch function with your own duration

import {createfetchUnlessCached} from "fetch-unless-cached"

/**
 * Create a custom fetch function which caches response for 300 minutes
 */
const cachedFetch = createfetchUnlessCached(300)

function fetchMyData(){
  /*
   * cachedFetch is just isomorphic-fetch but coupled with cache
   * Do not perform .then(res => res.json()) as this happens internally
   *
   */
  ...
  return cachedFetch('myapi.com').then(response => console.log(response))
}

Note: If you would like to fetch during an idle state you can set the second argument to true.

const cachedFetch = createfetchUnlessCached(300, true);

Credits

  • isomorphic-fetch - Fetch api for node and browser
  • lscache - localStorage caching with timers

Caveats

  • Works only with JSON responses
2.0.2

6 years ago

2.0.0

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago