@sobol/client

2.0.0 • Public • Published

@sobol/client

A JavaScript client library exposing Sobol's RESTful API.

Installation

npm install @sobol/client

The client will be avaliable in two distributions: Node and Browser.

Node

To use in node, include the client singleton as follows:

const Client = require('@sobol/client');

// ...

To use the constructor instead, instantiate the client as follows:

const SobolClient = require('@sobol/client/base');
const Client = new SobolClient();

// ...

Browser

To use in browser, include the client constructor as follows:

<!-- include library -->
<script type="text/javascript" src="./node_modules/@sobol/client/browser/sobol-client.js"></script>

<!-- instantiate the client -->
<script type="text/javascript">
  var Client = new SobolClient();

  // ...
</script>

Configuration

The configure() function can be called at any time and allows you to pass options such as the API Key as follows:

Client.configure({
  key: 'eyJhbGciOiJSUzI1NiIsI5cCI6IkpXVCJ9...',
})
  .then(function() {
    console.log('Session' + Client.getSession());
    console.log('Version' + Client.getVersion());

    // ...
  })
  .catch(function(error) {
    console.error(error);
  });

Options

Option Type Description Default
key String API Key ''
endpoint String API Endpoint https://sobol.io/d/api/v1
headers Object Request Headers {}
orgId String Organization ID ''
errorHandler Function Error Handler Promise.reject(err)

Note: to obtain a key, please go to Sobol and navigate to Settings -> Applications.

Query

To query the API, refer to the list of endpoints contained within the Client:

console.log(Client);

Example

Client.configure({
  key: 'eyJhbGciOiJSUzI1NiIsI5cCI6IkpXVCJ9...',
})
  .then(function() {
    Client.Users.find()
      .then(function(res) {
        console.log('Sobol has ' + res.data.length + ' users.');
      })
      .catch(function(error) {
        console.error('Network Error: ' + error.response.data);
      });
  })

Note: this client uses the Axios Response Schema.

To run a few samples, create a .env and add SOBOL_API_KEY=... in this project's root.
Then run:

  • Node: npm run start-node
  • Browser: npm run start-browser

Extending the Client

To extend the library, create a subclass as follows:

// client.js

const BaseClient = require('@sobol/client/base');

class MyClient extends BaseClient {
  constructor() {
    super();

    this.CustomEndpoints = {
      version: () => this._request.get('version'),
    };
  }
}

module.exports = new MyClient();

Then use in:

// index.js

const MyClient = require('./client.js');

MyClient.configure({
  key: 'eyJhbGciOiJSUzI1NiIsI5cCI6IkpXVCJ9...',
})
  .then(() => {
    MyClient.CustomEndpoints.version()
      .then((res) => {
        console.log('Sobol Version: ', res.data);
      });
  });

Note: the browser distribution can not be extended.

Helper Functions

Use these functions to get information about the current instance as follows:

  1. getSession() - returns the current session
  2. getKey() - returns the current key
  3. getOrgId() - returns the current organization Id
  4. getVersion() - returns the current version

Clean Up

The destroy() function can be called once to dismantle the current instance as follows:

Client.configure({
  key: 'eyJhbGciOiJSUzI1NiIsI5cCI6IkpXVCJ9...',
})
  .then(function() {
    Client.destroy();
  })

Help and Feedback

For help and feedback, please send us an email to team@sobol.io and we will respond as soon as possible.

Readme

Keywords

none

Package Sidebar

Install

npm i @sobol/client

Weekly Downloads

2

Version

2.0.0

License

MIT

Unpacked Size

965 kB

Total Files

17

Last publish

Collaborators

  • nathan-knight
  • leifdejong
  • vlad.kutsenko
  • victor.leipnik