roboto-js 1.8.4 → 1.8.6

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.
@@ -0,0 +1,235 @@
1
+ function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
2
+ function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
3
+ function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
4
+ function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
5
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
6
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
8
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
9
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
10
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
11
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
13
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
14
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
15
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
16
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
17
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
18
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
19
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
20
+ import _ from 'lodash';
21
+ var RbtUser = /*#__PURE__*/function () {
22
+ function RbtUser(record, axiosInstance) {
23
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
24
+ _classCallCheck(this, RbtUser);
25
+ this._axios = axiosInstance;
26
+ this._internalData = record;
27
+ this.id = record.id;
28
+ this.id_revision = record.id_revision;
29
+ this.rpcMeta = {
30
+ changes: [],
31
+ isNew: options.isNew || false
32
+ };
33
+ if (record.data) {
34
+ this._data = this._deepUnpackJson(record.data);
35
+ } else {
36
+ this._data = record.dataJson ? this._deepUnpackJson(record.dataJson) : {};
37
+ }
38
+ }
39
+ return _createClass(RbtUser, [{
40
+ key: "get",
41
+ value: function get(path) {
42
+ return _.get(this._data, path);
43
+ }
44
+ }, {
45
+ key: "getData",
46
+ value: function getData() {
47
+ return _objectSpread({}, this._data);
48
+ }
49
+ }, {
50
+ key: "_addChange",
51
+ value: function _addChange(path) {
52
+ // Ensure no duplicate paths
53
+ if (!this.rpcMeta.changes.includes(path)) {
54
+ this.rpcMeta.changes.push(path);
55
+ }
56
+ }
57
+ }, {
58
+ key: "set",
59
+ value: function set(path, value) {
60
+ var currentValue = _.get(this._data, path);
61
+ if (!_.isEqual(currentValue, value)) {
62
+ _.set(this._data, path, value);
63
+ this._addChange(path);
64
+ }
65
+ }
66
+ }, {
67
+ key: "setData",
68
+ value: function setData(newData) {
69
+ var _this = this;
70
+ if (_typeof(newData) !== 'object' || newData === null) {
71
+ throw new Error('setData expects an object');
72
+ }
73
+ Object.keys(newData).forEach(function (key) {
74
+ if (!_.isEqual(_.get(_this._data, key), newData[key])) {
75
+ _.set(_this._data, key, newData[key]);
76
+ _this._addChange(key);
77
+ }
78
+ });
79
+ }
80
+
81
+ //
82
+ // For Arrays
83
+ //
84
+ }, {
85
+ key: "setAppend",
86
+ value: function setAppend(key, value) {
87
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
88
+ var existingValue = this.get(key) || []; // Get the existing value or default to an empty array
89
+ if (!Array.isArray(existingValue)) {
90
+ existingValue = []; // Ensure existingValue is an array if not already
91
+ }
92
+ var valuesToAdd = Array.isArray(value) ? value : [value]; // Convert value to an array if it's not one
93
+
94
+ // Combine existingValue and valuesToAdd, filtering out duplicates
95
+ var mergedValues = Array.from(new Set([].concat(_toConsumableArray(existingValue), _toConsumableArray(valuesToAdd))));
96
+ this.set(key, mergedValues, options); // Set the updated array back to the user data
97
+ }
98
+ }, {
99
+ key: "setRemove",
100
+ value: function setRemove(key, value) {
101
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
102
+ var existingValue = this.get(key) || []; // Get the existing value or default to an empty array
103
+ if (!Array.isArray(existingValue)) {
104
+ return; // If it's not an array, there's nothing to remove, so exit early
105
+ }
106
+ var valuesToRemove = Array.isArray(value) ? value : [value]; // Convert value to an array if it's not one
107
+
108
+ // Filter out the values to remove from the existing value array
109
+ var filteredValues = existingValue.filter(function (item) {
110
+ return !valuesToRemove.includes(item);
111
+ });
112
+ this.set(key, filteredValues); // Set the updated array back to the user data
113
+ }
114
+
115
+ //
116
+ //
117
+ //
118
+ }, {
119
+ key: "toRecord",
120
+ value: function toRecord() {
121
+ return _objectSpread(_objectSpread({}, this._internalData), {}, {
122
+ dataJson: JSON.stringify(this._data),
123
+ rpcMeta: this.rpcMeta
124
+ });
125
+ }
126
+ }, {
127
+ key: "clone",
128
+ value: function clone() {
129
+ // Create a deep copy of the current object's data
130
+ var clonedData = _.cloneDeep(this._internalData);
131
+
132
+ // Reset unique identifiers to ensure a new ID is generated upon saving
133
+ delete clonedData.id;
134
+ delete clonedData.id_revision;
135
+
136
+ // Create a new instance of RbtUser with the cloned data
137
+ var clonedObject = new RbtUser(clonedData, this._axios, {
138
+ isNew: true
139
+ });
140
+ return clonedObject;
141
+ }
142
+ }, {
143
+ key: "save",
144
+ value: function () {
145
+ var _save = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
146
+ var record, response, _t;
147
+ return _regenerator().w(function (_context) {
148
+ while (1) switch (_context.p = _context.n) {
149
+ case 0:
150
+ _context.p = 0;
151
+ debugger;
152
+ record = this.toRecord();
153
+ record.type = '<@iac.user>';
154
+ _context.n = 1;
155
+ return this._axios.post('/user_service/saveUser', [record]);
156
+ case 1:
157
+ response = _context.v;
158
+ if (!(response.data.ok === false)) {
159
+ _context.n = 2;
160
+ break;
161
+ }
162
+ throw new Error(response.data.message);
163
+ case 2:
164
+ this._internalData = response.data;
165
+ this.id = response.data.id;
166
+ this.id_revision = response.data.id_revision;
167
+ this.type = response.data.type;
168
+ this.rpcMeta.isNew = false;
169
+ return _context.a(2, this);
170
+ case 3:
171
+ _context.p = 3;
172
+ _t = _context.v;
173
+ console.log('RbtUser.save.error:', _t);
174
+ //console.log(e.response.data);
175
+ throw _t;
176
+ case 4:
177
+ return _context.a(2);
178
+ }
179
+ }, _callee, this, [[0, 3]]);
180
+ }));
181
+ function save() {
182
+ return _save.apply(this, arguments);
183
+ }
184
+ return save;
185
+ }()
186
+ }, {
187
+ key: "_deepUnpackJson",
188
+ value: function _deepUnpackJson(value) {
189
+ if (typeof value === 'string') {
190
+ try {
191
+ // Only parse as JSON if it's not a large number
192
+ // https://chatgpt.com/c/6745902c-edf4-800c-ab52-31bf27dde2bd
193
+ //
194
+ if (!/^\d{16,}$/.test(value)) {
195
+ var parsed = JSON.parse(value);
196
+ // Recursively parse if the result is a string, object, or array
197
+ return this._deepUnpackJson(parsed);
198
+ }
199
+ } catch (e) {
200
+ return value; // Return the original string if parsing fails
201
+ }
202
+ } else if (value !== null && _typeof(value) === 'object') {
203
+ // If it's an object (including arrays), recursively parse each value
204
+ for (var key in value) {
205
+ value[key] = this._deepUnpackJson(value[key]);
206
+ }
207
+ }
208
+ return value;
209
+ }
210
+
211
+ //async delete() {
212
+ // if (!this._internalData.type) {
213
+ // throw new Error('Cannot delete object without type');
214
+ // }
215
+ //
216
+ // try {
217
+ // const record = this.toRecord();
218
+ // const response = await this._axios.post('/object_service/deleteObject', [record]);
219
+ //
220
+ // if (response.data.ok === false) {
221
+ // throw new Error(response.data.message);
222
+ // }
223
+ //
224
+ // this._internalData = response.data;
225
+ // return this;
226
+ //
227
+ // } catch (e) {
228
+ // console.log('RbtUser.delete.error:');
229
+ // console.log(e.response.data);
230
+ // throw e;
231
+ // }
232
+ //}
233
+ }]);
234
+ }();
235
+ export { RbtUser as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roboto-js",
3
- "version": "1.8.4",
3
+ "version": "1.8.6",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "dist/cjs/index.cjs",
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "clean": "rm -rf node_modules; rm package-lock.json; npm install",
13
13
  "build": "npm run build:cjs && npm run build:esm",
14
- "build:cjs": "babel src --out-dir dist/cjs --presets=@babel/preset-env --extensions \".js,.jsx\" --out-file-extension .cjs && npm run postbuild:cjs",
14
+ "build:cjs": "babel src --out-dir dist/cjs --extensions \".js,.jsx\" --out-file-extension .cjs && npm run postbuild:cjs",
15
15
  "build:esm": "babel src --out-dir dist/esm",
16
16
  "xbuild:esm": "babel src --out-dir dist/esm --presets=@babel/preset-env --no-babelrc --config-file ./babel.esm.config.json",
17
17
  "postbuild:cjs": "node postbuild-cjs.js",
@@ -32,6 +32,7 @@
32
32
  "devDependencies": {
33
33
  "@babel/cli": "^7.23.9",
34
34
  "@babel/core": "^7.23.9",
35
- "@babel/preset-env": "^7.23.9"
35
+ "@babel/preset-env": "^7.23.9",
36
+ "babel-plugin-transform-remove-console": "^6.9.4"
36
37
  }
37
38
  }
package/src/index.js CHANGED
@@ -20,8 +20,10 @@ export default class Roboto{
20
20
  return '1.7.3';
21
21
  }
22
22
 
23
- constructor({ host, accessKey, localStorageAdaptor, disableWebSocket = false, metricsHost, useCookies = true }, proxyReq = null) {
23
+ constructor({ host, accessKey, apiKey, authToken, localStorageAdaptor, disableWebSocket = false, metricsHost, useCookies = true }, proxyReq = null) {
24
24
 
25
+ console.log('[Roboto] constructor', { host, accessKey, apiKey, authToken, localStorageAdaptor, disableWebSocket, metricsHost, useCookies, proxyReq });
26
+
25
27
  if (Roboto.instance && !proxyReq) {
26
28
  // if on client, there can only be one instance
27
29
  // on server, with proxyReq, each request has its own instance
@@ -51,28 +53,36 @@ export default class Roboto{
51
53
  }
52
54
 
53
55
  this.config = {
54
- accessKey: accessKey, // Use passed accessKey
56
+ accesskey: accessKey, // Use passed accessKey
55
57
  baseUrl: `https://${host}`, // Use passed host
56
58
  localStorageAdaptor: storageAdaptor
57
59
  };
60
+
61
+ // Add apikey and authtoken if provided directly
62
+ if (apiKey) {
63
+ this.config.apikey = apiKey;
64
+ }
65
+ if (authToken) {
66
+ this.config.authtoken = authToken;
67
+ }
58
68
 
59
69
  // DEVELOPMENT
60
70
  this.config.baseUrl = this._stripHttpsForDomains(this.config.baseUrl, ['localhost']);
61
71
 
62
72
  // Check if a request object is provided
63
73
  if (proxyReq && proxyReq.headers) {
64
- const authtoken = proxyReq.headers.authtoken;
65
- const accesskey = proxyReq.headers.accesskey;
66
- const apikey = proxyReq.headers.apikey;
74
+ const header_authtoken = proxyReq.headers.authtoken;
75
+ const header_accesskey = proxyReq.headers.accesskey;
76
+ const header_apikey = proxyReq.headers.apikey;
67
77
  // Optionally add more headers as needed
68
- if (authtoken) {
69
- this.config.authtoken = authtoken; // Set the authtoken in the config
78
+ if (header_authtoken) {
79
+ this.config.authtoken = header_authtoken; // Set the authtoken in the config
70
80
  }
71
- if (apikey) {
72
- this.config.apikey = apikey; // Set the authtoken in the config
81
+ if (header_apikey) {
82
+ this.config.apikey = header_apikey; // Set the authtoken in the config
73
83
  }
74
- if (accesskey) {
75
- this.config.accesskey = accesskey; // Set the accesskey in the config
84
+ if (header_accesskey) {
85
+ this.config.accesskey = header_accesskey; // Set the accesskey in the config
76
86
  }
77
87
 
78
88
  }
package/src/rbt_api.js CHANGED
@@ -10,6 +10,8 @@ export default class RbtApi {
10
10
 
11
11
  constructor({ baseUrl, accesskey, authtoken=null, apikey=null, localStorageAdaptor=null }) {
12
12
 
13
+ console.log('[RbtApi] constructor received:', { baseUrl, accesskey, authtoken, apikey });
14
+
13
15
  this.websocketClient = null;
14
16
 
15
17
  // Object cache for sharing instances across multiple load() calls
@@ -55,6 +57,13 @@ export default class RbtApi {
55
57
 
56
58
  // Use the storageAdaptor to get the authToken, if available
57
59
  this.initAuthToken(authtoken);
60
+
61
+ // Set apiKey synchronously if provided (don't wait for async initApiKey)
62
+ if (apikey) {
63
+ this.apikey = apikey;
64
+ this.axios.defaults.headers.common['apikey'] = this.apikey;
65
+ console.log('[RbtApi] Set apikey header:', this.axios.defaults.headers.common['apikey']); // ADD THIS LINE
66
+ }
58
67
  this.initApiKey(apikey);
59
68
 
60
69