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