Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imbhargav5/fetch-unless-cached
Store fetch responses in localStorage with expire timers! And fetch when idle and update cache in the background
https://github.com/imbhargav5/fetch-unless-cached
isomorphic-fetch json lscache
Last synced: about 1 month ago
JSON representation
Store fetch responses in localStorage with expire timers! And fetch when idle and update cache in the background
- Host: GitHub
- URL: https://github.com/imbhargav5/fetch-unless-cached
- Owner: imbhargav5
- License: mit
- Created: 2018-02-26T10:34:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-04-02T14:33:14.000Z (over 6 years ago)
- Last Synced: 2024-06-21T06:37:38.944Z (5 months ago)
- Topics: isomorphic-fetch, json, lscache
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fetch-unless-cached
- Size: 214 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fetch-unless-cached
Store fetch JSON responses in localStorage with expire timers! And fetch only if the timer has expired.
[![](https://nodei.co/npm/fetch-unless-cached.png?compact=true)](https://nodei.co/npm/fetch-unless-cached/)
[![npm](https://img.shields.io/npm/dm/fetch-unless-cached.svg?style=for-the-badge)](https://www.npmjs.com/package/fetch-unless-cached)
[![npm](https://img.shields.io/npm/l/fetch-unless-cached.svg?style=for-the-badge)](https://www.npmjs.com/package/fetch-unless-cached)## 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
```shell
npm i --save fetch-unless-cached
```## Usage
1. Use the inbuilt cached fetch which caches response for 600 minutes.
```javascript
import cachedFetch from "fetch-unless-cached";
```2. Or create a custom cached fetch function with your own duration
```javascript
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.
```javascript
const cachedFetch = createfetchUnlessCached(300, true);
```### Credits
* [x] isomorphic-fetch - Fetch api for node and browser
* [x] lscache - localStorage caching with timers## Caveats
* [x] Works only with JSON responses