jxp-helper 1.4.2 → 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 +239 -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,7 +56,7 @@ class JXPHelper {
35
56
 
36
57
  _displayError(err) {
37
58
  try {
38
- if (this.opts.hideErrors) return;
59
+ if (this.hideErrors) return;
39
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'}`);
40
61
  } catch (err) {
41
62
  console.error(err);
@@ -46,6 +67,13 @@ class JXPHelper {
46
67
  return `${this.server}/${ep}/${type}?${this._configParams(opts)}`;
47
68
  }
48
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
+ */
49
77
  async login(email, password) {
50
78
  try {
51
79
  const data = (await axios.post(`${this.server}/login`, { email, password })).data;
@@ -56,6 +84,15 @@ class JXPHelper {
56
84
  }
57
85
  }
58
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
+ */
59
96
  async getOne(type, id, opts) {
60
97
  const label = `getOne.${type}-${this._randomString()}`;
61
98
  if (this.debug) console.time(label);
@@ -74,6 +111,14 @@ class JXPHelper {
74
111
  }
75
112
  }
76
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
+ */
77
122
  async get(type, opts) {
78
123
  const label = `get.${type}-${this._randomString()}`;
79
124
  if (this.debug) console.time(label);
@@ -92,6 +137,13 @@ class JXPHelper {
92
137
  }
93
138
  }
94
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
+ */
95
147
  async csv(type, opts) {
96
148
  const label = `get.${type}-${this._randomString()}`;
97
149
  if (this.debug) console.time(label);
@@ -110,6 +162,14 @@ class JXPHelper {
110
162
  }
111
163
  }
112
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
+ */
113
173
  async query(type, query, opts) {
114
174
  const label = `query.${type}-${this._randomString()}`;
115
175
  if (this.debug) console.time(label);
@@ -128,6 +188,14 @@ class JXPHelper {
128
188
  }
129
189
  }
130
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
+ */
131
199
  async aggregate(type, query, opts) {
132
200
  const label = `aggregate.${type}-${this._randomString()}`;
133
201
  if (this.debug) console.time(label);
@@ -146,6 +214,17 @@ class JXPHelper {
146
214
  }
147
215
  }
148
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
+ */
149
228
  async bulk_postput(type, key, data) {
150
229
  try {
151
230
  if (!Array.isArray(data)) return await this.postput(type, key, data);
@@ -174,6 +253,14 @@ class JXPHelper {
174
253
  }
175
254
  }
176
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
+ */
177
264
  async bulk_put(type, key, data) {
178
265
  try {
179
266
  const updates = data.map(item => {
@@ -195,6 +282,13 @@ class JXPHelper {
195
282
  }
196
283
  }
197
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
+ */
198
292
  async bulk_post(type, data) {
199
293
  try {
200
294
  const updates = data.map(item => {
@@ -212,6 +306,13 @@ class JXPHelper {
212
306
  }
213
307
  }
214
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
+ */
215
316
  async bulk(type, query) {
216
317
  try {
217
318
  if (this.debug) console.log("bulk", type);
@@ -223,7 +324,13 @@ class JXPHelper {
223
324
  }
224
325
  }
225
326
 
226
- // 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
+ */
227
334
  async put_all(type, data) {
228
335
  try {
229
336
  if (this.debug) console.log("put_all", type);
@@ -244,6 +351,13 @@ class JXPHelper {
244
351
  }
245
352
  }
246
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
+ */
247
361
  async count(type, opts) {
248
362
  const label = `count.${type}-${this._randomString()}`;
249
363
  if (this.debug) console.time(label);
@@ -264,6 +378,13 @@ class JXPHelper {
264
378
  }
265
379
  }
266
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
+ */
267
388
  async post(type, data) {
268
389
  var url = `${this.api}/${type}?apikey=${this.apikey}`;
269
390
  if (this.debug) console.log("POSTing to ", url, data);
@@ -275,6 +396,16 @@ class JXPHelper {
275
396
  }
276
397
  }
277
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
+ */
278
409
  async put(type, id, data) {
279
410
  var url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
280
411
  if (this.debug) console.log("PUTting to ", url, data);
@@ -286,6 +417,16 @@ class JXPHelper {
286
417
  }
287
418
  }
288
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
+ */
289
430
  async postput(type, key, data) {
290
431
  // Post if we find key=id, else put
291
432
  var obj = {};
@@ -303,7 +444,15 @@ class JXPHelper {
303
444
  throw(err.response ? err.response.data : err);
304
445
  }
305
446
  }
306
-
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
+ */
307
456
  async del(type, id) {
308
457
  const url = `${this.api}/${type}/${id}?apikey=${this.apikey}`;
309
458
  try {
@@ -314,7 +463,15 @@ class JXPHelper {
314
463
  }
315
464
  }
316
465
 
317
- // 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
+ */
318
475
  async del_perm(type, id) {
319
476
  const url = `${this.api}/${type}/${id}?_permaDelete=1&apikey=${this.apikey}`;
320
477
  try {
@@ -325,6 +482,13 @@ class JXPHelper {
325
482
  }
326
483
  }
327
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
+ */
328
492
  async del_cascade(type, id) {
329
493
  var url = `${this.api}/${type}/${id}?_cascade=1&apikey=${this.apikey}`;
330
494
  try {
@@ -335,6 +499,13 @@ class JXPHelper {
335
499
  }
336
500
  }
337
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
+ */
338
509
  async del_perm_cascade(type, id) {
339
510
  var url = `${this.api}/${type}/${id}?_cascade=1&_permaDelete=1&apikey=${this.apikey}`;
340
511
  try {
@@ -345,7 +516,16 @@ class JXPHelper {
345
516
  }
346
517
  }
347
518
 
348
- // 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
+ */
349
529
  async del_all(type, key, id) {
350
530
  var obj = {};
351
531
  obj[`filter[${key}]`] = id;
@@ -395,6 +575,15 @@ class JXPHelper {
395
575
  }
396
576
  }
397
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
+ */
398
587
  async call(type, cmd, data) {
399
588
  //Call a function in the model
400
589
  var url = `${this.server}/call/${type}/${cmd}?apikey=${this.apikey}`;
@@ -406,6 +595,14 @@ class JXPHelper {
406
595
  }
407
596
  }
408
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
+ */
409
606
  async groups_put(user_id, groups) {
410
607
  var url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
411
608
  try {
@@ -415,6 +612,14 @@ class JXPHelper {
415
612
  }
416
613
  }
417
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
+ */
418
623
  async groups_del(user_id, group) {
419
624
  var url = `${this.server}/groups/${user_id}?group=${group}&apikey=${this.apikey}`;
420
625
  try {
@@ -425,6 +630,14 @@ class JXPHelper {
425
630
  }
426
631
  }
427
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
+ */
428
641
  async groups_post(user_id, groups) {
429
642
  var url = `${this.server}/groups/${user_id}?apikey=${this.apikey}`;
430
643
  var data = { group: groups };
@@ -437,6 +650,13 @@ class JXPHelper {
437
650
  }
438
651
  }
439
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
+ */
440
660
  async getjwt(email) {
441
661
  try {
442
662
  const jwt = (await axios.post(`${ this.server }/login/getjwt?apikey=${ this.apikey }`, { email })).data;
@@ -448,6 +668,13 @@ class JXPHelper {
448
668
  }
449
669
  }
450
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
+ */
451
678
  async model(modelname) {
452
679
  try {
453
680
  const modeldef = (await axios.get(`${ this.server }/model/${ modelname }?apikey=${ this.apikey }`)).data;
@@ -459,6 +686,12 @@ class JXPHelper {
459
686
  }
460
687
  }
461
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
+ */
462
695
  async models() {
463
696
  try {
464
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.2",
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
  }