@bbc/http-transport
Advanced tools
Comparing version 4.4.1 to 4.4.2
@@ -59,3 +59,3 @@ import * as fetch from 'node-fetch'; | ||
export declare function toJson(opts: ToJsonOpts): Plugin; | ||
export declare function toJson(opts?: ToJsonOpts): Plugin; | ||
export declare function logger(logger?: any): Plugin; | ||
@@ -134,3 +134,3 @@ export declare function setContextProperty(opts: any, path: string): Plugin; | ||
timeout(timeout: number): HttpTransportClient<ContextCurrent>; | ||
retries(retries: number): HttpTransportClient<ContextCurrent>; | ||
retry(retries: number): HttpTransportClient<ContextCurrent>; | ||
retryDelay(retryDelay: number): HttpTransportClient<ContextCurrent>; | ||
@@ -137,0 +137,0 @@ asBody(): Promise<ContextCurrent["res"]["body"]>; |
@@ -53,2 +53,4 @@ 'use strict'; | ||
if (this.jsonMode(ctx)) req.addHeader('Accept', 'application/json'); | ||
if (req.hasHeaders()) opts.headers = req.getHeaders(); | ||
@@ -59,6 +61,6 @@ | ||
parseAsJson(ctx, contentType) { | ||
jsonMode(ctx) { | ||
if (ctx.opts?.json !== undefined) return ctx.opts.json; | ||
return this.defaults?.json || contentType?.includes('json'); | ||
return this.defaults?.json; | ||
} | ||
@@ -81,3 +83,3 @@ | ||
const contentType = to.headers['content-type']; | ||
if (this.parseAsJson(ctx, contentType)) { | ||
if (this.jsonMode(ctx, contentType) || contentType?.includes('json')) { | ||
try { | ||
@@ -84,0 +86,0 @@ to.body = JSON.parse(to.body); |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "4.4.1", | ||
"version": "4.4.2", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -249,3 +249,3 @@ 'use strict'; | ||
it('allows disabling of timing request', () => { | ||
it('allows disabling of timing request', () => { | ||
nock.cleanAll(); | ||
@@ -292,2 +292,28 @@ api.get('/').reply(200, responseBody); | ||
it('if json default option is passed in as true, send an accept: application/json header', () => { | ||
nock.cleanAll(); | ||
nock(host, { | ||
reqheaders: { | ||
accept: 'application/json' | ||
} | ||
}) | ||
.get(path) | ||
.reply(200, responseBody, header); | ||
const ctx = createContext(url); | ||
const options = { | ||
defaults: { | ||
json: true | ||
} | ||
}; | ||
const fetchTransport = new FetchTransport(options); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then((ctx) => { | ||
assert.equal(ctx.res.statusCode, 200); | ||
}); | ||
}); | ||
it('if there is no json default option passed in, but the content type header includes application/json, then parse body as json', () => { | ||
@@ -294,0 +320,0 @@ nock.cleanAll(); |
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
386679
6786
3