jxp-helper 1.4.1 → 1.4.3

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.
Files changed (2) hide show
  1. package/jxp-helper.js +240 -6
  2. package/package.json +2 -2
package/jxp-helper.js CHANGED
@@ -1,12 +1,33 @@
1
1
  var axios = require("axios");
2
2
 
3
+ /**
4
+ * JXPHelper class for interacting with a JXP server.
5
+ * @class
6
+ */
3
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
+ */
4
16
  constructor(opts) {
17
+ const defaults = {
18
+ debug: false,
19
+ hideErrors: false
20
+ };
21
+ opts = Object.assign({}, defaults, opts);
5
22
  this.config(opts);
6
23
  if (!this.server) throw ("parameter 'server' required");
7
24
  this.api = this.server + "/api";
8
25
  }
9
-
26
+
27
+ /**
28
+ * Configures the options for the jxp-helper.
29
+ * @param {Object} opts - The options to configure.
30
+ */
10
31
  config(opts) {
11
32
  for (var opt in opts) {
12
33
  this[opt] = opts[opt];
@@ -35,8 +56,9 @@ class JXPHelper {
35
56
 
36
57
  _displayError(err) {
37
58
  try {
59
+ if (this.hideErrors) return;
38
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'}`);
39
- } catch (parseErr) {
61
+ } catch (err) {
40
62
  console.error(err);
41
63
  }
42
64
  }
@@ -45,6 +67,13 @@ class JXPHelper {
45
67
  return `${this.server}/${ep}/${type}?${this._configParams(opts)}`;
46
68
  }
47
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
+ */
48
77
  async login(email, password) {
49
78
  try {
50
79
  const data = (await axios.post(`${this.server}/login`, { email, password })).data;
@@ -55,6 +84,15 @@ class JXPHelper {
55
84
  }
56
85
  }
57
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
+ */
58
96
  async getOne(type, id, opts) {
59
97
  const label = `getOne.${type}-${this._randomString()}`;
60
98
  if (this.debug) console.time(label);
@@ -73,6 +111,14 @@ class JXPHelper {
73
111
  }
74
112
  }
75
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
+ */
76
122
  async get(type, opts) {
77
123
  const label = `get.${type}-${this._randomString()}`;
78
124
  if (this.debug) console.time(label);
@@ -91,6 +137,13 @@ class JXPHelper {
91
137
  }
92
138
  }
93
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
+ */
94
147
  async csv(type, opts) {
95
148
  const label = `get.${type}-${this._randomString()}`;
96
149
  if (this.debug) console.time(label);
@@ -109,6 +162,14 @@ class JXPHelper {
109
162
  }
110
163
  }
111
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
+ */
112
173
  async query(type, query, opts) {
113
174
  const label = `query.${type}-${this._randomString()}`;
114
175
  if (this.debug) console.time(label);
@@ -127,6 +188,14 @@ class JXPHelper {
127
188
  }
128
189
  }
129
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
+ */
130
199
  async aggregate(type, query, opts) {
131
200
  const label = `aggregate.${type}-${this._randomString()}`;
132
201
  if (this.debug) console.time(label);
@@ -145,6 +214,17 @@ class JXPHelper {
145
214
  }
146
215
  }
147
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
+ */
148
228
  async bulk_postput(type, key, data) {
149
229
  try {
150
230
  if (!Array.isArray(data)) return await this.postput(type, key, data);
@@ -173,6 +253,14 @@ class JXPHelper {
173
253
  }
174
254
  }
175
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
+ */
176
264
  async bulk_put(type, key, data) {
177
265
  try {
178
266
  const updates = data.map(item => {
@@ -194,6 +282,13 @@ class JXPHelper {
194
282
  }
195
283
  }
196
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
+ */
197
292
  async bulk_post(type, data) {
198
293
  try {
199
294
  const updates = data.map(item => {
@@ -211,6 +306,13 @@ class JXPHelper {
211
306
  }
212
307
  }
213
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
+ */
214
316
  async bulk(type, query) {
215
317
  try {
216
318
  if (this.debug) console.log("bulk", type);
@@ -222,7 +324,13 @@ class JXPHelper {
222
324
  }
223
325
  }
224
326
 
225
- // A very fast way to update ALL rows in a collection, with no seatbelts
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
+ */
226
334
  async put_all(type, data) {
227
335
  try {
228
336
  if (this.debug) console.log("put_all", type);
@@ -243,6 +351,13 @@ class JXPHelper {
243
351
  }
244
352
  }
245
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
+ */
246
361
  async count(type, opts) {
247
362
  const label = `count.${type}-${this._randomString()}`;
248
363
  if (this.debug) console.time(label);
@@ -263,6 +378,13 @@ class JXPHelper {
263
378
  }
264
379
  }
265
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
+ */
266
388
  async post(type, data) {
267
389
  var url = `${this.api}/${type}?apikey=${this.apikey}`;
268
390
  if (this.debug) console.log("POSTing to ", url, data);
@@ -274,6 +396,16 @@ class JXPHelper {
274
396
  }
275
397
  }
276
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
+ */
277
409
  async put(type, id, data) {
278
410
  var url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
279
411
  if (this.debug) console.log("PUTting to ", url, data);
@@ -285,6 +417,16 @@ class JXPHelper {
285
417
  }
286
418
  }
287
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
+ */
288
430
  async postput(type, key, data) {
289
431
  // Post if we find key=id, else put
290
432
  var obj = {};
@@ -302,7 +444,15 @@ class JXPHelper {
302
444
  throw(err.response ? err.response.data : err);
303
445
  }
304
446
  }
305
-
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
+ */
306
456
  async del(type, id) {
307
457
  const url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
308
458
  try {
@@ -313,7 +463,15 @@ class JXPHelper {
313
463
  }
314
464
  }
315
465
 
316
- // Permanently delete
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
+ */
317
475
  async del_perm(type, id) {
318
476
  const url = `${this.api}/${type}/${id}?_permaDelete=1&apikey=${this.apikey}`;
319
477
  try {
@@ -324,6 +482,13 @@ class JXPHelper {
324
482
  }
325
483
  }
326
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
+ */
327
492
  async del_cascade(type, id) {
328
493
  var url = `${this.api}/${type}/${id}?_cascade=1&apikey=${this.apikey}`;
329
494
  try {
@@ -334,6 +499,13 @@ class JXPHelper {
334
499
  }
335
500
  }
336
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
+ */
337
509
  async del_perm_cascade(type, id) {
338
510
  var url = `${this.api}/${type}/${id}?_cascade=1&_permaDelete=1&apikey=${this.apikey}`;
339
511
  try {
@@ -344,7 +516,16 @@ class JXPHelper {
344
516
  }
345
517
  }
346
518
 
347
- // This should be rewritten as an async pattern
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
+ */
348
529
  async del_all(type, key, id) {
349
530
  var obj = {};
350
531
  obj[`filter[${key}]`] = id;
@@ -394,6 +575,15 @@ class JXPHelper {
394
575
  }
395
576
  }
396
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
+ */
397
587
  async call(type, cmd, data) {
398
588
  //Call a function in the model
399
589
  var url = `${this.server}/call/${type}/${cmd}?apikey=${this.apikey}`;
@@ -405,6 +595,14 @@ class JXPHelper {
405
595
  }
406
596
  }
407
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
+ */
408
606
  async groups_put(user_id, groups) {
409
607
  var url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
410
608
  try {
@@ -414,6 +612,14 @@ class JXPHelper {
414
612
  }
415
613
  }
416
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
+ */
417
623
  async groups_del(user_id, group) {
418
624
  var url = `${this.server}/groups/${user_id}?group=${group}&apikey=${this.apikey}`;
419
625
  try {
@@ -424,6 +630,14 @@ class JXPHelper {
424
630
  }
425
631
  }
426
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
+ */
427
641
  async groups_post(user_id, groups) {
428
642
  var url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
429
643
  var data = { group: groups };
@@ -436,6 +650,13 @@ class JXPHelper {
436
650
  }
437
651
  }
438
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
+ */
439
660
  async getjwt(email) {
440
661
  try {
441
662
  const jwt = (await axios.post(`${ this.server }/login/getjwt?apikey=${ this.apikey }`, { email })).data;
@@ -447,6 +668,13 @@ class JXPHelper {
447
668
  }
448
669
  }
449
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
+ */
450
678
  async model(modelname) {
451
679
  try {
452
680
  const modeldef = (await axios.get(`${ this.server }/model/${ modelname }?apikey=${ this.apikey }`)).data;
@@ -458,6 +686,12 @@ class JXPHelper {
458
686
  }
459
687
  }
460
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
+ */
461
695
  async models() {
462
696
  try {
463
697
  const modeldef = (await axios.get(`${ this.server }/model?apikey=${ this.apikey }`)).data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jxp-helper",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "A bunch of helpful functions for talking to a JXP API server ",
5
5
  "main": "jxp-helper.js",
6
6
  "scripts": {
@@ -17,6 +17,6 @@
17
17
  },
18
18
  "homepage": "https://github.com/WorkSpaceMan/jxp-helper#readme",
19
19
  "dependencies": {
20
- "axios": "^1.4.0"
20
+ "axios": "^1.6.2"
21
21
  }
22
22
  }