roboto-js 1.0.20 → 1.1.1

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