Classes: Gateway

Gateway

The Gateway class is used to communicate to the backend.

The Gateway uses isomorphic-fetch to execute the XHR requests, so both client and server usage is supported.

Setup

The Gateway is created like this:

this.gateway = new Gateway({
     // the base url
     url: 'http://www.example.com/api/v1/',
     mode: 'cors', // cors, no-cors, or same-origin
     // can be changed to other handles to support different kind of communication structures
     outputHandler: new RESTOutputHandler(),
     inputHandler: new RESTInputHandler(),
     // can be used to capture errors that happen on each request, and log them somewhere
     onError(error) {
       captureError(error);
     },
     // allows you to alter the request before it is sent, useful for things that rely on
     // 'global state', that you don't want to pass along in each request
     // e.g. adding auth headers
     beforeRequest(options) {
       return options;
     },
}, true);

You can pass any of the following options for global configuration:

  • url // the base url for all requests, the request url will be appended here
  • onError // can be used to capture errors that happen on each request, and log them somewhere
  • beforeRequest // allows you to alter the request before it is sent, useful for things that // rely on 'global state', that you don't want to pass along in each request // e.g. adding auth headers

In addition you can set defaults for the normal request options used in the fetch api: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch

The options passed into the Gateway constructor are merged with the options that can be passed for each request.

Examples

// normal get request
gatewayInstance.get('/subscription-packages/')
  .then(response => console.log(response));

// when needing to add cookies, using the options
gatewayInstance.get('/antiforgery/', null, { credentials: 'include' })
  .then(response => console.log(response));

// when sending data and adding extra headers
gatewayInstance.post('/accounts/', data, {
    headers: { 'X-XSRF-Token': response.data },
    credentials: 'include',
  })
  .then(response => console.log(response));

new Gateway(options)

Parameters:
Name Type Description
options object

Extends the fetch init options object, this will be merged with the same options object that can be specified per call, and passed to fetch when a request is done.

Source:

Methods


delete(action, data, options_)

DELETE shorthand for the execute method

Parameters:
Name Type Default Description
action string
data any null
options_ Object
Source:
Returns:

A IGatewayResult Promise

Type
Promise

<async> execute(action, data, options_)

Executes the gateway request

Parameters:
Name Type Description
action string
data any
options_ Object
Source:
Returns:

A IGatewayResult Promise

Type
Promise

get(action, data, options_)

GET shorthand for the execute method

Parameters:
Name Type Default Description
action string
data any null
options_ Object
Source:

getOptions()

Returns the passed options

Source:
Returns:
Type
Object

HEAD shorthand for the execute method

Parameters:
Name Type Default Description
action string
data any null
options_ Object
Source:
Returns:

A IGatewayResult Promise

Type
Promise

patch(action, data, options_)

PATCH shorthand for the execute method

Parameters:
Name Type Description
action string
data any
options_ Object
Source:
Returns:

A IGatewayResult Promise

Type
Promise

post(action, data, options_)

POST shorthand for the execute method

Parameters:
Name Type Description
action string
data any
options_ IGatewayCallOptions
Source:
Returns:

A IGatewayResult Promise

Type
Promise

put(action, data, options_)

PUT shorthand for the execute method

Parameters:
Name Type Description
action string
data any
options_ Object
Source:
Returns:

A IGatewayResult Promise

Type
Promise

setOptions(options)

Sets global options

Gateway defaults are managed here

Parameters:
Name Type Description
options Object

global gateway options

Source: