@bbc/http-transport
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -5,3 +5,3 @@ 'use strict'; | ||
const context = require('./context'); | ||
const delayBy = require('./delayedPromise'); | ||
const rejectedPromise = require('./rejectedPromise'); | ||
const packageInfo = require('../package'); | ||
@@ -16,8 +16,4 @@ const bind = require('./bind'); | ||
this._httpTransport = httpTransport; | ||
this._ctx = context.create(); | ||
this._instancePlugins = []; | ||
this._defaultHeaders = { | ||
'User-Agent': `${packageInfo.name}/${packageInfo.version}` | ||
}; | ||
this.headers(this._defaultHeaders); | ||
this._initContext(); | ||
bind(this); | ||
@@ -344,9 +340,8 @@ } | ||
asResponse() { | ||
const requestContext = this._ctx; | ||
this._ctx = context.create(); | ||
const retries = { | ||
count: this._retries, | ||
delay: this._retryDelay >= 0 ? this._retryDelay : RETRY_DELAY | ||
}; | ||
return retry(this._executeRequest, requestContext, retries).then((ctx) => ctx.res); | ||
const currentContext = this._ctx; | ||
currentContext.retries = this._retries; | ||
currentContext.retryDelay = this._retryDelay >= 0 ? this._retryDelay : RETRY_DELAY; | ||
this._initContext(); | ||
return retry(this._executeRequest, currentContext).then((ctx) => ctx.res); | ||
} | ||
@@ -370,2 +365,10 @@ | ||
} | ||
_initContext() { | ||
this._ctx = context.create(); | ||
this._defaultHeaders = { | ||
'User-Agent': `${packageInfo.name}/${packageInfo.version}` | ||
}; | ||
this.headers(this._defaultHeaders); | ||
} | ||
} | ||
@@ -387,12 +390,18 @@ | ||
function retry(fn, ctx, retries) { | ||
function retry(fn, ctx) { | ||
ctx.res.retries = []; | ||
ctx.res.maxAttempts = retries.count; | ||
ctx.res.maxAttempts = ctx.retries; | ||
const request = fn.bind(this, ctx); | ||
const attempt = (i) => { | ||
function attempt(i) { | ||
return request() | ||
.catch(delayBy(retries.delay)) | ||
.catch((err) => { | ||
if (i < retries.count && isCriticalError(err)) { | ||
if (ctx.retries > 0) { | ||
const delayBy = rejectedPromise(ctx.retryDelay); | ||
return delayBy(err); | ||
} | ||
throw err; | ||
}) | ||
.catch((err) => { | ||
if (i < ctx.retries && isCriticalError(err)) { | ||
ctx.res.retries.push(toRetry(err)); | ||
@@ -403,3 +412,3 @@ return attempt(++i); | ||
}); | ||
}; | ||
} | ||
return attempt(0); | ||
@@ -406,0 +415,0 @@ } |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -100,3 +100,3 @@ 'use strict'; | ||
it('sets a default User-agent', () => { | ||
it('sets a default User-agent for every request', () => { | ||
nock.cleanAll(); | ||
@@ -111,7 +111,15 @@ | ||
.get(path) | ||
.times(2) | ||
.reply(200, responseBody); | ||
return HttpTransport.createClient() | ||
const client = HttpTransport.createClient(); | ||
const pending1 = client | ||
.get(url) | ||
.asResponse(); | ||
const pending2 = client | ||
.get(url) | ||
.asResponse(); | ||
return Promise.all([pending1, pending2]); | ||
}); | ||
@@ -175,2 +183,18 @@ | ||
it('disables retryDelay if retries if set to zero', () => { | ||
nock.cleanAll(); | ||
api.get(path).reply(500); | ||
return HttpTransport.createClient() | ||
.useGlobal(toError()) | ||
.get(url) | ||
.retry(0) | ||
.retryDelay(10000) | ||
.asResponse() | ||
.then(() => assert.ok(false, 'Promise should have failed')) | ||
.catch((e) => { | ||
assert.equal(e.message, 'something bad happend.'); | ||
}); | ||
}); | ||
it('overrides the minimum wait time between retries', () => { | ||
@@ -177,0 +201,0 @@ nockRetries(1); |
404457
8527