new Connection(options) → {Connection}
Parameters:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
options |
Object |
<optional> |
{} | Object containing the properties listed below |
Properties:
Name | Type | Argument | Default | Description |
---|---|---|---|---|
name |
String | The name of the Connection | ||
url |
String |
<optional> |
The current protocol, hostname and port | The (base) url of the connection, protocol + host + port, http://localhost:1337 for example |
adapter |
String | A string referring to the name of an Adapter |
Returns:
- Type
- Connection
Example
// construct a Connection, the new keyword is not necessary const connection = Connection({ name: 'local-xhr', url: 'http://localhost:1337', adapter: 'XHR' });
Members
-
requests :Object.<(Request|Object.<Request>)>
-
Type:
Example
// request name is 'deep.name' in this case connection.requests.deep.name.execute(splat1, splat2, requestBody) .then(...); // request name is 'name' in this case connection.requests.name.execute(splat1, splat2, requestBody) .then(...);
-
server :Object.<(function()|Object.<function()>)>
-
Type:
- Object.<(function()|Object.<function()>)>
Example
// request name is 'deep.name' in this case connection.server.deep.name(splat1, splat2, requestBody) .then(...); // request name is 'name' in this case connection.server.name(splat1, splat2, requestBody) .then(...);
Methods
-
connect() → {Promise}
-
Connects this Connection to the server using its url and adapter.
Returns:
- Type
- Promise
Example
// connect this connection to the server connection.connect() .then(...);
-
delete()
-
Does a DELETE request using this
Connection
Returns:
PromiseExample
connection.delete('/user/3') .then(...);
-
disconnect() → {Promise}
-
Disconnects this Connection from the server using its url and adapter.
Returns:
- Type
- Promise
Example
// disconnect this connection from the server connection.disconnect() .then(...);
-
emit(event, data)
-
Emits a client event to the server.
Parameters:
Name Type Description event
String Client event to emit. data
* Data passed to event handlers Example
// trigger an event from the client to the server connection.emit('serverEvent', data);
-
get()
-
Does a GET request using this
Connection
Returns:
PromiseExample
connection.get('/user', {id: 3}) .then(...);
-
on(event, eventHandler)
-
Listens for a server event
Parameters:
Name Type Description event
String Server event to listen for. eventHandler
function Function to execute when the event has occurred. Example
// listen for an event being triggered by the server connection.on('serverEvent', function callback(data) {});
-
post()
-
Does a POST request using this
Connection
Returns:
PromiseExample
connection.post('/user', {id: 3, firstName: 'asd'}) .then(...);
-
put()
-
Does a PUT request using this
Connection
Returns:
PromiseExample
connection.put('/user/3', {id: 3, firstName: 'rsgsrg'}) .then(...);
-
Request(options) → {Request}
-
Adds a
Request
to thisConnection
Parameters:
Name Type Description options
Object Options for the Request
Returns:
- Type
- Request
-
request(options) → {Promise}
-
Makes a request to the server
Parameters:
Name Type Argument Default Description options
Object <optional>
{} Object containing a url, method and optionally a data property Returns:
Returns a promise that resolves with the server response- Type
- Promise
Example
// make a request to the server connection.request({ url: '/user', method: 'get', data: { id: 67 } }) .then(...);