new Communicator(options)
The
Communicator class serves to abstract communications with a server,
it does so by executing Requests using a Connection that talks to the server using an Adapter.
Options passed into the constructor will be available under the options property
Parameters:
| Name | Type | Description |
|---|---|---|
options |
Object | Object containing the properties |
- See:
Properties:
| Name | Type | Description |
|---|---|---|
Adapter |
Adapter | The Adapter class, exposed so Adapters can be constructed, available on the instance as well as the class |
Request |
Request | The Request class, exposed so Requests can be constructed, available on the instance as well as the class |
Connection |
Connection | The Connection class, exposed so Connections can be constructed, available on the instance as well as the class |
options |
Object | Options object that was passed into the constructor |
config |
Object | Configuration for the communicator, containing properties such as 'defaultConnection' |
adapters |
Object | Hashmap containing adapters, this object will be registered using Communicator#registerAdapters |
connections |
Object | Hashmap containing connections, this object will be registered using Communicator#registerConnections |
requests |
Object | Hashmap containing requests, this object will be registered using Communicator#registerRequests |
Example
const communicator = new Communicator();
// register an Adapter
communicator.registerAdapter({
name: 'XHR',
request(request) {
// handle request and return a promise that resolves with the server response
// reject the promise if the request was unsuccessful
}
});
// or
const adapter = new communicator.Adapter({
...
});
// register a Connection
const connection = communicator.registerConnection({
name: 'local-xhr',
url: 'http://localhost:1337',
adapter: 'XHR'
});
// or
const connection = new communicator.Connection({
...
});
// register a Request
const request = communicator.registerRequest({
name: 'FindByIdUsersRequest',
shortName: 'findById',
route: '/user/:id',
method: 'get',
connection: 'local-xhr'
});
// or
const request = new communicator.Request({
...
});
// to execute the Request using its Connection
request.execute({
id: 4
})
.then(...);
// to execute a Request using a Connection
connection.request(request, {
id: 4
})
.then(...);
Methods
-
connect(name) → {Promise}
-
Connects a previously registered
Connection.Parameters:
Name Type Description nameString | Connection Name of the connection(or aConnectioninstance) that should be connected.Returns:
- Type
- Promise
Example
communicator.connect('local-xhr') .then(...); -
disconnect(name) → {Promise}
-
Disconnects a
ConnectionParameters:
Name Type Description nameString | Connection Name of the connection(or aConnectioninstance) that should be disconnected.Returns:
- Type
- Promise
Example
communicator.disconnect('local-xhr') .then(...); -
registerAdapter(adapter)
-
Registers a
Adapterso it can be used byConnections, also available as a static method.Parameters:
Name Type Description adapterObject Object containing the properties for an Adapter- See:
Example
communicator.registerAdapter({...}); -
registerAdapters(adapters)
-
Registers
Adapters (provided as a hashmap) so they can be used byConnections, also available as a static method.Parameters:
Name Type Description adaptersObject.<Object> Hashmap containing objects containing the properties for Adapters- See:
Example
communicator.registerAdapters({...}); -
registerConnection(connection)
-
Register a
Connectionso it can be used byRequests, also available as a static method. Registers aConnectionParameters:
Name Type Description connectionObject Object containing the properties for a Connection- See:
Example
communicator.registerConnection({...}); -
registerConnections(connections)
-
Registers
Connections (provided as a hashmap) so they can be used byRequests, also available as a static method.Parameters:
Name Type Description connectionsObject.<Object> Hashmap containing objects containing the properties for Connections- See:
Example
communicator.registerConnections({...}); -
registerRequest(request)
-
Registers a
RequestParameters:
Name Type Description requestObject Object containing the properties for a Request- See:
Example
communicator.registerRequest({...}); -
registerRequests(requests)
-
Registers
Requests (provided as a hashmap), also available as a static method.Parameters:
Name Type Description requestsObject.<Object> Hashmap containing objects containing the properties for Requests- See:
Example
communicator.registerRequests({...});