Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apiway

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apiway

Client side for Apiway framework

  • 0.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Apiway - Client side

Server side

Getting started

# Install
$ npm install apiway --save                
// Require
import { Api, Resource, Store } from "apiway";

Hierarchy

EventEmitter 
|
|---> Api             
|
|---> Resource
|
|---> Store

EventEmitter docs

Api

Api provides an requests interface to actions of controllers server.

Methods

Api.connect( address[, options ] )

Open websocket connection to Apiway server, options:

  • aliveDelay - seconds between ping sendings, default: 0 (off)
Api.query( "controller.action", params )

Make query to controller action on server side with params, return Promise.

Api.send( event, data )

Send your custom event with data.

Api.disconnect()

Close connection.

Api.beforeReadyPromise( callback )

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.onReady( callback, context )  # call on every trigger
Api.oneReady( callback, context ) # call on first trigger

Attach callback with context to "ready" event.

Api.offReady( callback, context )

Detach callback with context from "ready" event.

Api.onUnready( callback, context )  # call on every trigger
Api.oneUnready( callback, context ) # call on first trigger

Attach callback with context to "unready" event.

Api.offUnready( callback, context )

Detach callback with context from "unready" event.

Example

Api
  .connect( "ws://localhost:3000", { aliveDelay: 5000 } )
  .beforeReadyPromise( function(){
    return Api.query( "Users.auth_by_token", { token: Cookie.get( "token" ) } );
  })
  .oneReady( function(){
    RouterRun();
  });
Api.query( "Messages.new", { text: "Hello world!" } )
  .then(  (e)=>{ console.log( "success" ) } )
  .catch( (e)=>{ console.log( "failure" ) } );

Resource

Resources provide access to relevant data and automatically synchronized when they change.

Create

let resource = new 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.onChange( callback, context )  # call on every trigger
resource.oneChange( callback, context ) # call on first trigger

Attach callback with context to "change" event.

resource.offChange( callback, context )

Detach callback with context from "change" event.

resource.onError( callback, context )  # call on every trigger
resource.oneError( callback, context ) # call on first trigger

Attach callback with context to "error" event.

resource.offError( callback, context )

Detach callback with context from "error" event.

Example

let currentUser = new Resource( "CurrentUser" );
currentUser
  .onChange( ( e )=>{ console.log( "Data of current user:", currentUser.data ) })
  .onError(  ( e )=>{ console.log( "Error", e ) });

// > Error auth_error

Api.query( "Users.auth_by_name", { name: "Bob" } );

// > Data of current user: {id: 1, name: "Bob"}

let messages = new Resource( "Messages", { limit: 3 } );
messages.onChange( ()=>{ console.log( "Messages:", messages.data ) } );

// > Messages [{text: "Hello world"}, {text: "Hello world"}, {text: "Hello world"}]

messages.set({ limit: 2 });

// > Messages [{text: "Hello world"}, {text: "Hello world"}]

messages.get( "limit" );

// > 2

Store

Store is a simple object for the storage data of your application and access to them from different parts of the application.

FAQs

Package last updated on 29 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc