Class: Request

Request

new Request(options)

A Request is a blueprint for a request, after defining one, all you need is a the data to execute it.
Parameters:
Name Type Description
options Object Object containing the properties
Properties:
Name Type Description
name String Full name of the request, 'UserLoginRequest' for example
shortName String The short name of this request, 'login' for example
method String The method of this request, 'get' for example
connection String The connection this Request should use to execute
route String The route of the this request, relative to the url of the connection, '/user/:id' for example
resolve function When specified this method gets called when the request was successful
reject function When specified this method gets called when the request was unsuccessful
See:
Example
const request = new Request({
  name: 'UserLoginRequest',
  shortName: 'login',
  method: 'get',
  route: '/user/login',
  connection: 'local-xhr',
  resolve(data) {
    return new Promise((resolve, reject) => {
      // we can make a resolving request reject if we want to here
      reject(data);
    });
  },
  resolve(data) {
    return new Promise((resolve, reject) => {
      // we can make a reject request resolve if we want to here
      resolve(data);
    });
  }
});

// executing a Request using it's Connection
request.execute({data: {asd: true}})
  .then(...);

// executing a Request using an explicit Connection
request.execute({data: {asd: true}}, connection)
  .then(...);

Methods

<static> validateImplementation(options, anonymous)

Validates whether an object contains all properties to be considered a valid Request implementation. A valid configuration consists of an object containing valid: name, method and route attributes. If connection is specified it has to be valid also (meaning a connection under the that name has to be registered).
Parameters:
Name Type Description
options Object Object containing the implementation for a ClassWithConnection
anonymous Boolean Indicates whether the request that is passed in to verify is an anonymous request, a request that won't be registered and doesn't need a name
Throws:
Error

execute(data, connection) → {Promise}

Executes this Request using it's Connection or the Connection specified in the arguments, fills the splats in the route with the data provided.
Parameters:
Name Type Description
data Object Data that has to be sent to the server, this data is also used to fill splats in the routes
connection Connection Connection with which to execute this Request, defaults to this.connection
Returns:
Type
Promise
Example
request.execute({id: 3})
  .then(data => {
    console.log(data);
  });

<abstract> reject(data) → {Promise}

Reject method, for the user to override by specifying it in the options, this method gets executed whenever this Request rejects. When implemented this method should return a Promise.
Parameters:
Name Type Description
data * Data received from the server by executing this Request
Returns:
Type
Promise

<abstract> resolve(data) → {Promise}

Resolve method, for the user to override by specifying it in the options, this method gets executed whenever this Request resolves. When implemented this method should return a Promise.
Parameters:
Name Type Description
data * Data received from the server by executing this Request
Returns:
Type
Promise
Rik Hoffbauer 2015
Documentation generated by JSDoc 3.3.3 on 2015-10-20T14:38:13+02:00