0.1.0 • Published 6 years ago

gdax-node-observables v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

GDAX Node.js library with Observables

Observables functionality, based on RxJS, for the official Node.js library for Coinbase's GDAX API.

The library has all the functionality as the official library but also offers observables. Promises are still the default and you need to enable observables with the option { useObservables: true }.

This documentation only covers the observables functionality of the library. For the full documentation please view the official repository documentation.

Features

  • Observables functionality for the public and authenticated client as well as for websockets.

Installation

npm install gdax-node-observables

Quick Start

The GDAX API has both public and private endpoints. If you're only interested in the public endpoints, you should use a PublicClient.

const Gdax = require('gdax-node-observables');
const { of } = require('rxjs/Observable/of');
const { catchError } = require('rxjs/operators');

const publicClient$ = new Gdax.PublicClient('https://api.gdax.com', {
  useObservables: true,
});

With the option { useObservables: true } all methods will now return an observable instead of the default promise.

Using Observables

publicClient$
  .getProducts()
  .pipe(
    catchError(error => of(error)) // catch errors
  )
  .subscribe(data => {
    // work with data
  });

The Authenticated API Client

The AuthenticatedClient works in the same way as the official API Client when observables are enabled. In order to use observables just set the options to { useObservables: true }.

const key = 'your_api_key';
const secret = 'your_b64_secret';
const passphrase = 'your_passphrase';

const apiURI = 'https://api.gdax.com';

const authedClient$ = new Gdax.AuthenticatedClient(
  key,
  secret,
  passphrase,
  apiURI,
  { useObservables: true }
);

Like PublicClient, all API methods will now return an observable.

Websocket Client

The WebsocketClient works slightly different with observables enabled in comparison to the PublicClient and AuthenticatedClient. In the same way you need to specify the option { useObservables: true } to enable observables. But the returned observable is now available as a method on the WebsocketClient instance.

const productIDs = ['BTC-USD', 'ETH-USD'];
const wsURI = 'wss://ws-feed-public.sandbox.gdax.com';
const auth = {
  key: 'suchkey',
  secret: 'suchsecret',
  passphrase: 'muchpassphrase',
};
const options = {
  channels: ['full', 'level2'],
  useObservables: true,
};

const websocketClient = new Gdax.WebsocketClient(
  productIDs,
  wsURI,
  auth,
  options
);

websocketClient.observable.subscribe(data => {
  // Work with data
});

All observable data is now emitted through WebsocketClient.observables.