Connect

Connect

Simple & Powerful HTTP request client.

Constructor

new Connect()

Source:

Creates a new Connect object.

Methods

(static) get(url) → {Connect}

Source:
Parameters:
Name Type Description
url string
Returns:

A new Connect object with url set to the given url

Type
Connect

(static) newCookieJar(…args) → {CookieJar}

Source:
Parameters:
Name Type Attributes Description
args Array.<any> <repeatable>
Returns:

A cookie jar

Type
CookieJar

(static) post(url) → {Connect}

Source:
Parameters:
Name Type Description
url string
Returns:

A new Connect object with url set to the given url

Type
Connect

(static) put(url) → {Connect}

Source:
Parameters:
Name Type Description
url string
Returns:

A new Connect object with url set to the given url

Type
Connect

(static) url(url) → {Connect}

Source:
Parameters:
Name Type Description
url string
Returns:

A new Connect object with url set to the given url

Type
Connect

apiToken(token) → {Connect}

Source:

Set api token using x-api-token header

Parameters:
Name Type Description
token string
Returns:

self

Type
Connect

asBuffer(returnAsBufferopt) → {Connect}

Source:

Set if the body is to be returned as a buffer

Parameters:
Name Type Attributes Default Description
returnAsBuffer boolean <optional>
true
Returns:

self

Type
Connect

bearerToken(token) → {Connect}

Source:

Set bearer token for authorization

Parameters:
Name Type Description
token string
Returns:

self

Type
Connect

body(body, contentTypeopt) → {Connect}

Source:

Set the body of the connection object.

Parameters:
Name Type Attributes Default Description
body any

value for body
if body is an object, contentType will be set to application/json and body will be stringified

contentType string <optional>
null

string representing the content type of the body
contentType can be null or json

Returns:

self

Type
Connect

cacheDir(dir) → {Connect}

Source:

Set cache directory for the connection.

Parameters:
Name Type Description
dir string

name or path to the directory

Returns:

self

Type
Connect

(async) catch(errorCallback) → {Promise.<T>}

Source:

It is also used for method chaining, but handles rejected cases only.

Parameters:
Name Type Description
errorCallback function

function to be called if the Promise is rejected

Returns:

a Promise in pending state

Type
Promise.<T>

clone() → {Connect}

Source:

clone this instance

Returns:

cloned instance

Type
Connect

compress(askForCompressionopt) → {Connect}

Source:

set whether to ask for compressed response (handles decompression automatically)

Parameters:
Name Type Attributes Default Description
askForCompression boolean <optional>
true

whether to ask for compressed response

Returns:

self

Type
Connect

contentType(contentType) → {Connect}

Source:

Set the 'Content-Type' field in the headers.

Parameters:
Name Type Description
contentType string

value for content-type

Returns:
Type
Connect
Source:

Sets the value of a cookie.
Can be used to enable global cookies, if cookieName is set to true
and cookieValue is undefined (or is not passed as an argument).
Can also be used to set multiple cookies by passing in an object
representing the cookies and their values as key:value pairs.

Parameters:
Name Type Attributes Description
cookieName string | boolean | object

represents the name of the
cookie to be set, or the cookies object

cookieValue string | undefined <optional>

cookie value to be set

Returns:

self

Type
Connect

cookieFile(fileName) → {Connect}

Source:

Set the value of cookie jar based on a file (cookie store).

Parameters:
Name Type Description
fileName string

name of (or path to) the file

Returns:

self

Type
Connect

cookieJar(cookieJar) → {Connect}

Source:

Set the value of cookie jar.

Parameters:
Name Type Description
cookieJar CookieJar

value to be set

Returns:

self

Type
Connect

cookies(cookies) → {Connect}

Source:

Sets multiple cookies.
Can be used to enable global cookies, if cookies is set to true.

Parameters:
Name Type Description
cookies object | boolean

object representing the cookies
and their values as key:value pairs.

Returns:

self

Type
Connect

(async) fetch() → {Promise.<response>}

Source:

It creates and returns a promise based on the information
passed to and parameters of this object.

Returns:

a promise that resolves to response

Type
Promise.<response>

field(fieldName, fieldValueopt) → {Connect}

Source:

Set value of a field in the options.
Can also be used to set multiple fields by passing in an object
representing the field-names and their values as key:value pairs.

Parameters:
Name Type Attributes Description
fieldName string | object

name of the field to be set, or the fields object

fieldValue string | undefined <optional>

value to be set

Returns:

self

Type
Connect

fields(fields) → {Connect}

Source:

Set multiple fields.

Parameters:
Name Type Description
fields object

object representing the field-names and their
values as key:value pairs

Returns:

self

Type
Connect

(async) finally(callback) → {Promise.<T>}

Source:

finally method of promise returned

Parameters:
Name Type Description
callback function

function to be called if the promise is fullfilled or rejected

Returns:

a Promise in pending state

Type
Promise.<T>

followRedirect(shouldFollowRedirect) → {Connect}

Source:

Set or unset the followRedirect option for the connection.

Parameters:
Name Type Default Description
shouldFollowRedirect boolean true

boolean representing whether to follow redirect or not

Returns:

self

Type
Connect

get() → {Connect}

Source:

Set request method to 'GET'.

Returns:

self

Type
Connect

globalCookies(optionsopt) → {Connect}

Source:

Enable global cookies.

Parameters:
Name Type Attributes Default Description
options boolean | object <optional>
true
Returns:

self

Type
Connect
Source:

Set value of a header parameter for the connection.

Parameters:
Name Type Description
headerName string | object

name of the header parameter whose value is to be set

headerValue string | undefined

value to be set

Returns:

self

Type
Connect

headers(headers) → {Connect}

Source:

Set value of the headers for the connection.

Parameters:
Name Type Description
headers object

object representing the headers for the connection

Returns:

self

Type
Connect

httpAuth(username, password) → {Connect}

Source:

Set username and password for authentication.

Parameters:
Name Type Description
username string | auth
password string | undefined
Returns:

self

Type
Connect

httpProxy(proxyAddress, proxyPort) → {Connect}

Source:

Set address and port for an http proxy.

Parameters:
Name Type Description
proxyAddress string
proxyPort number
Returns:

self

Type
Connect

isForm() → {boolean}

Source:

Returns whether the content-type is Form or not

Returns:

true, if content-type is JSON; false, otherwise

Type
boolean

isJSON() → {boolean}

Source:

Returns whether the content-type is JSON or not

Returns:

true, if content-type is JSON; false, otherwise

Type
boolean

maxRedirects(numRedirects)

Source:

Set the number of maximum redirects to follow

Parameters:
Name Type Description
numRedirects number

max number of redirects

method(method) → {Connect}

Source:

Set the request method for the connection.

Parameters:
Name Type Description
method string

one of the HTTP request methods ('GET', 'PUT', 'POST', etc.)

Returns:

self

Type
Connect

post() → {Connect}

Source:

Set request method to 'POST'.

Returns:

self

Type
Connect

proxy(proxy) → {Connect}

Source:

Set proxy address (or options).

Parameters:
Name Type Description
proxy string | object

proxy address, or object representing proxy options

Returns:

self

Type
Connect

proxyAuth(username, password) → {Connect}

Source:

Set username and password for proxy.

Parameters:
Name Type Description
username string
password string
Returns:

self

Type
Connect

put() → {Connect}

Source:

Set request method to 'PUT'.

Returns:

self

Type
Connect

query(fieldName, fieldValueopt) → {Connect}

Source:

Set value of a query parameter
Can also be used to set multiple query params by passing in an object
representing the param-names and their values as key:value pairs.

Parameters:
Name Type Attributes Description
fieldName string | object

name of the field to be set, or the fields object

fieldValue string | undefined <optional>

value to be set

Returns:

self

Type
Connect

referer(referer) → {Connect}

Source:

Set the 'Referer' field in the headers.

Parameters:
Name Type Description
referer string

referer value

Returns:
Type
Connect

save(filePath) → {Connect}

Source:

Set the path for file for saving the response.

Parameters:
Name Type Description
filePath string
Returns:

self

Type
Connect

socksProxy(proxyAddress, proxyPort) → {Connect}

Source:

Set address and port for a socks proxy.

Parameters:
Name Type Description
proxyAddress string
proxyPort number
Returns:

self

Type
Connect

(async) then(successCallback, errorCallbackopt) → {Promise.<T>}

Source:

It is used for method chaining.

Parameters:
Name Type Attributes Description
successCallback function

To be called if the Promise is fulfilled

errorCallback function <optional>

function to be called if the Promise is rejected

Returns:

a Promise in pending state

Type
Promise.<T>

timeout(timeout) → {Connect}

Source:

Set request timeout.

Parameters:
Name Type Description
timeout number

timeout value in seconds

Returns:

self

Type
Connect

timeoutMilli(timeoutInMs) → {Connect}

Source:

alias for timeoutMs

Parameters:
Name Type Description
timeoutInMs number
Returns:
Type
Connect

timeoutMs(timeoutInMs) → {Connect}

Source:

Set request timeout.

Parameters:
Name Type Description
timeoutInMs number

timeout value in milliseconds

Returns:

self

Type
Connect

url(url) → {Connect}

Source:

Set the url for the connection.

Parameters:
Name Type Description
url string
Returns:

self

Type
Connect

userAgent(userAgent) → {Connect}

Source:

Set the 'User-Agent' field in the headers.
can be either name of the browser / platform or full user agent
name can be:
chrome, firefox, ie, edge, safari, googlebot
chromeMobile, safariMobile, googlebotMobile, operaMini
windows, android, iphone, ipad, desktop, mobile, tablet

Parameters:
Name Type Description
userAgent string

name of the user-agent or its value

Returns:

self

Type
Connect