Promise based OpenTMI javascript client for node.js and browser. Extendable for custom API's provided by opentmi addons.
npm i opentmi-jsclient
to build minified version to dist -folder run:
> npm run build
build api documentations
> npm run doc
Note: all available commands are visible when you run: npm run
const {Authentication, Transport, Resources} = require('opentmi-client');
const transport = new Transport('http://localhost:3000')
const auth = new Authentication(transport);
auth
.login('user@mail.com', 'password')
.then(() => transport.connect())
.then(() => {
const collection = new Resources(transport);
return collection.find()
.haveTag('fantastic')
.exec()
.then(resources => resources[0])
.then(resource => {
return resource
.name('new name')
.save();
});
})
<script src="dist/opentmi-client.js"></script>
<script>
const {Authentication, Transport} = opentmiClient;
const transport = new Transport('http://localhost:3000')
const auth = new Authentication(transport);
auth
.login('user@mail.com', 'password')
.then(() => transport.connect())
</script>
Note: see more examples from sample -folder.
It is easy to create support for custom API's. See example below:
class CustomAPI {
constructor(transport) {
this._transport = transport;
}
some() {
debug('attempt to get something from custom addon api');
return this._transport
.get('/addon-api')
.then(response => response.data);
}
}
const customApi = new CustomAPI(transport);
customApi.some().then(data => console.log(data));