@bbc/http-transport
Advanced tools
Comparing version 4.4.5 to 4.5.0
11
docs.md
@@ -113,2 +113,13 @@ # HttpTransport | ||
#### Handling redirects | ||
Set the redirect handling to manual | ||
```js | ||
const body = await HttpTransport.createClient() | ||
.redirect('manual') | ||
.get(url) | ||
.asBody(); | ||
``` | ||
#### Retries | ||
@@ -115,0 +126,0 @@ |
@@ -137,2 +137,3 @@ import * as fetch from 'node-fetch'; | ||
timeout(timeout: number): HttpTransportClient<ContextCurrent>; | ||
redirect(redirectType: fetch.RequestRedirect): HttpTransportClient<ContextCurrent>; | ||
retry(retries: number): HttpTransportClient<ContextCurrent>; | ||
@@ -156,2 +157,4 @@ retryDelay(retryDelay: number): HttpTransportClient<ContextCurrent>; | ||
addPlugin(plugin: Plugin): Context; | ||
redirect?: fetch.RequestRedirect; | ||
} | ||
@@ -166,2 +169,3 @@ | ||
proxy?: string | ||
redirect?: fetch.RequestRedirect | ||
} | ||
@@ -168,0 +172,0 @@ } |
@@ -219,2 +219,22 @@ 'use strict'; | ||
/** | ||
* Set the redirect handling: | ||
* `follow` (default) to follow the redirects automatically, | ||
* `manual` to extract redirect headers, | ||
* `error` to reject redirect | ||
* | ||
* @param {'follow'|'manual'|'error'} redirect - redirect handling | ||
* @return a HttpTransport instance | ||
* @example | ||
* const httpTransport = require('@bbc/http-transport'); | ||
* | ||
* const response = await httpTransport.createClient() | ||
* .redirect('manual') // for this request only | ||
* .asResponse(); | ||
*/ | ||
redirect(redirectType) { | ||
this._ctx.req.redirect(redirectType); | ||
return this; | ||
} | ||
/** | ||
* Set the number of retries on failure for the request | ||
@@ -221,0 +241,0 @@ * |
@@ -14,2 +14,3 @@ 'use strict'; | ||
this.retries = 0; | ||
this.redirect = undefined; | ||
this._retryAttempts = []; | ||
@@ -16,0 +17,0 @@ this.plugins = []; |
@@ -10,2 +10,3 @@ 'use strict'; | ||
this._timeout = undefined; | ||
this._redirect = undefined; | ||
this._method = undefined; | ||
@@ -54,2 +55,7 @@ this._baseUrl = undefined; | ||
redirect(redirectType) { | ||
this._redirect = redirectType; | ||
return this; | ||
} | ||
getMethod() { | ||
@@ -63,2 +69,6 @@ return this._method; | ||
getRedirect() { | ||
return this._redirect; | ||
} | ||
getUrl() { | ||
@@ -65,0 +75,0 @@ if (this.hasQueries()) { |
@@ -37,2 +37,3 @@ 'use strict'; | ||
opts.redirect = req.getRedirect() || this.defaults?.redirect; | ||
opts.timeout = req.getTimeout() || this.defaults?.timeout; | ||
@@ -39,0 +40,0 @@ opts.compress = this.defaults?.compress; |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "4.4.5", | ||
"version": "4.5.0", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -51,2 +51,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@types/node-fetch": "^2.6.11", | ||
"http-proxy-agent": "^7.0.0", | ||
@@ -53,0 +54,0 @@ "koa-compose": "^4.0.0", |
'use strict'; | ||
const assert = require('chai').assert; | ||
const expect = require('chai').expect; | ||
const nock = require('nock'); | ||
@@ -480,2 +481,57 @@ const sinon = require('sinon'); | ||
describe('.redirect', () => { | ||
describe('sets the type of redirect handling', async () => { | ||
it('redirects automatically if value is not set', async () => { | ||
nock.cleanAll(); | ||
api | ||
.get('/') | ||
.reply(303, '', { Location: `${url}new-path` }); | ||
api | ||
.get('/new-path') | ||
.reply(200, 'It works'); | ||
const client = HttpTransport.createClient() | ||
.get(url); | ||
const response = await client.asResponse(); | ||
assert(response.statusCode, 200); | ||
assert(response.body, 'It works'); | ||
}); | ||
it('returns 303 and the location if value is `manual`', async () => { | ||
nock.cleanAll(); | ||
api | ||
.get('/') | ||
.reply(303, '', { Location: `${url}new-path` }); | ||
const client = HttpTransport.createClient() | ||
.redirect('manual') | ||
.get(url); | ||
const response = await client.asResponse(); | ||
assert(response.statusCode, 303); | ||
assert(response.headers.location, `${url}new-path`); | ||
}); | ||
it('throws error if value is `error`', async () => { | ||
nock.cleanAll(); | ||
api | ||
.get('/') | ||
.reply(303, '', { Location: `${url}new-path` }); | ||
try { | ||
const client = HttpTransport.createClient() | ||
.redirect('error') | ||
.get(url); | ||
await client.asResponse(); | ||
} catch (err) { | ||
expect(err.message).to.include('Request failed for GET http://www.example.com/'); | ||
return; | ||
} | ||
assert.fail('Should have thrown'); | ||
}); | ||
}); | ||
}); | ||
describe('plugins', () => { | ||
@@ -482,0 +538,0 @@ it('supports a per request plugin', async () => { |
@@ -9,2 +9,7 @@ 'use strict'; | ||
describe('Request', () => { | ||
it('sets leave the redirect handling undefined', () => { | ||
const request = Request.create(); | ||
assert.equal(request._redirect, undefined); | ||
}); | ||
describe('.baseUrl', () => { | ||
@@ -90,2 +95,10 @@ it('sets the base URL', () => { | ||
}); | ||
describe('.redirect', () => { | ||
it('sets the redirect handling', () => { | ||
const request = Request.create(); | ||
request.redirect('manual'); | ||
assert.equal(request._redirect, 'manual'); | ||
}); | ||
}); | ||
}); |
@@ -234,2 +234,22 @@ 'use strict'; | ||
it('sets a default redirect', () => { | ||
nock.cleanAll(); | ||
api | ||
.get('/') | ||
.reply(303, '', { Location: `${url}new-path` }); | ||
const ctx = createContext(url); | ||
return new FetchTransport({ | ||
defaults: { | ||
redirect: 'manual' | ||
} | ||
}) | ||
.execute(ctx) | ||
.then(() => { | ||
assert.equal(ctx.res.statusCode, 303); | ||
assert.equal(ctx.res.headers.location, `${url}new-path`); | ||
}); | ||
}); | ||
it('enables timing request by default', () => { | ||
@@ -270,2 +290,19 @@ nock.cleanAll(); | ||
it('sets redirect', () => { | ||
nock.cleanAll(); | ||
api | ||
.get('/') | ||
.reply(303, '', { Location: `${url}new-path` }); | ||
const ctx = createContext(url); | ||
ctx.req.redirect('manual'); | ||
return new FetchTransport() | ||
.execute(ctx) | ||
.then((ctx) => { | ||
assert.equal(ctx.res.statusCode, 303); | ||
assert.equal(ctx.res.headers.location, `${url}new-path`); | ||
}); | ||
}); | ||
describe('JSON parsing', () => { | ||
@@ -272,0 +309,0 @@ it('if json default option is passed in as true, parse body as json', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
394263
6914
6
+ Added@types/node-fetch@^2.6.11
+ Added@types/node@22.12.0(transitive)
+ Added@types/node-fetch@2.6.12(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedundici-types@6.20.0(transitive)