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
ConnectionReturns:
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 eventString 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
ConnectionReturns:
PromiseExample
connection.get('/user', {id: 3}) .then(...); -
on(event, eventHandler)
-
Listens for a server event
Parameters:
Name Type Description eventString Server event to listen for. eventHandlerfunction 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
ConnectionReturns:
PromiseExample
connection.post('/user', {id: 3, firstName: 'asd'}) .then(...); -
put()
-
Does a PUT request using this
ConnectionReturns:
PromiseExample
connection.put('/user/3', {id: 3, firstName: 'rsgsrg'}) .then(...); -
Request(options) → {Request}
-
Adds a
Requestto thisConnectionParameters:
Name Type Description optionsObject Options for the RequestReturns:
- Type
- Request
-
request(options) → {Promise}
-
Makes a request to the server
Parameters:
Name Type Argument Default Description optionsObject <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(...);