@ryanburnette/request-cache
v4.0.0
Published
Add caching mechanisms to your favorite HTTP request library.
Downloads
3
Readme
@ryanburnette/request-cache
Add caching mechanisms to your favorite HTTP request library.
Request Libraries
Choose your preferred library that provides HTTP request functionality. Some are supported now, others are on the roadmap.
At the moment, callback-based libraries must be converted to Promises using
util.promisify
.
- [x] Request - Simplified HTTP client
- [x] µRequest
- [ ] axios
- [ ] node-fetch
Caching Mechanisms
- [x] Library Options
- [x] Expires header
- [ ] ETag
- [ ] Cache-Control
Library Options
Library options take precedence over all other caching mechanisms. For example,
if the Expires header sets an expiration time of one hour from now, but the
minAge
option is set to two hours, the Expires header will be ignored and a
subsequent request will not be made until the two hours has elapsed. If
minAge
were set to 30 minutes, the Expires header would be respected. This
logic is reversed for maxAge
.
Expires Header
The Expires header is respected unless minAge
or maxAge
is less or more
restrictive as appropriate.
Installation
npm install @ryanburnette/request-cache
You'll need to install and include a request library. Libraries structured using callbacks should be promisified.
npm install --save @root/request
Usage
var util = require('util');
var RequestCache = require('@ryanburnette/request-cache');
var request = RequestCache.create({
request: util.promisify(require('@root/request'))
});
request({ url: 'https://example.com/api/items' })
.then(function (resp) {
console.log(resp.statusCode);
});
console.log(request.cachedUrls);
request.clearCache('https://example.com/api/items');
Development/Testing
Tests are writting in Jest, but the library is not saved to
devDependencies
. Install request library and test library manually for
development.
npm install --no-save @root/request jest
Then watch for changes, rerunning tests.
npm run test -- --watch