@bbc/http-transport
Advanced tools
Comparing version 4.5.1 to 4.6.0
@@ -103,2 +103,3 @@ import * as fetch from 'node-fetch'; | ||
retryDelay(retryDelay: number): HttpTransportBuilder<ContextCurrent>; | ||
criticalErrorDetector(criticalErrorDetector: (err, ctx: Context) => boolean); | ||
use<ContextExtra = {}>( | ||
@@ -145,3 +146,3 @@ fn: Plugin<ContextExtra, ContextCurrent> | ||
asResponse<ResponseBody = ContextCurrent["res"]["body"]>(): Promise< | ||
AsResponse<ContextCurrent["res"], ResponseBody> | ||
AsResponse<ContextCurrent["res"], ResponseBody> | ||
>; | ||
@@ -148,0 +149,0 @@ } |
@@ -73,2 +73,32 @@ 'use strict'; | ||
/** | ||
* default the criticalErrorDetector which parses errors and decides if they are critical. This mainly enables retry logic. | ||
* @param {function} criticalErrorDetector (err, ctx) => bool - a function that takes the error and the context and returns a boolean which evaluates whether an error is a critical error. | ||
* This is useful if you want to customise error behaviour. See below for example that prevents circuitBreaker errors from being classed as critical errors. | ||
* criticalErrors trigger retry behaviour and so may not be desirable in all scenarios. | ||
* @return a HttpTransportBuilder instance | ||
* @example | ||
* const httpTransport = require('@bbc/http-transport'); | ||
* | ||
* const builder = httpTransport.createBuilder(); | ||
* builder.criticalErrorDetector(() => { | ||
* if (err && (err.statusCode < 500 || err.isBrokenCircuitError)) { | ||
* return false; | ||
* } | ||
* return true; | ||
* }); | ||
* | ||
* @default | ||
* (err, ctx) => { | ||
* if (err && err.statusCode < 500) { | ||
* return false; | ||
* } | ||
* return true; | ||
* } | ||
*/ | ||
criticalErrorDetector(criticalErrorDetector) { | ||
_.set(this._defaults, 'ctx.criticalErrorDetector', criticalErrorDetector); | ||
return this; | ||
} | ||
/** | ||
* Registers a global plugin, which is used for all requests | ||
@@ -75,0 +105,0 @@ * |
@@ -342,3 +342,5 @@ 'use strict'; | ||
function isCriticalError(err) { | ||
function isCriticalError(err, ctx) { | ||
if (ctx.criticalErrorDetector) return ctx.criticalErrorDetector(err, ctx); | ||
if (err && err.statusCode < 500) { | ||
@@ -367,3 +369,3 @@ return false; | ||
.catch((err) => { | ||
if (i < maxAttempts && isCriticalError(err) && ctx.retryDelay && ctx.retryDelay > 0) { | ||
if (i < maxAttempts && isCriticalError(err, ctx) && ctx.retryDelay && ctx.retryDelay > 0) { | ||
const delayBy = rejectedPromise(ctx.retryDelay); | ||
@@ -375,3 +377,3 @@ return delayBy(err); | ||
.catch((err) => { | ||
if (i < maxAttempts && isCriticalError(err)) { | ||
if (i < maxAttempts && isCriticalError(err, ctx)) { | ||
ctx.retryAttempts.push(toRetry(err)); | ||
@@ -378,0 +380,0 @@ return attempt(++i); |
@@ -22,2 +22,3 @@ 'use strict'; | ||
this.res = Response.create(); | ||
this.criticalErrorDetector = undefined; | ||
if (defaults) this._applyDefaults(defaults); | ||
@@ -46,2 +47,3 @@ } | ||
this.retryDelay = defaults.ctx?.retryDelay || RETRY_DELAY; | ||
this.criticalErrorDetector = defaults.ctx?.criticalErrorDetector; | ||
} | ||
@@ -48,0 +50,0 @@ |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "4.5.1", | ||
"version": "4.6.0", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -144,2 +144,18 @@ 'use strict'; | ||
}); | ||
it('sets default criticalErrorDetector in the context', async () => { | ||
const transport = new Transport(); | ||
sandbox.stub(transport, 'execute').returns(Promise.resolve()); | ||
const client = HttpTransport.createBuilder(transport) | ||
.criticalErrorDetector(() => false) | ||
.createClient(); | ||
await client | ||
.get(url) | ||
.asResponse(); | ||
const ctx = transport.execute.getCall(0).args[0]; | ||
assert.equal(ctx.criticalErrorDetector.toString(), (() => false).toString()); | ||
}); | ||
}); | ||
@@ -146,0 +162,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
396478
6965
2