jxp-helper 2.0.0 → 3.2.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.
@@ -1,17 +1,24 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.JXPHelper = void 0;
7
- const axios_1 = __importDefault(require("axios"));
4
+ const types_1 = require("./types");
5
+ const http_1 = require("./http");
6
+ const errors_1 = require("./errors");
8
7
  /**
9
8
  * JXPHelper class for interacting with a JXP server.
10
9
  */
11
10
  class JXPHelper {
11
+ server;
12
+ apikey;
13
+ token;
14
+ api;
15
+ debug;
16
+ hideErrors;
12
17
  /**
13
18
  * Creates a new instance of the JXP Helper class.
14
19
  * @param opts - The options for configuring the JXP Helper.
20
+ * Provide `apikey` and/or `token`. Credentials may be omitted when only calling
21
+ * unauthenticated auth endpoints (`login`, `completeMfa`, `refresh`).
15
22
  */
16
23
  constructor(opts) {
17
24
  const defaults = {
@@ -35,20 +42,87 @@ class JXPHelper {
35
42
  }
36
43
  }
37
44
  }
45
+ /** Prefer bearer access token for subsequent authenticated calls. */
46
+ useAccessToken(accessToken) {
47
+ this.token = accessToken;
48
+ return this;
49
+ }
50
+ _isPlainObject(value) {
51
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
52
+ }
53
+ _isEmptyParam(value) {
54
+ return value === null || value === undefined || value === '';
55
+ }
56
+ _encodeParam(key, value) {
57
+ return key + '=' + encodeURIComponent(String(value));
58
+ }
59
+ _appendObjectEntries(parts, prefix, obj) {
60
+ for (const sub of Object.keys(obj)) {
61
+ const value = obj[sub];
62
+ if (this._isEmptyParam(value))
63
+ continue;
64
+ if (this._isPlainObject(value) || Array.isArray(value))
65
+ continue;
66
+ parts.push(this._encodeParam(`${prefix}[${sub}]`, value));
67
+ }
68
+ }
38
69
  _configParams(opts = {}) {
39
- opts.apikey = this.apikey;
40
70
  const parts = [];
41
71
  for (const opt in opts) {
42
- if (Array.isArray(opts[opt])) {
43
- opts[opt].forEach(val => {
44
- parts.push(opt + "=" + encodeURIComponent(val.toString()));
45
- });
72
+ if (['apikey', 'api_key', 'apiKey', 'x-api-key'].includes(opt.toLowerCase()))
73
+ continue;
74
+ const value = opts[opt];
75
+ if (this._isEmptyParam(value))
76
+ continue;
77
+ // `filters` (plural) → expand into `filter[key]=value` (never send `filters=`).
78
+ if (opt === 'filters') {
79
+ if (Array.isArray(value)) {
80
+ for (const item of value) {
81
+ if (this._isPlainObject(item))
82
+ this._appendObjectEntries(parts, 'filter', item);
83
+ }
84
+ }
85
+ else if (this._isPlainObject(value)) {
86
+ this._appendObjectEntries(parts, 'filter', value);
87
+ }
88
+ continue;
46
89
  }
47
- else {
48
- parts.push(opt + "=" + encodeURIComponent(opts[opt].toString()));
90
+ // `search: { q: '…' }` → full-text `search=…`; other objects → `search[field]=…`.
91
+ if (opt === 'search' && this._isPlainObject(value)) {
92
+ const keys = Object.keys(value);
93
+ if (keys.length === 1 && keys[0] === 'q') {
94
+ const q = value.q;
95
+ if (!this._isEmptyParam(q) && (typeof q === 'string' || typeof q === 'number' || typeof q === 'boolean')) {
96
+ const trimmed = String(q).trim();
97
+ if (trimmed)
98
+ parts.push(this._encodeParam('search', trimmed));
99
+ }
100
+ }
101
+ else {
102
+ this._appendObjectEntries(parts, 'search', value);
103
+ }
104
+ continue;
105
+ }
106
+ if (Array.isArray(value)) {
107
+ for (const item of value) {
108
+ if (this._isEmptyParam(item))
109
+ continue;
110
+ if (this._isPlainObject(item)) {
111
+ this._appendObjectEntries(parts, opt, item);
112
+ }
113
+ else if (!Array.isArray(item)) {
114
+ parts.push(this._encodeParam(opt, item));
115
+ }
116
+ }
117
+ continue;
118
+ }
119
+ if (this._isPlainObject(value)) {
120
+ this._appendObjectEntries(parts, opt, value);
121
+ continue;
49
122
  }
123
+ parts.push(this._encodeParam(opt, value));
50
124
  }
51
- return parts.join("&");
125
+ return parts.join('&');
52
126
  }
53
127
  _randomString() {
54
128
  return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
@@ -57,32 +131,72 @@ class JXPHelper {
57
131
  try {
58
132
  if (this.hideErrors)
59
133
  return;
60
- const config = err.config;
61
- const response = err.response;
62
- console.error(`${new Date().toISOString()}\turl: ${config?.url}\tmethod: ${config?.method}\tstatus: ${response?.status}\tstatusText: ${response?.statusText}\tdata: ${(response?.data) ? JSON.stringify(response.data) : 'No data'}`);
134
+ if (err instanceof errors_1.JXPError) {
135
+ console.error(`${new Date().toISOString()}\turl: ${err.url}\tmethod: ${err.method}\tstatus: ${err.status}\tstatusText: ${err.statusText}\tdata: ${err.body ? JSON.stringify(err.body) : 'No data'}`);
136
+ }
137
+ else {
138
+ console.error(err);
139
+ }
63
140
  }
64
141
  catch (error) {
65
142
  console.error(error);
66
143
  }
67
144
  }
145
+ _request(url, options = {}) {
146
+ return (0, http_1.jxpRequest)(url, this.apikey, this.token, options);
147
+ }
148
+ async _hydrateLogin(data) {
149
+ this.token = data.token;
150
+ // Prefer bearer for the user fetch even if a system API key is also configured.
151
+ const user = await (0, http_1.jxpRequest)(`${this.api}/user/${data.user_id}`, undefined, data.token);
152
+ return { data, user };
153
+ }
68
154
  url(type, opts, ep = "api") {
69
- return `${this.server}/${ep}/${type}?${this._configParams(opts)}`;
155
+ const params = this._configParams(opts);
156
+ return `${this.server}/${ep}/${type}${params ? `?${params}` : ''}`;
70
157
  }
71
158
  /**
72
- * Logs in a user with the provided email and password.
73
- * @param email - The user's email.
74
- * @param password - The user's password.
75
- * @returns A promise that resolves to an object containing the login data and user information, or rejects with an error object.
159
+ * Password login. May return an MFA challenge instead of tokens.
160
+ * On success, stores the access token on this helper for subsequent calls.
76
161
  */
77
162
  async login(email, password) {
78
- try {
79
- const data = (await axios_1.default.post(`${this.server}/login`, { email, password })).data;
80
- const user = (await axios_1.default.get(`${this.api}/user/${data.user_id}?apikey=${this.apikey}`)).data;
81
- return { data, user };
163
+ const data = await this._request(`${this.server}/login`, {
164
+ method: 'POST',
165
+ body: { email, password }
166
+ });
167
+ if ((0, types_1.isMfaRequired)(data))
168
+ return data;
169
+ if (!data?.token || !data?.user_id) {
170
+ throw new Error('Login response missing token/user_id');
82
171
  }
83
- catch (err) {
84
- return err.response?.data;
172
+ return this._hydrateLogin(data);
173
+ }
174
+ /**
175
+ * Complete MFA after `login` returned `{ status: "mfa_required", challenge, methods }`.
176
+ */
177
+ async completeMfa(opts) {
178
+ const data = await this._request(`${this.server}/login/mfa`, {
179
+ method: 'POST',
180
+ body: {
181
+ method: opts.method || 'totp',
182
+ challenge: opts.challenge,
183
+ code: String(opts.code).trim().replace(/\s+/g, '')
184
+ }
185
+ });
186
+ if (!data?.token || !data?.user_id) {
187
+ throw new Error('MFA response missing token/user_id');
85
188
  }
189
+ return this._hydrateLogin(data);
190
+ }
191
+ /**
192
+ * Exchange a refresh token for a new access / refresh pair.
193
+ */
194
+ async refresh(refreshToken) {
195
+ const data = await (0, http_1.jxpRequest)(`${this.server}/refresh`, undefined, refreshToken, { method: 'POST' });
196
+ if (!data?.token || !data?.user_id) {
197
+ throw new Error('Refresh response missing token/user_id');
198
+ }
199
+ return this._hydrateLogin(data);
86
200
  }
87
201
  /**
88
202
  * Retrieves a single item of a specified type by its ID.
@@ -98,19 +212,16 @@ class JXPHelper {
98
212
  console.time(label);
99
213
  const url = `${this.api}/${type}/${id}?${this._configParams(opts)}`;
100
214
  try {
101
- const result = await axios_1.default.get(url);
215
+ const result = await this._request(url);
102
216
  if (this.debug)
103
217
  console.timeEnd(label);
104
- if (result.status !== 200) {
105
- throw new Error(result.statusText);
106
- }
107
- return result.data;
218
+ return result;
108
219
  }
109
220
  catch (err) {
110
221
  if (this.debug)
111
222
  console.timeEnd(label);
112
223
  this._displayError(err);
113
- throw (err.response ? err.response.data : err);
224
+ throw err;
114
225
  }
115
226
  }
116
227
  /**
@@ -126,19 +237,16 @@ class JXPHelper {
126
237
  console.time(label);
127
238
  const url = this.url(type, opts);
128
239
  try {
129
- const result = await axios_1.default.get(url);
240
+ const result = await this._request(url);
130
241
  if (this.debug)
131
242
  console.timeEnd(label);
132
- if (result.status !== 200) {
133
- throw new Error(result.statusText);
134
- }
135
- return result.data;
243
+ return result;
136
244
  }
137
245
  catch (err) {
138
246
  if (this.debug)
139
247
  console.timeEnd(label);
140
248
  this._displayError(err);
141
- throw (err.response ? err.response.data : err);
249
+ throw err;
142
250
  }
143
251
  }
144
252
  /**
@@ -154,19 +262,16 @@ class JXPHelper {
154
262
  console.time(label);
155
263
  const url = `${this.server}/csv/${type}?${this._configParams(opts)}`;
156
264
  try {
157
- const result = await axios_1.default.get(url);
265
+ const result = await this._request(url);
158
266
  if (this.debug)
159
267
  console.timeEnd(label);
160
- if (result.status !== 200) {
161
- throw new Error(result.statusText);
162
- }
163
- return result.data;
268
+ return result;
164
269
  }
165
270
  catch (err) {
166
271
  if (this.debug)
167
272
  console.timeEnd(label);
168
273
  this._displayError(err);
169
- throw (err.response ? err.response.data : err);
274
+ throw err;
170
275
  }
171
276
  }
172
277
  /**
@@ -183,19 +288,16 @@ class JXPHelper {
183
288
  console.time(label);
184
289
  const url = `${this.server}/query/${type}?${this._configParams(opts)}`;
185
290
  try {
186
- const result = await axios_1.default.post(url, { query });
291
+ const result = await this._request(url, { method: 'POST', body: { query } });
187
292
  if (this.debug)
188
293
  console.timeEnd(label);
189
- if (result.status !== 200) {
190
- throw new Error(result.statusText);
191
- }
192
- return result.data;
294
+ return result;
193
295
  }
194
296
  catch (err) {
195
297
  if (this.debug)
196
298
  console.timeEnd(label);
197
299
  this._displayError(err);
198
- throw (err.response ? err.response.data : err);
300
+ throw err;
199
301
  }
200
302
  }
201
303
  /**
@@ -212,19 +314,16 @@ class JXPHelper {
212
314
  console.time(label);
213
315
  const url = `${this.server}/aggregate/${type}?${this._configParams(opts)}`;
214
316
  try {
215
- const result = await axios_1.default.post(url, { query });
317
+ const result = await this._request(url, { method: 'POST', body: { query } });
216
318
  if (this.debug)
217
319
  console.timeEnd(label);
218
- if (result.status !== 200) {
219
- throw new Error(result.statusText);
220
- }
221
- return result.data;
320
+ return result;
222
321
  }
223
322
  catch (err) {
224
323
  if (this.debug)
225
324
  console.timeEnd(label);
226
325
  this._displayError(err);
227
- throw (err.response ? err.response.data : err);
326
+ throw err;
228
327
  }
229
328
  }
230
329
  /**
@@ -259,12 +358,12 @@ class JXPHelper {
259
358
  }
260
359
  return updateQuery;
261
360
  });
262
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
263
- return (await axios_1.default.post(url, updates)).data;
361
+ const url = `${this.server}/bulkwrite/${type}`;
362
+ return await this._request(url, { method: 'POST', body: updates });
264
363
  }
265
364
  catch (err) {
266
365
  this._displayError(err);
267
- throw (err.response ? err.response.data : err);
366
+ throw err;
268
367
  }
269
368
  }
270
369
  /**
@@ -288,12 +387,12 @@ class JXPHelper {
288
387
  updateQuery.updateOne.filter[key] = item[key];
289
388
  return updateQuery;
290
389
  });
291
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
292
- return (await axios_1.default.post(url, updates)).data;
390
+ const url = `${this.server}/bulkwrite/${type}`;
391
+ return await this._request(url, { method: 'POST', body: updates });
293
392
  }
294
393
  catch (err) {
295
394
  this._displayError(err);
296
- throw (err.response ? err.response.data : err);
395
+ throw err;
297
396
  }
298
397
  }
299
398
  /**
@@ -312,12 +411,12 @@ class JXPHelper {
312
411
  }
313
412
  };
314
413
  });
315
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
316
- return (await axios_1.default.post(url, updates)).data;
414
+ const url = `${this.server}/bulkwrite/${type}`;
415
+ return await this._request(url, { method: 'POST', body: updates });
317
416
  }
318
417
  catch (err) {
319
418
  this._displayError(err);
320
- throw (err.response ? err.response.data : err);
419
+ throw err;
321
420
  }
322
421
  }
323
422
  /**
@@ -331,12 +430,12 @@ class JXPHelper {
331
430
  try {
332
431
  if (this.debug)
333
432
  console.log("bulk", type);
334
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
335
- return (await axios_1.default.post(url, query)).data;
433
+ const url = `${this.server}/bulkwrite/${type}`;
434
+ return await this._request(url, { method: 'POST', body: query });
336
435
  }
337
436
  catch (err) {
338
437
  this._displayError(err);
339
- throw (err.response ? err.response.data : err);
438
+ throw err;
340
439
  }
341
440
  }
342
441
  /**
@@ -359,12 +458,12 @@ class JXPHelper {
359
458
  },
360
459
  }
361
460
  ];
362
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
363
- return (await axios_1.default.post(url, query)).data;
461
+ const url = `${this.server}/bulkwrite/${type}`;
462
+ return await this._request(url, { method: 'POST', body: query });
364
463
  }
365
464
  catch (err) {
366
465
  this._displayError(err);
367
- throw (err.response ? err.response.data : err);
466
+ throw err;
368
467
  }
369
468
  }
370
469
  /**
@@ -381,19 +480,16 @@ class JXPHelper {
381
480
  options.limit = 1;
382
481
  const url = this.url(type, options, "count");
383
482
  try {
384
- const result = await axios_1.default.get(url);
483
+ const result = await this._request(url);
385
484
  if (this.debug)
386
485
  console.timeEnd(label);
387
- if (result.status !== 200) {
388
- throw new Error(result.statusText);
389
- }
390
- return result.data.count;
486
+ return result.count;
391
487
  }
392
488
  catch (err) {
393
489
  if (this.debug)
394
490
  console.timeEnd(label);
395
491
  this._displayError(err);
396
- throw (err.response ? err.response.data : err);
492
+ throw err;
397
493
  }
398
494
  }
399
495
  /**
@@ -403,15 +499,15 @@ class JXPHelper {
403
499
  * @returns The response data from the post operation.
404
500
  */
405
501
  async post(type, data) {
406
- const url = `${this.api}/${type}?apikey=${this.apikey}`;
502
+ const url = `${this.api}/${type}`;
407
503
  if (this.debug)
408
504
  console.log("POSTing to ", url, data);
409
505
  try {
410
- return (await axios_1.default.post(url, data)).data;
506
+ return await this._request(url, { method: 'POST', body: data });
411
507
  }
412
508
  catch (err) {
413
509
  this._displayError(err);
414
- throw (err.response ? err.response.data : err);
510
+ throw err;
415
511
  }
416
512
  }
417
513
  /**
@@ -423,15 +519,15 @@ class JXPHelper {
423
519
  * @throws If an error occurs during the request.
424
520
  */
425
521
  async put(type, id, data) {
426
- const url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
522
+ const url = `${this.api}/${type}/${id}`;
427
523
  if (this.debug)
428
524
  console.log("PUTting to ", url, data);
429
525
  try {
430
- return (await axios_1.default.put(url, data)).data;
526
+ return await this._request(url, { method: 'PUT', body: data });
431
527
  }
432
528
  catch (err) {
433
529
  this._displayError(err);
434
- throw (err.response ? err.response.data : err);
530
+ throw err;
435
531
  }
436
532
  }
437
533
  /**
@@ -459,7 +555,7 @@ class JXPHelper {
459
555
  }
460
556
  catch (err) {
461
557
  this._displayError(err);
462
- throw (err.response ? err.response.data : err);
558
+ throw err;
463
559
  }
464
560
  }
465
561
  /**
@@ -470,13 +566,13 @@ class JXPHelper {
470
566
  * @throws If an error occurs during the deletion process.
471
567
  */
472
568
  async del(type, id) {
473
- const url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
569
+ const url = `${this.api}/${type}/${id}`;
474
570
  try {
475
- return (await axios_1.default.delete(url)).data;
571
+ return await this._request(url, { method: 'DELETE' });
476
572
  }
477
573
  catch (err) {
478
574
  this._displayError(err);
479
- throw (err.response ? err.response.data : err);
575
+ throw err;
480
576
  }
481
577
  }
482
578
  /**
@@ -487,13 +583,13 @@ class JXPHelper {
487
583
  * @throws If an error occurs during the deletion process.
488
584
  */
489
585
  async del_perm(type, id) {
490
- const url = `${this.api}/${type}/${id}?_permaDelete=1&apikey=${this.apikey}`;
586
+ const url = `${this.api}/${type}/${id}?_permaDelete=1`;
491
587
  try {
492
- return (await axios_1.default.delete(url)).data;
588
+ return await this._request(url, { method: 'DELETE' });
493
589
  }
494
590
  catch (err) {
495
591
  this._displayError(err);
496
- throw (err.response ? err.response.data : err);
592
+ throw err;
497
593
  }
498
594
  }
499
595
  /**
@@ -504,13 +600,13 @@ class JXPHelper {
504
600
  * @throws If an error occurs during the deletion process.
505
601
  */
506
602
  async del_cascade(type, id) {
507
- const url = `${this.api}/${type}/${id}?_cascade=1&apikey=${this.apikey}`;
603
+ const url = `${this.api}/${type}/${id}?_cascade=1`;
508
604
  try {
509
- return (await axios_1.default.delete(url)).data;
605
+ return await this._request(url, { method: 'DELETE' });
510
606
  }
511
607
  catch (err) {
512
608
  this._displayError(err);
513
- throw (err.response ? err.response.data : err);
609
+ throw err;
514
610
  }
515
611
  }
516
612
  /**
@@ -521,13 +617,13 @@ class JXPHelper {
521
617
  * @throws If an error occurs during the delete request.
522
618
  */
523
619
  async del_perm_cascade(type, id) {
524
- const url = `${this.api}/${type}/${id}?_cascade=1&_permaDelete=1&apikey=${this.apikey}`;
620
+ const url = `${this.api}/${type}/${id}?_cascade=1&_permaDelete=1`;
525
621
  try {
526
- return (await axios_1.default.delete(url)).data;
622
+ return await this._request(url, { method: 'DELETE' });
527
623
  }
528
624
  catch (err) {
529
625
  this._displayError(err);
530
- throw (err.response ? err.response.data : err);
626
+ throw err;
531
627
  }
532
628
  }
533
629
  /**
@@ -551,7 +647,7 @@ class JXPHelper {
551
647
  }
552
648
  catch (err) {
553
649
  this._displayError(err);
554
- throw (err.response ? err.response.data : err);
650
+ throw err;
555
651
  }
556
652
  }
557
653
  async sync(type, key, id, data) {
@@ -587,7 +683,7 @@ class JXPHelper {
587
683
  }
588
684
  catch (err) {
589
685
  this._displayError(err);
590
- throw (err.response ? err.response.data : err);
686
+ throw err;
591
687
  }
592
688
  }
593
689
  /**
@@ -600,14 +696,30 @@ class JXPHelper {
600
696
  */
601
697
  async call(type, cmd, data) {
602
698
  //Call a function in the model
603
- const url = `${this.server}/call/${type}/${cmd}?apikey=${this.apikey}`;
699
+ const url = `${this.server}/call/${type}/${cmd}`;
604
700
  if (this.debug)
605
701
  console.log("CALLing ", url, data);
606
702
  try {
607
- return (await axios_1.default.post(url, data)).data;
703
+ return await this._request(url, { method: 'POST', body: data });
704
+ }
705
+ catch (err) {
706
+ throw err;
707
+ }
708
+ }
709
+ /**
710
+ * Retrieves the groups for a user.
711
+ * @param user_id - The ID of the user.
712
+ * @returns A promise that resolves to the usergroup document (or `{ groups: [] }`).
713
+ * @throws If an error occurs during the request.
714
+ */
715
+ async groups_get(user_id) {
716
+ const url = `${this.server}/groups/${user_id}`;
717
+ try {
718
+ return await this._request(url);
608
719
  }
609
720
  catch (err) {
610
- throw (err.response ? err.response.data : err);
721
+ this._displayError(err);
722
+ throw err;
611
723
  }
612
724
  }
613
725
  /**
@@ -618,12 +730,12 @@ class JXPHelper {
618
730
  * @throws If an error occurs during the update.
619
731
  */
620
732
  async groups_put(user_id, groups) {
621
- const url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
733
+ const url = `${this.server}/groups/${user_id}`;
622
734
  try {
623
- return (await axios_1.default.put(url, { group: groups })).data;
735
+ return await this._request(url, { method: 'PUT', body: { group: groups } });
624
736
  }
625
737
  catch (err) {
626
- throw (err.response ? err.response.data : err);
738
+ throw err;
627
739
  }
628
740
  }
629
741
  /**
@@ -634,13 +746,13 @@ class JXPHelper {
634
746
  * @throws If an error occurs during the deletion process.
635
747
  */
636
748
  async groups_del(user_id, group) {
637
- const url = `${this.server}/groups/${user_id}?group=${group}&apikey=${this.apikey}`;
749
+ const url = `${this.server}/groups/${user_id}?group=${encodeURIComponent(group)}`;
638
750
  try {
639
- return (await axios_1.default.delete(url)).data;
751
+ return await this._request(url, { method: 'DELETE' });
640
752
  }
641
753
  catch (err) {
642
754
  this._displayError(err);
643
- throw (err.response ? err.response.data : err);
755
+ throw err;
644
756
  }
645
757
  }
646
758
  /**
@@ -651,16 +763,16 @@ class JXPHelper {
651
763
  * @throws If an error occurs during the post request.
652
764
  */
653
765
  async groups_post(user_id, groups) {
654
- const url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
766
+ const url = `${this.server}/groups/${user_id}`;
655
767
  const data = { group: groups };
656
768
  if (this.debug)
657
769
  console.log("GROUP POSTing", url, data);
658
770
  try {
659
- return (await axios_1.default.post(url, data)).data;
771
+ return await this._request(url, { method: 'POST', body: data });
660
772
  }
661
773
  catch (err) {
662
774
  this._displayError(err);
663
- throw (err.response ? err.response.data : err);
775
+ throw err;
664
776
  }
665
777
  }
666
778
  /**
@@ -671,12 +783,11 @@ class JXPHelper {
671
783
  */
672
784
  async getjwt(email) {
673
785
  try {
674
- const jwt = (await axios_1.default.post(`${this.server}/login/getjwt?apikey=${this.apikey}`, { email })).data;
675
- return jwt;
786
+ return await this._request(`${this.server}/login/getjwt`, { method: 'POST', body: { email } });
676
787
  }
677
788
  catch (err) {
678
- if (err.response && err.response.data)
679
- return Promise.reject(err.response.data);
789
+ if (err instanceof errors_1.JXPError)
790
+ return Promise.reject(err.body);
680
791
  return Promise.reject(err);
681
792
  }
682
793
  }
@@ -688,12 +799,11 @@ class JXPHelper {
688
799
  */
689
800
  async model(modelname) {
690
801
  try {
691
- const modeldef = (await axios_1.default.get(`${this.server}/model/${modelname}?apikey=${this.apikey}`)).data;
692
- return modeldef;
802
+ return await this._request(`${this.server}/model/${modelname}`);
693
803
  }
694
804
  catch (err) {
695
- if (err.response && err.response.data)
696
- return Promise.reject(err.response.data);
805
+ if (err instanceof errors_1.JXPError)
806
+ return Promise.reject(err.body);
697
807
  return Promise.reject(err);
698
808
  }
699
809
  }
@@ -704,12 +814,11 @@ class JXPHelper {
704
814
  */
705
815
  async models() {
706
816
  try {
707
- const modeldef = (await axios_1.default.get(`${this.server}/model?apikey=${this.apikey}`)).data;
708
- return modeldef;
817
+ return await this._request(`${this.server}/model`);
709
818
  }
710
819
  catch (err) {
711
- if (err.response && err.response.data)
712
- return Promise.reject(err.response.data);
820
+ if (err instanceof errors_1.JXPError)
821
+ return Promise.reject(err.body);
713
822
  return Promise.reject(err);
714
823
  }
715
824
  }