roboto-js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/client_test.js ADDED
@@ -0,0 +1,51 @@
1
+
2
+ // use secrets
3
+ const fs = require('fs');
4
+ const creds = JSON.parse(fs.readFileSync('.secrets/roboto/roboto_svcs_credentials.live.json', 'utf-8'));
5
+ process.env.ROBOTO_API_KEY = creds.roboto.accessKey;
6
+ process.env.ROBOTO_API_KEY = creds.roboto.host;
7
+ //
8
+
9
+ const { RobotoApi } = require('./src/index.js');
10
+ const roboto = RobotoApi.configure({
11
+ apiKey: process.env.ROBOTO_API_KEY,
12
+ baseUrl: process.env.ROBOTO_HOST
13
+ });
14
+
15
+ console.log('Starting RobotoAPI client library test..');
16
+
17
+
18
+ async function main(){
19
+ //roboto.query({})
20
+ var user = await roboto.login({ email: null, password: null });
21
+
22
+ console.log('iac-session started:');
23
+ console.log(user);
24
+
25
+ debugger;
26
+
27
+ try{
28
+
29
+ var newSite = await roboto.create('<@doc_website.site>');
30
+ newSite.set('configs.label', "my-new-site2");
31
+ await newSite.save();
32
+
33
+ }
34
+ catch(e){
35
+ console.log('client-test.error');
36
+ console.log(e);
37
+ }
38
+
39
+
40
+ // var websites = await roboto.query('<@doc_website.site>', { where: 'id=s78ef2e99ab2ceae' });
41
+ //
42
+ // //console.log(websites);
43
+ // //debugger;
44
+ //
45
+ // for (let website of websites) {
46
+ // console.log('Label: '+website.get('configs.label'));
47
+ // }
48
+ //
49
+ }
50
+
51
+ main();
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "roboto-js",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "",
10
+ "license": "ISC",
11
+ "dependencies": {
12
+ "axios": "^1.5.1",
13
+ "crypto-js": "^4.1.1"
14
+ }
15
+ }
package/src/index.js ADDED
@@ -0,0 +1,38 @@
1
+
2
+ import RbtApi from './rbt_api';
3
+ import RbtObject from './rbt_object';
4
+
5
+ export {
6
+ RbtApi,
7
+ RbtObject
8
+ //User,
9
+ //Site
10
+ };
11
+
12
+ export default class Roboto{
13
+
14
+ constructor() {
15
+
16
+ this.config = {
17
+ apiKey: process.env.NEXT_PUBLIC_ROBOTO_API_KEY,
18
+ baseUrl: 'https://' + (process.env.NEXT_PUBLIC_ROBOTO_HOST),
19
+ };
20
+
21
+ this.api = RbtApi.configure(this.config);
22
+
23
+ }
24
+
25
+ async login(params){
26
+ return this.api.login(params);
27
+ }
28
+
29
+ async create(params, data={}){
30
+ return this.api.create(params, data);
31
+ }
32
+
33
+ async query(params){
34
+ return this.api.query(params);
35
+ }
36
+
37
+ }
38
+
package/src/rbt_api.js ADDED
@@ -0,0 +1,99 @@
1
+ import axios from 'axios';
2
+ import CryptoJS from 'crypto-js';
3
+ import RbtObject from './rbt_object.js';
4
+ import _ from 'lodash';
5
+
6
+ export default class RbtApi {
7
+
8
+ constructor(axiosInstance) {
9
+ this.axios = axiosInstance;
10
+ this.iac_session = null;
11
+ this.authtoken = null;
12
+ }
13
+
14
+ static configure({ apiKey, baseUrl }) {
15
+ if (!RbtApi.instance) {
16
+ const axiosInstance = axios.create({
17
+ baseURL: baseUrl,
18
+ headers: {
19
+ accesskey: apiKey
20
+ }
21
+ });
22
+ RbtApi.instance = new RbtApi(axiosInstance);
23
+ }
24
+ return RbtApi.instance;
25
+ }
26
+
27
+ async login(params) {
28
+ try {
29
+ // create a md5 hash of the password using crypto-js
30
+ const hash_password = CryptoJS.MD5(params.password).toString(CryptoJS.enc.Hex);
31
+ const response = await this.axios.post('/user_service/loginUser', [{
32
+ email: params.email,
33
+ password: hash_password
34
+ }]);
35
+
36
+ if (response.data.ok === false) {
37
+ return this._handleError(response);
38
+ }
39
+
40
+ this.iac_session = response.data;
41
+ // update axios instance headers with authtoken
42
+ this.axios.defaults.headers.common['authtoken'] = response.data.authToken;
43
+ return response.data;
44
+ } catch (e) {
45
+
46
+ this._handleError(e);
47
+ }
48
+ }
49
+
50
+ async create(type, dataHash={}) {
51
+ try {
52
+ const params = {
53
+ type
54
+ };
55
+ const response = await this.axios.post('/object_service/createObject', [params.type]);
56
+ const record = response.data;
57
+
58
+ if(dataHash){
59
+ record.data = dataHash;
60
+ }
61
+ return new RbtObject(record, this.axios);
62
+ } catch (e) {
63
+ return this._handleError(e);
64
+ }
65
+ }
66
+
67
+ async query(type, params = {}) {
68
+ try {
69
+ params.type = type;
70
+ const response = await this.axios.post('/object_service/queryObjects', [params]);
71
+ if (response.data.ok === false) {
72
+ return this._handleError(response);
73
+ }
74
+
75
+ // Process items into RbtObject instances
76
+ if (Array.isArray(response.data.items)) {
77
+ response.data.items = response.data.items.map(record => new RbtObject(record, this.axios));
78
+ }
79
+ return response.data.items;
80
+ } catch (e) {
81
+ return this._handleError(e);
82
+ }
83
+ }
84
+
85
+ _handleError(err){
86
+
87
+ if(_.isObject(err)){
88
+ // response object?
89
+ let msg = _.get(err, 'response.data.message');
90
+ msg = (msg)? msg: err;
91
+ throw new Error(msg);
92
+ }
93
+ else{
94
+ // all other errors
95
+ throw new Error(err);
96
+ }
97
+
98
+ }
99
+ }
@@ -0,0 +1,75 @@
1
+ import _ from 'lodash';
2
+
3
+ export default class RbtObject {
4
+
5
+ constructor(record, axiosInstance) {
6
+ this._axios = axiosInstance;
7
+ this._internalData = record;
8
+ if(record.data){
9
+ this._data = record.data;
10
+ }
11
+ else{
12
+ this._data = record.dataJson ? JSON.parse(record.dataJson) : {};
13
+ }
14
+ }
15
+
16
+ get(path) {
17
+ return _.get(this._data, path);
18
+ }
19
+
20
+ set(path, value) {
21
+ _.set(this._data, path, value);
22
+ }
23
+
24
+ toRecord() {
25
+ return {
26
+ ...this._internalData,
27
+ dataJson: JSON.stringify(this._data)
28
+ };
29
+ }
30
+
31
+ async save() {
32
+ if (!this._internalData.type) {
33
+ throw new Error('Cannot save object without type');
34
+ }
35
+
36
+ try {
37
+ const record = this.toRecord();
38
+ const response = await this._axios.post('/object_service/saveObject', [record]);
39
+
40
+ if (response.data.ok === false) {
41
+ throw new Error(response.data.message);
42
+ }
43
+
44
+ this._internalData = response.data;
45
+ return this;
46
+
47
+ } catch (e) {
48
+ console.log('RbtObject.save.error:');
49
+ console.log(e.response.data);
50
+ }
51
+ }
52
+
53
+ async delete() {
54
+ if (!this._internalData.type) {
55
+ throw new Error('Cannot delete object without type');
56
+ }
57
+
58
+ try {
59
+ const record = this.toRecord();
60
+ const response = await this._axios.post('/object_service/deleteObject', [record]);
61
+
62
+ if (response.data.ok === false) {
63
+ throw new Error(response.data.message);
64
+ }
65
+
66
+ this._internalData = response.data;
67
+ return this;
68
+
69
+ } catch (e) {
70
+ console.log('RbtObject.delete.error:');
71
+ console.log(e.response.data);
72
+ }
73
+ }
74
+
75
+ }