npm package: @sobol/client
Description: A JavaScript client library exposing Sobol's RESTful API.
Author: Sobol -
Latest version: 2.0.0

# @sobol/client

A JavaScript client library exposing Sobol's RESTful API.

## Installation

```bash
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:

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

// ...
```

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

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

// ...
```

### Browser

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

```html
<!-- 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:

```javascript
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](https://sobol.io/d/login) and navigate to `Settings` -> `Applications`.

## Query

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

```javascript
console.log(Client);
```

### Example

```javascript
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](https://github.com/axios/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:

```javascript
// 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:

```javascript
// 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:

```javascript
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.