Apiway - Client side
Getting started
# Install$ npm install apiway --save
// Require;
Hierarchy
EventEmitter ||---> Api ||---> Resource||---> Store
Api
Api provides an requests interface to actions of controllers server.
Methods
Api
Open websocket connection to Apiway server, options:
- aliveDelay - seconds between ping sendings, default: 0 (off)
Api
Make query to controller action on server side with params, return Promise.
Api
Send your custom event with data.
Api
Close connection.
Api
Set a method that returns a promise. Called after the connection and triggering ready/unready events. It can be used for pre-authentication before ready event.
Api # call on every triggerApi # call on first trigger
Attach callback with context to "ready" event.
Api
Detach callback with context from "ready" event.
Api # call on every triggerApi # call on first trigger
Attach callback with context to "unready" event.
Api
Detach callback with context from "unready" event.
Example
Api ;
Api ;
Resource
Resources provide access to relevant data and automatically synchronized when they change.
Create
let resource = resourceName params ;
Create new resource with params.
Methods
resource.name
Getter to resource name.
resource.data
Getter to resource data.
resource.get( paramName )
Return value of resource param.
resource.set( { paramName: paramValue, paramName: paramValue } )
Set values of resource params.
resource.unset( paramName )
Remove param from resource params.
resource # call on every triggerresource # call on first trigger
Attach callback with context to "change" event.
resource
Detach callback with context from "change" event.
resource # call on every triggerresource # call on first trigger
Attach callback with context to "error" event.
resource
Detach callback with context from "error" event.
Example
let currentUser = "CurrentUser" ;currentUser ; // > Error auth_error Api; // > Data of current user: {id: 1, name: "Bob"} let messages = "Messages" limit: 3 ;messages; // > Messages [{text: "Hello world"}, {text: "Hello world"}, {text: "Hello world"}] messages; // > Messages [{text: "Hello world"}, {text: "Hello world"}] messages; // > 2
Store
Store is a simple object for the storage data of your application and access to them from different parts of the application.