roboto-js 1.0.21 → 1.1.2

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.
@@ -7,6 +7,7 @@ exports["default"] = void 0;
7
7
  var _axios = _interopRequireDefault(require("axios"));
8
8
  var _cryptoJs = _interopRequireDefault(require("crypto-js"));
9
9
  var _rbt_object = _interopRequireDefault(require("./rbt_object.cjs"));
10
+ var _rbt_user = _interopRequireDefault(require("./rbt_user.cjs"));
10
11
  var _rbt_file = _interopRequireDefault(require("./rbt_file.cjs"));
11
12
  var _lodash = _interopRequireDefault(require("lodash"));
12
13
  var _idb = require("idb");
@@ -73,6 +74,14 @@ var RbtApi = exports["default"] = /*#__PURE__*/function () {
73
74
  }
74
75
  return initLocalDb;
75
76
  }()
77
+ /**
78
+ * Logs in a user and stores the authToken.
79
+ *
80
+ * @param {Object} params - The login parameters.
81
+ * @param {string} params.email - The email of the user.
82
+ * @param {string} params.password - The password of the user.
83
+ * @returns {Promise<Object>} - The response data from the API.
84
+ */
76
85
  }, {
77
86
  key: "login",
78
87
  value: function () {
@@ -119,213 +128,581 @@ var RbtApi = exports["default"] = /*#__PURE__*/function () {
119
128
  }
120
129
  return login;
121
130
  }()
131
+ }, {
132
+ key: "logout",
133
+ value: function () {
134
+ var _logout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
135
+ var response;
136
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
137
+ while (1) switch (_context3.prev = _context3.next) {
138
+ case 0:
139
+ _context3.prev = 0;
140
+ _context3.next = 3;
141
+ return this.axios.post('/user_service/logoutUser');
142
+ case 3:
143
+ response = _context3.sent;
144
+ if (!(response.data.ok === false)) {
145
+ _context3.next = 6;
146
+ break;
147
+ }
148
+ return _context3.abrupt("return", this._handleError(response));
149
+ case 6:
150
+ // Clear the iac_session and remove the auth token from axios headers
151
+ this.iac_session = null;
152
+ if (this.axios.defaults.headers.common['authtoken']) {
153
+ delete this.axios.defaults.headers.common['authtoken'];
154
+ }
155
+
156
+ // Clear localStorage if it's being used
157
+ if (typeof localStorage !== 'undefined') {
158
+ localStorage.removeItem('authtoken');
159
+ }
160
+
161
+ // Return some kind of success response or the response from the server
162
+ return _context3.abrupt("return", response.data);
163
+ case 12:
164
+ _context3.prev = 12;
165
+ _context3.t0 = _context3["catch"](0);
166
+ this._handleError(_context3.t0);
167
+ case 15:
168
+ case "end":
169
+ return _context3.stop();
170
+ }
171
+ }, _callee3, this, [[0, 12]]);
172
+ }));
173
+ function logout() {
174
+ return _logout.apply(this, arguments);
175
+ }
176
+ return logout;
177
+ }()
178
+ }, {
179
+ key: "loadCurrentUser",
180
+ value: function () {
181
+ var _loadCurrentUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
182
+ var params, _response$data, response, userData, User;
183
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
184
+ while (1) switch (_context4.prev = _context4.next) {
185
+ case 0:
186
+ // TODO - get the actual user from the session
187
+ params = {
188
+ id: 'superuser_tom'
189
+ };
190
+ _context4.prev = 1;
191
+ _context4.next = 4;
192
+ return this.axios.post('/user_service/loadUser', [params]);
193
+ case 4:
194
+ response = _context4.sent;
195
+ userData = response === null || response === void 0 || (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.user;
196
+ User = new _rbt_user["default"]({
197
+ id: userData.id
198
+ }, this.axios);
199
+ User.setData(userData);
200
+ return _context4.abrupt("return", User);
201
+ case 11:
202
+ _context4.prev = 11;
203
+ _context4.t0 = _context4["catch"](1);
204
+ return _context4.abrupt("return", this._handleError(_context4.t0));
205
+ case 14:
206
+ case "end":
207
+ return _context4.stop();
208
+ }
209
+ }, _callee4, this, [[1, 11]]);
210
+ }));
211
+ function loadCurrentUser() {
212
+ return _loadCurrentUser.apply(this, arguments);
213
+ }
214
+ return loadCurrentUser;
215
+ }()
216
+ }, {
217
+ key: "refreshAuthToken",
218
+ value: function () {
219
+ var _refreshAuthToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(authtoken) {
220
+ var response;
221
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
222
+ while (1) switch (_context5.prev = _context5.next) {
223
+ case 0:
224
+ _context5.prev = 0;
225
+ _context5.next = 3;
226
+ return this.axios.post('/user_service/refreshAuthToken', [authtoken]);
227
+ case 3:
228
+ response = _context5.sent;
229
+ return _context5.abrupt("return", response.data);
230
+ case 7:
231
+ _context5.prev = 7;
232
+ _context5.t0 = _context5["catch"](0);
233
+ this._handleError(_context5.t0);
234
+ case 10:
235
+ case "end":
236
+ return _context5.stop();
237
+ }
238
+ }, _callee5, this, [[0, 7]]);
239
+ }));
240
+ function refreshAuthToken(_x2) {
241
+ return _refreshAuthToken.apply(this, arguments);
242
+ }
243
+ return refreshAuthToken;
244
+ }()
245
+ /**
246
+ * Registers a new user.
247
+ *
248
+ * @param {Object} dataHash - The data for the new user.
249
+ * @returns {Promise<RbtObject>} - The newly created user as an RbtObject.
250
+ *
251
+ */
122
252
  }, {
123
253
  key: "registerUser",
124
254
  value: function () {
125
- var _registerUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
255
+ var _registerUser = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
126
256
  var dataHash,
127
257
  response,
128
258
  record,
129
- _args3 = arguments;
130
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
131
- while (1) switch (_context3.prev = _context3.next) {
259
+ _args6 = arguments;
260
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
261
+ while (1) switch (_context6.prev = _context6.next) {
132
262
  case 0:
133
- dataHash = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : {};
134
- _context3.prev = 1;
135
- _context3.next = 4;
263
+ dataHash = _args6.length > 0 && _args6[0] !== undefined ? _args6[0] : {};
264
+ _context6.prev = 1;
265
+ _context6.next = 4;
136
266
  return this.axios.post('/user_service/registerUser', [dataHash]);
137
267
  case 4:
138
- response = _context3.sent;
268
+ response = _context6.sent;
139
269
  record = response.data;
140
270
  if (dataHash) {
141
271
  record.data = dataHash;
142
272
  }
143
- return _context3.abrupt("return", new _rbt_object["default"](record, this.axios));
273
+ return _context6.abrupt("return", new _rbt_object["default"](record, this.axios));
144
274
  case 10:
145
- _context3.prev = 10;
146
- _context3.t0 = _context3["catch"](1);
147
- return _context3.abrupt("return", this._handleError(_context3.t0));
148
- case 13:
275
+ _context6.prev = 10;
276
+ _context6.t0 = _context6["catch"](1);
277
+ debugger;
278
+ return _context6.abrupt("return", this._handleError(_context6.t0));
279
+ case 14:
149
280
  case "end":
150
- return _context3.stop();
281
+ return _context6.stop();
151
282
  }
152
- }, _callee3, this, [[1, 10]]);
283
+ }, _callee6, this, [[1, 10]]);
153
284
  }));
154
285
  function registerUser() {
155
286
  return _registerUser.apply(this, arguments);
156
287
  }
157
288
  return registerUser;
158
289
  }()
290
+ /**
291
+ * Creates a new file in the system.
292
+ *
293
+ * @param {Object} dataHash - The data for the new file.
294
+ * @returns {Promise<RbtFile>} - The newly created file as an RbtFile.
295
+ *
296
+ */
159
297
  }, {
160
298
  key: "createFile",
161
299
  value: function () {
162
- var _createFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(dataHash) {
300
+ var _createFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(dataHash) {
163
301
  var response, record;
164
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
165
- while (1) switch (_context4.prev = _context4.next) {
302
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
303
+ while (1) switch (_context7.prev = _context7.next) {
166
304
  case 0:
167
- _context4.prev = 0;
168
- _context4.next = 3;
305
+ _context7.prev = 0;
306
+ _context7.next = 3;
169
307
  return this.axios.post('/object_service/createObject', ['<@filekit.file>', dataHash]);
170
308
  case 3:
171
- response = _context4.sent;
309
+ response = _context7.sent;
172
310
  record = response.data;
173
311
  if (dataHash) {
174
312
  record.data = dataHash;
175
313
  }
176
- return _context4.abrupt("return", new _rbt_file["default"](record, this.axios, this.localDb));
314
+ return _context7.abrupt("return", new _rbt_file["default"](record, this.axios, this.localDb));
177
315
  case 9:
178
- _context4.prev = 9;
179
- _context4.t0 = _context4["catch"](0);
180
- return _context4.abrupt("return", this._handleError(_context4.t0));
316
+ _context7.prev = 9;
317
+ _context7.t0 = _context7["catch"](0);
318
+ return _context7.abrupt("return", this._handleError(_context7.t0));
181
319
  case 12:
182
320
  case "end":
183
- return _context4.stop();
321
+ return _context7.stop();
184
322
  }
185
- }, _callee4, this, [[0, 9]]);
323
+ }, _callee7, this, [[0, 9]]);
186
324
  }));
187
- function createFile(_x2) {
325
+ function createFile(_x3) {
188
326
  return _createFile.apply(this, arguments);
189
327
  }
190
328
  return createFile;
191
329
  }()
330
+ /**
331
+ * Creates a new object of the given type.
332
+ *
333
+ * @param {string} type - The type of object to create.
334
+ * @param {Object} dataHash - The data for the new object.
335
+ * @returns {Promise<RbtObject>} - The newly created object as an RbtObject.
336
+ */
192
337
  }, {
193
338
  key: "create",
194
339
  value: function () {
195
- var _create = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(type) {
340
+ var _create = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(type) {
196
341
  var dataHash,
197
342
  response,
198
343
  record,
199
- _args5 = arguments;
200
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
201
- while (1) switch (_context5.prev = _context5.next) {
344
+ _args8 = arguments;
345
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
346
+ while (1) switch (_context8.prev = _context8.next) {
202
347
  case 0:
203
- dataHash = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : {};
204
- _context5.prev = 1;
205
- _context5.next = 4;
348
+ dataHash = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
349
+ _context8.prev = 1;
350
+ _context8.next = 4;
206
351
  return this.axios.post('/object_service/createObject', [type, dataHash]);
207
352
  case 4:
208
- response = _context5.sent;
353
+ response = _context8.sent;
209
354
  record = response.data;
210
355
  if (dataHash) {
211
356
  record.data = dataHash;
212
357
  }
213
- return _context5.abrupt("return", new _rbt_object["default"](record, this.axios));
358
+ return _context8.abrupt("return", new _rbt_object["default"](record, this.axios, {
359
+ isNew: true
360
+ }));
214
361
  case 10:
215
- _context5.prev = 10;
216
- _context5.t0 = _context5["catch"](1);
217
- return _context5.abrupt("return", this._handleError(_context5.t0));
362
+ _context8.prev = 10;
363
+ _context8.t0 = _context8["catch"](1);
364
+ return _context8.abrupt("return", this._handleError(_context8.t0));
218
365
  case 13:
219
366
  case "end":
220
- return _context5.stop();
367
+ return _context8.stop();
221
368
  }
222
- }, _callee5, this, [[1, 10]]);
369
+ }, _callee8, this, [[1, 10]]);
223
370
  }));
224
- function create(_x3) {
371
+ function create(_x4) {
225
372
  return _create.apply(this, arguments);
226
373
  }
227
374
  return create;
228
375
  }()
376
+ /**
377
+ * Queries objects of a given type based on specified parameters.
378
+ *
379
+ * @param {string} type - The type of object to query, specified as a doctree.typedef.
380
+ * @param {Object} params - The query parameters, including optional filters and configurations.
381
+ * @returns {Promise<Array<RbtObject>>} - An array of queried objects as RbtObjects.
382
+ *
383
+ * The `params` object can include the following properties:
384
+ * - where: A SQL-like where clause string for filtering the results.
385
+ * - orderBy: An object specifying the ordering of the results. It should include:
386
+ * - column: The attribute name to sort by. This must be either a column in the @doctree.model schema or an indexed type attribute.
387
+ * - direction: The sort direction, either 'ASC' for ascending or 'DESC' for descending.
388
+ * - limit: An object to control the pagination of results. It includes:
389
+ * - offset: The starting point from where to fetch the results.
390
+ * - results: The maximum number of results to return.
391
+ * - resolveReferences: An array of attribute names whose references should be resolved in the returned objects.
392
+ * - timeout: A numerical value in milliseconds to set a maximum time limit for the query execution.
393
+ *
394
+ * Example usage:
395
+ * query("<@testuser>", {
396
+ * where: 'email="tom@pospa.com"',
397
+ * orderBy: { column: 'timeCreated', direction: 'DESC' },
398
+ * limit: { offset: 0, results: 50 },
399
+ * resolveReferences: ['translatableContent']
400
+ * });
401
+ *
402
+ * Note: A default orderBy is applied if none is provided, ordering items by 'timeCreated' in descending order.
403
+ */
229
404
  }, {
230
405
  key: "query",
231
406
  value: function () {
232
- var _query = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(type) {
407
+ var _query = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(type) {
233
408
  var _this = this;
234
409
  var params,
235
410
  defaultOrderBy,
411
+ defaultLimit,
236
412
  mergedParams,
237
413
  response,
238
- _args6 = arguments;
239
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
240
- while (1) switch (_context6.prev = _context6.next) {
414
+ _args9 = arguments;
415
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
416
+ while (1) switch (_context9.prev = _context9.next) {
241
417
  case 0:
242
- params = _args6.length > 1 && _args6[1] !== undefined ? _args6[1] : {};
243
- _context6.prev = 1;
418
+ params = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : {};
419
+ _context9.prev = 1;
244
420
  params.type = type;
245
421
 
246
- // default
422
+ // Default ordering and pagination
247
423
  defaultOrderBy = {
248
424
  orderBy: {
249
425
  column: 'timeCreated',
250
426
  direction: 'DESC'
251
427
  }
252
428
  };
253
- mergedParams = _objectSpread(_objectSpread({}, defaultOrderBy), params);
254
- _context6.next = 7;
429
+ defaultLimit = {
430
+ limit: {
431
+ offset: 0,
432
+ results: 50
433
+ }
434
+ }; // Merge defaults with provided params
435
+ mergedParams = _objectSpread(_objectSpread(_objectSpread({}, defaultOrderBy), defaultLimit), params);
436
+ _context9.next = 8;
255
437
  return this.axios.post('/object_service/queryObjects', [mergedParams]);
256
- case 7:
257
- response = _context6.sent;
438
+ case 8:
439
+ response = _context9.sent;
258
440
  if (!(response.data.ok === false)) {
259
- _context6.next = 10;
441
+ _context9.next = 11;
260
442
  break;
261
443
  }
262
- return _context6.abrupt("return", this._handleError(response));
263
- case 10:
444
+ return _context9.abrupt("return", this._handleError(response));
445
+ case 11:
264
446
  // Process items into RbtObject instances
265
447
  if (Array.isArray(response.data.items)) {
266
448
  response.data.items = response.data.items.map(function (record) {
267
- return new _rbt_object["default"](record, _this.axios);
449
+ return new _rbt_object["default"](record, _this.axios, {
450
+ isNew: true
451
+ });
268
452
  });
269
453
  }
270
- return _context6.abrupt("return", response.data.items);
271
- case 14:
272
- _context6.prev = 14;
273
- _context6.t0 = _context6["catch"](1);
274
- return _context6.abrupt("return", this._handleError(_context6.t0));
275
- case 17:
454
+ return _context9.abrupt("return", response.data.items);
455
+ case 15:
456
+ _context9.prev = 15;
457
+ _context9.t0 = _context9["catch"](1);
458
+ return _context9.abrupt("return", this._handleError(_context9.t0));
459
+ case 18:
276
460
  case "end":
277
- return _context6.stop();
461
+ return _context9.stop();
278
462
  }
279
- }, _callee6, this, [[1, 14]]);
463
+ }, _callee9, this, [[1, 15]]);
280
464
  }));
281
- function query(_x4) {
465
+ function query(_x5) {
282
466
  return _query.apply(this, arguments);
283
467
  }
284
468
  return query;
285
469
  }()
470
+ /**
471
+ * Loads one or multiple objects of a given type by their IDs.
472
+ *
473
+ * @param {string} type - The type of object to load.
474
+ * @param {Array<string>|string} ids - The ID(s) of the object(s) to load.
475
+ *
476
+ * @returns {Promise<RbtObject|RbtObject[]>} - The loaded object(s) as RbtObject(s).
477
+ */
286
478
  }, {
287
479
  key: "load",
288
480
  value: function () {
289
- var _load = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(type, ids) {
290
- var res;
291
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
292
- while (1) switch (_context7.prev = _context7.next) {
481
+ var _load = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(type, ids) {
482
+ var params,
483
+ mergedParams,
484
+ res,
485
+ _args10 = arguments;
486
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
487
+ while (1) switch (_context10.prev = _context10.next) {
293
488
  case 0:
294
- _context7.prev = 0;
295
- debugger;
489
+ params = _args10.length > 2 && _args10[2] !== undefined ? _args10[2] : {};
490
+ _context10.prev = 1;
296
491
  if (!Array.isArray(ids)) {
297
- _context7.next = 6;
492
+ _context10.next = 7;
298
493
  break;
299
494
  }
300
- return _context7.abrupt("return", this.query(type, {
495
+ mergedParams = _objectSpread(_objectSpread({}, params), {}, {
301
496
  where: "id IN (" + ids.join(',') + ")"
302
- }));
303
- case 6:
304
- _context7.next = 8;
305
- return this.query(type, {
497
+ });
498
+ return _context10.abrupt("return", this.query(type, mergedParams));
499
+ case 7:
500
+ mergedParams = _objectSpread(_objectSpread({}, params), {}, {
306
501
  where: "id=" + ids
307
502
  });
308
- case 8:
309
- res = _context7.sent;
310
- return _context7.abrupt("return", res[0]);
503
+ _context10.next = 10;
504
+ return this.query(type, mergedParams);
311
505
  case 10:
312
- _context7.next = 15;
313
- break;
506
+ res = _context10.sent;
507
+ return _context10.abrupt("return", res[0]);
314
508
  case 12:
315
- _context7.prev = 12;
316
- _context7.t0 = _context7["catch"](0);
317
- return _context7.abrupt("return", this._handleError(_context7.t0));
318
- case 15:
509
+ _context10.next = 17;
510
+ break;
511
+ case 14:
512
+ _context10.prev = 14;
513
+ _context10.t0 = _context10["catch"](1);
514
+ return _context10.abrupt("return", this._handleError(_context10.t0));
515
+ case 17:
319
516
  case "end":
320
- return _context7.stop();
517
+ return _context10.stop();
321
518
  }
322
- }, _callee7, this, [[0, 12]]);
519
+ }, _callee10, this, [[1, 14]]);
323
520
  }));
324
- function load(_x5, _x6) {
521
+ function load(_x6, _x7) {
325
522
  return _load.apply(this, arguments);
326
523
  }
327
524
  return load;
328
525
  }()
526
+ /**
527
+ * Makes a POST request to a specific endpoint to run a task and handle progress updates.
528
+ *
529
+ * @param {Object} params - The parameters to be sent in the POST request.
530
+ * @param {Object} callbacks - An object containing callback functions for progress and error handling.
531
+ *
532
+ * The function expects a response in the following format:
533
+ * {
534
+ * ok: boolean, // Indicates if the request was successful or not
535
+ * jobId: string, // The job identifier
536
+ * status: string // Can be 'RUNNING', 'DONE', or 'ERROR'
537
+ * }
538
+ */
539
+ }, {
540
+ key: "runTask",
541
+ value: function () {
542
+ var _runTask = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11() {
543
+ var params,
544
+ callbacks,
545
+ onProgress,
546
+ onError,
547
+ onFinish,
548
+ response,
549
+ ok,
550
+ jobId,
551
+ status,
552
+ message,
553
+ output,
554
+ _args11 = arguments;
555
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
556
+ while (1) switch (_context11.prev = _context11.next) {
557
+ case 0:
558
+ params = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : {};
559
+ callbacks = _args11.length > 1 && _args11[1] !== undefined ? _args11[1] : {};
560
+ onProgress = callbacks.onProgress, onError = callbacks.onError, onFinish = callbacks.onFinish;
561
+ _context11.prev = 3;
562
+ _context11.next = 6;
563
+ return this.post('http://localhost:3004/runChain', params);
564
+ case 6:
565
+ response = _context11.sent;
566
+ if (response) {
567
+ _context11.next = 9;
568
+ break;
569
+ }
570
+ throw new Error('Invalid server response');
571
+ case 9:
572
+ // Validate response structure
573
+ ok = response.ok, jobId = response.jobId, status = response.status, message = response.message, output = response.output;
574
+ if (!(!ok || typeof jobId !== 'string' || typeof status !== 'string')) {
575
+ _context11.next = 12;
576
+ break;
577
+ }
578
+ throw new Error('Invalid response structure');
579
+ case 12:
580
+ // If the task is still in progress, start polling for updates
581
+ if (status === 'RUNNING' || status === 'SCHEDULED' || status === 'QUEUED') {
582
+ this.pollTaskProgress(jobId, callbacks);
583
+ }
584
+ if (status === 'ERROR' && onError) {
585
+ // Provide the current progress to the callback function
586
+ onError(response);
587
+ }
588
+ if (status === 'DONE' && onFinish) {
589
+ // Provide the current progress to the callback function
590
+ console.log('Finish (request) ', response);
591
+ onFinish(response);
592
+ }
593
+ return _context11.abrupt("return", {
594
+ ok: ok,
595
+ jobId: jobId,
596
+ status: status,
597
+ message: message,
598
+ output: output
599
+ });
600
+ case 18:
601
+ _context11.prev = 18;
602
+ _context11.t0 = _context11["catch"](3);
603
+ if (typeof onError === 'function') {
604
+ onError(_context11.t0);
605
+ } else {
606
+ console.error('Error in runTask:', _context11.t0);
607
+ }
608
+ return _context11.abrupt("return", {
609
+ ok: false,
610
+ jobId: null,
611
+ status: 'ERROR',
612
+ error: _context11.t0.message
613
+ });
614
+ case 22:
615
+ case "end":
616
+ return _context11.stop();
617
+ }
618
+ }, _callee11, this, [[3, 18]]);
619
+ }));
620
+ function runTask() {
621
+ return _runTask.apply(this, arguments);
622
+ }
623
+ return runTask;
624
+ }()
625
+ /**
626
+ * Polls the progress of a long-running task.
627
+ *
628
+ * @param {string} jobId - The ID of the job to poll for progress.
629
+ * @param {function} onProgress - Callback function that receives progress updates.
630
+ *
631
+ * The function periodically sends GET requests to check the task's progress
632
+ * and reports back via the provided callback function. The polling stops when
633
+ * the task is completed or an error occurs.
634
+ */
635
+ }, {
636
+ key: "pollTaskProgress",
637
+ value: function () {
638
+ var _pollTaskProgress = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee13(jobId, callbacks) {
639
+ var _this2 = this;
640
+ var onProgress, onError, onFinish, checkProgress;
641
+ return _regeneratorRuntime().wrap(function _callee13$(_context13) {
642
+ while (1) switch (_context13.prev = _context13.next) {
643
+ case 0:
644
+ onProgress = callbacks.onProgress, onError = callbacks.onError, onFinish = callbacks.onFinish;
645
+ _context13.prev = 1;
646
+ checkProgress = /*#__PURE__*/function () {
647
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12() {
648
+ var response;
649
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
650
+ while (1) switch (_context12.prev = _context12.next) {
651
+ case 0:
652
+ _context12.next = 2;
653
+ return _this2.get("http://localhost:3004/pollChainProgress", {
654
+ jobId: jobId
655
+ });
656
+ case 2:
657
+ response = _context12.sent;
658
+ // If the task is still in progress, start polling for updates
659
+ if (response.status === 'DONE' && onFinish) {
660
+ // Provide the current progress to the callback function
661
+ console.log('Finish (progress) ', response);
662
+ onFinish(response);
663
+ }
664
+ if (response.status === 'ERROR' && onError) {
665
+ // Provide the current progress to the callback function
666
+ onError(response);
667
+ }
668
+
669
+ // Provide the current progress to the callback function
670
+ if (response.status == 'RUNNING') {
671
+ onProgress(response);
672
+ }
673
+
674
+ // Continue polling if the status is 'RUNNING'
675
+ if (['RUNNING'].includes(response.status)) {
676
+ setTimeout(checkProgress, 1000); // Poll every 2 seconds
677
+ }
678
+ case 7:
679
+ case "end":
680
+ return _context12.stop();
681
+ }
682
+ }, _callee12);
683
+ }));
684
+ return function checkProgress() {
685
+ return _ref2.apply(this, arguments);
686
+ };
687
+ }();
688
+ checkProgress();
689
+ _context13.next = 9;
690
+ break;
691
+ case 6:
692
+ _context13.prev = 6;
693
+ _context13.t0 = _context13["catch"](1);
694
+ return _context13.abrupt("return", this._handleError(_context13.t0));
695
+ case 9:
696
+ case "end":
697
+ return _context13.stop();
698
+ }
699
+ }, _callee13, this, [[1, 6]]);
700
+ }));
701
+ function pollTaskProgress(_x8, _x9) {
702
+ return _pollTaskProgress.apply(this, arguments);
703
+ }
704
+ return pollTaskProgress;
705
+ }()
329
706
  /**
330
707
  * Performs a GET request to the specified endpoint.
331
708
  *
@@ -340,45 +717,45 @@ var RbtApi = exports["default"] = /*#__PURE__*/function () {
340
717
  }, {
341
718
  key: "get",
342
719
  value: function () {
343
- var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(endpoint) {
720
+ var _get = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee14(endpoint) {
344
721
  var params,
345
722
  headers,
346
723
  response,
347
- _args8 = arguments;
348
- return _regeneratorRuntime().wrap(function _callee8$(_context8) {
349
- while (1) switch (_context8.prev = _context8.next) {
724
+ _args14 = arguments;
725
+ return _regeneratorRuntime().wrap(function _callee14$(_context14) {
726
+ while (1) switch (_context14.prev = _context14.next) {
350
727
  case 0:
351
- params = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
352
- _context8.prev = 1;
728
+ params = _args14.length > 1 && _args14[1] !== undefined ? _args14[1] : {};
729
+ _context14.prev = 1;
353
730
  // Add the authToken to the headers
354
731
  headers = {
355
732
  authtoken: this.authtoken
356
733
  }; // Make the GET request using Axios
357
- _context8.next = 5;
734
+ _context14.next = 5;
358
735
  return this.axios.get(endpoint, {
359
736
  params: params,
360
737
  headers: headers
361
738
  });
362
739
  case 5:
363
- response = _context8.sent;
740
+ response = _context14.sent;
364
741
  if (!(response.data.ok === false)) {
365
- _context8.next = 8;
742
+ _context14.next = 8;
366
743
  break;
367
744
  }
368
- return _context8.abrupt("return", this._handleError(response));
745
+ return _context14.abrupt("return", this._handleError(response));
369
746
  case 8:
370
- return _context8.abrupt("return", response.data);
747
+ return _context14.abrupt("return", response.data);
371
748
  case 11:
372
- _context8.prev = 11;
373
- _context8.t0 = _context8["catch"](1);
374
- return _context8.abrupt("return", this._handleError(_context8.t0));
749
+ _context14.prev = 11;
750
+ _context14.t0 = _context14["catch"](1);
751
+ return _context14.abrupt("return", this._handleError(_context14.t0));
375
752
  case 14:
376
753
  case "end":
377
- return _context8.stop();
754
+ return _context14.stop();
378
755
  }
379
- }, _callee8, this, [[1, 11]]);
756
+ }, _callee14, this, [[1, 11]]);
380
757
  }));
381
- function get(_x7) {
758
+ function get(_x10) {
382
759
  return _get.apply(this, arguments);
383
760
  }
384
761
  return get;
@@ -398,59 +775,71 @@ var RbtApi = exports["default"] = /*#__PURE__*/function () {
398
775
  }, {
399
776
  key: "post",
400
777
  value: function () {
401
- var _post = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9(endpoint) {
778
+ var _post = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee15(endpoint) {
402
779
  var data,
403
780
  headers,
404
781
  response,
405
- _args9 = arguments;
406
- return _regeneratorRuntime().wrap(function _callee9$(_context9) {
407
- while (1) switch (_context9.prev = _context9.next) {
782
+ _args15 = arguments;
783
+ return _regeneratorRuntime().wrap(function _callee15$(_context15) {
784
+ while (1) switch (_context15.prev = _context15.next) {
408
785
  case 0:
409
- data = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : {};
410
- _context9.prev = 1;
786
+ data = _args15.length > 1 && _args15[1] !== undefined ? _args15[1] : {};
787
+ _context15.prev = 1;
411
788
  // Add the authToken to the headers
412
789
  headers = {
413
790
  authtoken: this.authtoken
414
791
  }; // Make the POST request using Axios
415
- _context9.next = 5;
792
+ _context15.next = 5;
416
793
  return this.axios.post(endpoint, data, {
417
794
  headers: headers
418
795
  });
419
796
  case 5:
420
- response = _context9.sent;
797
+ response = _context15.sent;
421
798
  if (!(response.data.ok === false)) {
422
- _context9.next = 8;
799
+ _context15.next = 8;
423
800
  break;
424
801
  }
425
- return _context9.abrupt("return", this._handleError(response));
802
+ return _context15.abrupt("return", this._handleError(response));
426
803
  case 8:
427
- return _context9.abrupt("return", response.data);
804
+ return _context15.abrupt("return", response.data);
428
805
  case 11:
429
- _context9.prev = 11;
430
- _context9.t0 = _context9["catch"](1);
431
- return _context9.abrupt("return", this._handleError(_context9.t0));
806
+ _context15.prev = 11;
807
+ _context15.t0 = _context15["catch"](1);
808
+ return _context15.abrupt("return", this._handleError(_context15.t0));
432
809
  case 14:
433
810
  case "end":
434
- return _context9.stop();
811
+ return _context15.stop();
435
812
  }
436
- }, _callee9, this, [[1, 11]]);
813
+ }, _callee15, this, [[1, 11]]);
437
814
  }));
438
- function post(_x8) {
815
+ function post(_x11) {
439
816
  return _post.apply(this, arguments);
440
817
  }
441
818
  return post;
442
819
  }()
820
+ }, {
821
+ key: "setErrorHandler",
822
+ value: function setErrorHandler(customErrorHandler) {
823
+ this.customErrorHandler = customErrorHandler;
824
+ }
443
825
  }, {
444
826
  key: "_handleError",
445
827
  value: function _handleError(err) {
446
- if (_lodash["default"].isObject(err)) {
447
- // response object?
448
- var msg = _lodash["default"].get(err, 'response.data.message');
449
- msg = msg ? msg : err;
450
- throw new Error(msg);
828
+ debugger;
829
+ // Invoke the custom error handler if provided
830
+ if (this.customErrorHandler) {
831
+ var res = this.customErrorHandler(err);
832
+ if (res) return;
833
+ }
834
+ if (_lodash["default"].isObject(err) && _lodash["default"].get(err, 'response')) {
835
+ var msg = _lodash["default"].get(err, 'response.data.message', 'Error in API response');
836
+ if (msg.key) {
837
+ throw new Error(msg.key);
838
+ } else {
839
+ throw new Error(msg);
840
+ }
451
841
  } else {
452
- // all other errors
453
- throw new Error(err);
842
+ throw new Error(err.message || 'Unknown error');
454
843
  }
455
844
  }
456
845
  }]);