Module: rgjs/xhr

RGJS6 XHR module.

Members


<inner, constant> _options :object

Default options for get(), getJson(), post(), etc.

Type:
  • object
Properties:
Name Type Description
id string

Identifier for request. Used to abort ongoing requests.
Generated automatially if not set. Ongoing request with matching id will be aborted.

cache boolean

Use cache. Defaults to false.

cacheMaxAgeMs number

Max age of cached data in milliseconds. Defaults to 1 hr.

mimeType string

Mime type for get. Defaults to 'text/html'.

dataType string

Data type for post. Defaults to 'application/json'.

headers array

Headers for the request. Defaults to null.

Methods


<static> abort(requestId [, cleanUp])

Abort a request.

Parameters:
Name Type Argument Default Description
requestId string

The request id returned by getJson(), post(), etc.

cleanUp boolean <optional>
true

Also cleans up request if true. Defaults to true.

Returns:

True if the request was aborted & cleaned up, false if it wasn't found.

Type
boolean
Example
// Perform a request, then abort it.
const request_id = rgjs.xhr.getJson('//example.com/api/', callback, error);
rgjs.xhr.abort(request_id);

<static> abortAll()

Abort all requests.

Example
// Perform two requests, then abort both.
rgjs.xhr.getJson('//example.com/api/v1', callback, error);
rgjs.xhr.getJson('//example.com/api/v2', callback, error);
rgjs.xhr.abortAll();

<static> clearCache( [url])

Clear internal cache.

Parameters:
Name Type Argument Description
url string <optional>

Optional. If specified, only clears cache for the specific url.

Examples
// Clear all cache
rgjs.xhr.clearCache();
// Clear cache only for a specific url
rgjs.xhr.clearCache('//example.com/api/v1');

<static> get(url [, cb] [, cberr] [, opts])

XMLHttpRequest get request with sugar on top.

Parameters:
Name Type Argument Description
url string

The url for the request.

cb function <optional>

The callback. Arguments: response, status, statusText, xhr.

cberr function <optional>

The error callback. Arguments: status, statusText, xhr.

opts object <optional>

Options are optional. See _options.

Returns:

An identifier that can be used to abort the request.

Type
string
Example
// Get json response and pass it to onSuccess().
const request_id = rgjs.xhr.get('//example.com/api/', onSuccess, onError, {mimeType: 'application/json'});

<static> getJson(url [, cb] [, cberr] [, opts])

Convenient get() with opts.mimeType set to application/json.

Parameters:
Name Type Argument Description
url string

The url for the request.

cb function <optional>

The callback. Arguments: response, status, statusText, xhr.

cberr function <optional>

The error callback. Arguments: status, statusText, xhr.

opts object <optional>

Options are optional. See _options.

Returns:

An identifier that can be used to abort the request.

Type
string
Example
// Get json response and pass it to callback().
const request_id = rgjs.xhr.getJson('//example.com/api/', onSuccess, onError);

<static> post(url, data [, cb] [, cberr] [, opts])

XMLHttpRequest get request with sugar on top.

Parameters:
Name Type Argument Description
url string

The url for the request.

data object

An object containing key-value pairs.

cb function <optional>

The callback. Arguments: response, status, statusText, xhr.

cberr function <optional>

The error callback. Arguments: status, statusText, xhr.

opts object <optional>

Options are optional. See See _options.

Returns:

An identifier that can be used to cancel the request.

Type
string
Example
// Post json data.
const data = {uid: '...', account: '...'};
const request_id = rgjs.xhr.post('//example.com/api/', data, callback, error);

<inner> _send(url [, cb] [, cberr] [, opts])

Send an XMLHTTPRequest.

Parameters:
Name Type Argument Description
url string

The url for the request.

cb function <optional>

The callback. Arguments: response, status, statusText, xhr.

cberr function <optional>

The error callback. Arguments: status, statusText, xhr.

opts object <optional>

Options are optional. See _options.

Properties
Name Type Argument Description
method string <optional>

Optional method (GET, POST, etc). Defaults to 'GET'.

body object <optional>

Optional body for POST request.

Returns:

An identifier that can be used to abort the request.

Type
string