new Communicator(options)
The
Communicator
class serves to abstract communications with a server,
it does so by executing Request
s 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 Adapter s can be constructed, available on the instance as well as the class |
Request |
Request | The Request class, exposed so Request s can be constructed, available on the instance as well as the class |
Connection |
Connection | The Connection class, exposed so Connection s 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 name
String | Connection Name of the connection
(or aConnection
instance) that should be connected.Returns:
- Type
- Promise
Example
communicator.connect('local-xhr') .then(...);
-
disconnect(name) → {Promise}
-
Disconnects a
Connection
Parameters:
Name Type Description name
String | Connection Name of the connection
(or aConnection
instance) that should be disconnected.Returns:
- Type
- Promise
Example
communicator.disconnect('local-xhr') .then(...);
-
registerAdapter(adapter)
-
Registers a
Adapter
so it can be used byConnection
s, also available as a static method.Parameters:
Name Type Description adapter
Object Object containing the properties for an Adapter
- See:
Example
communicator.registerAdapter({...});
-
registerAdapters(adapters)
-
Registers
Adapter
s (provided as a hashmap) so they can be used byConnection
s, also available as a static method.Parameters:
Name Type Description adapters
Object.<Object> Hashmap containing objects containing the properties for Adapter
s- See:
Example
communicator.registerAdapters({...});
-
registerConnection(connection)
-
Register a
Connection
so it can be used byRequest
s, also available as a static method. Registers aConnection
Parameters:
Name Type Description connection
Object Object containing the properties for a Connection
- See:
Example
communicator.registerConnection({...});
-
registerConnections(connections)
-
Registers
Connection
s (provided as a hashmap) so they can be used byRequest
s, also available as a static method.Parameters:
Name Type Description connections
Object.<Object> Hashmap containing objects containing the properties for Connection
s- See:
Example
communicator.registerConnections({...});
-
registerRequest(request)
-
Registers a
Request
Parameters:
Name Type Description request
Object Object containing the properties for a Request
- See:
Example
communicator.registerRequest({...});
-
registerRequests(requests)
-
Registers
Request
s (provided as a hashmap), also available as a static method.Parameters:
Name Type Description requests
Object.<Object> Hashmap containing objects containing the properties for Request
s- See:
Example
communicator.registerRequests({...});