jxp-helper 1.4.3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/jxp-helper.js DELETED
@@ -1,708 +0,0 @@
1
- var axios = require("axios");
2
-
3
- /**
4
- * JXPHelper class for interacting with a JXP server.
5
- * @class
6
- */
7
- class JXPHelper {
8
- /**
9
- * Creates a new instance of the JXP Helper class.
10
- * @param {Object} opts - The options for configuring the JXP Helper.
11
- * @param {string} opts.server - The server URL.
12
- * @param {string} opts.apikey - The API key for the user.
13
- * @param {boolean} [opts.debug=false] - Whether to enable debug mode.
14
- * @param {boolean} [opts.hideErrors=false] - Whether to hide errors.
15
- */
16
- constructor(opts) {
17
- const defaults = {
18
- debug: false,
19
- hideErrors: false
20
- };
21
- opts = Object.assign({}, defaults, opts);
22
- this.config(opts);
23
- if (!this.server) throw ("parameter 'server' required");
24
- this.api = this.server + "/api";
25
- }
26
-
27
- /**
28
- * Configures the options for the jxp-helper.
29
- * @param {Object} opts - The options to configure.
30
- */
31
- config(opts) {
32
- for (var opt in opts) {
33
- this[opt] = opts[opt];
34
- }
35
- };
36
-
37
- _configParams(opts) {
38
- opts = opts || {};
39
- opts.apikey = this.apikey;
40
- var parts = [];
41
- for (var opt in opts) {
42
- if (Array.isArray(opts[opt])) {
43
- opts[opt].forEach(val => {
44
- parts.push(opt + "=" + encodeURIComponent(val));
45
- });
46
- } else {
47
- parts.push(opt + "=" + encodeURIComponent(opts[opt]));
48
- }
49
- }
50
- return parts.join("&");
51
- };
52
-
53
- _randomString() {
54
- return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
55
- }
56
-
57
- _displayError(err) {
58
- try {
59
- if (this.hideErrors) return;
60
- console.error(`${new Date().toISOString()}\turl: ${err.config.url}\tmethod: ${err.request.method}\tstatus: ${err.response.status}\tstatusText: ${err.response.statusText}\tdata: ${(err.response.data) ? JSON.stringify(err.response.data) : 'No data'}`);
61
- } catch (err) {
62
- console.error(err);
63
- }
64
- }
65
-
66
- url(type, opts, ep="api") {
67
- return `${this.server}/${ep}/${type}?${this._configParams(opts)}`;
68
- }
69
-
70
-
71
- /**
72
- * Logs in a user with the provided email and password.
73
- * @param {string} email - The user's email.
74
- * @param {string} password - The user's password.
75
- * @returns {Promise<{ data: any, user: any } | any>} - A promise that resolves to an object containing the login data and user information, or rejects with an error object.
76
- */
77
- async login(email, password) {
78
- try {
79
- const data = (await axios.post(`${this.server}/login`, { email, password })).data;
80
- const user = (await axios.get(`${this.api}/user/${data.user_id}?apikey=${this.apikey}`)).data;
81
- return { data, user };
82
- } catch (err) {
83
- return err.response.data;
84
- }
85
- }
86
-
87
- /**
88
- * Retrieves a single item of a specified type by its ID.
89
- *
90
- * @param {string} type - The type of the item.
91
- * @param {string} id - The ID of the item.
92
- * @param {Object} opts - Additional options for the request.
93
- * @returns {Promise<Object>} - A promise that resolves to the retrieved item.
94
- * @throws {Error} - If the request fails or returns a non-200 status code.
95
- */
96
- async getOne(type, id, opts) {
97
- const label = `getOne.${type}-${this._randomString()}`;
98
- if (this.debug) console.time(label);
99
- const url = `${this.api}/${type}/${id}?${this._configParams(opts)}`;
100
- try {
101
- var result = await axios.get(url);
102
- if (this.debug) console.timeEnd(label);
103
- if (result.status !== 200) {
104
- throw(result.statusText);
105
- }
106
- return result.data;
107
- } catch(err) {
108
- if (this.debug) console.timeEnd(label);
109
- this._displayError(err);
110
- throw(err.response ? err.response.data : err);
111
- }
112
- }
113
-
114
-
115
- /**
116
- * Retrieves data of a specified type from a URL.
117
- * @param {string} type - The type of data to retrieve.
118
- * @param {Object} opts - Additional options for the request.
119
- * @returns {Promise<any>} - A promise that resolves with the retrieved data.
120
- * @throws {Error} - If the request fails or returns a non-200 status code.
121
- */
122
- async get(type, opts) {
123
- const label = `get.${type}-${this._randomString()}`;
124
- if (this.debug) console.time(label);
125
- var url = this.url(type, opts);
126
- try {
127
- var result = await axios.get(url);
128
- if (this.debug) console.timeEnd(label);
129
- if (result.status !== 200) {
130
- throw(result.statusText);
131
- }
132
- return result.data;
133
- } catch(err) {
134
- if (this.debug) console.timeEnd(label);
135
- this._displayError(err);
136
- throw(err.response ? err.response.data : err);
137
- }
138
- }
139
-
140
- /**
141
- * Retrieves data in CSV format from the server.
142
- * @param {string} type - The type of data to retrieve.
143
- * @param {Object} opts - Additional options for the request.
144
- * @returns {Promise<string>} - The CSV data.
145
- * @throws {Error} - If the request fails or returns a non-200 status code.
146
- */
147
- async csv(type, opts) {
148
- const label = `get.${type}-${this._randomString()}`;
149
- if (this.debug) console.time(label);
150
- var url = `${this.server}/csv/${type}?${this._configParams(opts)}`;
151
- try {
152
- var result = await axios.get(url);
153
- if (this.debug) console.timeEnd(label);
154
- if (result.status !== 200) {
155
- throw(result.statusText);
156
- }
157
- return result.data;
158
- } catch(err) {
159
- if (this.debug) console.timeEnd(label);
160
- this._displayError(err);
161
- throw(err.response ? err.response.data : err);
162
- }
163
- }
164
-
165
- /**
166
- * Executes a query of the specified type with the given parameters.
167
- * @param {string} type - The type of query to execute.
168
- * @param {string} query - The query string.
169
- * @param {Object} opts - Additional options for the query.
170
- * @returns {Promise<any>} - A promise that resolves to the query result.
171
- * @throws {Error} - If the query fails or returns a non-200 status code.
172
- */
173
- async query(type, query, opts) {
174
- const label = `query.${type}-${this._randomString()}`;
175
- if (this.debug) console.time(label);
176
- var url = `${this.server}/query/${type}?${this._configParams(opts)}`;
177
- try {
178
- var result = await axios.post(url, {query});
179
- if (this.debug) console.timeEnd(label);
180
- if (result.status !== 200) {
181
- throw(result.statusText);
182
- }
183
- return result.data;
184
- } catch(err) {
185
- if (this.debug) console.timeEnd(label);
186
- this._displayError(err);
187
- throw(err.response ? err.response.data : err);
188
- }
189
- }
190
-
191
- /**
192
- * Performs an aggregate operation on the specified type with the given query and options.
193
- * @param {string} type - The type to perform the aggregate operation on.
194
- * @param {object} query - The query object for the aggregate operation.
195
- * @param {object} opts - The options for the aggregate operation.
196
- * @returns {Promise<object>} - The result of the aggregate operation.
197
- * @throws {Error} - If the aggregate operation fails.
198
- */
199
- async aggregate(type, query, opts) {
200
- const label = `aggregate.${type}-${this._randomString()}`;
201
- if (this.debug) console.time(label);
202
- var url = `${this.server}/aggregate/${type}?${this._configParams(opts)}`;
203
- try {
204
- var result = await axios.post(url, { query });
205
- if (this.debug) console.timeEnd(label);
206
- if (result.status !== 200) {
207
- throw (result.statusText);
208
- }
209
- return result.data;
210
- } catch (err) {
211
- if (this.debug) console.timeEnd(label);
212
- this._displayError(err);
213
- throw (err.response ? err.response.data : err);
214
- }
215
- }
216
-
217
- /**
218
- * Performs a bulk post or put operation.
219
- * If the data parameter is an array, it performs a bulk update operation.
220
- * If the data parameter is an object, it performs a single post or put operation.
221
- *
222
- * @param {string} type - The type of operation to perform (post or put).
223
- * @param {string|string[]} key - The key(s) used to filter the data for the update operation.
224
- * @param {object|object[]} data - The data to be updated or inserted.
225
- * @returns {Promise} - A promise that resolves with the result of the bulk operation.
226
- * @throws {Error} - If an error occurs during the bulk operation.
227
- */
228
- async bulk_postput(type, key, data) {
229
- try {
230
- if (!Array.isArray(data)) return await this.postput(type, key, data);
231
- const updates = data.map(item => {
232
- const updateQuery = {
233
- "updateOne": {
234
- "upsert": true
235
- }
236
- }
237
- updateQuery.updateOne.update = item;
238
- updateQuery.updateOne.filter = {};
239
- if (Array.isArray(key)) {
240
- key.forEach(k => {
241
- updateQuery.updateOne.filter[k] = item[k];
242
- });
243
- } else {
244
- updateQuery.updateOne.filter[key] = item[key];
245
- }
246
- return updateQuery;
247
- });
248
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
249
- return (await axios.post(url, updates)).data;
250
- } catch (err) {
251
- this._displayError(err);
252
- throw (err.response ? err.response.data : err);
253
- }
254
- }
255
-
256
- /**
257
- * Performs a bulk update operation for a given type of data.
258
- * @param {string} type - The type of data to update.
259
- * @param {string} key - The key to use for filtering and updating the data.
260
- * @param {Array<Object>} data - The array of data objects to update.
261
- * @returns {Promise<Object>} - A promise that resolves to the response data from the bulk update operation.
262
- * @throws {Error} - If an error occurs during the bulk update operation.
263
- */
264
- async bulk_put(type, key, data) {
265
- try {
266
- const updates = data.map(item => {
267
- const updateQuery = {
268
- "updateOne": {
269
- "upsert": false
270
- }
271
- }
272
- updateQuery.updateOne.update = item;
273
- updateQuery.updateOne.filter = {};
274
- updateQuery.updateOne.filter[key] = item[key];
275
- return updateQuery;
276
- });
277
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
278
- return (await axios.post(url, updates)).data;
279
- } catch (err) {
280
- this._displayError(err);
281
- throw (err.response ? err.response.data : err);
282
- }
283
- }
284
-
285
- /**
286
- * Performs a bulk post operation.
287
- * @param {string} type - The type of data to be posted.
288
- * @param {Array} data - The data to be posted.
289
- * @returns {Promise} - A promise that resolves with the response data.
290
- * @throws {Error} - If an error occurs during the operation.
291
- */
292
- async bulk_post(type, data) {
293
- try {
294
- const updates = data.map(item => {
295
- return {
296
- "insertOne": {
297
- "document": item
298
- }
299
- }
300
- });
301
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
302
- return (await axios.post(url, updates)).data;
303
- } catch (err) {
304
- this._displayError(err);
305
- throw (err.response ? err.response.data : err);
306
- }
307
- }
308
-
309
- /**
310
- * Performs a bulk write operation for a given type using the specified query.
311
- * @param {string} type - The type of the bulk write operation.
312
- * @param {object} query - The query object for the bulk write operation.
313
- * @returns {Promise<any>} - A promise that resolves to the result of the bulk write operation.
314
- * @throws {Error} - If an error occurs during the bulk write operation.
315
- */
316
- async bulk(type, query) {
317
- try {
318
- if (this.debug) console.log("bulk", type);
319
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
320
- return (await axios.post(url, query)).data;
321
- } catch(err) {
322
- this._displayError(err);
323
- throw(err.response ? err.response.data : err);
324
- }
325
- }
326
-
327
- /**
328
- * Updates multiple documents of a specified type in the database.
329
- * @param {string} type - The type of documents to update.
330
- * @param {object} data - The data to update the documents with.
331
- * @returns {Promise<object>} - The response data from the database.
332
- * @throws {Error} - If an error occurs during the update process.
333
- */
334
- async put_all(type, data) {
335
- try {
336
- if (this.debug) console.log("put_all", type);
337
- const query = [
338
- {
339
- "updateMany": {
340
- "upsert": false,
341
- filter: {},
342
- update: { $set: data },
343
- },
344
- }
345
- ];
346
- const url = `${this.server}/bulkwrite/${type}?apikey=${this.apikey}`;
347
- return (await axios.post(url, query)).data;
348
- } catch(err) {
349
- this._displayError(err);
350
- throw(err.response ? err.response.data : err);
351
- }
352
- }
353
-
354
- /**
355
- * Counts the number of items of a given type.
356
- *
357
- * @param {string} type - The type of items to count.
358
- * @param {object} opts - Additional options for counting.
359
- * @returns {Promise<number>} - The count of items.
360
- */
361
- async count(type, opts) {
362
- const label = `count.${type}-${this._randomString()}`;
363
- if (this.debug) console.time(label);
364
- opts = opts || {};
365
- opts.limit = 1;
366
- var url = this.url(type, opts, "count");
367
- try {
368
- var result = await axios.get(url);
369
- if (this.debug) console.timeEnd(label);
370
- if (result.status !== 200) {
371
- throw (result.statusText);
372
- }
373
- return result.data.count;
374
- } catch (err) {
375
- if (this.debug) console.timeEnd(label);
376
- this._displayError(err);
377
- throw (err.response ? err.response.data : err);
378
- }
379
- }
380
-
381
- /**
382
- * Creates a new record by making a POST request to the specified URL.
383
- *
384
- * @param {string} type - The type of data to post.
385
- * @param {object} data - The data to post.
386
- * @returns {<Promise<object>} - The response data from the post operation.
387
- */
388
- async post(type, data) {
389
- var url = `${this.api}/${type}?apikey=${this.apikey}`;
390
- if (this.debug) console.log("POSTing to ", url, data);
391
- try {
392
- return (await axios.post(url, data)).data;
393
- } catch(err) {
394
- this._displayError(err);
395
- throw(err.response ? err.response.data : err);
396
- }
397
- }
398
-
399
-
400
- /**
401
- * Updates an existing record by making a PUT request to the specified URL.
402
- *
403
- * @param {string} type - The type of the record.
404
- * @param {string} id - The ID of the record.
405
- * @param {Object} data - The data to be sent in the request body.
406
- * @returns {Promise<Object>} - A promise that resolves to the response data.
407
- * @throws {Error} - If an error occurs during the request.
408
- */
409
- async put(type, id, data) {
410
- var url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
411
- if (this.debug) console.log("PUTting to ", url, data);
412
- try {
413
- return (await axios.put(url, data)).data;
414
- } catch(err) {
415
- this._displayError(err);
416
- throw(err.response ? err.response.data : err);
417
- }
418
- }
419
-
420
- /**
421
- * Performs a POST or PUT request based on the existence of a specific key in the data object.
422
- * If the key exists in the data object, a PUT request is made with the corresponding ID.
423
- * If the key does not exist, a POST request is made with the data object.
424
- *
425
- * @param {string} type - The type of resource to perform the request on.
426
- * @param {string} key - The key to check in the data object.
427
- * @param {object} data - The data object to be sent in the request.
428
- * @returns {Promise} - A promise that resolves with the response data or rejects with an error.
429
- */
430
- async postput(type, key, data) {
431
- // Post if we find key=id, else put
432
- var obj = {};
433
- obj[`filter[${key}]`] = data[key];
434
- try {
435
- var result = await this.get(type, obj);
436
- if (result.data.length) {
437
- var id = result.data[0]._id;
438
- return this.put(type, id, data);
439
- } else {
440
- return this.post(type, data);
441
- }
442
- } catch(err) {
443
- this._displayError(err);
444
- throw(err.response ? err.response.data : err);
445
- }
446
- }
447
-
448
- /**
449
- * Deletes an item of the specified type by its ID.
450
- *
451
- * @param {string} type - The type of the item to delete.
452
- * @param {string} id - The ID of the item to delete.
453
- * @returns {Promise<any>} - A promise that resolves to the deleted item.
454
- * @throws {Error} - If an error occurs during the deletion process.
455
- */
456
- async del(type, id) {
457
- const url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
458
- try {
459
- return (await axios.delete(url)).data;
460
- } catch(err) {
461
- this._displayError(err);
462
- throw(err.response ? err.response.data : err);
463
- }
464
- }
465
-
466
-
467
- /**
468
- * Deletes a resource permanently.
469
- *
470
- * @param {string} type - The type of resource to delete.
471
- * @param {string} id - The ID of the resource to delete.
472
- * @returns {Promise<any>} - A promise that resolves to the deleted resource data.
473
- * @throws {Error} - If an error occurs during the deletion process.
474
- */
475
- async del_perm(type, id) {
476
- const url = `${this.api}/${type}/${id}?_permaDelete=1&apikey=${this.apikey}`;
477
- try {
478
- return (await axios.delete(url)).data;
479
- } catch(err) {
480
- this._displayError(err);
481
- throw(err.response ? err.response.data : err);
482
- }
483
- }
484
-
485
- /**
486
- * Soft Deletes a resource and its cascading dependencies.
487
- * @param {string} type - The type of the resource to delete.
488
- * @param {string} id - The ID of the resource to delete.
489
- * @returns {Promise<any>} - A promise that resolves with the deleted resource data.
490
- * @throws {Error} - If an error occurs during the deletion process.
491
- */
492
- async del_cascade(type, id) {
493
- var url = `${this.api}/${type}/${id}?_cascade=1&apikey=${this.apikey}`;
494
- try {
495
- return (await axios.delete(url)).data;
496
- } catch(err) {
497
- this._displayError(err);
498
- throw(err.response ? err.response.data : err);
499
- }
500
- }
501
-
502
- /**
503
- * Permanently Deletes a resource and its associated data permanently, including all cascading dependencies.
504
- * @param {string} type - The type of resource to delete.
505
- * @param {string} id - The ID of the resource to delete.
506
- * @returns {Promise<any>} - A promise that resolves to the response data from the delete request.
507
- * @throws {Error} - If an error occurs during the delete request.
508
- */
509
- async del_perm_cascade(type, id) {
510
- var url = `${this.api}/${type}/${id}?_cascade=1&_permaDelete=1&apikey=${this.apikey}`;
511
- try {
512
- return (await axios.delete(url)).data;
513
- } catch(err) {
514
- this._displayError(err);
515
- throw(err.response ? err.response.data : err);
516
- }
517
- }
518
-
519
-
520
- /**
521
- * Deletes all items of a specified type that match a given key-value pair.
522
- *
523
- * @param {string} type - The type of items to delete.
524
- * @param {string} key - The key to filter the items.
525
- * @param {string} id - The value to match against the key.
526
- * @returns {Promise<Array>} - A promise that resolves to an array of results from deleting each item.
527
- * @throws {Error} - If an error occurs during the deletion process.
528
- */
529
- async del_all(type, key, id) {
530
- var obj = {};
531
- obj[`filter[${key}]`] = id;
532
- try {
533
- const results = [];
534
- const items = (await self.get(type, obj)).data;
535
- for (let item of items) {
536
- results.push(await this.del(type, item._id));
537
- }
538
- return results;
539
- } catch(err) {
540
- this._displayError(err);
541
- throw (err.response ? err.response.data : err);
542
- }
543
- }
544
-
545
- async sync(type, key, id, data) {
546
- // Given the records filtered by key = id, we create, update or delete until we are in sync with data.
547
- var obj = {};
548
- obj[`filter[${key}]`] = id;
549
- try {
550
- let results = [];
551
- const data = await this.get(type, obj).data;
552
- const data_ids = data.filter(row => row._id).map(row => row._id);
553
- const dest_ids = data.map(row => row._id);
554
- const deletes = dest_ids.filter(n => data_ids.indexOf(n) == -1) || [];
555
- const moreinserts = data_ids.filter(n => dest_ids.indexOf(n) == -1) || [];
556
- const inserts = data.filter(row => moreinserts.indexOf(row._id) != -1) || !(row._id);
557
- const update_ids = dest_ids.filter(n => data_ids.indexOf(n) != -1) || [];
558
- const updates = data.filter(row => update_ids.indexOf(row._id) != -1) || [];
559
- for (let insert of inserts) {
560
- if (this.debug) console.log("Inserting", insert);
561
- results.push(await this.post(type, insert));
562
- }
563
- for (let update of updates) {
564
- if (this.debug) console.log("Updating", update);
565
- results.push(await self.put(type, update._id, update));
566
- }
567
- for (let del of deletes) {
568
- if (this.debug) console.log("Deleting", del);
569
- results.push(await this.del(type, del));
570
- }
571
- return results;
572
- } catch(err) {
573
- this._displayError(err);
574
- throw (err.response ? err.response.data : err);
575
- }
576
- }
577
-
578
- /**
579
- * Calls a function in the model.
580
- *
581
- * @param {string} type - The type of the function.
582
- * @param {string} cmd - The command to be executed.
583
- * @param {object} data - The data to be sent with the request.
584
- * @returns {Promise<any>} - A promise that resolves to the response data.
585
- * @throws {any} - Throws an error if the request fails.
586
- */
587
- async call(type, cmd, data) {
588
- //Call a function in the model
589
- var url = `${this.server}/call/${type}/${cmd}?apikey=${this.apikey}`;
590
- if (this.debug) console.log("CALLing ", url, data);
591
- try {
592
- return (await axios.post(url, data)).data;
593
- } catch(err) {
594
- throw(err.response ? err.response.data : err);
595
- }
596
- }
597
-
598
- /**
599
- * Updates the groups for a user.
600
- *
601
- * @param {string} user_id - The ID of the user.
602
- * @param {Array} groups - The groups to update.
603
- * @returns {Promise} - A promise that resolves to the updated data.
604
- * @throws {Error} - If an error occurs during the update.
605
- */
606
- async groups_put(user_id, groups) {
607
- var url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
608
- try {
609
- return (await axios.put(url, { group: groups })).data;
610
- } catch(err) {
611
- throw(err.response ? err.response.data : err);
612
- }
613
- }
614
-
615
- /**
616
- * Deletes a group for a specific user.
617
- *
618
- * @param {string} user_id - The ID of the user.
619
- * @param {string} group - The name of the group to delete.
620
- * @returns {Promise} - A promise that resolves to the response data from the server.
621
- * @throws {Error} - If an error occurs during the deletion process.
622
- */
623
- async groups_del(user_id, group) {
624
- var url = `${this.server}/groups/${user_id}?group=${group}&apikey=${this.apikey}`;
625
- try {
626
- return (await axios.delete(url)).data;
627
- } catch(err) {
628
- this._displayError(err);
629
- throw(err.response ? err.response.data : err);
630
- }
631
- }
632
-
633
- /**
634
- * Add a user to a group
635
- *
636
- * @param {string} user_id - The ID of the user.
637
- * @param {Array} groups - The groups to be posted.
638
- * @returns {Promise} - A promise that resolves to the response data.
639
- * @throws {Error} - If an error occurs during the post request.
640
- */
641
- async groups_post(user_id, groups) {
642
- var url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
643
- var data = { group: groups };
644
- if (this.debug) console.log("GROUP POSTing", url, data);
645
- try {
646
- return (await axios.post(url, data)).data;
647
- } catch(err) {
648
- this._displayError(err);
649
- throw(err.response ? err.response.data : err);
650
- }
651
- }
652
-
653
- /**
654
- * Generates a JWT (JSON Web Token) for the specified email.
655
- *
656
- * @param {string} email - The email address used for authentication.
657
- * @returns {Promise<string>} - A promise that resolves with the JWT.
658
- * @throws {Error} - If an error occurs during the retrieval of the JWT.
659
- */
660
- async getjwt(email) {
661
- try {
662
- const jwt = (await axios.post(`${ this.server }/login/getjwt?apikey=${ this.apikey }`, { email })).data;
663
- return jwt;
664
- } catch (err) {
665
- if (err.response && err.response.data)
666
- return Promise.reject(err.response.data);
667
- return Promise.reject(err);
668
- }
669
- }
670
-
671
- /**
672
- * Retrieves the definition of a model from the server.
673
- *
674
- * @param {string} modelname - The name of the model to retrieve.
675
- * @returns {Promise<object>} - A promise that resolves to the model definition.
676
- * @throws {Error} - If an error occurs during the retrieval process.
677
- */
678
- async model(modelname) {
679
- try {
680
- const modeldef = (await axios.get(`${ this.server }/model/${ modelname }?apikey=${ this.apikey }`)).data;
681
- return modeldef;
682
- } catch (err) {
683
- if (err.response && err.response.data)
684
- return Promise.reject(err.response.data);
685
- return Promise.reject(err);
686
- }
687
- }
688
-
689
- /**
690
- * Retrieves the model definitions from the server.
691
- *
692
- * @returns {Promise<Object>} A promise that resolves to the model definitions.
693
- * @throws {Error} If an error occurs while retrieving the model definitions.
694
- */
695
- async models() {
696
- try {
697
- const modeldef = (await axios.get(`${ this.server }/model?apikey=${ this.apikey }`)).data;
698
- return modeldef;
699
- } catch (err) {
700
- if (err.response && err.response.data)
701
- return Promise.reject(err.response.data);
702
- return Promise.reject(err);
703
- }
704
- }
705
- };
706
-
707
- module.exports = JXPHelper;
708
-