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

sdk-base

Package Overview
Dependencies
Maintainers
4
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sdk-base

a base class for sdk with default error handler

  • 3.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
133K
increased by1%
Maintainers
4
Weekly downloads
 
Created
Source

sdk-base

NPM version build status Test coverage Gittip David deps npm download

A base class for sdk with some common & useful functions.

Installation

$ npm install sdk-base

Usage

Constructor argument:

  • {Object} options

    • {String} [initMethod] - the async init method name, the method should be a generator function. If set, will execute the function in the constructor.
    'use strict';
    
    const co = require('co');
    const Base = require('sdk-base');
    
    class Client extends Base {
      constructor() {
        super({
          initMethod: 'init',
        });
      }
    
      * init() {
        // put your async init logic here
      }
    }
    
    co(function* () {
      const client = new Client();
      // wait client ready, if init failed, client will throw an error.
      yield client.ready();
    
      // support generator event listener
      client.on('data', function* (data) {
        // put your async process logic here
        //
        // @example
        // ----------
        // yield submit(data);
      });
    
      client.emit('data', { foo: 'bar' });
    
    }).catch(err => { console.error(err); });
    

API

  • .ready(flagOrFunction) flagOrFunction is optional, and the argument type can be Boolean, Error or Function.

    // init ready
    client.ready(true);
    // init failed
    client.ready(new Error('init failed'));
    
    // listen client ready
    client.ready(err => {
      if (err) {
        console.log('client init failed');
        console.error(err);
        return;
      }
      console.log('client is ready');
    });
    
    // support promise style call
    client.ready()
      .then(() => { ... })
      .catch(err => { ... });
    
    // support generator style call
    yield client.ready();
    
  • .on(event, listener) wrap the EventEmitter.prototype.on(event, listener), the only difference is to support adding generator listener on events, except 'error' event.

  • once(event, listener) wrap the EventEmitter.prototype.once(event, listener), the only difference is to support adding generator listener on events, except 'error' event.

  • prependListener(event, listener) wrap the EventEmitter.prototype.prependListener(event, listener), the only difference is to support adding generator listener on events, except 'error' event.

  • prependOnceListener(event, listener) wrap the EventEmitter.prototype.prependOnceListener(event, listener), the only difference is to support adding generator listener on events, except 'error' event.

  • addListener(event, listener) wrap the EventEmitter.prototype.addListener(event, listener), the only difference is to support adding generator listener on events, except 'error' event.

    client.on('data', function* (data) {
      // your async process logic here
    });
    client.once('foo', function* (bar) {
      // ...
    });
    
    // listen error event
    client.on('error', function(err) {
      console.error(err.stack);
    });
    
  • .await(event): await an event, return a promise, and it will resolve(reject if event is error) once this event emmited.

    co(function* () {
      const data = yield client.await('data');
    });
    

License

MIT

Keywords

FAQs

Package last updated on 14 Mar 2017

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