HttpTransportClient

HttpTransportClient

Core client

Constructor

new HttpTransportClient(transport, defaults)

Source:

Create a HttpTransport.

Parameters:
Name Type Description
transport Transport

Transport instance.

defaults object

default configuration

Methods

(async) asBody()

Source:

Initiates the request, returning the response body, if successful.

Example
const httpTransport = require('@bbc/http-transport');

const body = await httpTransport.createClient()
   .asBody();

   console.log(body);
Returns:

a Promise. If the Promise fulfils, the fulfilment value is the response body. The body type defaults to string. If the content-type response header contains 'json' or the json: true option has been set on transport layer then the body type will be json.

(async) asResponse()

Source:

Initiates the request, returning a http transport response object, if successful.

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .asResponse()

   console.log(response);
Returns:

a Promise. If the Promise fulfils, the fulfilment value is response object.

delete(baseUrl)

Source:

Make a HTTP DELETE request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .delete(baseUrl)
   .asResponse();
Parameters:
Name Type Description
baseUrl string
Returns:

a HttpTransport instance

get(baseUrl)

Source:

Make a HTTP GET request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .get(url)
   .asResponse();
Parameters:
Name Type Description
baseUrl string
Returns:

a HttpTransport instance

Source:

Make a HTTP HEAD request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .head(baseUrl)
   .asResponse();
Parameters:
Name Type Description
baseUrl string
Returns:

a HttpTransport instance

headers(name, value)

Source:

Sets the request headers

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .headers({
     'User-Agent' : 'someUserAgent'
   })
   .asResponse();
Parameters:
Name Type Description
name string | object

header name or headers object

value string | object

header value

Returns:

a HttpTransport instance

patch(baseUrl, request)

Source:

Make a HTTP PATCH request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .put(baseUrl, requestBody)
   .asResponse();
Parameters:
Name Type Description
baseUrl string
request object

body

Returns:

a HttpTransport instance

post(baseUrl, request)

Source:

Make a HTTP POST request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .post(baseUrl, requestBody)
   .asResponse();
Parameters:
Name Type Description
baseUrl string
request object

body

Returns:

a HttpTransport instance

put(baseUrl, request)

Source:

Make a HTTP PUT request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .put(baseUrl, requestBody)
   .asResponse();
Parameters:
Name Type Description
baseUrl string
request object

body

Returns:

a HttpTransport instance

query(name, value)

Source:

Sets the query strings

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .query({
     'perPage' : 1
   })
   .asResponse();
Parameters:
Name Type Description
name string | object

query name or query object

value string | object

query value

Returns:

a HttpTransport instance

redirect(redirect)

Source:

Set the redirect handling: follow (default) to follow the redirects automatically, manual to extract redirect headers, error to reject redirect

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .redirect('manual') // for this request only
   .asResponse();
Parameters:
Name Type Description
redirect 'follow' | 'manual' | 'error'

redirect handling

Returns:

a HttpTransport instance

retry(retries)

Source:

Set the number of retries on failure for the request

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .retry(5) // for this request only
   .asResponse();
Parameters:
Name Type Description
retries integer

number of times to retry a failed request

Returns:

a HttpTransport instance

retryDelay(delay)

Source:

Set the delay between retries in ms

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .retry(2)
   .retryDelay(200)
   .asResponse();
Parameters:
Name Type Description
delay integer

number of ms to wait between retries (default: 100)

Returns:

a HttpTransport instance

timeout(time)

Source:

Sets a request timeout

Example
const httpTransport = require('@bbc/http-transport');

const response = await httpTransport.createClient()
   .timeout(1)
   .asResponse();
Parameters:
Name Type Description
time integer

timeout in seconds

Returns:

a HttpTransport instance

use(fn)

Source:

Registers a per request plugin

Example
const toError = require('@bbc/http-transport-to-error');
const httpTransport = require('@bbc/http-transport');

httpTransport.createClient()
   .use(toError(404));
Parameters:
Name Type Description
fn function

per request plugin

Returns:

a HttpTransport instance