cacheable-request
Advanced tools
Comparing version 2.0.1 to 2.1.0
{ | ||
"name": "cacheable-request", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Wrap native HTTP requests with RFC compliant cache support", | ||
@@ -46,3 +46,3 @@ "main": "src/index.js", | ||
"@keyv/sqlite": "^1.2.6", | ||
"ava": "^0.22.0", | ||
"ava": "^0.23.0", | ||
"coveralls": "^3.0.0", | ||
@@ -49,0 +49,0 @@ "create-test-server": "^2.0.0", |
@@ -159,11 +159,17 @@ # cacheable-request | ||
Database connection errors are emitted here, however `cacheable-request` will attempt to re-request the resource and bypass the cache on a connection error. Therefore a database connection error doesn't necessarily mean the request won't be fulfilled. | ||
Errors emitted here will be an instance of `CacheableRequest.RequestError` or `CacheableRequest.CacheError`. You will only ever receive a `RequestError` if the request function throws (normally caused by invalid user input). Normal request errors should be handled inside the `request` event. | ||
**Note:** You should still handle request errors in the `request` event. e.g: | ||
To properly handle all error scenarios you should use the following pattern: | ||
```js | ||
cacheableRequest('example.com', cb) | ||
.on('error', handleCacheError) | ||
.on('error', err => { | ||
if (err instanceof CacheableRequest.CacheError) { | ||
handleCacheError(err); // Cache error | ||
} else if (err instanceof CacheableRequest.RequestError) { | ||
handleRequestError(err); // Request function thrown | ||
} | ||
}) | ||
.on('request', req => { | ||
req.on('error', handleRequestError); | ||
req.on('error', handleRequestError); // Request error emitted | ||
req.end(); | ||
@@ -173,4 +179,6 @@ }); | ||
**Note:** Database connection errors are emitted here, however `cacheable-request` will attempt to re-request the resource and bypass the cache on a connection error. Therefore a database connection error doesn't necessarily mean the request won't be fulfilled. | ||
## License | ||
MIT © Luke Childs |
@@ -49,3 +49,3 @@ 'use strict'; | ||
madeRequest = true; | ||
const req = request(opts, response => { | ||
const handler = response => { | ||
if (revalidate) { | ||
@@ -80,6 +80,6 @@ const revalidatedPolicy = CachePolicy.fromObject(revalidate.cachePolicy).revalidatedPolicy(opts, response); | ||
}) | ||
.catch(err => ee.emit('error', err)); | ||
.catch(err => ee.emit('error', new CacheableRequest.CacheError(err))); | ||
} else if (opts.cache && revalidate) { | ||
this.cache.delete(key) | ||
.catch(err => ee.emit('error', err)); | ||
.catch(err => ee.emit('error', new CacheableRequest.CacheError(err))); | ||
} | ||
@@ -91,4 +91,10 @@ | ||
} | ||
}); | ||
ee.emit('request', req); | ||
}; | ||
try { | ||
const req = request(opts, handler); | ||
ee.emit('request', req); | ||
} catch (err) { | ||
ee.emit('error', new CacheableRequest.RequestError(err)); | ||
} | ||
}; | ||
@@ -121,3 +127,3 @@ | ||
this.cache.on('error', err => ee.emit('error', err)); | ||
this.cache.on('error', err => ee.emit('error', new CacheableRequest.CacheError(err))); | ||
@@ -128,3 +134,3 @@ get(opts).catch(err => { | ||
} | ||
ee.emit('error', err); | ||
ee.emit('error', new CacheableRequest.CacheError(err)); | ||
}); | ||
@@ -137,2 +143,18 @@ | ||
CacheableRequest.RequestError = class extends Error { | ||
constructor(err) { | ||
super(err.message); | ||
this.name = 'RequestError'; | ||
Object.assign(this, err); | ||
} | ||
}; | ||
CacheableRequest.CacheError = class extends Error { | ||
constructor(err) { | ||
super(err.message); | ||
this.name = 'CacheError'; | ||
Object.assign(this, err); | ||
} | ||
}; | ||
module.exports = CacheableRequest; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13670
134
183