@nomad-media/public 1.1.13 → 1.1.15
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/README.md +45 -14
- package/dist/nomad-media-sdk-debug.js +293 -227
- package/dist/nomad-media-sdk.min.js +6 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
This package contains the public version of the Nomad Media SDK. It includes all of the media methods of the SDK.
|
|
1
|
+
## Prerequisites
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
- Node.js 12.0.0
|
|
3
|
+
- Node package manager (npm).
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
> 📘 Note
|
|
6
|
+
>
|
|
7
|
+
> You can download npm [here](https://nodejs.org/en/download).
|
|
9
8
|
|
|
9
|
+
## Setup
|
|
10
|
+
|
|
11
|
+
To use the Nomad SDK, download the @nomad-media npm. There are two versions, the full version which includes all of the Nomad-Media endpoints, and the public version which only includes the media endpoints.
|
|
12
|
+
|
|
13
|
+
To install the full version, use the following command:
|
|
14
|
+
```shell
|
|
15
|
+
npm install @nomad-media/full
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
To install the public version, use the following command:
|
|
10
19
|
```shell
|
|
11
20
|
npm install @nomad-media/public
|
|
12
21
|
```
|
|
13
22
|
|
|
14
|
-
Then to import it, you can either use the default version, or the debug version. The difference is that the debug version isn't compressed and has more documentation and named parameters.
|
|
23
|
+
Then to import it, you can either use the default version, or the debug version. The difference is that the debug version isn't compressed and has more documentation and named parameters. Both full and public have a debug version. Currently the debug version's tooltips don't show up. This is an issue with npm.
|
|
15
24
|
|
|
16
25
|
To use the default version use:
|
|
17
26
|
|
|
18
27
|
```javascript
|
|
19
|
-
import NomadMediaSDK from "@nomad-media/
|
|
28
|
+
import NomadMediaSDK from "@nomad-media/full";
|
|
20
29
|
```
|
|
21
30
|
|
|
22
31
|
To use the debug version user:
|
|
23
32
|
|
|
24
33
|
```javascript
|
|
25
|
-
import NomadMediaSDK from "@nomad-media/
|
|
34
|
+
import NomadMediaSDK from "@nomad-media/full/debug";
|
|
26
35
|
```
|
|
27
36
|
|
|
28
37
|
To initialize the sdk, call the NomadMediaSDK class as shown below:
|
|
@@ -31,6 +40,10 @@ To initialize the sdk, call the NomadMediaSDK class as shown below:
|
|
|
31
40
|
const SDK = NomadMediaSDK(config);
|
|
32
41
|
```
|
|
33
42
|
|
|
43
|
+
## Logging in
|
|
44
|
+
|
|
45
|
+
To log in to the sdk, in the config, enter the username/email of your user. Then enter your api key if you are using an api key, or your password.
|
|
46
|
+
|
|
34
47
|
## Configure Environmental Variables
|
|
35
48
|
|
|
36
49
|
Follow this format for the config:
|
|
@@ -39,24 +52,42 @@ Follow this format for the config:
|
|
|
39
52
|
const config = {
|
|
40
53
|
"username": "username",
|
|
41
54
|
"password": "password",
|
|
55
|
+
"apiKey": "apiKey",
|
|
42
56
|
"serviceApiUrl": "serverApiUrl",
|
|
43
57
|
"apiType": "admin",
|
|
44
|
-
"debugMode": true
|
|
58
|
+
"debugMode": true,
|
|
59
|
+
"singleton": false
|
|
45
60
|
};
|
|
46
61
|
```
|
|
47
|
-
serviceApiUrl: Specifies the url of the server you are trying to connect to.
|
|
48
62
|
|
|
49
63
|
apiType: Specifies whether the function you are trying to run is **admin** or **portal**.
|
|
50
64
|
|
|
51
65
|
debugMode: Specifies when running functions, whether of not to print api call information.
|
|
52
66
|
|
|
67
|
+
singleton: Whether or not to only create one class instance.
|
|
68
|
+
|
|
69
|
+
Place the config in a file called config.js and import it into your project.
|
|
70
|
+
|
|
53
71
|
You are now ready to use the SDK in your project.
|
|
54
72
|
|
|
55
73
|
> 📘 Contact: Make sure to contact Nomad Media support to get username, password, and url for your enviromnent.
|
|
56
74
|
|
|
57
75
|
> 🚧 Make sure to keep your configuration options secret and do not share them publicly.
|
|
58
76
|
|
|
59
|
-
##
|
|
77
|
+
## Upgrading NPM
|
|
78
|
+
|
|
79
|
+
To upgrade the npm, use the following command:
|
|
80
|
+
|
|
81
|
+
```shell
|
|
82
|
+
npm update @nomad-media/full
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Nomad SDK References
|
|
86
|
+
|
|
87
|
+
An overview of all of the Nomad SDK methods. Includes prameter details for the methods. Scroll down to the Nomad SDK section.
|
|
88
|
+
|
|
89
|
+
[Nomad SDK References](https://developer.nomad-cms.com/reference)
|
|
90
|
+
|
|
91
|
+
## Nomad SDK NPM Samples
|
|
60
92
|
|
|
61
|
-
|
|
62
|
-
- [Nomad SDK NPM Samples](https://github.com/Nomad-Media/samples-js)
|
|
93
|
+
[Nomad SDK NPM Samples](https://github.com/Nomad-Media/samples-js)
|
|
@@ -131,6 +131,10 @@
|
|
|
131
131
|
|
|
132
132
|
|
|
133
133
|
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
134
138
|
|
|
135
139
|
|
|
136
140
|
|
|
@@ -249,6 +253,14 @@
|
|
|
249
253
|
|
|
250
254
|
|
|
251
255
|
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
252
264
|
|
|
253
265
|
|
|
254
266
|
|
|
@@ -263,8 +275,6 @@
|
|
|
263
275
|
|
|
264
276
|
|
|
265
277
|
|
|
266
|
-
import { promises as fs } from 'fs';
|
|
267
|
-
import path from 'path';
|
|
268
278
|
|
|
269
279
|
/**
|
|
270
280
|
* @class NomadSDK
|
|
@@ -284,14 +294,14 @@ class NomadMediaSDK {
|
|
|
284
294
|
this.expirationSeconds = null;
|
|
285
295
|
this.userSessionId = null;
|
|
286
296
|
this.id = null
|
|
287
|
-
this.
|
|
297
|
+
this.debug = config.debugMode || false;
|
|
288
298
|
|
|
289
299
|
if (this.config.serviceApiUrl[this.config.serviceApiUrl.length - 1] === "/")
|
|
290
300
|
{
|
|
291
301
|
this.config.serviceApiUrl = this.config.serviceApiUrl.slice(0, -1);
|
|
292
302
|
}
|
|
293
303
|
|
|
294
|
-
NomadMediaSDK.instance = this;
|
|
304
|
+
if (this.config.singleton) NomadMediaSDK.instance = this;
|
|
295
305
|
|
|
296
306
|
return this;
|
|
297
307
|
}
|
|
@@ -307,7 +317,10 @@ class NomadMediaSDK {
|
|
|
307
317
|
*/
|
|
308
318
|
async _init()
|
|
309
319
|
{
|
|
310
|
-
|
|
320
|
+
if (this.config.username && this.config.password)
|
|
321
|
+
{
|
|
322
|
+
await this.login();
|
|
323
|
+
}
|
|
311
324
|
}
|
|
312
325
|
|
|
313
326
|
// account functions
|
|
@@ -316,7 +329,7 @@ class NomadMediaSDK {
|
|
|
316
329
|
* @async
|
|
317
330
|
* @description Logs in using the username and password from the config.
|
|
318
331
|
* @returns {Promise<void>} - A promise that resolves when the login is successful.
|
|
319
|
-
* Updates the token, refresh token, expiration seconds, and user session ID class
|
|
332
|
+
* Updates the token, refresh token, expiration seconds, and user session ID class variables.
|
|
320
333
|
* @throws {Error} - An error is thrown if the login fails.
|
|
321
334
|
*/
|
|
322
335
|
async login()
|
|
@@ -326,8 +339,9 @@ class NomadMediaSDK {
|
|
|
326
339
|
|
|
327
340
|
try
|
|
328
341
|
{
|
|
329
|
-
const LOGIN_INFO = await _login(this
|
|
330
|
-
|
|
342
|
+
const LOGIN_INFO = await _login(this);
|
|
343
|
+
if (!LOGIN_INFO) throw new Error("Login failed");
|
|
344
|
+
|
|
331
345
|
this.token = LOGIN_INFO["token"];
|
|
332
346
|
this.refreshTokenVal = LOGIN_INFO["refreshToken"];
|
|
333
347
|
this.expirationSeconds = LOGIN_INFO["expirationSeconds"];
|
|
@@ -360,7 +374,7 @@ class NomadMediaSDK {
|
|
|
360
374
|
|
|
361
375
|
try
|
|
362
376
|
{
|
|
363
|
-
await _logout(this
|
|
377
|
+
await _logout(this);
|
|
364
378
|
this.token = null;
|
|
365
379
|
this.refreshTokenVal = null;
|
|
366
380
|
this.expirationSeconds = null;
|
|
@@ -371,7 +385,7 @@ class NomadMediaSDK {
|
|
|
371
385
|
}
|
|
372
386
|
catch (error)
|
|
373
387
|
{
|
|
374
|
-
_printDatetime("
|
|
388
|
+
_printDatetime("Logout failed");
|
|
375
389
|
throw error;
|
|
376
390
|
}
|
|
377
391
|
}
|
|
@@ -388,10 +402,14 @@ class NomadMediaSDK {
|
|
|
388
402
|
{
|
|
389
403
|
_printDatetime(`Refreshing token`);
|
|
390
404
|
|
|
405
|
+
if (this.token === null && this.config.username)
|
|
406
|
+
{
|
|
407
|
+
await this._init();
|
|
408
|
+
}
|
|
409
|
+
|
|
391
410
|
try
|
|
392
411
|
{
|
|
393
|
-
const REFRESHED = await _refreshToken(this
|
|
394
|
-
this.refreshTokenVal, this.debugMode);
|
|
412
|
+
const REFRESHED = await _refreshToken(this);
|
|
395
413
|
if (REFRESHED) _printDatetime(`Token refresh successful`);
|
|
396
414
|
}
|
|
397
415
|
catch (error)
|
|
@@ -418,7 +436,7 @@ class NomadMediaSDK {
|
|
|
418
436
|
|
|
419
437
|
try
|
|
420
438
|
{
|
|
421
|
-
await _forgotPassword(this
|
|
439
|
+
await _forgotPassword(this);
|
|
422
440
|
_printDatetime(`Code sent to ${USERNAME}`);
|
|
423
441
|
}
|
|
424
442
|
catch (error)
|
|
@@ -447,8 +465,7 @@ class NomadMediaSDK {
|
|
|
447
465
|
|
|
448
466
|
try
|
|
449
467
|
{
|
|
450
|
-
await _resetPassword(this,
|
|
451
|
-
this.debugMode);
|
|
468
|
+
await _resetPassword(this, CODE, NEW_PASSWORD);
|
|
452
469
|
_printDatetime(`Password reset for ${USERNAME}`);
|
|
453
470
|
}
|
|
454
471
|
catch (error)
|
|
@@ -487,8 +504,7 @@ class NomadMediaSDK {
|
|
|
487
504
|
|
|
488
505
|
try
|
|
489
506
|
{
|
|
490
|
-
await _clearContinueWatching(this,
|
|
491
|
-
this.debugMode);
|
|
507
|
+
await _clearContinueWatching(this, USER_ID, ASSET_ID);
|
|
492
508
|
_printDatetime(`Continue watching cleared`);
|
|
493
509
|
}
|
|
494
510
|
catch (error)
|
|
@@ -526,7 +542,7 @@ class NomadMediaSDK {
|
|
|
526
542
|
{
|
|
527
543
|
USER_ID = USER_ID || this.id;
|
|
528
544
|
|
|
529
|
-
await _clearWatchlist(this,
|
|
545
|
+
await _clearWatchlist(this, USER_ID);
|
|
530
546
|
_printDatetime(`Watchlist cleared`);
|
|
531
547
|
}
|
|
532
548
|
catch (error)
|
|
@@ -565,8 +581,7 @@ class NomadMediaSDK {
|
|
|
565
581
|
|
|
566
582
|
try
|
|
567
583
|
{
|
|
568
|
-
const GET_FORMS_INFO = await _createForm(this,
|
|
569
|
-
CONTENT_DEFINITION_ID, FORM_INFO, this.debugMode);
|
|
584
|
+
const GET_FORMS_INFO = await _createForm(this, CONTENT_DEFINITION_ID, FORM_INFO);
|
|
570
585
|
if (GET_FORMS_INFO) _printDatetime(`Form created`);
|
|
571
586
|
return GET_FORMS_INFO;
|
|
572
587
|
}
|
|
@@ -605,8 +620,7 @@ class NomadMediaSDK {
|
|
|
605
620
|
|
|
606
621
|
try
|
|
607
622
|
{
|
|
608
|
-
const GET_CONTENT_COOKIES_INFO = await _getContentCookies(this,
|
|
609
|
-
this.config.serviceApiUrl, CONTENT_ID, this.debugMode);
|
|
623
|
+
const GET_CONTENT_COOKIES_INFO = await _getContentCookies(this, CONTENT_ID);
|
|
610
624
|
if (GET_CONTENT_COOKIES_INFO) _printDatetime(`Content cookies gotten`);
|
|
611
625
|
return GET_CONTENT_COOKIES_INFO;
|
|
612
626
|
}
|
|
@@ -642,8 +656,7 @@ class NomadMediaSDK {
|
|
|
642
656
|
|
|
643
657
|
try
|
|
644
658
|
{
|
|
645
|
-
const GET_DYNAMIC_CONTENTS_INFO = await _getDefaultSiteConfig(this
|
|
646
|
-
this.config.serviceApiUrl, this.debugMode);
|
|
659
|
+
const GET_DYNAMIC_CONTENTS_INFO = await _getDefaultSiteConfig(this);
|
|
647
660
|
if (GET_DYNAMIC_CONTENTS_INFO) _printDatetime(`Default site config gotten`);
|
|
648
661
|
return GET_DYNAMIC_CONTENTS_INFO;
|
|
649
662
|
}
|
|
@@ -675,8 +688,7 @@ class NomadMediaSDK {
|
|
|
675
688
|
|
|
676
689
|
try
|
|
677
690
|
{
|
|
678
|
-
const GET_DYNAMIC_CONTENT_INFO = await _getDynamicContent(this,
|
|
679
|
-
this.config.serviceApiUrl, DYNAMIC_CONTENT_RECORD_ID, this.debugMode);
|
|
691
|
+
const GET_DYNAMIC_CONTENT_INFO = await _getDynamicContent(this, DYNAMIC_CONTENT_RECORD_ID);
|
|
680
692
|
if (GET_DYNAMIC_CONTENT_INFO) _printDatetime(`Dynamic content gotten: ${DYNAMIC_CONTENT_RECORD_ID}`);
|
|
681
693
|
return GET_DYNAMIC_CONTENT_INFO;
|
|
682
694
|
}
|
|
@@ -712,8 +724,7 @@ class NomadMediaSDK {
|
|
|
712
724
|
|
|
713
725
|
try
|
|
714
726
|
{
|
|
715
|
-
const GET_DYNAMIC_CONTENTS_INFO = await _getDynamicContents(this
|
|
716
|
-
this.config.serviceApiUrl, this.debugMode);
|
|
727
|
+
const GET_DYNAMIC_CONTENTS_INFO = await _getDynamicContents(this);
|
|
717
728
|
if (GET_DYNAMIC_CONTENTS_INFO) _printDatetime(`Dynamic contents gotten`);
|
|
718
729
|
return GET_DYNAMIC_CONTENTS_INFO;
|
|
719
730
|
}
|
|
@@ -729,12 +740,13 @@ class NomadMediaSDK {
|
|
|
729
740
|
* @async
|
|
730
741
|
* @description Gets media group.
|
|
731
742
|
* @param {string} MEDIA_GROUP_ID - The ID of the media group.
|
|
743
|
+
* @param { Array<string> } FILTER_IDS - The IDs of the media group items to filter by.
|
|
732
744
|
* @returns {Promise<JSON>} - A promise that resolves when the media group is gotten.
|
|
733
745
|
* Returns the information of the gotten media group.
|
|
734
746
|
* @throws {Error} - An error is thrown if the media group fails to get.
|
|
735
747
|
* @throws {Error} - An error is thrown if the API type is not portal.
|
|
736
748
|
*/
|
|
737
|
-
async getMediaGroup(MEDIA_GROUP_ID)
|
|
749
|
+
async getMediaGroup(MEDIA_GROUP_ID, FILTER_IDS)
|
|
738
750
|
{
|
|
739
751
|
if (this.token === null && this.config.username)
|
|
740
752
|
{
|
|
@@ -750,8 +762,7 @@ class NomadMediaSDK {
|
|
|
750
762
|
|
|
751
763
|
try
|
|
752
764
|
{
|
|
753
|
-
const GET_MEDIA_GROUP_INFO = await _getMediaGroup(this,
|
|
754
|
-
MEDIA_GROUP_ID, this.debugMode);
|
|
765
|
+
const GET_MEDIA_GROUP_INFO = await _getMediaGroup(this, MEDIA_GROUP_ID, FILTER_IDS);
|
|
755
766
|
if (GET_MEDIA_GROUP_INFO) _printDatetime(`Media group gotten: ${MEDIA_GROUP_ID}`);
|
|
756
767
|
return GET_MEDIA_GROUP_INFO;
|
|
757
768
|
}
|
|
@@ -788,8 +799,7 @@ class NomadMediaSDK {
|
|
|
788
799
|
|
|
789
800
|
try
|
|
790
801
|
{
|
|
791
|
-
const GET_MEDIA_ITEM_INFO = await _getMediaItem(this,
|
|
792
|
-
MEDIA_ITEM_ID, this.debugMode);
|
|
802
|
+
const GET_MEDIA_ITEM_INFO = await _getMediaItem(this, MEDIA_ITEM_ID);
|
|
793
803
|
if (GET_MEDIA_ITEM_INFO) _printDatetime(`Media item gotten: ${MEDIA_ITEM_ID}`);
|
|
794
804
|
return GET_MEDIA_ITEM_INFO;
|
|
795
805
|
}
|
|
@@ -827,8 +837,7 @@ class NomadMediaSDK {
|
|
|
827
837
|
|
|
828
838
|
try
|
|
829
839
|
{
|
|
830
|
-
const GET_MY_CONTENT_INFO = await _getMyContent(this
|
|
831
|
-
this.debugMode);
|
|
840
|
+
const GET_MY_CONTENT_INFO = await _getMyContent(this);
|
|
832
841
|
if (GET_MY_CONTENT_INFO) _printDatetime(`My content gotten`);
|
|
833
842
|
return GET_MY_CONTENT_INFO;
|
|
834
843
|
}
|
|
@@ -860,8 +869,7 @@ class NomadMediaSDK {
|
|
|
860
869
|
|
|
861
870
|
try
|
|
862
871
|
{
|
|
863
|
-
const GET_MY_GROUP_INFO = await _getMyGroup(this,
|
|
864
|
-
GROUP_ID, this.debugMode);
|
|
872
|
+
const GET_MY_GROUP_INFO = await _getMyGroup(this, GROUP_ID);
|
|
865
873
|
if (GET_MY_GROUP_INFO) _printDatetime(`My group gotten: ${GROUP_ID}`);
|
|
866
874
|
return GET_MY_GROUP_INFO;
|
|
867
875
|
}
|
|
@@ -893,8 +901,7 @@ class NomadMediaSDK {
|
|
|
893
901
|
|
|
894
902
|
try
|
|
895
903
|
{
|
|
896
|
-
const GET_DYNAMIC_CONTENT_INFO = await _getSiteConfig(this,
|
|
897
|
-
this.config.serviceApiUrl, SITE_CONFIG_RECORD_ID, this.debugMode);
|
|
904
|
+
const GET_DYNAMIC_CONTENT_INFO = await _getSiteConfig(this, SITE_CONFIG_RECORD_ID);
|
|
898
905
|
if (GET_DYNAMIC_CONTENT_INFO) _printDatetime(`Site config gotten: ${SITE_CONFIG_RECORD_ID}`);
|
|
899
906
|
return GET_DYNAMIC_CONTENT_INFO;
|
|
900
907
|
}
|
|
@@ -936,8 +943,7 @@ class NomadMediaSDK {
|
|
|
936
943
|
|
|
937
944
|
try
|
|
938
945
|
{
|
|
939
|
-
const MEDIA_SEARCH_INFO = await _mediaSearch(this,
|
|
940
|
-
QUERY, IDS, SORT_FIELDS, OFFSET, SIZE, this.debugMode);
|
|
946
|
+
const MEDIA_SEARCH_INFO = await _mediaSearch(this, QUERY, IDS, SORT_FIELDS, OFFSET, SIZE);
|
|
941
947
|
if (MEDIA_SEARCH_INFO) _printDatetime(`Media search complete`);
|
|
942
948
|
return MEDIA_SEARCH_INFO;
|
|
943
949
|
}
|
|
@@ -950,7 +956,7 @@ class NomadMediaSDK {
|
|
|
950
956
|
|
|
951
957
|
// misc functions
|
|
952
958
|
/**
|
|
953
|
-
* @function
|
|
959
|
+
* @function miscFunctions
|
|
954
960
|
* @async
|
|
955
961
|
* @description Calls any nomad function given URL method and body.
|
|
956
962
|
* @param {string} URL_PATH - The URL path of the function.
|
|
@@ -988,11 +994,7 @@ class NomadMediaSDK {
|
|
|
988
994
|
HEADERS.append("Authorization", `Bearer ${this.token}`);
|
|
989
995
|
}
|
|
990
996
|
|
|
991
|
-
if (this.
|
|
992
|
-
{
|
|
993
|
-
console.log(`URL: ${API_URL}\nMETHOD: ${METHOD}`);
|
|
994
|
-
if (BODY) console.log(`BODY: ${JSON.stringify(BODY, null, 4 )}`);
|
|
995
|
-
}
|
|
997
|
+
if (this.debug) console.log(`URL: ${API_URL}\nMETHOD: ${METHOD}` + (BODY ? `\nBODY: ${JSON.stringify(BODY, null, 4)}` : ""));
|
|
996
998
|
|
|
997
999
|
const REQUEST = {
|
|
998
1000
|
method: METHOD,
|
|
@@ -1037,32 +1039,32 @@ export default NomadMediaSDK;
|
|
|
1037
1039
|
|
|
1038
1040
|
|
|
1039
1041
|
|
|
1040
|
-
async function _forgotPassword(instance
|
|
1042
|
+
async function _forgotPassword(instance)
|
|
1041
1043
|
{
|
|
1042
|
-
const API_URL = `${
|
|
1044
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/account/forgot-password`;
|
|
1043
1045
|
|
|
1044
1046
|
const BODY = {
|
|
1045
|
-
username:
|
|
1047
|
+
username: instance.config.username
|
|
1046
1048
|
};
|
|
1047
1049
|
|
|
1048
|
-
return await _sendRequest(instance,
|
|
1050
|
+
return await _sendRequest(instance, "Forgot Password", API_URL, "POST", undefined, BODY);
|
|
1049
1051
|
}
|
|
1050
1052
|
|
|
1051
1053
|
|
|
1052
1054
|
|
|
1053
1055
|
|
|
1054
|
-
async function _login(instance
|
|
1056
|
+
async function _login(instance)
|
|
1055
1057
|
{
|
|
1056
|
-
const API_URL = `${
|
|
1058
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/account/login`;
|
|
1057
1059
|
|
|
1058
1060
|
const BODY = {
|
|
1059
|
-
"userName":
|
|
1060
|
-
"password":
|
|
1061
|
+
"userName": instance.config.username,
|
|
1062
|
+
"password": instance.config.password
|
|
1061
1063
|
};
|
|
1062
1064
|
|
|
1063
1065
|
try
|
|
1064
1066
|
{
|
|
1065
|
-
return await _sendRequest(instance,
|
|
1067
|
+
return await _sendRequest(instance, "Login", API_URL, "POST", undefined, BODY);
|
|
1066
1068
|
}
|
|
1067
1069
|
catch (error)
|
|
1068
1070
|
{
|
|
@@ -1081,11 +1083,11 @@ async function _login(instance, USERNAME, PASSWORD, URL, DEBUG_MODE)
|
|
|
1081
1083
|
|
|
1082
1084
|
|
|
1083
1085
|
|
|
1084
|
-
async function _logout(instance
|
|
1086
|
+
async function _logout(instance)
|
|
1085
1087
|
{
|
|
1086
|
-
const API_URL = `${
|
|
1088
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/account/logout`;
|
|
1087
1089
|
|
|
1088
|
-
return await _sendRequest(instance,
|
|
1090
|
+
return await _sendRequest(instance, "Logout", API_URL, "POST", undefined, undefined);
|
|
1089
1091
|
}
|
|
1090
1092
|
|
|
1091
1093
|
|
|
@@ -1094,15 +1096,15 @@ async function _logout(instance, AUTH_TOKEN, URL, DEBUG_MODE)
|
|
|
1094
1096
|
|
|
1095
1097
|
|
|
1096
1098
|
|
|
1097
|
-
async function _refreshToken(instance
|
|
1099
|
+
async function _refreshToken(instance)
|
|
1098
1100
|
{
|
|
1099
|
-
const API_URL = `${
|
|
1101
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/account/refresh-token`;
|
|
1100
1102
|
|
|
1101
1103
|
const BODY = {
|
|
1102
|
-
refreshToken:
|
|
1104
|
+
refreshToken: instance.refreshTokenVal
|
|
1103
1105
|
};
|
|
1104
1106
|
|
|
1105
|
-
const TOKEN_INFO = await _sendRequest(instance,
|
|
1107
|
+
const TOKEN_INFO = await _sendRequest(instance, "Refreshing Token", API_URL, "POST", undefined, BODY);
|
|
1106
1108
|
instance.token = TOKEN_INFO.token;
|
|
1107
1109
|
|
|
1108
1110
|
return true;
|
|
@@ -1112,74 +1114,76 @@ async function _refreshToken(instance, AUTH_TOKEN, URL, REFRESH_TOKEN, DEBUG_MOD
|
|
|
1112
1114
|
|
|
1113
1115
|
|
|
1114
1116
|
|
|
1115
|
-
async function _resetPassword(instance,
|
|
1117
|
+
async function _resetPassword(instance, CODE, NEW_PASSWORD)
|
|
1116
1118
|
{
|
|
1117
|
-
const API_URL = `${
|
|
1119
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/account/reset-password`;
|
|
1118
1120
|
|
|
1119
1121
|
const BODY = {
|
|
1120
|
-
userName:
|
|
1121
|
-
|
|
1122
|
+
userName: instance.username,
|
|
1123
|
+
token: CODE,
|
|
1122
1124
|
newPassword: NEW_PASSWORD
|
|
1123
1125
|
};
|
|
1124
1126
|
|
|
1125
|
-
return await _sendRequest(instance,
|
|
1127
|
+
return await _sendRequest(instance, "Reset Password", API_URL, "POST", undefined, BODY);
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
function _apiExceptionHandler(error, message)
|
|
1132
|
+
{
|
|
1133
|
+
// Check if we have a response object and error message
|
|
1134
|
+
if (!error)
|
|
1135
|
+
{
|
|
1136
|
+
console.error(message);
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// Check if error is a string
|
|
1140
|
+
if (typeof error === "string")
|
|
1141
|
+
{
|
|
1142
|
+
console.error(`${message}: ${error}`);
|
|
1143
|
+
}
|
|
1144
|
+
else if (error.errors)
|
|
1145
|
+
{
|
|
1146
|
+
if (Array.isArray(error.errors))
|
|
1147
|
+
{
|
|
1148
|
+
let errorString = "";
|
|
1149
|
+
for (let errorIdx = 0; errorIdx < error.errors.length; errorIdx++)
|
|
1150
|
+
{
|
|
1151
|
+
errorString += `${error.errors[errorIdx].code}: ${error.errors[errorIdx].field}: ${error.errors[errorIdx].message}\n`;
|
|
1152
|
+
}
|
|
1153
|
+
console.error(`${message}: ${errorString}`);
|
|
1154
|
+
}
|
|
1155
|
+
else if (typeof error.errors === "object")
|
|
1156
|
+
{
|
|
1157
|
+
let errorString = "";
|
|
1158
|
+
for (let key in error.errors)
|
|
1159
|
+
{
|
|
1160
|
+
if (Object.prototype.hasOwnProperty.call(error.errors, key))
|
|
1161
|
+
{
|
|
1162
|
+
errorString += `${key}: ${error.errors[key]}\n`;
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
console.error(`${message}: ${errorString}`);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
else
|
|
1169
|
+
{
|
|
1170
|
+
console.error(`${message}: ${error.message}`);
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
return null;
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
import { getRandomValues } from 'crypto';
|
|
1178
|
+
|
|
1179
|
+
function _newGuid()
|
|
1180
|
+
{
|
|
1181
|
+
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
|
|
1182
|
+
(c ^ (getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
|
|
1183
|
+
);
|
|
1126
1184
|
}
|
|
1127
1185
|
|
|
1128
1186
|
|
|
1129
|
-
function _apiExceptionHandler(error, message) {
|
|
1130
|
-
// Check if we have a response object and error message
|
|
1131
|
-
if (!error) {
|
|
1132
|
-
console.error(message);
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
// Check if error is a string
|
|
1136
|
-
if (typeof error === "string") {
|
|
1137
|
-
console.error(`${message}: ${error}`);
|
|
1138
|
-
}
|
|
1139
|
-
else if (error.errors)
|
|
1140
|
-
{
|
|
1141
|
-
if (Array.isArray(error.errors)) {
|
|
1142
|
-
let errorString = "";
|
|
1143
|
-
for (let errorIdx = 0; errorIdx < error.errors.length; errorIdx++)
|
|
1144
|
-
{
|
|
1145
|
-
errorString += `${error.errors[errorIdx].code}: ${error.errors[errorIdx].field}: ${error.errors[errorIdx].message}\n`;
|
|
1146
|
-
}
|
|
1147
|
-
console.error(`${message}: ${errorString}`);
|
|
1148
|
-
}
|
|
1149
|
-
else if (typeof error.errors === "object") {
|
|
1150
|
-
let errorString = "";
|
|
1151
|
-
for (let key in error.errors) {
|
|
1152
|
-
if (Object.prototype.hasOwnProperty.call(error.errors, key)) {
|
|
1153
|
-
errorString += `${key}: ${error.errors[key]}\n`;
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
console.error(`${message}: ${errorString}`);
|
|
1157
|
-
}
|
|
1158
|
-
}
|
|
1159
|
-
else
|
|
1160
|
-
{
|
|
1161
|
-
console.error(`${message}: ${error.message}`);
|
|
1162
|
-
}
|
|
1163
|
-
|
|
1164
|
-
return null;
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
/**
|
|
1169
|
-
* New GUID
|
|
1170
|
-
*
|
|
1171
|
-
* @returns UUID v4 GUID
|
|
1172
|
-
*/
|
|
1173
|
-
|
|
1174
|
-
import { getRandomValues } from 'crypto';
|
|
1175
|
-
|
|
1176
|
-
function _newGuid() {
|
|
1177
|
-
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
|
|
1178
|
-
(c ^ (getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
|
|
1179
|
-
);
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
1187
|
|
|
1184
1188
|
function _printDatetime(msg)
|
|
1185
1189
|
{
|
|
@@ -1189,25 +1193,77 @@ function _printDatetime(msg)
|
|
|
1189
1193
|
}
|
|
1190
1194
|
|
|
1191
1195
|
|
|
1196
|
+
import { promises as fs } from 'fs';
|
|
1197
|
+
import path from 'path';
|
|
1198
|
+
|
|
1199
|
+
async function _processFile(FILE)
|
|
1200
|
+
{
|
|
1201
|
+
let FILE_PATH = null;
|
|
1202
|
+
if (typeof FILE === "string")
|
|
1203
|
+
{
|
|
1204
|
+
// if running in node
|
|
1205
|
+
if (typeof window === "undefined")
|
|
1206
|
+
{
|
|
1207
|
+
FILE_PATH = FILE;
|
|
1208
|
+
FILE = await fs.stat(FILE);
|
|
1209
|
+
FILE.originalName = path.basename(FILE_PATH);
|
|
1210
|
+
if (!FILE.buffer)
|
|
1211
|
+
{
|
|
1212
|
+
FILE.buffer = await fs.readFile(FILE_PATH);
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
else
|
|
1216
|
+
{
|
|
1217
|
+
throw new Error("File path is not supported in browser");
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
else
|
|
1221
|
+
{
|
|
1222
|
+
FILE.originalName = FILE.name;
|
|
1223
|
+
// if running in browser
|
|
1224
|
+
if (!FILE.buffer)
|
|
1225
|
+
{
|
|
1226
|
+
FILE.buffer = await _readFileAsArrayBuffer(FILE);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
return FILE;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
async function _readFileAsArrayBuffer(file)
|
|
1234
|
+
{
|
|
1235
|
+
return new Promise((resolve, reject) =>
|
|
1236
|
+
{
|
|
1237
|
+
/* eslint-disable no-undef */
|
|
1238
|
+
const reader = new FileReader();
|
|
1239
|
+
/* eslint-enable no-undef */
|
|
1240
|
+
reader.onload = () => resolve(reader.result);
|
|
1241
|
+
reader.onerror = reject;
|
|
1242
|
+
reader.readAsArrayBuffer(file);
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
|
|
1192
1247
|
|
|
1193
1248
|
|
|
1194
1249
|
|
|
1195
1250
|
|
|
1196
1251
|
import axios from 'axios';
|
|
1197
1252
|
|
|
1198
|
-
const MAX_RETRIES =
|
|
1199
|
-
const TIMEOUT =
|
|
1200
|
-
const SLEEP_TIME =
|
|
1253
|
+
const MAX_RETRIES = 3;
|
|
1254
|
+
const TIMEOUT = 10000;
|
|
1255
|
+
const SLEEP_TIME = 2;
|
|
1201
1256
|
|
|
1202
|
-
async function _sendRequest(instance,
|
|
1257
|
+
async function _sendRequest(instance, FUNCTION_NAME, URL, METHOD_TYPE, PARAMS, BODY)
|
|
1203
1258
|
{
|
|
1204
1259
|
const HEADERS = {
|
|
1205
1260
|
"Content-Type": "application/json"
|
|
1206
1261
|
};
|
|
1207
1262
|
|
|
1208
|
-
if (
|
|
1263
|
+
if (instance.token) HEADERS["Authorization"] = `Bearer ${instance.token}`;
|
|
1264
|
+
else if (instance.config.apiKey) HEADERS["Authorization"] = `ApiKey ${instance.config.apiKey}`;
|
|
1209
1265
|
|
|
1210
|
-
if (
|
|
1266
|
+
if (instance.debug && FUNCTION_NAME !== "Login") console.log(`URL: ${URL}\nMETHOD: ${METHOD_TYPE}` + (PARAMS && PARAMS.size === 0 ? `\nPARAMS: ${JSON.stringify(PARAMS, null, 4)}` : "") + (BODY ? `\nBODY: ${JSON.stringify(BODY, null, 4)}` : ""));
|
|
1211
1267
|
|
|
1212
1268
|
let retries = 0;
|
|
1213
1269
|
let RESPONSE = null;
|
|
@@ -1216,7 +1272,6 @@ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYP
|
|
|
1216
1272
|
{
|
|
1217
1273
|
try
|
|
1218
1274
|
{
|
|
1219
|
-
refreshedToken = true;
|
|
1220
1275
|
RESPONSE = await axios({
|
|
1221
1276
|
method: METHOD_TYPE,
|
|
1222
1277
|
url: URL,
|
|
@@ -1235,6 +1290,11 @@ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYP
|
|
|
1235
1290
|
}
|
|
1236
1291
|
catch (error)
|
|
1237
1292
|
{
|
|
1293
|
+
if (retries >= MAX_RETRIES)
|
|
1294
|
+
{
|
|
1295
|
+
return _apiExceptionHandler(error.response?.data.message ? error.response.data.message : error.message, `${FUNCTION_NAME} failed`);
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1238
1298
|
if (error?.response?.status === 403 && FUNCTION_NAME !== "Refresh Token" && !refreshedToken)
|
|
1239
1299
|
{
|
|
1240
1300
|
await instance.refreshToken();
|
|
@@ -1243,7 +1303,7 @@ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYP
|
|
|
1243
1303
|
}
|
|
1244
1304
|
else if (error.code === 'ECONNABORTED')
|
|
1245
1305
|
{
|
|
1246
|
-
await _sleep(SLEEP_TIME * 2 ** (retries
|
|
1306
|
+
await _sleep(SLEEP_TIME * 2 ** (retries + 1));
|
|
1247
1307
|
}
|
|
1248
1308
|
else
|
|
1249
1309
|
{
|
|
@@ -1257,181 +1317,187 @@ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYP
|
|
|
1257
1317
|
}
|
|
1258
1318
|
|
|
1259
1319
|
|
|
1260
|
-
async function _sleep(duration)
|
|
1261
|
-
// Check for valid duration
|
|
1262
|
-
if (!duration || duration === 0) {
|
|
1263
|
-
// Ignore
|
|
1264
|
-
return;
|
|
1265
|
-
}
|
|
1266
|
-
|
|
1267
|
-
// Sleep
|
|
1268
|
-
return new Promise((resolve) => {
|
|
1269
|
-
setTimeout(() => {
|
|
1270
|
-
resolve();
|
|
1271
|
-
}, duration * 1000);
|
|
1272
|
-
});
|
|
1273
|
-
}
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
function _slugify(text) {
|
|
1278
|
-
if (!text || text.trim().length === 0) return "";
|
|
1279
|
-
return text
|
|
1280
|
-
.toString()
|
|
1281
|
-
.normalize("NFD")
|
|
1282
|
-
.replace(/[\u0300-\u036f]/g, "")
|
|
1283
|
-
.toLowerCase()
|
|
1284
|
-
.trim()
|
|
1285
|
-
.replace(/\s+/g, "-")
|
|
1286
|
-
.replace(/[^\w-]+/g, "")
|
|
1287
|
-
.replace(/--+/g, "-");
|
|
1288
|
-
}
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
async function _clearContinueWatching(instance, AUTH_TOKEN, URL, USER_ID, ASSET_ID, DEBUG_MODE)
|
|
1320
|
+
async function _sleep(duration)
|
|
1295
1321
|
{
|
|
1296
|
-
|
|
1322
|
+
// Check for valid duration
|
|
1323
|
+
if (!duration || duration === 0)
|
|
1324
|
+
{
|
|
1325
|
+
// Ignore
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
// Sleep
|
|
1330
|
+
return new Promise((resolve) => {
|
|
1331
|
+
setTimeout(() => {
|
|
1332
|
+
resolve();
|
|
1333
|
+
}, duration * 1000);
|
|
1334
|
+
});
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
function _slugify(text)
|
|
1340
|
+
{
|
|
1341
|
+
if (!text || text.trim().length === 0) return "";
|
|
1342
|
+
return text
|
|
1343
|
+
.toString()
|
|
1344
|
+
.normalize("NFD")
|
|
1345
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
1346
|
+
.toLowerCase()
|
|
1347
|
+
.trim()
|
|
1348
|
+
.replace(/\s+/g, "-")
|
|
1349
|
+
.replace(/[^\w-]+/g, "")
|
|
1350
|
+
.replace(/--+/g, "-");
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
async function _clearContinueWatching(instance, USER_ID, ASSET_ID)
|
|
1358
|
+
{
|
|
1359
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/clear-watching`;
|
|
1297
1360
|
|
|
1298
1361
|
const PARAMS = {
|
|
1299
1362
|
userId: USER_ID,
|
|
1300
1363
|
assetId: ASSET_ID
|
|
1301
1364
|
};
|
|
1302
1365
|
|
|
1303
|
-
return await _sendRequest(instance,
|
|
1366
|
+
return await _sendRequest(instance, "Clear Continue Watching", API_URL, "POST", PARAMS, undefined);
|
|
1304
1367
|
}
|
|
1305
1368
|
|
|
1306
1369
|
|
|
1307
1370
|
|
|
1308
1371
|
|
|
1309
|
-
async function _clearWatchlist(instance,
|
|
1372
|
+
async function _clearWatchlist(instance, USER_ID)
|
|
1310
1373
|
{
|
|
1311
|
-
const API_URL = `${
|
|
1374
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/clear-watchlist?userId=${USER_ID}`;
|
|
1312
1375
|
|
|
1313
|
-
return await _sendRequest(instance,
|
|
1376
|
+
return await _sendRequest(instance, "Clear Watchlist", API_URL, "POST", undefined, undefined);
|
|
1314
1377
|
}
|
|
1315
1378
|
|
|
1316
1379
|
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
async function _createForm(instance,
|
|
1320
|
-
{
|
|
1321
|
-
const API_URL = `${
|
|
1322
|
-
|
|
1323
|
-
const BODY = FORM_INFO;
|
|
1324
|
-
|
|
1325
|
-
return await _sendRequest(instance,
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
async function _createForm(instance, CONTENT_DEFINITION_ID, FORM_INFO)
|
|
1383
|
+
{
|
|
1384
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/form/${CONTENT_DEFINITION_ID}`;
|
|
1385
|
+
|
|
1386
|
+
const BODY = FORM_INFO;
|
|
1387
|
+
|
|
1388
|
+
return await _sendRequest(instance, "Creating Form", API_URL, "POST", undefined, BODY);
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1328
1391
|
|
|
1329
1392
|
|
|
1330
1393
|
|
|
1331
|
-
async function _getContentCookies(instance,
|
|
1394
|
+
async function _getContentCookies(instance, ID)
|
|
1332
1395
|
{
|
|
1333
|
-
const API_URL = `${
|
|
1396
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/set-cookies/${ID}`;
|
|
1334
1397
|
|
|
1335
|
-
return await _sendRequest(instance,
|
|
1398
|
+
return await _sendRequest(instance, "Get Content Cookies", API_URL, "GET", undefined, undefined);
|
|
1336
1399
|
}
|
|
1337
1400
|
|
|
1338
1401
|
|
|
1339
1402
|
|
|
1340
1403
|
|
|
1341
|
-
async function _getDefaultSiteConfig(instance
|
|
1404
|
+
async function _getDefaultSiteConfig(instance)
|
|
1342
1405
|
{
|
|
1343
|
-
const API_URL = `${
|
|
1406
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/config`;
|
|
1344
1407
|
|
|
1345
|
-
return await _sendRequest(instance,
|
|
1408
|
+
return await _sendRequest(instance, "Get Default Site Config", API_URL, "GET", undefined, undefined);
|
|
1346
1409
|
}
|
|
1347
1410
|
|
|
1348
1411
|
|
|
1349
1412
|
|
|
1350
1413
|
|
|
1351
|
-
async function _getDynamicContent(instance,
|
|
1414
|
+
async function _getDynamicContent(instance, ID)
|
|
1352
1415
|
{
|
|
1353
|
-
const API_URL = `${
|
|
1416
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/content/${ID}`;
|
|
1354
1417
|
|
|
1355
|
-
return await _sendRequest(instance,
|
|
1418
|
+
return await _sendRequest(instance, "Get Dynamic Content", API_URL, "GET", undefined, undefined);
|
|
1356
1419
|
}
|
|
1357
1420
|
|
|
1358
1421
|
|
|
1359
1422
|
|
|
1360
1423
|
|
|
1361
|
-
async function _getDynamicContents(instance
|
|
1424
|
+
async function _getDynamicContents(instance)
|
|
1362
1425
|
{
|
|
1363
|
-
const API_URL = `${
|
|
1426
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/content`;
|
|
1364
1427
|
|
|
1365
|
-
return await _sendRequest(instance,
|
|
1428
|
+
return await _sendRequest(instance, "Get Dynamic Contents", API_URL, "GET", undefined, undefined);
|
|
1366
1429
|
}
|
|
1367
1430
|
|
|
1368
1431
|
|
|
1369
1432
|
|
|
1370
1433
|
|
|
1371
|
-
async function _getMediaGroup(instance,
|
|
1434
|
+
async function _getMediaGroup(instance, ID, FILTER_IDS)
|
|
1372
1435
|
{
|
|
1373
|
-
|
|
1436
|
+
let apiUrl = `${instance.config.serviceApiUrl}/api/media/group/${ID}`;
|
|
1437
|
+
if (FILTER_IDS && FILTER_IDS.length > 0) {
|
|
1438
|
+
apiUrl += '?' + FILTER_IDS.map(id => `filterIds=${id}`).join('&');
|
|
1439
|
+
}
|
|
1374
1440
|
|
|
1375
|
-
return await _sendRequest(instance,
|
|
1441
|
+
return await _sendRequest(instance, "Get Media Group", apiUrl, "GET", undefined, undefined);
|
|
1376
1442
|
}
|
|
1377
1443
|
|
|
1378
1444
|
|
|
1379
1445
|
|
|
1380
1446
|
|
|
1381
|
-
async function _getMediaItem(instance,
|
|
1447
|
+
async function _getMediaItem(instance, ID)
|
|
1382
1448
|
{
|
|
1383
|
-
const API_URL = `${
|
|
1449
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/item/${ID}`;
|
|
1384
1450
|
|
|
1385
|
-
return await _sendRequest(instance,
|
|
1451
|
+
return await _sendRequest(instance, "Get Media Item", API_URL, "GET", undefined, undefined);
|
|
1386
1452
|
}
|
|
1387
1453
|
|
|
1388
1454
|
|
|
1389
1455
|
|
|
1390
1456
|
|
|
1391
|
-
async function _getMyContent(instance
|
|
1457
|
+
async function _getMyContent(instance)
|
|
1392
1458
|
{
|
|
1393
|
-
const API_URL = `${
|
|
1459
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/my-content`;
|
|
1394
1460
|
|
|
1395
|
-
return await _sendRequest(instance,
|
|
1461
|
+
return await _sendRequest(instance, "Get My Content", API_URL, "GET", undefined, undefined);
|
|
1396
1462
|
}
|
|
1397
1463
|
|
|
1398
1464
|
|
|
1399
1465
|
|
|
1400
1466
|
|
|
1401
|
-
async function _getMyGroup(instance,
|
|
1467
|
+
async function _getMyGroup(instance, ID)
|
|
1402
1468
|
{
|
|
1403
|
-
const API_URL = `${
|
|
1469
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/my-group/${ID}`;
|
|
1404
1470
|
|
|
1405
|
-
return await _sendRequest(instance,
|
|
1471
|
+
return await _sendRequest(instance, "Get My Group", API_URL, "GET", undefined, undefined);
|
|
1406
1472
|
}
|
|
1407
1473
|
|
|
1408
1474
|
|
|
1409
1475
|
|
|
1410
1476
|
|
|
1411
|
-
async function _getSiteConfig(instance,
|
|
1477
|
+
async function _getSiteConfig(instance, ID)
|
|
1412
1478
|
{
|
|
1413
|
-
const API_URL = `${
|
|
1479
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/config/${ID}`;
|
|
1414
1480
|
|
|
1415
|
-
return await _sendRequest(instance,
|
|
1481
|
+
return await _sendRequest(instance, "Get Site Config", API_URL, "GET", undefined, undefined);
|
|
1416
1482
|
}
|
|
1417
1483
|
|
|
1418
1484
|
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
async function _mediaSearch(instance,
|
|
1422
|
-
PAGE_OFFSET, PAGE_SIZE
|
|
1423
|
-
{
|
|
1424
|
-
const API_URL = `${
|
|
1425
|
-
|
|
1426
|
-
const BODY = {
|
|
1427
|
-
searchQuery: SEARCH_QUERY,
|
|
1428
|
-
ids: IDS,
|
|
1429
|
-
sortFields: SORT_FIELDS
|
|
1430
|
-
};
|
|
1431
|
-
|
|
1432
|
-
PAGE_OFFSET ? BODY["pageOffset"] = PAGE_OFFSET : BODY["pageOffset"] = 0;
|
|
1433
|
-
PAGE_SIZE ? BODY["pageSize"] = PAGE_SIZE : BODY["pageSize"] = 100;
|
|
1434
|
-
|
|
1435
|
-
return await _sendRequest(instance,
|
|
1436
|
-
}
|
|
1437
|
-
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
async function _mediaSearch(instance, SEARCH_QUERY, IDS, SORT_FIELDS,
|
|
1488
|
+
PAGE_OFFSET, PAGE_SIZE)
|
|
1489
|
+
{
|
|
1490
|
+
const API_URL = `${instance.config.serviceApiUrl}/api/media/search`;
|
|
1491
|
+
|
|
1492
|
+
const BODY = {
|
|
1493
|
+
searchQuery: SEARCH_QUERY,
|
|
1494
|
+
ids: IDS,
|
|
1495
|
+
sortFields: SORT_FIELDS
|
|
1496
|
+
};
|
|
1497
|
+
|
|
1498
|
+
PAGE_OFFSET ? BODY["pageOffset"] = PAGE_OFFSET : BODY["pageOffset"] = 0;
|
|
1499
|
+
PAGE_SIZE ? BODY["pageSize"] = PAGE_SIZE : BODY["pageSize"] = 100;
|
|
1500
|
+
|
|
1501
|
+
return await _sendRequest(instance, "Media Search", API_URL, "POST", undefined, BODY);
|
|
1502
|
+
}
|
|
1503
|
+
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
METHOD: `+i
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
class NomadMediaSDK{constructor(t={}){return NomadMediaSDK.instance||(this.config=t,this.token=null,this.refreshTokenVal=null,this.expirationSeconds=null,this.userSessionId=null,this.id=null,this.debug=t.debugMode||!1,"/"===this.config.serviceApiUrl[this.config.serviceApiUrl.length-1]&&(this.config.serviceApiUrl=this.config.serviceApiUrl.slice(0,-1)),this.config.singleton&&(NomadMediaSDK.instance=this),this)}async _init(){this.config.username&&this.config.password&&await this.login()}async login(){_printDatetime("Logging in as "+this.config.username);try{var t=await _login(this);if(!t)throw new Error("Login failed");this.token=t.token,this.refreshTokenVal=t.refreshToken,this.expirationSeconds=t.expirationSeconds,this.userSessionId=t.userSessionId,this.id=t.id,_printDatetime("Login successful")}catch(t){throw _printDatetime("Login failed"),t}}async logout(){null===this.token&&this.config.username&&await this._init(),_printDatetime("Logging out");try{await _logout(this),this.token=null,this.refreshTokenVal=null,this.expirationSeconds=null,this.userSessionId=null,this.id=null,_printDatetime("Logout successful")}catch(t){throw _printDatetime("Logout failed"),t}}async refreshToken(){_printDatetime("Refreshing token"),null===this.token&&this.config.username&&await this._init();try{await _refreshToken(this)&&_printDatetime("Token refresh successful")}catch(t){throw _printDatetime("Token refresh failed"),t}}async forgotPassword(){var e=this.config.username;_printDatetime("Sending code to "+e);try{await _forgotPassword(this),_printDatetime("Code sent to "+e)}catch(t){throw _printDatetime("Code failed to send to "+e),t}}async resetPassword(t,e){var i=this.config.username;_printDatetime("Resetting password for "+i);try{await _resetPassword(this,t,e),_printDatetime("Password reset for "+i)}catch(t){throw _printDatetime("Password failed to reset for "+i),t}}async clearContinueWatching(t,e){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Clearing continue watching");try{await _clearContinueWatching(this,t,e),_printDatetime("Continue watching cleared")}catch(t){throw _printDatetime("Continue watching failed to clear"),t}}async clearWatchlist(t){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Clearing watchlist");try{await _clearWatchlist(this,t=t||this.id),_printDatetime("Watchlist cleared")}catch(t){throw _printDatetime("Watchlist failed to clear"),t}}async createForm(t,e){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Creating form");try{var i=await _createForm(this,t,e);return i&&_printDatetime("Form created"),i}catch(t){throw _printDatetime("Form failed to create"),t}}async getContentCookies(t){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting content cookies");try{var e=await _getContentCookies(this,t);return e&&_printDatetime("Content cookies gotten"),e}catch(t){throw _printDatetime("Content cookies failed to get"),t}}async getDefaultSiteConfig(){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting default site config");try{var t=await _getDefaultSiteConfig(this);return t&&_printDatetime("Default site config gotten"),t}catch(t){throw _printDatetime("Default site config failed to get"),t}}async getDynamicContent(e){null===this.token&&this.config.username&&await this._init(),_printDatetime("Getting dynamic content: "+e);try{var t=await _getDynamicContent(this,e);return t&&_printDatetime("Dynamic content gotten: "+e),t}catch(t){throw _printDatetime("Dynamic content failed to get: "+e),t}}async getDynamicContents(){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting dynamic contents");try{var t=await _getDynamicContents(this);return t&&_printDatetime("Dynamic contents gotten"),t}catch(t){throw _printDatetime("Dynamic contents failed to get"),t}}async getMediaGroup(e,t){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting media group: "+e);try{var i=await _getMediaGroup(this,e,t);return i&&_printDatetime("Media group gotten: "+e),i}catch(t){throw _printDatetime("Media group failed to get: "+e),t}}async getMediaItem(e){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting media item: "+e);try{var t=await _getMediaItem(this,e);return t&&_printDatetime("Media item gotten: "+e),t}catch(t){throw _printDatetime("Media item failed to get: "+e),t}}async getMyContent(){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is not available for portal API type.");_printDatetime("Getting my content");try{var t=await _getMyContent(this);return t&&_printDatetime("My content gotten"),t}catch(t){throw _printDatetime("My content failed to get"),t}}async getMyGroup(e){null===this.token&&this.config.username&&await this._init(),_printDatetime("Getting my group: "+e);try{var t=await _getMyGroup(this,e);return t&&_printDatetime("My group gotten: "+e),t}catch(t){throw _printDatetime("My group failed to get: "+e),t}}async getSiteConfig(e){null===this.token&&this.config.username&&await this._init(),_printDatetime("Getting site config: "+e);try{var t=await _getSiteConfig(this,e);return t&&_printDatetime("Site config gotten: "+e),t}catch(t){throw _printDatetime("Site config failed to get: "+e),t}}async mediaSearch(t,e,i,n,r){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Searching media...");try{var a=await _mediaSearch(this,t,e,i,n,r);return a&&_printDatetime("Media search complete"),a}catch(t){throw _printDatetime("Media search failed"),t}}async miscFunctions(e,i,n,r){null===this.token&&this.config.username&&await this._init(),_printDatetime("Calling function: "+e);try{let t=this.config.serviceApiUrl+"/"+e;r&&(t=(t=(t=t.replace("/api","")).replace("app-api.","")).replace("admin-app.",""));var a=new Headers,o=(a.append("Content-Type","application/json"),null!==this.token&&a.append("Authorization","Bearer "+this.token),this.debug&&console.log(`URL: ${t}
|
|
2
|
+
METHOD: `+i+(n?`
|
|
3
|
+
BODY: `+JSON.stringify(n,null,4):"")),{method:i,headers:a}),s=(n&&(o.body=JSON.stringify(n)),await fetch(t,o)),c=await s.text();if(!s.ok)try{throw JSON.parse(c)}catch(t){throw c}try{return JSON.parse(c)}catch(t){return c}}catch(t){_apiExceptionHandler(t,"API call failed")}}}export default NomadMediaSDK;async function _forgotPassword(t){return _sendRequest(t,"Forgot Password",t.config.serviceApiUrl+"/api/account/forgot-password","POST",void 0,{username:t.config.username})}async function _login(t){var e=t.config.serviceApiUrl+"/api/account/login",i={userName:t.config.username,password:t.config.password};try{return await _sendRequest(t,"Login",e,"POST",void 0,i)}catch(t){throw t.message.includes("409")?new Error("Invalid username or password"):t}}async function _logout(t){return _sendRequest(t,"Logout",t.config.serviceApiUrl+"/api/account/logout","POST",void 0,void 0)}async function _refreshToken(t){var e=await _sendRequest(t,"Refreshing Token",t.config.serviceApiUrl+"/api/account/refresh-token","POST",void 0,{refreshToken:t.refreshTokenVal});return t.token=e.token,!0}async function _resetPassword(t,e,i){return _sendRequest(t,"Reset Password",t.config.serviceApiUrl+"/api/account/reset-password","POST",void 0,{userName:t.username,token:e,newPassword:i})}function _apiExceptionHandler(i,n){if(i||console.error(n),"string"==typeof i)console.error(n+": "+i);else if(i.errors){if(Array.isArray(i.errors)){let e="";for(let t=0;t<i.errors.length;t++)e+=`${i.errors[t].code}: ${i.errors[t].field}: ${i.errors[t].message}\n`;console.error(n+": "+e)}else if("object"==typeof i.errors){let t="";for(var e in i.errors)Object.prototype.hasOwnProperty.call(i.errors,e)&&(t+=`${e}: ${i.errors[e]}\n`);console.error(n+": "+t)}}else console.error(n+": "+i.message);return null}import{getRandomValues}from"crypto";function _newGuid(){return([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,t=>(t^getRandomValues(new Uint8Array(1))[0]&15>>t/4).toString(16))}function _printDatetime(t){var e=(new Date).toLocaleTimeString();console.log(e+": "+t)}import{promises as fs}from"fs";import path from"path";async function _processFile(t){var e;if("string"==typeof t){if("undefined"!=typeof window)throw new Error("File path is not supported in browser");e=t,(t=await fs.stat(t)).originalName=path.basename(e),t.buffer||(t.buffer=await fs.readFile(e))}else t.originalName=t.name,t.buffer||(t.buffer=await _readFileAsArrayBuffer(t));return t}async function _readFileAsArrayBuffer(n){return new Promise((t,e)=>{let i=new FileReader;i.onload=()=>t(i.result),i.onerror=e,i.readAsArrayBuffer(n)})}import axios from"axios";let MAX_RETRIES=3,TIMEOUT=1e4,SLEEP_TIME=2;async function _sendRequest(e,i,t,n,r,a){var o={"Content-Type":"application/json"};e.token?o.Authorization="Bearer "+e.token:e.config.apiKey&&(o.Authorization="ApiKey "+e.config.apiKey),e.debug&&"Login"!==i&&console.log(`URL: ${t}
|
|
4
|
+
METHOD: `+n+(r&&0===r.size?`
|
|
5
|
+
PARAMS: `+JSON.stringify(r,null,4):"")+(a?`
|
|
6
|
+
BODY: `+JSON.stringify(a,null,4):""));let s=0,c=null,l=!1;do{try{if(200<=(c=await axios({method:n,url:t,headers:o,params:r,data:a,timeout:TIMEOUT})).status&&c.status<300)break;throw new Error}catch(t){if(s>=MAX_RETRIES)return _apiExceptionHandler((t.response?.data.message?t.response.data:t).message,i+" failed");if(403!==t?.response?.status||"Refresh Token"===i||l){if("ECONNABORTED"!==t.code)return _apiExceptionHandler((t.response?.data.message?t.response.data:t).message,i+" failed");await _sleep(SLEEP_TIME*2**(s+1))}else await e.refreshToken(),o.Authorization="Bearer "+e.token,l=!0;++s}}while((!c||c.status<200||300<=c.status)&&s<MAX_RETRIES);try{return c.data}catch(t){}}async function _sleep(e){if(e&&0!==e)return new Promise(t=>{setTimeout(()=>{t()},1e3*e)})}function _slugify(t){return t&&0!==t.trim().length?t.toString().normalize("NFD").replace(/[\u0300-\u036f]/g,"").toLowerCase().trim().replace(/\s+/g,"-").replace(/[^\w-]+/g,"").replace(/--+/g,"-"):""}async function _clearContinueWatching(t,e,i){return _sendRequest(t,"Clear Continue Watching",t.config.serviceApiUrl+"/api/media/clear-watching","POST",{userId:e,assetId:i},void 0)}async function _clearWatchlist(t,e){return _sendRequest(t,"Clear Watchlist",t.config.serviceApiUrl+"/api/media/clear-watchlist?userId="+e,"POST",void 0,void 0)}async function _createForm(t,e,i){return _sendRequest(t,"Creating Form",t.config.serviceApiUrl+"/api/media/form/"+e,"POST",void 0,i)}async function _getContentCookies(t,e){return _sendRequest(t,"Get Content Cookies",t.config.serviceApiUrl+"/api/media/set-cookies/"+e,"GET",void 0,void 0)}async function _getDefaultSiteConfig(t){return _sendRequest(t,"Get Default Site Config",t.config.serviceApiUrl+"/api/media/config","GET",void 0,void 0)}async function _getDynamicContent(t,e){return _sendRequest(t,"Get Dynamic Content",t.config.serviceApiUrl+"/api/media/content/"+e,"GET",void 0,void 0)}async function _getDynamicContents(t){return _sendRequest(t,"Get Dynamic Contents",t.config.serviceApiUrl+"/api/media/content","GET",void 0,void 0)}async function _getMediaGroup(t,e,i){let n=t.config.serviceApiUrl+"/api/media/group/"+e;return i&&0<i.length&&(n+="?"+i.map(t=>"filterIds="+t).join("&")),_sendRequest(t,"Get Media Group",n,"GET",void 0,void 0)}async function _getMediaItem(t,e){return _sendRequest(t,"Get Media Item",t.config.serviceApiUrl+"/api/media/item/"+e,"GET",void 0,void 0)}async function _getMyContent(t){return _sendRequest(t,"Get My Content",t.config.serviceApiUrl+"/api/media/my-content","GET",void 0,void 0)}async function _getMyGroup(t,e){return _sendRequest(t,"Get My Group",t.config.serviceApiUrl+"/api/media/my-group/"+e,"GET",void 0,void 0)}async function _getSiteConfig(t,e){return _sendRequest(t,"Get Site Config",t.config.serviceApiUrl+"/api/media/config/"+e,"GET",void 0,void 0)}async function _mediaSearch(t,e,i,n,r,a){var o=t.config.serviceApiUrl+"/api/media/search",e={searchQuery:e,ids:i,sortFields:n};return e.pageOffset=r||0,e.pageSize=a||100,_sendRequest(t,"Media Search",o,"POST",void 0,e)}
|