@sobol/client
v2.0.0
Published
A JavaScript client library exposing Sobol's RESTful API.
Downloads
12
Keywords
Readme
@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:
getSession()
- returns the current sessiongetKey()
- returns the current keygetOrgId()
- returns the current organization IdgetVersion()
- 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 [email protected] and we will respond as soon as possible.