@stackfactor/client-api 1.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/lib/users.js ADDED
@@ -0,0 +1,622 @@
1
+ //
2
+ // User sepcific requests
3
+ //
4
+ import axios from "./axios";
5
+ import { removeNullProperties } from "../components/Utils";
6
+
7
+ export default {
8
+ axios: axios,
9
+
10
+ /**
11
+ * Add a new API Token
12
+ * @param {Date} expiration Expiration date of the token
13
+ * @param {String} authToken Authorization token
14
+ */
15
+ addAPIToken(name, expiration, authToken) {
16
+ return new Promise(function (resolve, reject) {
17
+ const requestData = removeNullProperties({
18
+ name: name,
19
+ expiration: expiration,
20
+ });
21
+ const addTokenRequest = axios.post(
22
+ `api/v1/users/addapitoken`,
23
+ requestData,
24
+ {
25
+ headers: { authorization: authToken },
26
+ }
27
+ );
28
+ addTokenRequest
29
+ .then((result) => {
30
+ if (result != null) {
31
+ resolve(result.data);
32
+ } else {
33
+ reject(process.env.ERROR_INVALID_INFORMATION);
34
+ }
35
+ })
36
+ .catch((error) => {
37
+ reject(error);
38
+ });
39
+ });
40
+ },
41
+
42
+ /**
43
+ * Confirm email address
44
+ *
45
+ * @param {String} validationCode The code was provided to the user in advance by email
46
+ *
47
+ */
48
+ confirmEmailAddress(validationCode) {
49
+ return new Promise(function (resolve, reject) {
50
+ const requestData = removeNullProperties({
51
+ validationCode: validationCode,
52
+ });
53
+ const confirmationRequest = axios.post(
54
+ "api/v1/users/confirmEmail",
55
+ requestData
56
+ );
57
+ confirmationRequest
58
+ .then(() => {
59
+ resolve();
60
+ })
61
+ .catch((error) => {
62
+ reject(error);
63
+ });
64
+ });
65
+ },
66
+
67
+ /**
68
+ *
69
+ * Create a new account (tenant). It returns a promise
70
+ *
71
+ * @param {String} email User's email address
72
+ * @param {String} firstName User's first name
73
+ * @param {String} lastName User's last name
74
+ * @param {String} password User's password
75
+ * @param {String} subSite Subsite where the new account will be created
76
+ * @param {String} inviteCode Invitation code received in advance from one of the account owners
77
+ * @param
78
+ */
79
+ createAccount(email, firstName, lastName, password, subSite, inviteCode) {
80
+ return new Promise(function (resolve, reject) {
81
+ if (
82
+ email.trim() === "" ||
83
+ firstName.trim() === "" ||
84
+ lastName.trim() === "" ||
85
+ password.trim() === "" ||
86
+ subSite.trim() === "" ||
87
+ inviteCode.trim() === ""
88
+ ) {
89
+ reject(process.env.ERROR_EMPTY_FIELDS);
90
+ } else {
91
+ const signupData = removeNullProperties({
92
+ email: email,
93
+ firstName: firstName,
94
+ inviteCode: inviteCode,
95
+ lastName: lastName,
96
+ password: password,
97
+ subSite: subSite,
98
+ });
99
+ let request = axios.post("api/v1/users/createAccount", signupData);
100
+ request
101
+ .then((result) => {
102
+ resolve(result.data.user);
103
+ })
104
+ .catch((error) => {
105
+ reject(error);
106
+ });
107
+ }
108
+ });
109
+ },
110
+
111
+ /**
112
+ * Create a new user account. It returns a promise
113
+ *
114
+ * @param {String} email User's email address
115
+ * @param {String} firstName User's first name
116
+ * @param {String} lastName User's last name
117
+ * @param {String} password User's password
118
+ * @param {String} token Invitation token provided to the user
119
+ *
120
+ */
121
+ createUserAccount(email, firstName, lastName, password, token) {
122
+ return new Promise(function (resolve, reject) {
123
+ if (
124
+ email.trim() === "" ||
125
+ firstName.trim() === "" ||
126
+ lastName.trim() === "" ||
127
+ password.trim() === "" ||
128
+ token.trim() === ""
129
+ ) {
130
+ reject(process.env.ERROR_EMPTY_FIELDS);
131
+ } else {
132
+ const signupData = removeNullProperties({
133
+ email: email,
134
+ firstName: firstName,
135
+ lastName: lastName,
136
+ password: password,
137
+ token: token,
138
+ });
139
+ let request = axios.post("api/v1/users/createUser", signupData);
140
+ request
141
+ .then((result) => {
142
+ resolve(result.data.user);
143
+ })
144
+ .catch((error) => {
145
+ reject(error);
146
+ });
147
+ }
148
+ });
149
+ },
150
+
151
+ /**
152
+ * Delete an existing API token
153
+ *
154
+ * @param {String} token Expiration date of the token
155
+ * @param {String} authToken Authorization token
156
+ */
157
+ deleteAPIToken(token, authToken) {
158
+ return new Promise(function (resolve, reject) {
159
+ const deleteTokenRequest = axios.delete(
160
+ `api/v1/users/user/token`,
161
+ removeNullProperties({
162
+ headers: { authorization: authToken },
163
+ data: {
164
+ token: token,
165
+ },
166
+ })
167
+ );
168
+ deleteTokenRequest
169
+ .then(() => {
170
+ resolve(token);
171
+ })
172
+ .catch((error) => {
173
+ reject(error);
174
+ });
175
+ });
176
+ },
177
+
178
+ /**
179
+ * Delete existing users
180
+ *
181
+ * @param {Array} userIds The ID of the users to be deleted
182
+ * @param {String} authToken Authorization token
183
+ */
184
+ deleteUsers(userIds, authToken) {
185
+ return new Promise(function (resolve, reject) {
186
+ const deleteTokenRequest = axios.delete(
187
+ `api/v1/users/`,
188
+ removeNullProperties({
189
+ headers: { authorization: authToken },
190
+ data: {
191
+ userIds: userIds,
192
+ },
193
+ })
194
+ );
195
+ deleteTokenRequest
196
+ .then((response) => {
197
+ resolve(response.data);
198
+ })
199
+ .catch((error) => {
200
+ reject(error);
201
+ });
202
+ });
203
+ },
204
+
205
+ /**
206
+ * Get all the API tokens
207
+ * @param {String} authToken Authorization token
208
+ */
209
+ getAPITokens(authToken) {
210
+ return new Promise(function (resolve, reject) {
211
+ const getTokensRequest = axios.get(`api/v1/users/getapitokens`, {
212
+ headers: { authorization: authToken },
213
+ });
214
+ getTokensRequest
215
+ .then((result) => {
216
+ resolve(result.data);
217
+ })
218
+ .catch((error) => {
219
+ reject(error);
220
+ });
221
+ });
222
+ },
223
+
224
+ /**
225
+ *
226
+ * Get the specified user account by Id. It returns a promise
227
+ *
228
+ * @param {String} id Id of the user for which information is being requested
229
+ *
230
+ */
231
+ getUserById(id) {
232
+ return new Promise(function (resolve, reject) {
233
+ const getUserInformationRequest = axios.get(`api/v1/users/user/${id}`);
234
+ getUserInformationRequest
235
+ .then((result) => {
236
+ if (result != null) {
237
+ resolve(result.data.user);
238
+ } else {
239
+ reject(process.env.ERROR_INVALID_INFORMATION);
240
+ }
241
+ })
242
+ .catch((error) => {
243
+ reject(error);
244
+ });
245
+ });
246
+ },
247
+
248
+ /**
249
+ * Get user information
250
+ * @param {String} userId User Id
251
+ * @param {String} category User information category
252
+ * @param {String} token Authorization token
253
+ */
254
+ getUserInformation(userId, category, token) {
255
+ return new Promise(function (resolve, reject) {
256
+ let confirmationRequest = axios.get(
257
+ `api/v1/users/user/${userId ? userId : 0}/${category ? category : "*"}`,
258
+ {
259
+ headers: { authorization: token },
260
+ }
261
+ );
262
+ confirmationRequest
263
+ .then((response) => {
264
+ resolve(response.data);
265
+ })
266
+ .catch((error) => {
267
+ reject(error);
268
+ });
269
+ });
270
+ },
271
+
272
+ /**
273
+ * Get the list of users
274
+ * @param {Object} filter Filter to select the users. Ex: {firstName : "John"}
275
+ * @param {Array} fields Fields to be loaded. Ex: ["firstName"]
276
+ * @param {String} token Authorization token
277
+ */
278
+ getUsers(filter, fields, token) {
279
+ return new Promise(function (resolve, reject) {
280
+ const requestData = removeNullProperties({
281
+ filter: filter,
282
+ fields: fields,
283
+ });
284
+ let confirmationRequest = axios.post(`api/v1/users/`, requestData, {
285
+ headers: { authorization: token },
286
+ });
287
+ confirmationRequest
288
+ .then((response) => {
289
+ resolve(response.data);
290
+ })
291
+ .catch((error) => {
292
+ reject(error);
293
+ });
294
+ });
295
+ },
296
+
297
+ /**
298
+ * Invite other users to join the same account
299
+ * @param {Array} invitees List of emails of the invitees
300
+ * @param {String} token Authorization token
301
+ */
302
+ inviteUsers(invitees, authToken) {
303
+ return new Promise(function (resolve, reject) {
304
+ const requestData = removeNullProperties({
305
+ invitees: invitees,
306
+ token: authToken,
307
+ });
308
+ let request = axios.post("api/v1/users/invite", requestData, {
309
+ headers: { authorization: authToken },
310
+ });
311
+ request
312
+ .then((response) => {
313
+ resolve(response.data);
314
+ })
315
+ .catch((error) => {
316
+ reject(error);
317
+ });
318
+ });
319
+ },
320
+
321
+ /**
322
+ * Login using email and password. It returns a promise
323
+ * @param {String} email User's email address
324
+ * @param {String} password User's password
325
+ * @param {boolean} rememberMe Set to true if the user wants his account to be saved on the current machine
326
+ */
327
+ login(email, password, rememberMe) {
328
+ return new Promise(function (resolve, reject) {
329
+ const requestData = removeNullProperties({
330
+ email: email,
331
+ password: password,
332
+ rememberMe: rememberMe,
333
+ });
334
+ let request = axios.post("api/v1/auth/login", requestData);
335
+ request
336
+ .then((response) => {
337
+ resolve(response.data);
338
+ })
339
+ .catch((error) => {
340
+ reject(error);
341
+ });
342
+ });
343
+ },
344
+
345
+ /**
346
+ * Logout from the server. It returns a promise
347
+ * @param {String} token Authorization token
348
+ */
349
+ logout(token) {
350
+ return new Promise(function (resolve, reject) {
351
+ const options = removeNullProperties({
352
+ headers: { authorization: token },
353
+ });
354
+ let request = axios.post("api/v1/auth/logout", {}, options);
355
+ request
356
+ .then((response) => {
357
+ resolve(response.data);
358
+ })
359
+ .catch((error) => {
360
+ reject(error);
361
+ });
362
+ });
363
+ },
364
+
365
+ /**
366
+ * Checks the current auth token and maintain it alive. It returns a promise. In case the token is invalid
367
+ * a new token can be restablished using login
368
+ * @param {String} token User's auth token to be refreshed
369
+ */
370
+ refreshToken(token) {
371
+ return new Promise(function (resolve, reject) {
372
+ let options = removeNullProperties({ headers: { Authorization: token } });
373
+ let request = axios.post("api/v1/auth/refreshToken", {}, options);
374
+ request
375
+ .then((response) => {
376
+ resolve(response.data);
377
+ })
378
+ .catch((error) => {
379
+ reject(error);
380
+ });
381
+ });
382
+ },
383
+
384
+ /**
385
+ * Remove an existing API token
386
+ * @param {String} id The ID of the token to be removed
387
+ * @param {String} authToken Authorization token
388
+ */
389
+ removeAPIToken(id, authToken) {
390
+ return new Promise(function (resolve, reject) {
391
+ const requestData = removeNullProperties({
392
+ id: id,
393
+ });
394
+ const removeTokenRequest = axios.post(
395
+ `api/v1/users/removeapitoken`,
396
+ requestData,
397
+ {
398
+ headers: { authorization: authToken },
399
+ }
400
+ );
401
+ removeTokenRequest
402
+ .then(() => {
403
+ resolve(id);
404
+ })
405
+ .catch((error) => {
406
+ reject(error);
407
+ });
408
+ });
409
+ },
410
+
411
+ /**
412
+ * Resend invitation emails
413
+ * @param {Array} invitees List of emails of the invitees
414
+ * @param {String} token Authorization token
415
+ */
416
+ resendInvitationEmails(invitees, authToken) {
417
+ return new Promise(function (resolve, reject) {
418
+ const requestData = removeNullProperties({
419
+ invitees: invitees,
420
+ token: authToken,
421
+ });
422
+ let request = axios.post("api/v1/users/resendinvite", requestData, {
423
+ headers: { authorization: authToken },
424
+ });
425
+ request
426
+ .then((response) => {
427
+ resolve(response.data);
428
+ })
429
+ .catch((error) => {
430
+ reject(error);
431
+ });
432
+ });
433
+ },
434
+
435
+ /**
436
+ * Reset the password. It returns a promise
437
+ * @param {String} code Code provided to the user by email
438
+ * @param {String} email User's email address
439
+ * @param {String} password User's new password
440
+ */
441
+ resetPassword(email, code, password) {
442
+ return new Promise(function (resolve, reject) {
443
+ let postData = removeNullProperties({
444
+ email: email,
445
+ code: code,
446
+ password: password,
447
+ });
448
+ let request = axios.post("api/v1/users/resetpassword", postData);
449
+ request
450
+ .then((response) => {
451
+ resolve(response.data);
452
+ })
453
+ .catch((error) => {
454
+ reject(error);
455
+ });
456
+ });
457
+ },
458
+
459
+ /**
460
+ * Send an email to the user indicated by the email address to reset the password. It returns a promise
461
+ * @param {String} email Email of the user who needs to reset the password
462
+ */
463
+ sendPasswordResetNotification(email) {
464
+ return new Promise(function (resolve, reject) {
465
+ let postData = removeNullProperties({
466
+ email: email,
467
+ });
468
+ let request = axios.post(
469
+ "api/v1/users/sendpasswordresetnotification",
470
+ postData
471
+ );
472
+ request
473
+ .then((response) => {
474
+ resolve(response.data);
475
+ })
476
+ .catch((error) => {
477
+ reject(error);
478
+ });
479
+ });
480
+ },
481
+
482
+ /**
483
+ * Set user profile information
484
+ * @param {String} userId User Id
485
+ * @param {String} category User information category
486
+ * @param {String} data New or updated user data information
487
+ * @param {Object} token Authorization token
488
+ */
489
+ setUserInformation(userId, category, data, token) {
490
+ return new Promise(function (resolve, reject) {
491
+ const requestData = removeNullProperties({
492
+ data: category ? { category: { ...data } } : data,
493
+ userId: userId,
494
+ });
495
+ let confirmationRequest = axios.post("api/v1/users/user", requestData, {
496
+ headers: { authorization: token },
497
+ });
498
+ confirmationRequest
499
+ .then((response) => {
500
+ resolve(response.data);
501
+ })
502
+ .catch((error) => {
503
+ reject(error);
504
+ });
505
+ });
506
+ },
507
+
508
+ /**
509
+ * Update user email
510
+ * @param {String} email The new email address
511
+ * @param {String} password The current password
512
+ * @param {String} token Authorization token
513
+ */
514
+ updateUserEmail(email, password, token) {
515
+ return new Promise(function (resolve, reject) {
516
+ const requestData = removeNullProperties({
517
+ email: email,
518
+ password: password,
519
+ });
520
+ let confirmationRequest = axios.post(
521
+ "api/v1/users/updateemail",
522
+ requestData,
523
+ {
524
+ headers: { authorization: token },
525
+ }
526
+ );
527
+ confirmationRequest
528
+ .then((response) => {
529
+ resolve(response.data);
530
+ })
531
+ .catch((error) => {
532
+ reject(error);
533
+ });
534
+ });
535
+ },
536
+
537
+ /**
538
+ * Update user groups
539
+ * @param {String} userIds The id of the user
540
+ * @param {Array<String>} groups The id of the groups the user should belong to
541
+ * @param {String} token Authorization token
542
+ */
543
+ updateUserGroups(userId, groups, token) {
544
+ return new Promise(function (resolve, reject) {
545
+ const requestData = removeNullProperties({
546
+ userId: userId,
547
+ groups: groups ? groups : [],
548
+ });
549
+ let confirmationRequest = axios.post(
550
+ "api/v1/users/user/updategroups",
551
+ requestData,
552
+ {
553
+ headers: { authorization: token },
554
+ }
555
+ );
556
+ confirmationRequest
557
+ .then(() => {
558
+ resolve(requestData);
559
+ })
560
+ .catch((error) => {
561
+ reject(error);
562
+ });
563
+ });
564
+ },
565
+
566
+ /**
567
+ * Update user password
568
+ * @param {String} password The current password
569
+ * @param {String} newPassword The new password
570
+ * @param {String} token Authorization token
571
+ */
572
+ updateUserPassword(password, newPassword, token) {
573
+ return new Promise(function (resolve, reject) {
574
+ const requestData = removeNullProperties({
575
+ password: password,
576
+ newPassword: newPassword,
577
+ });
578
+ let confirmationRequest = axios.post(
579
+ "api/v1/users/updatepassword",
580
+ requestData,
581
+ {
582
+ headers: { authorization: token },
583
+ }
584
+ );
585
+ confirmationRequest
586
+ .then((response) => {
587
+ resolve(response.data);
588
+ })
589
+ .catch((error) => {
590
+ reject(error);
591
+ });
592
+ });
593
+ },
594
+
595
+ /**
596
+ *
597
+ * Validate reset password code. It returns a promise
598
+ *
599
+ * @param {String} email User's email address
600
+ * @param {String} code Provided reset code
601
+ *
602
+ */
603
+ validateResetPasswordCode(email, code) {
604
+ return new Promise(function (resolve, reject) {
605
+ let postData = removeNullProperties({
606
+ email: email,
607
+ code: code,
608
+ });
609
+ let request = axios.post(
610
+ "api/v1/users/validateresetpasswordcode",
611
+ postData
612
+ );
613
+ request
614
+ .then((response) => {
615
+ resolve(response.data);
616
+ })
617
+ .catch((error) => {
618
+ reject(error);
619
+ });
620
+ });
621
+ },
622
+ };
package/lib/utils.js ADDED
@@ -0,0 +1,27 @@
1
+ //
2
+ // Utilities - various generla functions
3
+ //
4
+
5
+ const utils = {
6
+ /**
7
+ * Convert object to array
8
+ */
9
+ objectToArray: (data) => {
10
+ if (typeof data === "object") {
11
+ return [...data];
12
+ } else throw new Error("Invalid type");
13
+ },
14
+
15
+ /**
16
+ * Returns the backend base API URL
17
+ */
18
+ getBaseUrl: () => {
19
+ if (!process.env.NODE_ENV || process.env.NODE_ENV === "production") {
20
+ return "https://api.stackfactor.io/";
21
+ } else {
22
+ return "http://localhost:3100/";
23
+ }
24
+ },
25
+ };
26
+
27
+ export default utils;
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@stackfactor/client-api",
3
+ "version": "1.0.0",
4
+ "description": "StackFactor Client API",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/StackFactor/stackfactor-client-api.git"
12
+ },
13
+ "author": "StackFactor Inc",
14
+ "license": "ISC",
15
+ "bugs": {
16
+ "url": "https://github.com/StackFactor/stackfactor-client-api/issues"
17
+ },
18
+ "homepage": "https://github.com/StackFactor/stackfactor-client-api#readme",
19
+ "dependencies": {
20
+ "axios": "^1.4.0",
21
+ "html2plaintext": "^2.1.4",
22
+ "node-html-parser": "^6.1.5"
23
+ }
24
+ }