Bornite is a library for making HTTP requests
Bornite is available on npm:
npm install @botsocket/bornite
A simple GET request:
const Bornite = require('@botsocket/bornite');
const makeRequest = async function () {
const response = await Bornite.get('https://www.google.com');
response.payload; // <html>...
response.statusCode; // 200
response.statusMessage; // OK
};
makeRequest();
POST request with JSON payload:
const Bornite = require('@botsocket/bornite');
const makeRequest = async function () {
const response = await Bornite.post('https://my-url.com', {
payload: {
name: 'John Doe',
},
});
response.statusCode; // 200
response.statusMessage; // OK
};
makeRequest();
With Promise
:
const Bornite = require('@botsocket/bornite');
Bornite.get('https://www.google.com')
.then((response) => {
response.payload; // <html>...
response.statusCode; // 200
response.statusMessage; // OK
});
Creates a custom Bornite instance with common options where:
options
: Common options that apply to every request. Same as ones passed torequest()
.
const custom = Bornite.custom({
baseUrl: 'https://www.google.com',
redirects: Infinity,
gzip: true,
headers: {
'Some-Common-Header': 'abc',
},
});
custom.get('/images');
custom.post('/gmail');
Makes a request where:
url
: The URL to send the request.options
: Optional options where:method
: The request method to use. Case-insensitive.baseUrl
: Base URL that is resolved withurl
to form a full URL. Most useful when used withcustom()
.headers
: A hash of case-insensitive header names and their values.Content-Length
,Content-Type
andAccept-Encoding
are set by default if possible.payload
: The request payload. Can be a string, a buffer, a stream,URLSearchParams
or a serializable object. Ifmethod
isGET
orHEAD
, this key is forbidden.agent
: The HTTP Agent passed directly toHttp.request()
.redirects
: The number of redirects to follow. Set to0
orfalse
to disable redirection. Set toInfinity
to follow all possible redirects. Defaults to0
.redirectMethod
: The method to change to when performing301
and302
redirects. Note thatpayload
is not forwarded if set toGET
orHEAD
. Defaults to usingmethod
.gzip
: Whether to decompress gzipped payloads. Defaults tofalse
.maxBytes
: The maximum size in bytes the response payload can have. Set to0
to disable counting bytes. Defaults to0
.timeout
: Socket timeout in milliseconds. Passed directly toHttp.request()
.validateStatus
: Whether to validate response status code. Can be a boolean or a function of signaturefunction (statusCode) {}
returning a boolean indicating whether the response is accepted. If sets totrue
, Bornite will allow 2xx codes. Defaults tofalse
.
const response = await Bornite.request('/images', {
method: 'POST',
baseUrl: 'https://www.google.com',
headers: {},
payload: { a: 1 },
agent: new Http.Agent({ maxSockets: 1 }),
redirects: Infinity,
redirectMethod: 'GET',
gzip: true,
maxBytes: 5000,
timeout: 2000,
validateStatus: (statusCode) => statusCode === 200,
}); // Requests to https://www.google.com/images
Any error thrown after the response has been received (for example, when Bornite fails to parse JSON or when validateStatus
returns false) will be decorated with the property response
which can be accessed should it be processed further.
Payload returned by the server. Bornite automatically parses the content if the Content-Type
header is set to application/json
.
Response headers returned by the server.
The status code of the response.
The status message of the response.
The raw Node response object.
Bornite provides the following convenience methods for common HTTP methods:
Bornite.get(url); // Equivalent to Bornite.request(url, { method: 'GET' })
Bornite.post(url); // Equivalent to Bornite.request(url, { method: 'POST' })
Bornite.put(url); // Equivalent to Bornite.request(url, { method: 'PUT' })
Bornite.patch(url); // Equivalent to Bornite.request(url, { method: 'PATCH' })
Bornite.delete(url); // Equivalent to Bornite.request(url, { method: 'DELETE' })