2.0.3 • Published 5 years ago
@ryanburnette/api-cache-proxy v2.0.3
@ryanburnette/api-cache-proxy
Create a fetch function that is intended to be used as a caching proxy for a JSON API.
Specific to my use case.
- caches requests to the proxied api up to
maxAge
which defaults to5000
to avoid cache stampede - supports http basic auth as shown in example
- body will be obj (parsed json) if JSON is returned
- options starting with
_
are stripped before being passed to request - minimal dependencies
var ApiCacheProxy = require('@ryanburnette/api-cache-proxy');
var fetch = new ApiCacheProxy({
_maxAge: 5000, //default
_basicAuth: {
username: 'username',
password: 'password'
},
headers: {
'Content-Type': 'application/json'
}
});
var url = 'https://jsonplaceholder.typicode.com/posts';
fetch({url})
.then(function ({res,body}) {
console.log(res,body);
});