@splytech-io/splyt-partner-sdk
Splyt Partner SDK
Last updated 7 months ago by rainder .
ISC · Repository · Bugs · Original npm · Tarball
$ cnpm install @splytech-io/splyt-partner-sdk 
SYNC missed versions from official npm registry.

Splyt Partner SDK

Partner wrapper over @splytech-io/splyt-ws-connection module

Prerequisities

  • Node >= v6.0.0

Installation

$ npm install @splytech-io/splyt-partner-sdk

SplytPartnerSDK Class API

constructor(serverUrl: String, credentials: Object)

Creates an instance of SplytPartnerSDK, connects and signs in the partner. In case if connection is dropped SDK reconnects and relogins into Splyt automatically.

.request(method: String, data: Object): Promise

Sends a request message to the Splyt Backend and waits for the response. Once response is received a returned promise is resolved or rejected depending on a response message from the server.

Note: promise might be rejected if SDK fails to deliver a message to the backend.

const data = {
  login: 'username',
  password: 'password'
};

partner.request('partner.sign-in', data)
	.then(() => /* handle sucessfull response */)
	.catch((err) => /* handle erroneous response */);

.push(method: String, data: Object): Promise

Sends a push message to the Splyt Backend. Function returns a Promise which is resolved if message is delivered to the server.

.on(eventName: String, callbackFunction: Function): SplytPartnerSDK

All events are implemented using @rainder/co-event-emitter library. Only one event subscriber is allowed. It accepts GeneratorFunctions, Promises and Thunks as callback functions.

partner.on('event-name', callbackFunction);

Every request or push message from Splyt Backend to the SDK is converted into event where endpoint method name is used as event name. You can subscribe to specific method calls as following:

//example of GeneratorFunction usage as a callbackFunction
partner.on('endpoint-method-name', function *(data) {
  //handle request and send response
  
  return { response_data: 'some-data' };
});

If SDK receives a message to which you have not subscribed, SDK outputs a warning to the terminal.

Implementation example

const SplytPartnerSDK = require('@splytech-io/splyt-partner-sdk');

const partner = new SplytPartnerSDK.Client('wss://wsapi.sandbox.splytech.io', {
  login: 'username',
  password: 'password',
});

//ie using resolved Promise
partner.on('partner.journey.estimate', (data) => {
  //returns an estimate price of the journey
  
  return Promise.resolve({
    price_range: {
      lower: 1234,
      upper: 1566,
    }
  });
});

//ie using generator function
partner.on('partner.new-trip-request', function *(data) {
  if (data.passenger_groups[0].pickup.address === '83 Baker Street') {
    //sends an erroneous response to the Splyt Backend
    throw new SplytPartnerSDK.Error.BAD_REQUEST('invalid pickup address');
  }
  
  //make asynchronous calls
  const result = yield someExternalHttpCall(data);
  
  //sends successful response message to the server
  return { someData: result.data };
});

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 8
Dependencies (5)
Dev Dependencies (1)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |