Class: Rest

Rest

new Rest()

The globally available RestJS library

Source:

Classes

Config

Members

(private, static) _requestInterceptors :Array

Holds response extractors

Type:
  • Array
Source:
See:

(private, static) _responseExtractors :Array

Holds response extractors

Type:
  • Array
Source:
See:

(private, static) _responseInterceptors :Array

Holds the response interceptors

Type:
  • Array
Source:
See:

Methods

(private, static) _createUrl(route, paramsopt) → {String}

Create a url based off a route and a parameter object

Parameters:
Name Type Attributes Default Description
route String

The route for the request, from when the factory was created

params Object <optional>
{}

The URL parameters for the request

Source:
To Do:
  • Fix the config object; pass it in instead of referring to it directly
Returns:

The created url, complete with the baseURL prepended and the parameters URL-encoded and appended

Type
String

(private, static) _findBodyAndParams(args, element) → {Object}

Takes the arguments from a function call, figuring out what's what

The arguments is an array of parameters in three different formats. The last parameter is always an optional parameters object.

The first arguments are strings, denoting the properties to send in the request. The last argument is the parameters object
(String..., [Object])

The first argument is a list of the properties that should be sent in the request. The second (and last) is the parameters object
(Array, [Object])

The first argument is the body that should be sent in the request. The second…well…you can guess: it's our old friend, the parameters object! :)
(Object, [Object])

Parameters:
Name Type Description
args Array

The arguments array from the function call

element Element

The Element in question

Source:
Returns:

{body: Object, params: Object}

Type
Object
Example
let {body, params} = Rest._findBodyAndParams(args, element)

(private, static) _makeRequest(config, verb, route, params, factory, bodyopt) → {Promise.<xhr.response>}

Makes a request with the necessary config

Parameters:
Name Type Attributes Default Description
config Object
verb String

The HTTP verb: GET, POST, PATCH, PUT

route String
params Object {}

The URL parameters for the request

factory Factory
body Object <optional>

The body of the request

Source:
Returns:

A promise that is resolved or rejected based on the request

Type
Promise.<xhr.response>

(private, static) _restify(response, factory, config) → {Element}

"Restifies" an element: add the default Rest methods to the prototype, and run it through the transformer

Parameters:
Name Type Description
response Object

The response object from xhr.response

factory Factory

The factory object that created the request

config Object

The configuration for the element

Source:
Returns:

The newly created Element, as returned by Factory.create

Type
Element

(static) addRequestInterceptor(func)

Adds a response interceptor, which is run before a request is sent

Parameters:
Name Type Description
func function

The interceptor (see the example)

Source:
Example
Rest.addRequestInterceptor(function(requestConfig, xhr, reject)) {
  requestConfig.route  // the URL for the request, minus the
  requestConfig.params // the parameters for the request
  requestConfig.body   // the request body
})

Expected format:

function(requestConfig, xhr, reject)

(static) addResponseExtractor(func)

Adds a response extractor, which manipulates the data after the response is received, but after interceptors are run

Parameters:
Name Type Description
func function

The extractor

Source:
Example

MAKE SURE YOU RETURN THE DATA. If you don't, the request will not resolve with data

Rest.addResponseExtractor(function(data) {
  return data
})

(static) addResponseInterceptor(func)

Adds a response interceptor, which is run when a response is received

Parameters:
Name Type Description
func function

The interceptor (see the example)

Source:
Example
Rest.addResponseInterceptor(function(data, responseType, route, responseURL, reject, xhr)) {
    data:         The response data
    responseType: The response type. See the docs
    route:        The route used for _makeRequest
    responseURL:  See the docs for `XMLHttpRequest.responseURL`
    reject:       The `reject` method from the Promise for the request
    xhr:          The XHR object used to make the request
})

Expected format:

function(data, responseType, route, responseURL, reject)

(static) factory(route, factoryTransformer, elementTransformer, customConfig) → {Factory}

The factory creator method for RestJS

Parameters:
Name Type Description
route String

The route

factoryTransformer Object | function

A transformer that is either added to the factory (if it's an object) or run on the factory (if it's a function)

elementTransformer Object | function

A transformer that is either added to the element (if it's an object) or run on the element (if it's a function)

customConfig Object

A custom configuration object @see Rest.Config

Source:
Returns:

A newly created Factory

Type
Factory