npm.io
2.0.3 • Published 7 years ago

@ryanburnette/api-cache-proxy

Licence
ISC
Version
2.0.3
Deps
2
Size
5 kB
Vulns
0
Weekly
0
DeprecatedThis package is deprecated

@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 to 5000 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);
  });