fetchcacher
v1.0.0-alpha
Published
A browser caching library that provides fluent apis to cache json,blob,text and arrayBuffer data.
Downloads
2
Maintainers
Readme
FetchCache
A browser caching library that provides fluent apis to cache json,blob,text and arrayBuffer data.
Key Features
- The response data will be stored in browser cache.
- It can be either localStorage or IndexedDB , it will be decided based on response type and length.
- The callback function will be called immediately if data is available in browser cache.
- The callback will be called again only if the actual response is different from the cached response.
- 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
- 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);
});