@nomad-media/public 1.1.4

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.
@@ -0,0 +1,1702 @@
1
+ // account
2
+
3
+
4
+
5
+
6
+
7
+
8
+ // admin
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+ // common
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+ // portal
197
+
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+
212
+
213
+
214
+
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+
256
+ // helpers
257
+
258
+
259
+
260
+
261
+ import { promises as fs } from 'fs';
262
+ import path from 'path';
263
+
264
+ /**
265
+ * @class NomadSDK
266
+ * @classdesc This class is used to interact with the Nomad API.
267
+ */
268
+
269
+ class NomadMediaSDK {
270
+ constructor(config = {}) {
271
+ if (NomadMediaSDK.instance)
272
+ {
273
+ return NomadMediaSDK.instance;
274
+ }
275
+
276
+ this.config = config;
277
+ this.token = null;
278
+ this.refreshTokenVal = null;
279
+ this.expirationSeconds = null;
280
+ this.userSessionId = null;
281
+ this.id = null
282
+ this.debugMode = config.debugMode || false;
283
+
284
+ NomadMediaSDK.instance = this;
285
+
286
+ return this;
287
+ }
288
+
289
+ /**
290
+ * @function init
291
+ * @async
292
+ * @private
293
+ * @description Initializes the SDK. Logs in using the username and password from the config.
294
+ * Then schedules a token refresh for the duration set by the value returned in the login.
295
+ * @returns {Promise<void>} - A promise that resolves when the SDK is initialized.
296
+ * @throws {Error} - An error is thrown if the SDK fails to initialize.
297
+ */
298
+ async _init()
299
+ {
300
+ await this.login();
301
+
302
+ // Schedule token refresh
303
+ this.__scheduleTokenRefresh();
304
+ }
305
+
306
+ // account functions
307
+ /**
308
+ * @function login
309
+ * @async
310
+ * @description Logs in using the username and password from the config.
311
+ * @returns {Promise<void>} - A promise that resolves when the login is successful.
312
+ * Updates the token, refresh token, expiration seconds, and user session ID class varibles.
313
+ * @throws {Error} - An error is thrown if the login fails.
314
+ */
315
+ async login()
316
+ {
317
+
318
+ _printDatetime(`Logging in as ${this.config.username}`);
319
+
320
+ try
321
+ {
322
+ const LOGIN_INFO = await _login(this.config.username, this.config.password,
323
+ this.config.serviceApiUrl, this.debugMode);
324
+ this.token = LOGIN_INFO["token"];
325
+ this.refreshTokenVal = LOGIN_INFO["refreshToken"];
326
+ this.expirationSeconds = Date.now() + LOGIN_INFO["expirationSeconds"] * 1000;
327
+ this.userSessionId = LOGIN_INFO["userSessionId"];
328
+ this.id = LOGIN_INFO["id"];
329
+ _printDatetime(`Login successful`);
330
+ }
331
+ catch (error)
332
+ {
333
+ _printDatetime(`Login failed`);
334
+ throw error;
335
+ }
336
+ }
337
+
338
+ /**
339
+ * @function logout
340
+ * @async
341
+ * @description Logs out the current user.
342
+ * @returns {Promise<null>} - A promise that resolves when the logout is successful.
343
+ * @throws {Error} - An error is thrown if the logout fails.
344
+ */
345
+ async logout()
346
+ {
347
+ if (this.token === null)
348
+ {
349
+ await this._init();
350
+ }
351
+
352
+ _printDatetime(`Logging out`);
353
+
354
+ try
355
+ {
356
+ await _logout(this.token, this.config.serviceApiUrl, this.debugMode);
357
+ this.token = null;
358
+ this.refreshTokenVal = null;
359
+ this.expirationSeconds = null;
360
+ this.userSessionId = null;
361
+ this.id = null;
362
+
363
+ _printDatetime(`Logout successful`);
364
+ }
365
+ catch (error)
366
+ {
367
+ _printDatetime("Loggout failed");
368
+ throw error;
369
+ }
370
+ }
371
+
372
+ /**
373
+ * @function refreshToken
374
+ * @async
375
+ * @description Refreshes the current token.
376
+ * @returns {Promise<void>} - A promise that resolves when the token is refreshed.
377
+ * Updates the token class variable.
378
+ * @throws {Error} - An error is thrown if the token refresh fails.
379
+ */
380
+ async refreshToken()
381
+ {
382
+ _printDatetime(`Refreshing token`);
383
+
384
+ try
385
+ {
386
+ const TOKEN_INFO = await _refreshToken(this.token, this.config.serviceApiUrl,
387
+ this.refreshTokenVal, this.debugMode);
388
+ this.token = TOKEN_INFO["token"];
389
+ _printDatetime(`Token refresh successful`);
390
+ }
391
+ catch (error)
392
+ {
393
+ _printDatetime(`Token refresh failed`);
394
+ throw error;
395
+ }
396
+ }
397
+
398
+ /**
399
+ * @function scheduleTokenRefresh
400
+ * @private
401
+ * @description Schedules a token refresh for the duration set by the value returned in the login.
402
+ * @returns {Promise<void>} - A promise that resolves when the token is refreshed.
403
+ * Updates the token class variable.
404
+ * @throws {Error} - An error is thrown if the token refresh fails.
405
+ */
406
+ __scheduleTokenRefresh() {
407
+ if (this.token && this.expirationSeconds) {
408
+ const remainingTime = this.expirationSeconds - Date.now();
409
+ if (remainingTime > 0) {
410
+ // Clear existing timer (if any)
411
+ if (this.refreshTimer) {
412
+ clearTimeout(this.refreshTimer);
413
+ }
414
+ // Schedule a refresh slightly before token expiration
415
+ this.refreshTimer = setTimeout(async () => {
416
+ await this.refreshToken();
417
+ this.__scheduleTokenRefresh();
418
+ }, remainingTime - 5000); // Refresh 5 seconds before expiration
419
+ }
420
+ }
421
+ }
422
+
423
+ /**
424
+ * @function forgotPassword
425
+ * @async
426
+ * @description Sends a code to the email to the user to be used
427
+ * be used to reset the password.
428
+ * @returns {Promise<void>}
429
+ * Sends email with code to reset password.
430
+ * @throws {Error} - An error is thrown if the code fails to send.
431
+ */
432
+ async forgotPassword()
433
+ {
434
+ const USERNAME = this.config.username;
435
+
436
+ _printDatetime(`Sending code to ${USERNAME}`);
437
+
438
+ try
439
+ {
440
+ await _forgotPassword(this.config.serviceApiUrl, USERNAME, this.debugMode);
441
+ _printDatetime(`Code sent to ${USERNAME}`);
442
+ }
443
+ catch (error)
444
+ {
445
+ _printDatetime(`Code failed to send to ${USERNAME}`);
446
+ throw error;
447
+ }
448
+ }
449
+
450
+ /**
451
+ * @function resetPassword
452
+ * @async
453
+ * @description Resets the password for the specified username.
454
+ * @param {string} CODE - The code to use to reset the password. Sent to the user's email
455
+ * by calling forgotPassword().
456
+ * @param {string} NEW_PASSWORD - The new password to set.
457
+ * @returns {Promise<void>}
458
+ * Resets the password for the specified username.
459
+ * @throws {Error} - An error is thrown if the password fails to reset.
460
+ */
461
+ async resetPassword(CODE, NEW_PASSWORD)
462
+ {
463
+ const USERNAME = this.config.username;
464
+
465
+ _printDatetime(`Resetting password for ${USERNAME}`);
466
+
467
+ try
468
+ {
469
+ await _resetPassword(this.config.serviceApiUrl, USERNAME, CODE, NEW_PASSWORD,
470
+ this.debugMode);
471
+ _printDatetime(`Password reset for ${USERNAME}`);
472
+ }
473
+ catch (error)
474
+ {
475
+ _printDatetime(`Password failed to reset for ${USERNAME}`);
476
+ throw error;
477
+ }
478
+ }
479
+
480
+ // media functions
481
+ /**
482
+ * @function clearContinueWatching
483
+ * @async
484
+ * @description Delete continue watching markers.
485
+ * @param {string | null} USER_ID - The user ID of the user to clear the continue watching list.
486
+ * If not user Id is passed it clears the markers of the logged in user.
487
+ * @param {string | null} ASSET_ID - The asset ID of the asset to clear the continue watching
488
+ * list. If not asset Id is passed it clears the markers of all assets.
489
+ * @returns {Promise<void>} - A promise that resolves when the continue watching list is cleared.
490
+ * @throws {Error} - An error is thrown if the continue watching list fails to clear.
491
+ * @throws {Error} - An error is thrown if the API type is not portal.
492
+ */
493
+ async clearContinueWatching(USER_ID, ASSET_ID)
494
+ {
495
+ if (this.token === null)
496
+ {
497
+ await this._init();
498
+ }
499
+
500
+ if (this.config.apiType !== "portal")
501
+ {
502
+ throw new Error("This function is only available for portal API type.");
503
+ }
504
+
505
+ _printDatetime(`Clearing continue watching`);
506
+
507
+ try
508
+ {
509
+ await _clearContinueWatching(this.token, this.config.serviceApiUrl, USER_ID, ASSET_ID,
510
+ this.debugMode);
511
+ _printDatetime(`Continue watching cleared`);
512
+ }
513
+ catch (error)
514
+ {
515
+ _printDatetime(`Continue watching failed to clear`);
516
+ throw error;
517
+ }
518
+ }
519
+
520
+ /**
521
+ * @function clearWatchlist
522
+ * @async
523
+ * @description Clears the watchlist.
524
+ * @param {string | null} USER_ID - The user ID of the user to clear the watchlist.
525
+ * If not user Id is passed it clears the watchlist of the logged in user.
526
+ * @returns {Promise<void>} - A promise that resolves when the watchlist is cleared.
527
+ * @throws {Error} - An error is thrown if the watchlist fails to clear.
528
+ * @throws {Error} - An error is thrown if the API type is not portal.
529
+ */
530
+ async clearWatchlist(USER_ID)
531
+ {
532
+ if (this.token === null)
533
+ {
534
+ await this._init();
535
+ }
536
+
537
+ if (this.config.apiType !== "portal")
538
+ {
539
+ throw new Error("This function is only available for portal API type.");
540
+ }
541
+
542
+ _printDatetime(`Clearing watchlist`);
543
+
544
+ try
545
+ {
546
+ USER_ID = USER_ID || this.id;
547
+
548
+ await _clearWatchlist(this.token, this.config.serviceApiUrl, USER_ID, this.debugMode);
549
+ _printDatetime(`Watchlist cleared`);
550
+ }
551
+ catch (error)
552
+ {
553
+ _printDatetime(`Watchlist failed to clear`);
554
+ throw error;
555
+ }
556
+ }
557
+
558
+ /**
559
+ * @function createForm
560
+ * @async
561
+ * @description Gets media.
562
+ * @param {string} CONTENT_DEFINITION_ID - The ID of the content definition the form
563
+ * is going in.
564
+ * @param {JSON} FORM_INFO - The information of the form.
565
+ * JSON format: whatever key value pairs you want to add to the form.
566
+ * @returns {Promise<JSON>} - A promise that resolves when the from is created.
567
+ * Returns the id of the form.
568
+ * @throws {Error} - An error is thrown if the media fails to get.
569
+ * @throws {Error} - An error is thrown if the API type is not portal.
570
+ */
571
+ async createForm(CONTENT_DEFINITION_ID, FORM_INFO)
572
+ {
573
+ if (this.token === null)
574
+ {
575
+ await this._init();
576
+ }
577
+
578
+ if (this.config.apiType !== "portal")
579
+ {
580
+ throw new Error("This function is only available for portal API type.");
581
+ }
582
+
583
+ _printDatetime(`Creating form`);
584
+
585
+ try
586
+ {
587
+ const GET_FORMS_INFO = await _createForm(this.token, this.config.serviceApiUrl,
588
+ CONTENT_DEFINITION_ID, FORM_INFO, this.debugMode);
589
+ _printDatetime(`Form created`);
590
+ return GET_FORMS_INFO;
591
+ }
592
+ catch (error)
593
+ {
594
+ _printDatetime(`Form failed to create`);
595
+ throw error;
596
+ }
597
+ }
598
+
599
+ /**
600
+ * @function getContentCookies
601
+ * @async
602
+ * @description Gets content cookies.
603
+ * @param {string} CONTENT_ID - The Id of the content to retrieve the cookies for.
604
+ * This can be the ID for the content definition of the LiveChannel, or a folder asset
605
+ * ID or a specific Asset ID.
606
+ * @returns {Promise<JSON>} - A promise that resolves when the content cookies are gotten.
607
+ * Returns the information of the gotten content cookies.
608
+ * @throws {Error} - An error is thrown if the content cookies fail to get.
609
+ * @throws {Error} - An error is thrown if the API type is not portal.
610
+ */
611
+ async getContentCookies(CONTENT_ID)
612
+ {
613
+ if (this.token === null)
614
+ {
615
+ await this._init();
616
+ }
617
+
618
+ if (this.config.apiType !== "portal")
619
+ {
620
+ throw new Error("This function is only available for portal API type.");
621
+ }
622
+
623
+ _printDatetime(`Getting content cookies`);
624
+
625
+ try
626
+ {
627
+ const GET_CONTENT_COOKIES_INFO = await _getContentCookies(this.token,
628
+ this.config.serviceApiUrl, CONTENT_ID, this.debugMode);
629
+ _printDatetime(`Content cookies gotten`);
630
+ return GET_CONTENT_COOKIES_INFO;
631
+ }
632
+ catch (error)
633
+ {
634
+ _printDatetime(`Content cookies failed to get`);
635
+ throw error;
636
+ }
637
+ }
638
+
639
+ /**
640
+ * @function getDefaultSiteConfig
641
+ * @async
642
+ * @description Gets default site config.
643
+ * @returns {Promise<Array<JSON>>} - A promise that resolves when the default site config is gotten.
644
+ * Returns the information of the gotten dynamic content.
645
+ * @throws {Error} - An error is thrown if the dynamic content fails to get.
646
+ * @throws {Error} - An error is thrown if the API type is not portal.
647
+ */
648
+ async getDefaultSiteConfig()
649
+ {
650
+ if (this.token === null)
651
+ {
652
+ await this._init();
653
+ }
654
+
655
+ if (this.config.apiType !== "portal")
656
+ {
657
+ throw new Error("This function is only available for portal API type.");
658
+ }
659
+
660
+ _printDatetime(`Getting default site config`);
661
+
662
+ try
663
+ {
664
+ const GET_DYNAMIC_CONTENTS_INFO = await _getDefaultSiteConfig(this.token,
665
+ this.config.serviceApiUrl, this.debugMode);
666
+ _printDatetime(`Default site config gotten`);
667
+ return GET_DYNAMIC_CONTENTS_INFO;
668
+ }
669
+ catch (error)
670
+ {
671
+ _printDatetime(`Default site config failed to get`);
672
+ throw error;
673
+ }
674
+ }
675
+
676
+ /**
677
+ * @function getDynamicContent
678
+ * @async
679
+ * @description Gets dynamic content.
680
+ * @param {string} DYNAMIC_CONTENT_RECORD_ID - The dynamic content record ID.
681
+ * @returns {Promise<JSON>} - A promise that resolves when the dynamic content is gotten.
682
+ * Returns the information of the gotten dynamic content.
683
+ * @throws {Error} - An error is thrown if the dynamic content fails to get.
684
+ * @throws {Error} - An error is thrown if the API type is not portal.
685
+ */
686
+ async getDynamicContent(DYNAMIC_CONTENT_RECORD_ID)
687
+ {
688
+ if (this.token === null)
689
+ {
690
+ await this._init();
691
+ }
692
+
693
+ _printDatetime(`Getting dynamic content: ${DYNAMIC_CONTENT_RECORD_ID}`);
694
+
695
+ try
696
+ {
697
+ const GET_DYNAMIC_CONTENT_INFO = await _getDynamicContent(this.token,
698
+ this.config.serviceApiUrl, DYNAMIC_CONTENT_RECORD_ID, this.debugMode);
699
+ _printDatetime(`Dynamic content gotten: ${DYNAMIC_CONTENT_RECORD_ID}`);
700
+ return GET_DYNAMIC_CONTENT_INFO;
701
+ }
702
+ catch (error)
703
+ {
704
+ _printDatetime(`Dynamic content failed to get: ${DYNAMIC_CONTENT_RECORD_ID}`);
705
+ throw error;
706
+ }
707
+ }
708
+
709
+ /**
710
+ * @function getDynamicContents
711
+ * @async
712
+ * @description Gets dynamic contents.
713
+ * @returns {Promise<Array<JSON>>} - A promise that resolves when the dynamic contents is gotten.
714
+ * Returns the information of the gotten dynamic content.
715
+ * @throws {Error} - An error is thrown if the dynamic content fails to get.
716
+ * @throws {Error} - An error is thrown if the API type is not portal.
717
+ */
718
+ async getDynamicContents()
719
+ {
720
+ if (this.token === null)
721
+ {
722
+ await this._init();
723
+ }
724
+
725
+ if (this.config.apiType !== "portal")
726
+ {
727
+ throw new Error("This function is only available for portal API type.");
728
+ }
729
+
730
+ _printDatetime(`Getting dynamic contents`);
731
+
732
+ try
733
+ {
734
+ const GET_DYNAMIC_CONTENTS_INFO = await _getDynamicContents(this.token,
735
+ this.config.serviceApiUrl, this.debugMode);
736
+ _printDatetime(`Dynamic contents gotten`);
737
+ return GET_DYNAMIC_CONTENTS_INFO;
738
+ }
739
+ catch (error)
740
+ {
741
+ _printDatetime(`Dynamic contents failed to get`);
742
+ throw error;
743
+ }
744
+ }
745
+
746
+ /**
747
+ * @function getMediaGroup
748
+ * @async
749
+ * @description Gets media group.
750
+ * @param {string} MEDIA_GROUP_ID - The ID of the media group.
751
+ * @returns {Promise<JSON>} - A promise that resolves when the media group is gotten.
752
+ * Returns the information of the gotten media group.
753
+ * @throws {Error} - An error is thrown if the media group fails to get.
754
+ * @throws {Error} - An error is thrown if the API type is not portal.
755
+ */
756
+ async getMediaGroup(MEDIA_GROUP_ID)
757
+ {
758
+ if (this.token === null)
759
+ {
760
+ await this._init();
761
+ }
762
+
763
+ if (this.config.apiType !== "portal")
764
+ {
765
+ throw new Error("This function is only available for portal API type.");
766
+ }
767
+
768
+ _printDatetime(`Getting media group: ${MEDIA_GROUP_ID}`);
769
+
770
+ try
771
+ {
772
+ const GET_MEDIA_GROUP_INFO = await _getMediaGroup(this.token, this.config.serviceApiUrl,
773
+ MEDIA_GROUP_ID, this.debugMode);
774
+ _printDatetime(`Media group gotten: ${MEDIA_GROUP_ID}`);
775
+ return GET_MEDIA_GROUP_INFO;
776
+ }
777
+ catch (error)
778
+ {
779
+ _printDatetime(`Media group failed to get: ${MEDIA_GROUP_ID}`);
780
+ throw error;
781
+ }
782
+ }
783
+
784
+ /**
785
+ * @function getMediaItem
786
+ * @async
787
+ * @description Gets media item.
788
+ * @param {string} MEDIA_ITEM_ID - The ID of the media item.
789
+ * @returns {Promise<JSON>} - A promise that resolves when the media item is gotten.
790
+ * Returns the information of the gotten media item.
791
+ * @throws {Error} - An error is thrown if the media item fails to get.
792
+ * @throws {Error} - An error is thrown if the API type is not portal.
793
+ */
794
+ async getMediaItem(MEDIA_ITEM_ID)
795
+ {
796
+ if (this.token === null)
797
+ {
798
+ await this._init();
799
+ }
800
+
801
+ if (this.config.apiType !== "portal")
802
+ {
803
+ throw new Error("This function is only available for portal API type.");
804
+ }
805
+
806
+ _printDatetime(`Getting media item: ${MEDIA_ITEM_ID}`);
807
+
808
+ try
809
+ {
810
+ const GET_MEDIA_ITEM_INFO = await _getMediaItem(this.token, this.config.serviceApiUrl,
811
+ MEDIA_ITEM_ID, this.debugMode);
812
+ _printDatetime(`Media item gotten: ${MEDIA_ITEM_ID}`);
813
+ return GET_MEDIA_ITEM_INFO;
814
+ }
815
+ catch (error)
816
+ {
817
+ _printDatetime(`Media item failed to get: ${MEDIA_ITEM_ID}`);
818
+ throw error;
819
+ }
820
+ }
821
+
822
+ /**
823
+ * @function getMyContent
824
+ * @async
825
+ * @description Gets favorites and continue watching lists of IDs for the logged in user.
826
+ * @returns {Promise<JSON>} - A promise that resolves when the favorites and continue watching
827
+ * lists of IDs are gotten.
828
+ * Returns the information of the gotten favorites and continue watching lists of IDs.
829
+ * @throws {Error} - An error is thrown if the favorites and continue watching lists of IDs fail
830
+ * to get.
831
+ * @throws {Error} - An error is thrown if the API type is not portal.
832
+ */
833
+ async getMyContent()
834
+ {
835
+ if (this.token === null)
836
+ {
837
+ await this._init();
838
+ }
839
+
840
+ if (this.config.apiType !== "portal")
841
+ {
842
+ throw new Error("This function is not available for portal API type.");
843
+ }
844
+
845
+ _printDatetime(`Getting my content`);
846
+
847
+ try
848
+ {
849
+ const GET_MY_CONTENT_INFO = await _getMyContent(this.token, this.config.serviceApiUrl,
850
+ this.debugMode);
851
+ _printDatetime(`My content gotten`);
852
+ return GET_MY_CONTENT_INFO;
853
+ }
854
+ catch (error)
855
+ {
856
+ _printDatetime(`My content failed to get`);
857
+ throw error;
858
+ }
859
+ }
860
+
861
+ /**
862
+ * @function getMyGroup
863
+ * @async
864
+ * @description Gets user's group.
865
+ * @param {string} GROUP_ID - The ID of the group.
866
+ * @returns {Promise<JSON>} - A promise that resolves when the user's group is gotten.
867
+ * Returns the information of the gotten user's group.
868
+ * @throws {Error} - An error is thrown if the user's group fails to get.
869
+ * @throws {Error} - An error is thrown if the API type is not portal.
870
+ */
871
+ async getMyGroup(GROUP_ID)
872
+ {
873
+ if (this.token === null)
874
+ {
875
+ await this._init();
876
+ }
877
+
878
+ _printDatetime(`Getting my group: ${GROUP_ID}`);
879
+
880
+ try
881
+ {
882
+ const GET_MY_GROUP_INFO = await _getMyGroup(this.token, this.config.serviceApiUrl,
883
+ GROUP_ID, this.debugMode);
884
+ _printDatetime(`My group gotten: ${GROUP_ID}`);
885
+ return GET_MY_GROUP_INFO;
886
+ }
887
+ catch (error)
888
+ {
889
+ _printDatetime(`My group failed to get: ${GROUP_ID}`);
890
+ throw error;
891
+ }
892
+ }
893
+
894
+ /**
895
+ * @function getSiteConfig
896
+ * @async
897
+ * @description Gets site config.
898
+ * @param {string} SITE_CONFIG_RECORD_ID - The site config record ID.
899
+ * @returns {Promise<JSON>} - A promise that resolves when the site config is gotten.
900
+ * Returns the information of the gotten site config.
901
+ * @throws {Error} - An error is thrown if the site config fails to get.
902
+ * @throws {Error} - An error is thrown if the API type is not portal.
903
+ */
904
+ async getSiteConfig(SITE_CONFIG_RECORD_ID)
905
+ {
906
+ if (this.token === null)
907
+ {
908
+ await this._init();
909
+ }
910
+
911
+ _printDatetime(`Getting site config: ${SITE_CONFIG_RECORD_ID}`);
912
+
913
+ try
914
+ {
915
+ const GET_DYNAMIC_CONTENT_INFO = await _getSiteConfig(this.token,
916
+ this.config.serviceApiUrl, SITE_CONFIG_RECORD_ID, this.debugMode);
917
+ _printDatetime(`Site config gotten: ${SITE_CONFIG_RECORD_ID}`);
918
+ return GET_DYNAMIC_CONTENT_INFO;
919
+ }
920
+ catch (error)
921
+ {
922
+ _printDatetime(`Site config failed to get: ${SITE_CONFIG_RECORD_ID}`);
923
+ throw error;
924
+ }
925
+ }
926
+
927
+ /**
928
+ * @function mediaSearch
929
+ * @async
930
+ * @description Searches for media.
931
+ * @param {string | null} QUERY - The query of the search.
932
+ * @param {Array<string> | null} IDS - The IDs of the media to be searched.
933
+ * @param {Array<JSON> | null} SORT_FIELDS - The sort fields of the search.
934
+ * JSON format: [{"fieldName": "string", "sortType": ("Ascending" | "Descending")} ...]
935
+ * @param {number | null} OFFSET - The page offset of the search.
936
+ * @param {number | null} SIZE - The page size of the search.
937
+ * @returns {Promise<JSON>} - A promise that resolves when the media are searched.
938
+ * Returns the information of the searched media.
939
+ * @throws {Error} - An error is thrown if the media fail to search.
940
+ * @throws {Error} - An error is thrown if the API type is not portal.
941
+ */
942
+ async mediaSearch(QUERY, IDS, SORT_FIELDS, OFFSET, SIZE)
943
+ {
944
+ if (this.token === null)
945
+ {
946
+ await this._init();
947
+ }
948
+
949
+ if (this.config.apiType !== "portal")
950
+ {
951
+ throw new Error("This function is only available for portal API type.");
952
+ }
953
+
954
+ _printDatetime(`Searching media...`);
955
+
956
+ try
957
+ {
958
+ const MEDIA_SEARCH_INFO = await _mediaSearch(this.token, this.config.serviceApiUrl,
959
+ QUERY, IDS, SORT_FIELDS, OFFSET, SIZE, this.debugMode);
960
+ _printDatetime(`Media search complete`);
961
+ return MEDIA_SEARCH_INFO;
962
+ }
963
+ catch (error)
964
+ {
965
+ _printDatetime(`Media search failed`);
966
+ throw error;
967
+ }
968
+ }
969
+
970
+ }
971
+
972
+ export default NomadMediaSDK;
973
+
974
+
975
+ async function _forgotPassword(URL, USERNAME, DEBUG_MODE)
976
+ {
977
+ const API_URL = `${URL}/api/account/forgot-password`;
978
+
979
+ const HEADERS = new Headers();
980
+ HEADERS.append("Content-Type", "application/json");
981
+
982
+ const BODY = {
983
+ username: USERNAME
984
+ };
985
+
986
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST\nBODY: ${JSON.stringify(BODY, null, 4 )}`);
987
+
988
+ try
989
+ {
990
+ const RESPONSE = await fetch(API_URL, {
991
+ method: "POST",
992
+ headers: HEADERS,
993
+ body: JSON.stringify(BODY)
994
+ }).catch((exception) => {
995
+ throw exception;
996
+ });
997
+
998
+
999
+ if (!RESPONSE.ok) {
1000
+ throw await RESPONSE.json()
1001
+ }
1002
+ }
1003
+ catch (error)
1004
+ {
1005
+ _apiExceptionHandler(error, "Forgot Password Failed");
1006
+ }
1007
+ }
1008
+
1009
+
1010
+
1011
+
1012
+ async function _login(USERNAME, PASSWORD, URL, DEBUG_MODE)
1013
+ {
1014
+ const API_URL = `${URL}/api/account/login`;
1015
+
1016
+ const HEADERS = {
1017
+ "Content-Type": "application/json"
1018
+ };
1019
+
1020
+ const BODY = {
1021
+ "userName": USERNAME,
1022
+ "password": PASSWORD
1023
+ };
1024
+
1025
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST\nBODY: ${JSON.stringify(BODY, null, 4 )}`);
1026
+
1027
+ try
1028
+ {
1029
+ const RESPONSE = await fetch(API_URL, {
1030
+ method: "POST",
1031
+ headers: HEADERS,
1032
+ body: JSON.stringify(BODY)
1033
+ })
1034
+
1035
+ if (!RESPONSE.ok)
1036
+ {
1037
+ throw await RESPONSE.json()
1038
+ }
1039
+
1040
+ return await RESPONSE.json();
1041
+ }
1042
+ catch (error)
1043
+ {
1044
+ _apiExceptionHandler(error, "Login Failed");
1045
+ }
1046
+ }
1047
+
1048
+
1049
+
1050
+
1051
+
1052
+
1053
+ async function _logout(AUTH_TOKEN, URL, DEBUG_MODE)
1054
+ {
1055
+ const API_URL = `${URL}/api/account/logout`;
1056
+
1057
+ // Create header for the request
1058
+ const HEADERS = new Headers();
1059
+ HEADERS.append("Content-Type", "application/json");
1060
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1061
+
1062
+ // Build the POST payload body
1063
+
1064
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST`);
1065
+
1066
+ try
1067
+ {
1068
+ const RESPONSE = await fetch(API_URL, {
1069
+ method: "POST",
1070
+ headers: HEADERS
1071
+ });
1072
+
1073
+ if (!RESPONSE.ok) {
1074
+ throw await RESPONSE.json()
1075
+ }
1076
+ }
1077
+ catch (error)
1078
+ {
1079
+ _apiExceptionHandler(error, "Logout Failed");
1080
+ }
1081
+
1082
+ }
1083
+
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+ async function _refreshToken(AUTH_TOKEN, URL, REFRESH_TOKEN, DEBUG_MODE)
1090
+ {
1091
+ const API_URL = `${URL}/api/account/refresh-token`;
1092
+
1093
+ // Create header for the request
1094
+ const HEADERS = new Headers();
1095
+ HEADERS.append("Content-Type", "application/json");
1096
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1097
+
1098
+ // Build the payload body
1099
+ const BODY = {
1100
+ refreshToken: REFRESH_TOKEN
1101
+ };
1102
+
1103
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST\nBODY: ${JSON.stringify(BODY, null, 4 )}`);
1104
+
1105
+ try
1106
+ {
1107
+ const RESPONSE = await fetch(API_URL, {
1108
+ method: "POST",
1109
+ headers: HEADERS,
1110
+ body: JSON.stringify(BODY)
1111
+ });
1112
+
1113
+ if (!RESPONSE.ok) {
1114
+ throw await RESPONSE.json()
1115
+ }
1116
+
1117
+ return await RESPONSE.json();
1118
+
1119
+ }
1120
+ catch (error)
1121
+ {
1122
+ _apiExceptionHandler(error, "Refreshing Token Failed");
1123
+ }
1124
+ }
1125
+
1126
+
1127
+
1128
+
1129
+
1130
+ async function _resetPassword(URL, USERNAME, TOKEN, NEW_PASSWORD, DEBUG_MODE)
1131
+ {
1132
+ const API_URL = `${URL}/api/account/reset-password`;
1133
+
1134
+ // Create header for the request
1135
+ const HEADERS = new Headers();
1136
+ HEADERS.append("Content-Type", "application/json");
1137
+
1138
+ const BODY = {
1139
+ userName: USERNAME,
1140
+ TOKEN: TOKEN,
1141
+ newPassword: NEW_PASSWORD
1142
+ };
1143
+
1144
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST\nBODY: ${JSON.stringify(BODY, null, 4 )}`);
1145
+
1146
+ try
1147
+ {
1148
+ const RESPONSE = await fetch(API_URL, {
1149
+ method: "POST",
1150
+ headers: HEADERS,
1151
+ body: JSON.stringify(BODY)
1152
+ });
1153
+
1154
+
1155
+ if (!RESPONSE.ok) {
1156
+ throw await RESPONSE.json();
1157
+ }
1158
+ }
1159
+ catch (error)
1160
+ {
1161
+ _apiExceptionHandler(error, "Reset Password Failed");
1162
+ }
1163
+
1164
+ }
1165
+
1166
+
1167
+ function _apiExceptionHandler(error, message) {
1168
+ // Check if we have a response object and error message
1169
+ if (!error) {
1170
+ throw new Error(message);
1171
+ }
1172
+
1173
+ // Check if error is a string
1174
+ if (typeof error === "string") {
1175
+ throw new Error(`${message}: ${error}`);
1176
+ }
1177
+ else if (error.errors)
1178
+ {
1179
+ if (Array.isArray(error.errors)) {
1180
+ let errorString = "";
1181
+ for (let errorIdx = 0; errorIdx < error.errors.length; errorIdx++)
1182
+ {
1183
+ errorString += `${error.errors[errorIdx].code}: ${error.errors[errorIdx].field}: ${error.errors[errorIdx].message}\n`;
1184
+ }
1185
+ throw new Error(`${message}: ${errorString}`);
1186
+ }
1187
+ else if (typeof error.errors === "object") {
1188
+ let errorString = "";
1189
+ for (let key in error.errors) {
1190
+ if (Object.prototype.hasOwnProperty.call(error.errors, key)) {
1191
+ errorString += `${key}: ${error.errors[key]}\n`;
1192
+ }
1193
+ }
1194
+ throw new Error(`${message}: ${errorString}`);
1195
+ }
1196
+ }
1197
+ else
1198
+ {
1199
+ throw new Error(`${message}: ${error.message}`);
1200
+ }
1201
+ }
1202
+
1203
+
1204
+ /**
1205
+ * New GUID
1206
+ *
1207
+ * @returns UUID v4 GUID
1208
+ */
1209
+
1210
+ import { getRandomValues } from 'crypto';
1211
+
1212
+ function _newGuid() {
1213
+ return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
1214
+ (c ^ (getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
1215
+ );
1216
+ }
1217
+
1218
+
1219
+
1220
+ function _printDatetime(msg)
1221
+ {
1222
+ const NOW = new Date();
1223
+ const FORMATTED_TIME = NOW.toLocaleTimeString();
1224
+ console.log(`${FORMATTED_TIME}: ${msg}`);
1225
+ }
1226
+
1227
+
1228
+ async function _sleep(duration) {
1229
+ // Check for valid duration
1230
+ if (!duration || duration === 0) {
1231
+ // Ignore
1232
+ return;
1233
+ }
1234
+
1235
+ // Sleep
1236
+ return new Promise((resolve) => {
1237
+ setTimeout(() => {
1238
+ resolve();
1239
+ }, duration * 1000);
1240
+ });
1241
+ }
1242
+
1243
+
1244
+
1245
+ function _slugify(text) {
1246
+ if (!text || text.trim().length === 0) return "";
1247
+ return text
1248
+ .toString()
1249
+ .normalize("NFD")
1250
+ .replace(/[\u0300-\u036f]/g, "")
1251
+ .toLowerCase()
1252
+ .trim()
1253
+ .replace(/\s+/g, "-")
1254
+ .replace(/[^\w-]+/g, "")
1255
+ .replace(/--+/g, "-");
1256
+ }
1257
+
1258
+
1259
+
1260
+
1261
+
1262
+ async function _clearContinueWatching(AUTH_TOKEN, URL, USER_ID, ASSET_ID, DEBUG_MODE)
1263
+ {
1264
+ let apiUrl = `${URL}/api/media/clear-watching`;
1265
+
1266
+ let params = [];
1267
+ if (USER_ID) params.push(`userId=${USER_ID}`);
1268
+ if (ASSET_ID) params.push(`assetId=${ASSET_ID}`);
1269
+ apiUrl += params.length ? `?${params.join('&')}` : '';
1270
+
1271
+ // Create header for the request
1272
+ const HEADERS = new Headers();
1273
+ HEADERS.append("Content-Type", "application/json");
1274
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1275
+
1276
+ if (DEBUG_MODE) console.log(`URL: ${apiUrl}\nMETHOD: POST`);
1277
+
1278
+ try
1279
+ {
1280
+ const RESPONSE = await fetch(`${apiUrl}`, {
1281
+ method: "POST",
1282
+ headers: HEADERS
1283
+ });
1284
+
1285
+ if (!RESPONSE.ok) {
1286
+ throw await RESPONSE.json()
1287
+ }
1288
+ }
1289
+ catch (error)
1290
+ {
1291
+ _apiExceptionHandler(error, "Clear Continue Watching Failed");
1292
+ }
1293
+ }
1294
+
1295
+
1296
+
1297
+
1298
+ async function _clearWatchlist(AUTH_TOKEN, URL, USER_ID, DEBUG_MODE)
1299
+ {
1300
+ const API_URL = `${URL}/api/media/clear-watchlist?userId=${USER_ID}`;
1301
+
1302
+ // Create header for the request
1303
+ const HEADERS = new Headers();
1304
+ HEADERS.append("Content-Type", "application/json");
1305
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1306
+
1307
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST`);
1308
+
1309
+ try
1310
+ {
1311
+ const RESPONSE = await fetch(`${API_URL}`, {
1312
+ method: "POST",
1313
+ headers: HEADERS
1314
+ });
1315
+
1316
+ if (!RESPONSE.ok) {
1317
+ throw await RESPONSE.json()
1318
+ }
1319
+ }
1320
+ catch (error)
1321
+ {
1322
+ _apiExceptionHandler(error, "Clear Watchlist Failed");
1323
+ }
1324
+ }
1325
+
1326
+
1327
+
1328
+
1329
+ async function _createForm(AUTH_TOKEN, URL, CONTENT_DEFINITION_ID, FORM_INFO, DEBUG_MODE)
1330
+ {
1331
+ const API_URL = `${URL}/api/media/form/${CONTENT_DEFINITION_ID}`;
1332
+
1333
+ // Create header for the request
1334
+ const HEADERS = new Headers();
1335
+ HEADERS.append("Content-Type", "application/json");
1336
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1337
+
1338
+ const BODY = FORM_INFO;
1339
+
1340
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST\nBODY: ${JSON.stringify(BODY, null, 4 )}`);
1341
+
1342
+ try
1343
+ {
1344
+ const RESPONSE = await fetch(API_URL, {
1345
+ method: "POST",
1346
+ headers: HEADERS,
1347
+ body: JSON.stringify(BODY)
1348
+ });
1349
+
1350
+ if (!RESPONSE.ok) {
1351
+ throw await RESPONSE.json()
1352
+ }
1353
+
1354
+ return await RESPONSE.json();
1355
+ }
1356
+ catch (error)
1357
+ {
1358
+ _apiExceptionHandler(error, "Creating Form Failed");
1359
+ }
1360
+ }
1361
+
1362
+
1363
+
1364
+
1365
+ async function _getContentCookies(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1366
+ {
1367
+ const API_URL = `${URL}/api/media/set-cookies/${ID}`;
1368
+
1369
+ // Create header for the request
1370
+ const HEADERS = new Headers();
1371
+ HEADERS.append("Content-Type", "application/json");
1372
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1373
+
1374
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1375
+
1376
+ try
1377
+ {
1378
+ const RESPONSE = await fetch(`${API_URL}`, {
1379
+ method: "GET",
1380
+ headers: HEADERS
1381
+ });
1382
+
1383
+ if (!RESPONSE.ok) {
1384
+ throw await RESPONSE.json()
1385
+ }
1386
+
1387
+ return await RESPONSE.json();
1388
+ }
1389
+ catch (error)
1390
+ {
1391
+ _apiExceptionHandler(error, "Get Content Cookies Failed");
1392
+ }
1393
+ }
1394
+
1395
+
1396
+
1397
+
1398
+ async function _getDefaultSiteConfig(AUTH_TOKEN, URL, DEBUG_MODE)
1399
+ {
1400
+ const API_URL = `${URL}/api/media/config`;
1401
+
1402
+ // Create header for the request
1403
+ const HEADERS = new Headers();
1404
+ HEADERS.append("Content-Type", "application/json");
1405
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1406
+
1407
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1408
+
1409
+ try
1410
+ {
1411
+ const RESPONSE = await fetch(`${API_URL}`, {
1412
+ method: "GET",
1413
+ headers: HEADERS
1414
+ });
1415
+
1416
+ if (!RESPONSE.ok) {
1417
+ throw await RESPONSE.json()
1418
+ }
1419
+
1420
+ return await RESPONSE.json();
1421
+ }
1422
+ catch (error)
1423
+ {
1424
+ _apiExceptionHandler(error, "Get Default Site Config Failed");
1425
+ }
1426
+ }
1427
+
1428
+
1429
+
1430
+
1431
+ async function _getDynamicContent(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1432
+ {
1433
+ const API_URL = `${URL}/api/media/content/${ID}`;
1434
+
1435
+ // Create header for the request
1436
+ const HEADERS = new Headers();
1437
+ HEADERS.append("Content-Type", "application/json");
1438
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1439
+
1440
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1441
+
1442
+ try
1443
+ {
1444
+ const RESPONSE = await fetch(`${API_URL}`, {
1445
+ method: "GET",
1446
+ headers: HEADERS
1447
+ });
1448
+
1449
+ if (!RESPONSE.ok) {
1450
+ throw await RESPONSE.json()
1451
+ }
1452
+
1453
+ return await RESPONSE.json();
1454
+ }
1455
+ catch (error)
1456
+ {
1457
+ _apiExceptionHandler(error, "Get Dynamic Content Failed");
1458
+ }
1459
+ }
1460
+
1461
+
1462
+
1463
+
1464
+ async function _getDynamicContents(AUTH_TOKEN, URL, DEBUG_MODE)
1465
+ {
1466
+ const API_URL = `${URL}/api/media/content`;
1467
+
1468
+ // Create header for the request
1469
+ const HEADERS = new Headers();
1470
+ HEADERS.append("Content-Type", "application/json");
1471
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1472
+
1473
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1474
+
1475
+ try
1476
+ {
1477
+ const RESPONSE = await fetch(`${API_URL}`, {
1478
+ method: "GET",
1479
+ headers: HEADERS
1480
+ });
1481
+
1482
+ if (!RESPONSE.ok) {
1483
+ throw await RESPONSE.json()
1484
+ }
1485
+
1486
+ return await RESPONSE.json();
1487
+ }
1488
+ catch (error)
1489
+ {
1490
+ _apiExceptionHandler(error, "Get Dynamic Contents Failed");
1491
+ }
1492
+ }
1493
+
1494
+
1495
+
1496
+
1497
+ async function _getMediaGroup(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1498
+ {
1499
+ const API_URL = `${URL}/api/media/group/${ID}`;
1500
+
1501
+ // Create header for the request
1502
+ const HEADERS = new Headers();
1503
+ HEADERS.append("Content-Type", "application/json");
1504
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1505
+
1506
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1507
+
1508
+ try
1509
+ {
1510
+ const RESPONSE = await fetch(`${API_URL}`, {
1511
+ method: "GET",
1512
+ headers: HEADERS
1513
+ });
1514
+
1515
+ if (!RESPONSE.ok) {
1516
+ throw await RESPONSE.json()
1517
+ }
1518
+
1519
+ return await RESPONSE.json();
1520
+ }
1521
+ catch (error)
1522
+ {
1523
+ _apiExceptionHandler(error, "Get Media Group Failed");
1524
+ }
1525
+ }
1526
+
1527
+
1528
+
1529
+
1530
+ async function _getMediaItem(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1531
+ {
1532
+ const API_URL = `${URL}/api/media/item/${ID}`;
1533
+
1534
+ // Create header for the request
1535
+ const HEADERS = new Headers();
1536
+ HEADERS.append("Content-Type", "application/json");
1537
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1538
+
1539
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1540
+
1541
+ try
1542
+ {
1543
+ const RESPONSE = await fetch(`${API_URL}`, {
1544
+ method: "GET",
1545
+ headers: HEADERS
1546
+ });
1547
+
1548
+ if (!RESPONSE.ok) {
1549
+ throw await RESPONSE.json()
1550
+ }
1551
+
1552
+ return await RESPONSE.json();
1553
+ }
1554
+ catch (error)
1555
+ {
1556
+ _apiExceptionHandler(error, "Get Media Item Failed");
1557
+ }
1558
+ }
1559
+
1560
+
1561
+
1562
+
1563
+ async function _getMyContent(AUTH_TOKEN, URL, DEBUG_MODE)
1564
+ {
1565
+ const API_URL = `${URL}/api/media/my-content`;
1566
+
1567
+ // Create header for the request
1568
+ const HEADERS = new Headers();
1569
+ HEADERS.append("Content-Type", "application/json");
1570
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1571
+
1572
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1573
+
1574
+ try
1575
+ {
1576
+ const RESPONSE = await fetch(`${API_URL}`, {
1577
+ method: "GET",
1578
+ headers: HEADERS
1579
+ });
1580
+
1581
+ if (!RESPONSE.ok) {
1582
+ throw await RESPONSE.json()
1583
+ }
1584
+
1585
+ return await RESPONSE.json();
1586
+ }
1587
+ catch (error)
1588
+ {
1589
+ _apiExceptionHandler(error, "Get My Content Failed");
1590
+ }
1591
+ }
1592
+
1593
+
1594
+
1595
+
1596
+ async function _getMyGroup(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1597
+ {
1598
+ const API_URL = `${URL}/api/media/my-group/${ID}`;
1599
+
1600
+ // Create header for the request
1601
+ const HEADERS = new Headers();
1602
+ HEADERS.append("Content-Type", "application/json");
1603
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1604
+
1605
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1606
+
1607
+ try
1608
+ {
1609
+ const RESPONSE = await fetch(`${API_URL}`, {
1610
+ method: "GET",
1611
+ headers: HEADERS
1612
+ });
1613
+
1614
+ if (!RESPONSE.ok) {
1615
+ throw await RESPONSE.json()
1616
+ }
1617
+
1618
+ return await RESPONSE.json();
1619
+ }
1620
+ catch (error)
1621
+ {
1622
+ _apiExceptionHandler(error, "Get My Group Failed");
1623
+ }
1624
+ }
1625
+
1626
+
1627
+
1628
+
1629
+ async function _getSiteConfig(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1630
+ {
1631
+ const API_URL = `${URL}/api/media/config/${ID}`;
1632
+
1633
+ // Create header for the request
1634
+ const HEADERS = new Headers();
1635
+ HEADERS.append("Content-Type", "application/json");
1636
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1637
+
1638
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: GET`);
1639
+
1640
+ try
1641
+ {
1642
+ const RESPONSE = await fetch(`${API_URL}`, {
1643
+ method: "GET",
1644
+ headers: HEADERS
1645
+ });
1646
+
1647
+ if (!RESPONSE.ok) {
1648
+ throw await RESPONSE.json()
1649
+ }
1650
+
1651
+ return await RESPONSE.json();
1652
+ }
1653
+ catch (error)
1654
+ {
1655
+ _apiExceptionHandler(error, "Get Site Config Failed");
1656
+ }
1657
+ }
1658
+
1659
+
1660
+
1661
+
1662
+ async function _mediaSearch(AUTH_TOKEN, URL, SEARCH_QUERY, IDS, SORT_FIELDS,
1663
+ PAGE_OFFSET, PAGE_SIZE, DEBUG_MODE)
1664
+ {
1665
+ const API_URL = `${URL}/api/media/search`;
1666
+
1667
+ // Create header for the request
1668
+ const HEADERS = new Headers();
1669
+ HEADERS.append("Content-Type", "application/json");
1670
+ HEADERS.append("Authorization", `Bearer ${AUTH_TOKEN}`);
1671
+
1672
+ const BODY = {
1673
+ searchQuery: SEARCH_QUERY,
1674
+ ids: IDS,
1675
+ sortFields: SORT_FIELDS
1676
+ };
1677
+
1678
+ PAGE_OFFSET ? BODY["pageOffset"] = PAGE_OFFSET : BODY["pageOffset"] = 0;
1679
+ PAGE_SIZE ? BODY["pageSize"] = PAGE_SIZE : BODY["pageSize"] = 100;
1680
+
1681
+ if (DEBUG_MODE) console.log(`URL: ${API_URL}\nMETHOD: POST\nBODY: ${JSON.stringify(BODY, null, 4 )}`);
1682
+
1683
+ try
1684
+ {
1685
+ const RESPONSE = await fetch(`${API_URL}`, {
1686
+ method: "POST",
1687
+ headers: HEADERS,
1688
+ body: JSON.stringify(BODY)
1689
+ });
1690
+
1691
+ if (!RESPONSE.ok) {
1692
+ throw await RESPONSE.json()
1693
+ }
1694
+
1695
+ return await RESPONSE.json();
1696
+ }
1697
+ catch (error)
1698
+ {
1699
+ _apiExceptionHandler(error, "Media Search Failed");
1700
+ }
1701
+ }
1702
+