fetchcacher v1.0.0-alpha
FetchCache
A browser caching library that provides fluent apis to cache json,blob,text and arrayBuffer data.
Key Features 1. The response data will be stored in browser cache. 2. It can be either localStorage or IndexedDB , it will be decided based on response type and length. 3. The callback function will be called immediately if data is available in browser cache. 4. The callback will be called again only if the actual response is different from the cached response. 5. The callback will be called with three paramertes, i) Blob,String,Object,ArrayBuffer type converted response body based on which method is invoked ii) Boolean true if network response is different from cached response iii) Response wrapper of original response - can be used to perform any operation with the original response 6. The catch callback can be used to handle errors.
Installation
npm install fetchcache
Usage:
import cache from fetchcache;
..... cache.fetch("http://localhost:3000/v1/json") .json((data, hasDataModified, originalResponse) => { if (hasDataModified || originalResponse.status == 200) document.body.innerHTML = document.body.innerHTML + "" + JSON.stringify(data); } ).catch(error => { console.log(error); })
cache.fetch("http://localhost:3000/v1/blob")
.blob((data, hasDataModified) => {
blobtobase64(data).then(str => document.body.innerHTML = document.body.innerHTML + "<br><br>" + str);
}).catch(error => {
console.log(error);
})
cache.fetch("http://localhost:3000/v1/text")
.text((data, hasDataModified) => {
document.body.innerHTML = document.body.innerHTML + "<br><br>" + data;
}).catch(error => {
console.log(error);
});
cache.fetch("http://localhost:3000/v1/arrayBuffer")
.arrayBuffer((data, hasDataModified) => {
document.body.innerHTML = document.body.innerHTML + "<br><br>" + new Uint8Array(data);
});
2 years ago