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 |
- See:
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 |
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
Requestimplementation. 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 optionsObject Object containing the implementation for a ClassWithConnectionanonymousBoolean 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
Requestusing it'sConnectionor theConnectionspecified in the arguments, fills the splats in the route with the data provided.Parameters:
Name Type Description dataObject Data that has to be sent to the server, this data is also used to fill splats in the routes connectionConnection Connection with which to execute this Request, defaults to this.connectionReturns:
- 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
Requestrejects. When implemented this method should return a Promise.Parameters:
Name Type Description data* Data received from the server by executing this RequestReturns:
- 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
Requestresolves. When implemented this method should return a Promise.Parameters:
Name Type Description data* Data received from the server by executing this RequestReturns:
- Type
- Promise