cacheable-request
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@types/http-cache-semantics | 4.0.4 | 3.64 kB | MIT | prod | |
get-stream | 6.0.1 | 4.17 kB | MIT | prod | |
http-cache-semantics | 4.1.1 | 10.56 kB | BSD-2-Clause | prod | |
keyv | 4.5.4 | 8.42 kB | MIT | prod | |
mimic-response | 4.0.0 | 2.68 kB | MIT | prod | |
normalize-url | 8.0.1 | 25.25 kB | MIT | prod | |
responselike | 3.0.0 | 2.16 kB | MIT | prod |
Cacheable-Request is a popular npm package used to wrap native HTTP requests with RFC compliant cache support. It enables you to add caching capabilities to native Node.js HTTP/HTTPS requests in compliance with RFC 7234. This package works out of the box in memory, or you can easily plug in various storage adapters. What's great about it is that it only stores cacheable responses defined by RFC 7234, provides support for a broad variety of storage adapters, and it automatically bypasses the cache if DB connection fails.
To use Cacheable-Request, you first need to install it via npm using npm install cacheable-request
. After that, you can import it in your code and wrap your http request with cache support as shown in the following example:
import http from 'http';
import CacheableRequest from 'cacheable-request';
// Instead of
const req = http.request('http://example.com', cb);
req.end();
// Wrap the request with cache support
const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
const cacheReq = cacheableRequest('http://example.com', cb);
cacheReq.on('request', req => req.end());
In the above example, future requests to example.com
will be returned from cache if still valid.
This package also allows you to add additional request methods that are API compatible, such as https.request
and electron.net
, and wrap them with cache support as follows:
const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
Also, you can add hooks to modify responses before saving them in the cache.
The cacheable-request docs and further information about the package can be found in its GitHub page: Cacheable-Request GitHub. The documentation includes its latest changes, installation and usage guidelines, API details, information on how to use hooks, and more. If you want to contribute, ask questions or report issues, guidelines and protocols are provided as well.