LuCI. request

The Request class allows initiating HTTP requests and provides utilities for dealing with responses.

Classes

poll

Methods

addInterceptor(interceptorFn) → {LuCI.request.interceptorFn}

Register an HTTP response interceptor function. Interceptor functions are useful to perform default actions on incoming HTTP responses, such as checking for expired authentication or for implementing request retries before returning a failure.

Parameters:
NameTypeDescription
interceptorFnLuCI.request.interceptorFn

The interceptor function to register.

Returns:

The registered function.

Type: 
LuCI.request.interceptorFn

expandURL(url) → {string}

Turn the given relative URL into an absolute URL if necessary.

Parameters:
NameTypeDescription
urlstring

The URL to convert.

Returns:

The absolute URL derived from the given one, or the original URL if it already was absolute.

Type: 
string

get(url, optionsopt) → {Promise.<LuCI.response>}

Initiate an HTTP GET request to the given target.

Parameters:
NameTypeAttributesDescription
urlstring

The URL to request.

optionsLuCI.request.RequestOptions<optional>

Additional options to configure the request.

Returns:

The resulting HTTP response.

Type: 
Promise.<LuCI.response>

handleReadyStateChange(resolveFn, rejectFn, evopt) → {void}

Handle XHR readyState changes for an in-flight request and resolve or reject the originating promise.

Parameters:
NameTypeAttributesDescription
resolveFnfunction

Callback invoked on success with the constructed LuCI.response.

rejectFnfunction

Callback invoked on failure or abort with an Error instance.

evEvent<optional>

The XHR readystatechange event (optional).

Returns:

No return value; the function resolves or rejects the supplied callbacks.

Type: 
void

post(url, dataopt, optionsopt) → {Promise.<LuCI.response>}

Initiate an HTTP POST request to the given target.

Parameters:
NameTypeAttributesDescription
urlstring

The URL to request.

data*<optional>

The request data to send, see LuCI.request.RequestOptions for details.

optionsLuCI.request.RequestOptions<optional>

Additional options to configure the request.

Returns:

The resulting HTTP response.

Type: 
Promise.<LuCI.response>

removeInterceptor(interceptorFn) → {boolean}

Remove an HTTP response interceptor function. The passed function value must be the very same value that was used to register the function.

Parameters:
NameTypeDescription
interceptorFnLuCI.request.interceptorFn

The interceptor function to remove.

Returns:

Returns true if any function has been removed, else false.

Type: 
boolean

request(target, optionsopt) → {Promise.<LuCI.response>}

Initiate an HTTP request to the given target.

Parameters:
NameTypeAttributesDescription
targetstring

The URL to request.

optionsLuCI.request.RequestOptions<optional>

Additional options to configure the request.

Returns:

The resulting HTTP response.

Type: 
Promise.<LuCI.response>

Type Definitions

RequestOptions

Type:
  • Object
Properties
NameTypeAttributesDefaultDescription
methodstring<optional>
GET

The HTTP method to use, e.g. GET or POST.

queryObject.<string, (Object|string)><optional>

Query string data to append to the URL. Non-string values of the given object will be converted to JSON.

cacheboolean<optional>
false

Specifies whether the HTTP response may be retrieved from cache.

usernamestring<optional>

Provides a username for HTTP basic authentication.

passwordstring<optional>

Provides a password for HTTP basic authentication.

timeoutnumber<optional>

Specifies the request timeout in milliseconds.

credentialsboolean<optional>
false

Whether to include credentials such as cookies in the request.

responseTypestring<optional>
text

Overrides the request response type. Valid values or text to interpret the response as UTF-8 string or blob to handle the response as binary Blob data.

content*<optional>

Specifies the HTTP message body to send along with the request. If the value is a function, it is invoked and the return value used as content, if it is a FormData instance, it is used as-is, if it is an object, it will be converted to JSON, in all other cases it is converted to a string.

headerObject.<string, string><optional>

Specifies HTTP headers to set for the request.

progressfunction<optional>

An optional request callback function which receives ProgressEvent instances as sole argument during the HTTP request transfer.

responseProgressfunction<optional>

An optional request callback function which receives ProgressEvent instances as sole argument during the HTTP response transfer.

interceptorFn(res)

Interceptor functions are invoked whenever an HTTP reply is received, in the order these functions have been registered.

Parameters:
NameTypeDescription
resLuCI.response

The HTTP response object