Constructor
new HttpTransportClient(transport, defaults)
Create a HttpTransport.
Parameters:
Name | Type | Description |
---|---|---|
transport |
Transport | Transport instance. |
defaults |
object | default configuration |
Methods
(async) asBody()
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()
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)
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)
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
head(baseUrl)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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