@nomad-media/public 1.1.4 → 1.1.5

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.
@@ -125,6 +125,11 @@
125
125
 
126
126
 
127
127
 
128
+
129
+
130
+
131
+
132
+
128
133
 
129
134
 
130
135
 
@@ -319,7 +324,7 @@ class NomadMediaSDK {
319
324
 
320
325
  try
321
326
  {
322
- const LOGIN_INFO = await _login(this.config.username, this.config.password,
327
+ const LOGIN_INFO = await _login(this, this.config.username, this.config.password,
323
328
  this.config.serviceApiUrl, this.debugMode);
324
329
  this.token = LOGIN_INFO["token"];
325
330
  this.refreshTokenVal = LOGIN_INFO["refreshToken"];
@@ -353,7 +358,7 @@ class NomadMediaSDK {
353
358
 
354
359
  try
355
360
  {
356
- await _logout(this.token, this.config.serviceApiUrl, this.debugMode);
361
+ await _logout(this, this.token, this.config.serviceApiUrl, this.debugMode);
357
362
  this.token = null;
358
363
  this.refreshTokenVal = null;
359
364
  this.expirationSeconds = null;
@@ -383,7 +388,7 @@ class NomadMediaSDK {
383
388
 
384
389
  try
385
390
  {
386
- const TOKEN_INFO = await _refreshToken(this.token, this.config.serviceApiUrl,
391
+ const TOKEN_INFO = await _refreshToken(this, this.token, this.config.serviceApiUrl,
387
392
  this.refreshTokenVal, this.debugMode);
388
393
  this.token = TOKEN_INFO["token"];
389
394
  _printDatetime(`Token refresh successful`);
@@ -437,7 +442,7 @@ class NomadMediaSDK {
437
442
 
438
443
  try
439
444
  {
440
- await _forgotPassword(this.config.serviceApiUrl, USERNAME, this.debugMode);
445
+ await _forgotPassword(this, this.config.serviceApiUrl, USERNAME, this.debugMode);
441
446
  _printDatetime(`Code sent to ${USERNAME}`);
442
447
  }
443
448
  catch (error)
@@ -466,7 +471,7 @@ class NomadMediaSDK {
466
471
 
467
472
  try
468
473
  {
469
- await _resetPassword(this.config.serviceApiUrl, USERNAME, CODE, NEW_PASSWORD,
474
+ await _resetPassword(this, this.config.serviceApiUrl, USERNAME, CODE, NEW_PASSWORD,
470
475
  this.debugMode);
471
476
  _printDatetime(`Password reset for ${USERNAME}`);
472
477
  }
@@ -506,7 +511,7 @@ class NomadMediaSDK {
506
511
 
507
512
  try
508
513
  {
509
- await _clearContinueWatching(this.token, this.config.serviceApiUrl, USER_ID, ASSET_ID,
514
+ await _clearContinueWatching(this, this.token, this.config.serviceApiUrl, USER_ID, ASSET_ID,
510
515
  this.debugMode);
511
516
  _printDatetime(`Continue watching cleared`);
512
517
  }
@@ -545,7 +550,7 @@ class NomadMediaSDK {
545
550
  {
546
551
  USER_ID = USER_ID || this.id;
547
552
 
548
- await _clearWatchlist(this.token, this.config.serviceApiUrl, USER_ID, this.debugMode);
553
+ await _clearWatchlist(this, this.token, this.config.serviceApiUrl, USER_ID, this.debugMode);
549
554
  _printDatetime(`Watchlist cleared`);
550
555
  }
551
556
  catch (error)
@@ -584,7 +589,7 @@ class NomadMediaSDK {
584
589
 
585
590
  try
586
591
  {
587
- const GET_FORMS_INFO = await _createForm(this.token, this.config.serviceApiUrl,
592
+ const GET_FORMS_INFO = await _createForm(this, this.token, this.config.serviceApiUrl,
588
593
  CONTENT_DEFINITION_ID, FORM_INFO, this.debugMode);
589
594
  _printDatetime(`Form created`);
590
595
  return GET_FORMS_INFO;
@@ -624,7 +629,7 @@ class NomadMediaSDK {
624
629
 
625
630
  try
626
631
  {
627
- const GET_CONTENT_COOKIES_INFO = await _getContentCookies(this.token,
632
+ const GET_CONTENT_COOKIES_INFO = await _getContentCookies(this, this.token,
628
633
  this.config.serviceApiUrl, CONTENT_ID, this.debugMode);
629
634
  _printDatetime(`Content cookies gotten`);
630
635
  return GET_CONTENT_COOKIES_INFO;
@@ -661,7 +666,7 @@ class NomadMediaSDK {
661
666
 
662
667
  try
663
668
  {
664
- const GET_DYNAMIC_CONTENTS_INFO = await _getDefaultSiteConfig(this.token,
669
+ const GET_DYNAMIC_CONTENTS_INFO = await _getDefaultSiteConfig(this, this.token,
665
670
  this.config.serviceApiUrl, this.debugMode);
666
671
  _printDatetime(`Default site config gotten`);
667
672
  return GET_DYNAMIC_CONTENTS_INFO;
@@ -694,7 +699,7 @@ class NomadMediaSDK {
694
699
 
695
700
  try
696
701
  {
697
- const GET_DYNAMIC_CONTENT_INFO = await _getDynamicContent(this.token,
702
+ const GET_DYNAMIC_CONTENT_INFO = await _getDynamicContent(this, this.token,
698
703
  this.config.serviceApiUrl, DYNAMIC_CONTENT_RECORD_ID, this.debugMode);
699
704
  _printDatetime(`Dynamic content gotten: ${DYNAMIC_CONTENT_RECORD_ID}`);
700
705
  return GET_DYNAMIC_CONTENT_INFO;
@@ -731,7 +736,7 @@ class NomadMediaSDK {
731
736
 
732
737
  try
733
738
  {
734
- const GET_DYNAMIC_CONTENTS_INFO = await _getDynamicContents(this.token,
739
+ const GET_DYNAMIC_CONTENTS_INFO = await _getDynamicContents(this, this.token,
735
740
  this.config.serviceApiUrl, this.debugMode);
736
741
  _printDatetime(`Dynamic contents gotten`);
737
742
  return GET_DYNAMIC_CONTENTS_INFO;
@@ -769,7 +774,7 @@ class NomadMediaSDK {
769
774
 
770
775
  try
771
776
  {
772
- const GET_MEDIA_GROUP_INFO = await _getMediaGroup(this.token, this.config.serviceApiUrl,
777
+ const GET_MEDIA_GROUP_INFO = await _getMediaGroup(this, this.token, this.config.serviceApiUrl,
773
778
  MEDIA_GROUP_ID, this.debugMode);
774
779
  _printDatetime(`Media group gotten: ${MEDIA_GROUP_ID}`);
775
780
  return GET_MEDIA_GROUP_INFO;
@@ -807,7 +812,7 @@ class NomadMediaSDK {
807
812
 
808
813
  try
809
814
  {
810
- const GET_MEDIA_ITEM_INFO = await _getMediaItem(this.token, this.config.serviceApiUrl,
815
+ const GET_MEDIA_ITEM_INFO = await _getMediaItem(this, this.token, this.config.serviceApiUrl,
811
816
  MEDIA_ITEM_ID, this.debugMode);
812
817
  _printDatetime(`Media item gotten: ${MEDIA_ITEM_ID}`);
813
818
  return GET_MEDIA_ITEM_INFO;
@@ -846,7 +851,7 @@ class NomadMediaSDK {
846
851
 
847
852
  try
848
853
  {
849
- const GET_MY_CONTENT_INFO = await _getMyContent(this.token, this.config.serviceApiUrl,
854
+ const GET_MY_CONTENT_INFO = await _getMyContent(this, this.token, this.config.serviceApiUrl,
850
855
  this.debugMode);
851
856
  _printDatetime(`My content gotten`);
852
857
  return GET_MY_CONTENT_INFO;
@@ -879,7 +884,7 @@ class NomadMediaSDK {
879
884
 
880
885
  try
881
886
  {
882
- const GET_MY_GROUP_INFO = await _getMyGroup(this.token, this.config.serviceApiUrl,
887
+ const GET_MY_GROUP_INFO = await _getMyGroup(this, this.token, this.config.serviceApiUrl,
883
888
  GROUP_ID, this.debugMode);
884
889
  _printDatetime(`My group gotten: ${GROUP_ID}`);
885
890
  return GET_MY_GROUP_INFO;
@@ -912,7 +917,7 @@ class NomadMediaSDK {
912
917
 
913
918
  try
914
919
  {
915
- const GET_DYNAMIC_CONTENT_INFO = await _getSiteConfig(this.token,
920
+ const GET_DYNAMIC_CONTENT_INFO = await _getSiteConfig(this, this.token,
916
921
  this.config.serviceApiUrl, SITE_CONFIG_RECORD_ID, this.debugMode);
917
922
  _printDatetime(`Site config gotten: ${SITE_CONFIG_RECORD_ID}`);
918
923
  return GET_DYNAMIC_CONTENT_INFO;
@@ -955,7 +960,7 @@ class NomadMediaSDK {
955
960
 
956
961
  try
957
962
  {
958
- const MEDIA_SEARCH_INFO = await _mediaSearch(this.token, this.config.serviceApiUrl,
963
+ const MEDIA_SEARCH_INFO = await _mediaSearch(this, this.token, this.config.serviceApiUrl,
959
964
  QUERY, IDS, SORT_FIELDS, OFFSET, SIZE, this.debugMode);
960
965
  _printDatetime(`Media search complete`);
961
966
  return MEDIA_SEARCH_INFO;
@@ -967,82 +972,100 @@ class NomadMediaSDK {
967
972
  }
968
973
  }
969
974
 
970
- }
971
-
975
+ // misc functions
976
+ /**
977
+ * @function miscFunctons
978
+ * @async
979
+ * @description Calls any nomad function given URL method and body.
980
+ * @param {string} URL_PATH - The URL path of the function.
981
+ * @param {string} METHOD - The method of the function.
982
+ * @param {JSON | null} BODY - The body of the api call.
983
+ * @param {boolean | null} NOT_API_PATH - If the path has /api.
984
+ * @returns {Promise<JSON>} - A promise that resolves when the function is called.
985
+ * Returns the information of the called function.
986
+ * @throws {Error} - An error is thrown if the function fails to call.
987
+ */
988
+
989
+ async miscFunctions(URL_PATH, METHOD, BODY, NOT_API_PATH)
990
+ {
991
+ if (this.token === null)
992
+ {
993
+ await this._init();
994
+ }
995
+
996
+ _printDatetime(`Calling function: ${URL_PATH}`);
997
+
998
+ try
999
+ {
1000
+ let API_URL = `${this.config.serviceApiUrl}/${URL_PATH}`;
1001
+ if (NOT_API_PATH) {
1002
+ API_URL = API_URL.replace('/api', '');
1003
+ API_URL = API_URL.replace('app-api.', '');
1004
+ API_URL = API_URL.replace('admin-app.', '');
1005
+ }
1006
+
1007
+ const HEADERS = new Headers();
1008
+ HEADERS.append("Content-Type", "application/json");
1009
+ HEADERS.append("Authorization", `Bearer ${this.token}`);
1010
+
1011
+ if (this.debugMode)
1012
+ {
1013
+ console.log(`URL: ${API_URL}\nMETHOD: ${METHOD}`);
1014
+ if (BODY) console.log(`BODY: ${JSON.stringify(BODY, null, 4 )}`);
1015
+ }
1016
+
1017
+ const REQUEST = {
1018
+ method: METHOD,
1019
+ headers: HEADERS
1020
+ };
1021
+ if (BODY) {
1022
+ REQUEST.body = JSON.stringify(BODY);
1023
+ }
1024
+
1025
+ const RESPONSE = await fetch(API_URL, REQUEST);
1026
+
1027
+ if (!RESPONSE.ok)
1028
+ {
1029
+ throw await RESPONSE.json();
1030
+ }
1031
+
1032
+ return await RESPONSE.json();
1033
+ }
1034
+ catch (error)
1035
+ {
1036
+ _apiExceptionHandler(error, "API call failed");
1037
+ }
1038
+ }
1039
+ }
1040
+
972
1041
  export default NomadMediaSDK;
973
1042
 
974
1043
 
975
- async function _forgotPassword(URL, USERNAME, DEBUG_MODE)
1044
+
1045
+ async function _forgotPassword(instance, URL, USERNAME, DEBUG_MODE)
976
1046
  {
977
1047
  const API_URL = `${URL}/api/account/forgot-password`;
978
1048
 
979
- const HEADERS = new Headers();
980
- HEADERS.append("Content-Type", "application/json");
981
-
982
1049
  const BODY = {
983
1050
  username: USERNAME
984
1051
  };
985
1052
 
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
- }
1053
+ return await _sendRequest(instance, null, "Forgot Password", API_URL, "POST", null, BODY, DEBUG_MODE);
1007
1054
  }
1008
1055
 
1009
1056
 
1010
1057
 
1011
1058
 
1012
- async function _login(USERNAME, PASSWORD, URL, DEBUG_MODE)
1059
+ async function _login(instance, USERNAME, PASSWORD, URL, DEBUG_MODE)
1013
1060
  {
1014
1061
  const API_URL = `${URL}/api/account/login`;
1015
1062
 
1016
- const HEADERS = {
1017
- "Content-Type": "application/json"
1018
- };
1019
-
1020
1063
  const BODY = {
1021
1064
  "userName": USERNAME,
1022
1065
  "password": PASSWORD
1023
1066
  };
1024
1067
 
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
- }
1068
+ return await _sendRequest(instance, null, "Login", API_URL, "POST", null, BODY, DEBUG_MODE);
1046
1069
  }
1047
1070
 
1048
1071
 
@@ -1050,35 +1073,11 @@ async function _login(USERNAME, PASSWORD, URL, DEBUG_MODE)
1050
1073
 
1051
1074
 
1052
1075
 
1053
- async function _logout(AUTH_TOKEN, URL, DEBUG_MODE)
1076
+ async function _logout(instance, AUTH_TOKEN, URL, DEBUG_MODE)
1054
1077
  {
1055
1078
  const API_URL = `${URL}/api/account/logout`;
1056
1079
 
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
-
1080
+ return await _sendRequest(instance, AUTH_TOKEN, "Logout", API_URL, "POST", null, null, DEBUG_MODE);
1082
1081
  }
1083
1082
 
1084
1083
 
@@ -1086,81 +1085,33 @@ async function _logout(AUTH_TOKEN, URL, DEBUG_MODE)
1086
1085
 
1087
1086
 
1088
1087
 
1089
- async function _refreshToken(AUTH_TOKEN, URL, REFRESH_TOKEN, DEBUG_MODE)
1088
+ async function _refreshToken(instance, AUTH_TOKEN, URL, REFRESH_TOKEN, DEBUG_MODE)
1090
1089
  {
1091
1090
  const API_URL = `${URL}/api/account/refresh-token`;
1092
1091
 
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
1092
  const BODY = {
1100
1093
  refreshToken: REFRESH_TOKEN
1101
1094
  };
1102
1095
 
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
- }
1096
+ const TOKEN_INFO = await _sendRequest(instance, AUTH_TOKEN, "Refreshing Token", API_URL, "POST", null, BODY, DEBUG_MODE);
1097
+ instance.token = TOKEN_INFO.token;
1124
1098
  }
1125
1099
 
1126
1100
 
1127
1101
 
1128
1102
 
1129
1103
 
1130
- async function _resetPassword(URL, USERNAME, TOKEN, NEW_PASSWORD, DEBUG_MODE)
1104
+ async function _resetPassword(instance, URL, USERNAME, TOKEN, NEW_PASSWORD, DEBUG_MODE)
1131
1105
  {
1132
1106
  const API_URL = `${URL}/api/account/reset-password`;
1133
1107
 
1134
- // Create header for the request
1135
- const HEADERS = new Headers();
1136
- HEADERS.append("Content-Type", "application/json");
1137
-
1138
1108
  const BODY = {
1139
1109
  userName: USERNAME,
1140
1110
  TOKEN: TOKEN,
1141
1111
  newPassword: NEW_PASSWORD
1142
1112
  };
1143
1113
 
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
-
1114
+ return await _sendRequest(instance, null, "Reset Password", API_URL, "POST", null, BODY, DEBUG_MODE);
1164
1115
  }
1165
1116
 
1166
1117
 
@@ -1225,6 +1176,75 @@ function _printDatetime(msg)
1225
1176
  }
1226
1177
 
1227
1178
 
1179
+
1180
+
1181
+
1182
+
1183
+ import axios from 'axios';
1184
+
1185
+ const MAX_RETRIES = 5;
1186
+ const TIMEOUT = 30000;
1187
+ const SLEEP_TIME = 60000;
1188
+
1189
+ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYPE, PARAMS, BODY, DEBUG)
1190
+ {
1191
+ const HEADERS = {
1192
+ "Content-Type": "application/json"
1193
+ };
1194
+
1195
+ if (AUTH_TOKEN) HEADERS["Authorization"] = `Bearer ${AUTH_TOKEN}`;
1196
+
1197
+ if (DEBUG) console.log(`URL: ${URL}\nMETHOD: ${METHOD_TYPE}` + (PARAMS ? `\nPARAMS: ${JSON.stringify(PARAMS, null, 4)}` : "") + (BODY ? `\nBODY: ${JSON.stringify(BODY, null, 4)}` : ""));
1198
+
1199
+ let retries = 0;
1200
+ let RESPONSE = null;
1201
+ do
1202
+ {
1203
+ try
1204
+ {
1205
+ RESPONSE = await axios({
1206
+ method: METHOD_TYPE,
1207
+ url: URL,
1208
+ headers: HEADERS,
1209
+ params: PARAMS || {},
1210
+ data: BODY || {},
1211
+ timeout: TIMEOUT
1212
+ });
1213
+
1214
+ if (RESPONSE.status >= 200 && RESPONSE.status < 300)
1215
+ {
1216
+ break;
1217
+ }
1218
+
1219
+ if (RESPONSE.status === 403 && FUNCTION_NAME !== "Refreshing Token")
1220
+ {
1221
+ await instance.refreshToken();
1222
+ }
1223
+ else
1224
+ {
1225
+ throw new Error();
1226
+ }
1227
+
1228
+ }
1229
+ catch (error)
1230
+ {
1231
+ if (error.code === 'ECONNABORTED' && retries < MAX_RETRIES)
1232
+ {
1233
+ retries++;
1234
+ await _sleep(SLEEP_TIME * 2 ** retries);
1235
+ }
1236
+ else
1237
+ {
1238
+ console.error(`${FUNCTION_NAME} failed`, error);
1239
+ _apiExceptionHandler(error, FUNCTION_NAME);
1240
+ }
1241
+ }
1242
+ } while (!RESPONSE || RESPONSE.status < 200 || RESPONSE.status >= 300);
1243
+ try { return RESPONSE.data; }
1244
+ catch (error) { return; }
1245
+ }
1246
+
1247
+
1228
1248
  async function _sleep(duration) {
1229
1249
  // Check for valid duration
1230
1250
  if (!duration || duration === 0) {
@@ -1259,416 +1279,138 @@ function _slugify(text) {
1259
1279
 
1260
1280
 
1261
1281
 
1262
- async function _clearContinueWatching(AUTH_TOKEN, URL, USER_ID, ASSET_ID, DEBUG_MODE)
1282
+ async function _clearContinueWatching(instance, AUTH_TOKEN, URL, USER_ID, ASSET_ID, DEBUG_MODE)
1263
1283
  {
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}`);
1284
+ const API_URL = `${URL}/api/media/clear-watching`;
1275
1285
 
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
- });
1286
+ const PARAMS = {
1287
+ userId: USER_ID,
1288
+ assetId: ASSET_ID
1289
+ };
1284
1290
 
1285
- if (!RESPONSE.ok) {
1286
- throw await RESPONSE.json()
1287
- }
1288
- }
1289
- catch (error)
1290
- {
1291
- _apiExceptionHandler(error, "Clear Continue Watching Failed");
1292
- }
1291
+ return await _sendRequest(instance, AUTH_TOKEN, "Clear Continue Watching", API_URL, "POST", PARAMS, null, DEBUG_MODE);
1293
1292
  }
1294
1293
 
1295
1294
 
1296
1295
 
1297
1296
 
1298
- async function _clearWatchlist(AUTH_TOKEN, URL, USER_ID, DEBUG_MODE)
1297
+ async function _clearWatchlist(instance, AUTH_TOKEN, URL, USER_ID, DEBUG_MODE)
1299
1298
  {
1300
1299
  const API_URL = `${URL}/api/media/clear-watchlist?userId=${USER_ID}`;
1301
1300
 
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
- }
1301
+ return await _sendRequest(instance, AUTH_TOKEN, "Clear Watchlist", API_URL, "POST", null, null, DEBUG_MODE);
1324
1302
  }
1325
1303
 
1326
1304
 
1327
1305
 
1328
1306
 
1329
- async function _createForm(AUTH_TOKEN, URL, CONTENT_DEFINITION_ID, FORM_INFO, DEBUG_MODE)
1307
+ async function _createForm(instance, AUTH_TOKEN, URL, CONTENT_DEFINITION_ID, FORM_INFO, DEBUG_MODE)
1330
1308
  {
1331
1309
  const API_URL = `${URL}/api/media/form/${CONTENT_DEFINITION_ID}`;
1332
1310
 
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
1311
  const BODY = FORM_INFO;
1339
1312
 
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
- }
1313
+ return await _sendRequest(instance, AUTH_TOKEN, "Creating Form", API_URL, "POST", null, BODY, DEBUG_MODE);
1360
1314
  }
1361
1315
 
1362
1316
 
1363
1317
 
1364
1318
 
1365
- async function _getContentCookies(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1319
+ async function _getContentCookies(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
1366
1320
  {
1367
1321
  const API_URL = `${URL}/api/media/set-cookies/${ID}`;
1368
1322
 
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
- }
1323
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Content Cookies", API_URL, "GET", null, null, DEBUG_MODE);
1393
1324
  }
1394
1325
 
1395
1326
 
1396
1327
 
1397
1328
 
1398
- async function _getDefaultSiteConfig(AUTH_TOKEN, URL, DEBUG_MODE)
1329
+ async function _getDefaultSiteConfig(instance, AUTH_TOKEN, URL, DEBUG_MODE)
1399
1330
  {
1400
1331
  const API_URL = `${URL}/api/media/config`;
1401
1332
 
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
- }
1333
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Default Site Config", API_URL, "GET", null, null, DEBUG_MODE);
1426
1334
  }
1427
1335
 
1428
1336
 
1429
1337
 
1430
1338
 
1431
- async function _getDynamicContent(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1339
+ async function _getDynamicContent(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
1432
1340
  {
1433
1341
  const API_URL = `${URL}/api/media/content/${ID}`;
1434
1342
 
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
- }
1343
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Dynamic Content", API_URL, "GET", null, null, DEBUG_MODE);
1459
1344
  }
1460
1345
 
1461
1346
 
1462
1347
 
1463
1348
 
1464
- async function _getDynamicContents(AUTH_TOKEN, URL, DEBUG_MODE)
1349
+ async function _getDynamicContents(instance, AUTH_TOKEN, URL, DEBUG_MODE)
1465
1350
  {
1466
1351
  const API_URL = `${URL}/api/media/content`;
1467
1352
 
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
- }
1353
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Dynamic Contents", API_URL, "GET", null, null, DEBUG_MODE);
1492
1354
  }
1493
1355
 
1494
1356
 
1495
1357
 
1496
1358
 
1497
- async function _getMediaGroup(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1359
+ async function _getMediaGroup(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
1498
1360
  {
1499
1361
  const API_URL = `${URL}/api/media/group/${ID}`;
1500
1362
 
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
- }
1363
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Media Group", API_URL, "GET", null, null, DEBUG_MODE);
1525
1364
  }
1526
1365
 
1527
1366
 
1528
1367
 
1529
1368
 
1530
- async function _getMediaItem(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1369
+ async function _getMediaItem(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
1531
1370
  {
1532
1371
  const API_URL = `${URL}/api/media/item/${ID}`;
1533
1372
 
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
- }
1373
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Media Item", API_URL, "GET", null, null, DEBUG_MODE);
1558
1374
  }
1559
1375
 
1560
1376
 
1561
1377
 
1562
1378
 
1563
- async function _getMyContent(AUTH_TOKEN, URL, DEBUG_MODE)
1379
+ async function _getMyContent(instance, AUTH_TOKEN, URL, DEBUG_MODE)
1564
1380
  {
1565
1381
  const API_URL = `${URL}/api/media/my-content`;
1566
1382
 
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
- }
1383
+ return await _sendRequest(instance, AUTH_TOKEN, "Get My Content", API_URL, "GET", null, null, DEBUG_MODE);
1591
1384
  }
1592
1385
 
1593
1386
 
1594
1387
 
1595
1388
 
1596
- async function _getMyGroup(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1389
+ async function _getMyGroup(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
1597
1390
  {
1598
1391
  const API_URL = `${URL}/api/media/my-group/${ID}`;
1599
1392
 
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
- }
1393
+ return await _sendRequest(instance, AUTH_TOKEN, "Get My Group", API_URL, "GET", null, null, DEBUG_MODE);
1624
1394
  }
1625
1395
 
1626
1396
 
1627
1397
 
1628
1398
 
1629
- async function _getSiteConfig(AUTH_TOKEN, URL, ID, DEBUG_MODE)
1399
+ async function _getSiteConfig(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
1630
1400
  {
1631
1401
  const API_URL = `${URL}/api/media/config/${ID}`;
1632
1402
 
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
- }
1403
+ return await _sendRequest(instance, AUTH_TOKEN, "Get Site Config", API_URL, "GET", null, null, DEBUG_MODE);
1657
1404
  }
1658
1405
 
1659
1406
 
1660
1407
 
1661
1408
 
1662
- async function _mediaSearch(AUTH_TOKEN, URL, SEARCH_QUERY, IDS, SORT_FIELDS,
1409
+ async function _mediaSearch(instance, AUTH_TOKEN, URL, SEARCH_QUERY, IDS, SORT_FIELDS,
1663
1410
  PAGE_OFFSET, PAGE_SIZE, DEBUG_MODE)
1664
1411
  {
1665
1412
  const API_URL = `${URL}/api/media/search`;
1666
1413
 
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
1414
  const BODY = {
1673
1415
  searchQuery: SEARCH_QUERY,
1674
1416
  ids: IDS,
@@ -1678,25 +1420,6 @@ async function _mediaSearch(AUTH_TOKEN, URL, SEARCH_QUERY, IDS, SORT_FIELDS,
1678
1420
  PAGE_OFFSET ? BODY["pageOffset"] = PAGE_OFFSET : BODY["pageOffset"] = 0;
1679
1421
  PAGE_SIZE ? BODY["pageSize"] = PAGE_SIZE : BODY["pageSize"] = 100;
1680
1422
 
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
- }
1423
+ return await _sendRequest(instance, AUTH_TOKEN, "Media Search", API_URL, "POST", null, BODY, DEBUG_MODE);
1701
1424
  }
1702
1425
 
@@ -1,25 +1,5 @@
1
- import{promises as fs}from"fs";import path from"path";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.debugMode=t.debugMode||!1,NomadMediaSDK.instance=this)}async _init(){await this.login(),this.__scheduleTokenRefresh()}async login(){_printDatetime("Logging in as "+this.config.username);try{var t=await _login(this.config.username,this.config.password,this.config.serviceApiUrl,this.debugMode);this.token=t.token,this.refreshTokenVal=t.refreshToken,this.expirationSeconds=Date.now()+1e3*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&&await this._init(),_printDatetime("Logging out");try{await _logout(this.token,this.config.serviceApiUrl,this.debugMode),this.token=null,this.refreshTokenVal=null,this.expirationSeconds=null,this.userSessionId=null,this.id=null,_printDatetime("Logout successful")}catch(t){throw _printDatetime("Loggout failed"),t}}async refreshToken(){_printDatetime("Refreshing token");try{var t=await _refreshToken(this.token,this.config.serviceApiUrl,this.refreshTokenVal,this.debugMode);this.token=t.token,_printDatetime("Token refresh successful")}catch(t){throw _printDatetime("Token refresh failed"),t}}__scheduleTokenRefresh(){var t;this.token&&this.expirationSeconds&&0<(t=this.expirationSeconds-Date.now())&&(this.refreshTimer&&clearTimeout(this.refreshTimer),this.refreshTimer=setTimeout(async()=>{await this.refreshToken(),this.__scheduleTokenRefresh()},t-5e3))}async forgotPassword(){var e=this.config.username;_printDatetime("Sending code to "+e);try{await _forgotPassword(this.config.serviceApiUrl,e,this.debugMode),_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.config.serviceApiUrl,i,t,e,this.debugMode),_printDatetime("Password reset for "+i)}catch(t){throw _printDatetime("Password failed to reset for "+i),t}}async clearContinueWatching(t,e){if(null===this.token&&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.token,this.config.serviceApiUrl,t,e,this.debugMode),_printDatetime("Continue watching cleared")}catch(t){throw _printDatetime("Continue watching failed to clear"),t}}async clearWatchlist(t){if(null===this.token&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Clearing watchlist");try{t=t||this.id,await _clearWatchlist(this.token,this.config.serviceApiUrl,t,this.debugMode),_printDatetime("Watchlist cleared")}catch(t){throw _printDatetime("Watchlist failed to clear"),t}}async createForm(t,e){if(null===this.token&&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.token,this.config.serviceApiUrl,t,e,this.debugMode);return _printDatetime("Form created"),i}catch(t){throw _printDatetime("Form failed to create"),t}}async getContentCookies(t){if(null===this.token&&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.token,this.config.serviceApiUrl,t,this.debugMode);return _printDatetime("Content cookies gotten"),e}catch(t){throw _printDatetime("Content cookies failed to get"),t}}async getDefaultSiteConfig(){if(null===this.token&&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.token,this.config.serviceApiUrl,this.debugMode);return _printDatetime("Default site config gotten"),t}catch(t){throw _printDatetime("Default site config failed to get"),t}}async getDynamicContent(e){null===this.token&&await this._init(),_printDatetime("Getting dynamic content: "+e);try{var t=await _getDynamicContent(this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Dynamic content gotten: "+e),t}catch(t){throw _printDatetime("Dynamic content failed to get: "+e),t}}async getDynamicContents(){if(null===this.token&&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.token,this.config.serviceApiUrl,this.debugMode);return _printDatetime("Dynamic contents gotten"),t}catch(t){throw _printDatetime("Dynamic contents failed to get"),t}}async getMediaGroup(e){if(null===this.token&&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 t=await _getMediaGroup(this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Media group gotten: "+e),t}catch(t){throw _printDatetime("Media group failed to get: "+e),t}}async getMediaItem(e){if(null===this.token&&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.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Media item gotten: "+e),t}catch(t){throw _printDatetime("Media item failed to get: "+e),t}}async getMyContent(){if(null===this.token&&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.token,this.config.serviceApiUrl,this.debugMode);return _printDatetime("My content gotten"),t}catch(t){throw _printDatetime("My content failed to get"),t}}async getMyGroup(e){null===this.token&&await this._init(),_printDatetime("Getting my group: "+e);try{var t=await _getMyGroup(this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("My group gotten: "+e),t}catch(t){throw _printDatetime("My group failed to get: "+e),t}}async getSiteConfig(e){null===this.token&&await this._init(),_printDatetime("Getting site config: "+e);try{var t=await _getSiteConfig(this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Site config gotten: "+e),t}catch(t){throw _printDatetime("Site config failed to get: "+e),t}}async mediaSearch(t,e,i,a,n){if(null===this.token&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Searching media...");try{var o=await _mediaSearch(this.token,this.config.serviceApiUrl,t,e,i,a,n,this.debugMode);return _printDatetime("Media search complete"),o}catch(t){throw _printDatetime("Media search failed"),t}}}export default NomadMediaSDK;async function _forgotPassword(t,e,i){var t=t+"/api/account/forgot-password",a=new Headers,e=(a.append("Content-Type","application/json"),{username:e});i&&console.log(`URL: ${t}
2
- METHOD: POST
3
- BODY: `+JSON.stringify(e,null,4));try{var n=await fetch(t,{method:"POST",headers:a,body:JSON.stringify(e)}).catch(t=>{throw t});if(!n.ok)throw await n.json()}catch(t){_apiExceptionHandler(t,"Forgot Password Failed")}}async function _login(t,e,i,a){i+="/api/account/login",t={userName:t,password:e};a&&console.log(`URL: ${i}
4
- METHOD: POST
5
- BODY: `+JSON.stringify(t,null,4));try{var n=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Login Failed")}}async function _logout(t,e,i){var e=e+"/api/account/logout",a=new Headers;a.append("Content-Type","application/json"),a.append("Authorization","Bearer "+t),i&&console.log(`URL: ${e}
6
- METHOD: POST`);try{var n=await fetch(e,{method:"POST",headers:a});if(!n.ok)throw await n.json()}catch(t){_apiExceptionHandler(t,"Logout Failed")}}async function _refreshToken(t,e,i,a){var e=e+"/api/account/refresh-token",n=new Headers,t=(n.append("Content-Type","application/json"),n.append("Authorization","Bearer "+t),{refreshToken:i});a&&console.log(`URL: ${e}
7
- METHOD: POST
8
- BODY: `+JSON.stringify(t,null,4));try{var o=await fetch(e,{method:"POST",headers:n,body:JSON.stringify(t)});if(o.ok)return await o.json();throw await o.json()}catch(t){_apiExceptionHandler(t,"Refreshing Token Failed")}}async function _resetPassword(t,e,i,a,n){var t=t+"/api/account/reset-password",o=new Headers,e=(o.append("Content-Type","application/json"),{userName:e,TOKEN:i,newPassword:a});n&&console.log(`URL: ${t}
9
- METHOD: POST
10
- BODY: `+JSON.stringify(e,null,4));try{var r=await fetch(t,{method:"POST",headers:o,body:JSON.stringify(e)});if(!r.ok)throw await r.json()}catch(t){_apiExceptionHandler(t,"Reset Password Failed")}}function _apiExceptionHandler(i,a){if(!i)throw new Error(a);if("string"==typeof i)throw new Error(a+": "+i);if(!i.errors)throw new Error(a+": "+i.message);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`;throw new Error(a+": "+e)}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`);throw new Error(a+": "+t)}}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)}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,a,n){var e=e+"/api/media/clear-watching",o=[],i=(i&&o.push("userId="+i),a&&o.push("assetId="+a),e+=o.length?"?"+o.join("&"):"",new Headers);i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),n&&console.log(`URL: ${e}
11
- METHOD: POST`);try{var r=await fetch(e,{method:"POST",headers:i});if(!r.ok)throw await r.json()}catch(t){_apiExceptionHandler(t,"Clear Continue Watching Failed")}}async function _clearWatchlist(t,e,i,a){e=e+"/api/media/clear-watchlist?userId="+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
12
- METHOD: POST`);try{var n=await fetch(e,{method:"POST",headers:i});if(!n.ok)throw await n.json()}catch(t){_apiExceptionHandler(t,"Clear Watchlist Failed")}}async function _createForm(t,e,i,a,n){e=e+"/api/media/form/"+i,i=new Headers,i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),t=a;n&&console.log(`URL: ${e}
13
- METHOD: POST
14
- BODY: `+JSON.stringify(t,null,4));try{var o=await fetch(e,{method:"POST",headers:i,body:JSON.stringify(t)});if(o.ok)return await o.json();throw await o.json()}catch(t){_apiExceptionHandler(t,"Creating Form Failed")}}async function _getContentCookies(t,e,i,a){e=e+"/api/media/set-cookies/"+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
15
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:i});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Content Cookies Failed")}}async function _getDefaultSiteConfig(t,e,i){var e=e+"/api/media/config",a=new Headers;a.append("Content-Type","application/json"),a.append("Authorization","Bearer "+t),i&&console.log(`URL: ${e}
16
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:a});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Default Site Config Failed")}}async function _getDynamicContent(t,e,i,a){e=e+"/api/media/content/"+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
17
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:i});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Dynamic Content Failed")}}async function _getDynamicContents(t,e,i){var e=e+"/api/media/content",a=new Headers;a.append("Content-Type","application/json"),a.append("Authorization","Bearer "+t),i&&console.log(`URL: ${e}
18
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:a});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Dynamic Contents Failed")}}async function _getMediaGroup(t,e,i,a){e=e+"/api/media/group/"+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
19
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:i});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Media Group Failed")}}async function _getMediaItem(t,e,i,a){e=e+"/api/media/item/"+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
20
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:i});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Media Item Failed")}}async function _getMyContent(t,e,i){var e=e+"/api/media/my-content",a=new Headers;a.append("Content-Type","application/json"),a.append("Authorization","Bearer "+t),i&&console.log(`URL: ${e}
21
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:a});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get My Content Failed")}}async function _getMyGroup(t,e,i,a){e=e+"/api/media/my-group/"+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
22
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:i});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get My Group Failed")}}async function _getSiteConfig(t,e,i,a){e=e+"/api/media/config/"+i,i=new Headers;i.append("Content-Type","application/json"),i.append("Authorization","Bearer "+t),a&&console.log(`URL: ${e}
23
- METHOD: GET`);try{var n=await fetch(e,{method:"GET",headers:i});if(n.ok)return await n.json();throw await n.json()}catch(t){_apiExceptionHandler(t,"Get Site Config Failed")}}async function _mediaSearch(t,e,i,a,n,o,r,s){var e=e+"/api/media/search",c=new Headers,t=(c.append("Content-Type","application/json"),c.append("Authorization","Bearer "+t),{searchQuery:i,ids:a,sortFields:n});t.pageOffset=o||0,t.pageSize=r||100,s&&console.log(`URL: ${e}
24
- METHOD: POST
25
- BODY: `+JSON.stringify(t,null,4));try{var h=await fetch(e,{method:"POST",headers:c,body:JSON.stringify(t)});if(h.ok)return await h.json();throw await h.json()}catch(t){_apiExceptionHandler(t,"Media Search Failed")}}
1
+ import{promises as fs}from"fs";import path from"path";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.debugMode=t.debugMode||!1,NomadMediaSDK.instance=this)}async _init(){await this.login(),this.__scheduleTokenRefresh()}async login(){_printDatetime("Logging in as "+this.config.username);try{var t=await _login(this,this.config.username,this.config.password,this.config.serviceApiUrl,this.debugMode);this.token=t.token,this.refreshTokenVal=t.refreshToken,this.expirationSeconds=Date.now()+1e3*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&&await this._init(),_printDatetime("Logging out");try{await _logout(this,this.token,this.config.serviceApiUrl,this.debugMode),this.token=null,this.refreshTokenVal=null,this.expirationSeconds=null,this.userSessionId=null,this.id=null,_printDatetime("Logout successful")}catch(t){throw _printDatetime("Loggout failed"),t}}async refreshToken(){_printDatetime("Refreshing token");try{var t=await _refreshToken(this,this.token,this.config.serviceApiUrl,this.refreshTokenVal,this.debugMode);this.token=t.token,_printDatetime("Token refresh successful")}catch(t){throw _printDatetime("Token refresh failed"),t}}__scheduleTokenRefresh(){var t;this.token&&this.expirationSeconds&&0<(t=this.expirationSeconds-Date.now())&&(this.refreshTimer&&clearTimeout(this.refreshTimer),this.refreshTimer=setTimeout(async()=>{await this.refreshToken(),this.__scheduleTokenRefresh()},t-5e3))}async forgotPassword(){var e=this.config.username;_printDatetime("Sending code to "+e);try{await _forgotPassword(this,this.config.serviceApiUrl,e,this.debugMode),_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,this.config.serviceApiUrl,i,t,e,this.debugMode),_printDatetime("Password reset for "+i)}catch(t){throw _printDatetime("Password failed to reset for "+i),t}}async clearContinueWatching(t,e){if(null===this.token&&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,this.token,this.config.serviceApiUrl,t,e,this.debugMode),_printDatetime("Continue watching cleared")}catch(t){throw _printDatetime("Continue watching failed to clear"),t}}async clearWatchlist(t){if(null===this.token&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Clearing watchlist");try{t=t||this.id,await _clearWatchlist(this,this.token,this.config.serviceApiUrl,t,this.debugMode),_printDatetime("Watchlist cleared")}catch(t){throw _printDatetime("Watchlist failed to clear"),t}}async createForm(t,e){if(null===this.token&&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,this.token,this.config.serviceApiUrl,t,e,this.debugMode);return _printDatetime("Form created"),i}catch(t){throw _printDatetime("Form failed to create"),t}}async getContentCookies(t){if(null===this.token&&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,this.token,this.config.serviceApiUrl,t,this.debugMode);return _printDatetime("Content cookies gotten"),e}catch(t){throw _printDatetime("Content cookies failed to get"),t}}async getDefaultSiteConfig(){if(null===this.token&&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,this.token,this.config.serviceApiUrl,this.debugMode);return _printDatetime("Default site config gotten"),t}catch(t){throw _printDatetime("Default site config failed to get"),t}}async getDynamicContent(e){null===this.token&&await this._init(),_printDatetime("Getting dynamic content: "+e);try{var t=await _getDynamicContent(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Dynamic content gotten: "+e),t}catch(t){throw _printDatetime("Dynamic content failed to get: "+e),t}}async getDynamicContents(){if(null===this.token&&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,this.token,this.config.serviceApiUrl,this.debugMode);return _printDatetime("Dynamic contents gotten"),t}catch(t){throw _printDatetime("Dynamic contents failed to get"),t}}async getMediaGroup(e){if(null===this.token&&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 t=await _getMediaGroup(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Media group gotten: "+e),t}catch(t){throw _printDatetime("Media group failed to get: "+e),t}}async getMediaItem(e){if(null===this.token&&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,this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("Media item gotten: "+e),t}catch(t){throw _printDatetime("Media item failed to get: "+e),t}}async getMyContent(){if(null===this.token&&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,this.token,this.config.serviceApiUrl,this.debugMode);return _printDatetime("My content gotten"),t}catch(t){throw _printDatetime("My content failed to get"),t}}async getMyGroup(e){null===this.token&&await this._init(),_printDatetime("Getting my group: "+e);try{var t=await _getMyGroup(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return _printDatetime("My group gotten: "+e),t}catch(t){throw _printDatetime("My group failed to get: "+e),t}}async getSiteConfig(e){null===this.token&&await this._init(),_printDatetime("Getting site config: "+e);try{var t=await _getSiteConfig(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return _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&&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,this.token,this.config.serviceApiUrl,t,e,i,n,r,this.debugMode);return _printDatetime("Media search complete"),a}catch(t){throw _printDatetime("Media search failed"),t}}async miscFunctions(e,i,n,r){null===this.token&&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"),a.append("Authorization","Bearer "+this.token),this.debugMode&&(console.log(`URL: ${t}
2
+ METHOD: `+i),n)&&console.log("BODY: "+JSON.stringify(n,null,4)),{method:i,headers:a}),s=(n&&(o.body=JSON.stringify(n)),await fetch(t,o));if(s.ok)return await s.json();throw await s.json()}catch(t){_apiExceptionHandler(t,"API call failed")}}}export default NomadMediaSDK;async function _forgotPassword(t,e,i,n){return _sendRequest(t,null,"Forgot Password",e+"/api/account/forgot-password","POST",null,{username:i},n)}async function _login(t,e,i,n,r){return _sendRequest(t,null,"Login",n+"/api/account/login","POST",null,{userName:e,password:i},r)}async function _logout(t,e,i,n){return _sendRequest(t,e,"Logout",i+"/api/account/logout","POST",null,null,n)}async function _refreshToken(t,e,i,n,r){e=await _sendRequest(t,e,"Refreshing Token",i+"/api/account/refresh-token","POST",null,{refreshToken:n},r);t.token=e.token}async function _resetPassword(t,e,i,n,r,a){return _sendRequest(t,null,"Reset Password",e+"/api/account/reset-password","POST",null,{userName:i,TOKEN:n,newPassword:r},a)}function _apiExceptionHandler(i,n){if(!i)throw new Error(n);if("string"==typeof i)throw new Error(n+": "+i);if(!i.errors)throw new Error(n+": "+i.message);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`;throw new Error(n+": "+e)}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`);throw new Error(n+": "+t)}}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 axios from"axios";let MAX_RETRIES=5,TIMEOUT=3e4,SLEEP_TIME=6e4;async function _sendRequest(t,e,i,n,r,a,o,s){var c={"Content-Type":"application/json"};e&&(c.Authorization="Bearer "+e),s&&console.log(`URL: ${n}
3
+ METHOD: `+r+(a?`
4
+ PARAMS: `+JSON.stringify(a,null,4):"")+(o?`
5
+ BODY: `+JSON.stringify(o,null,4):""));let l=0,h=null;do{try{if(200<=(h=await axios({method:r,url:n,headers:c,params:a||{},data:o||{},timeout:TIMEOUT})).status&&h.status<300)break;if(403!==h.status||"Refreshing Token"===i)throw new Error;await t.refreshToken()}catch(t){"ECONNABORTED"===t.code&&l<MAX_RETRIES?(l++,await _sleep(SLEEP_TIME*2**l)):(console.error(i+" failed",t),_apiExceptionHandler(t,i))}}while(!h||h.status<200||300<=h.status);try{return h.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,n,r,a){return _sendRequest(t,e,"Clear Continue Watching",i+"/api/media/clear-watching","POST",{userId:n,assetId:r},null,a)}async function _clearWatchlist(t,e,i,n,r){return _sendRequest(t,e,"Clear Watchlist",i+"/api/media/clear-watchlist?userId="+n,"POST",null,null,r)}async function _createForm(t,e,i,n,r,a){return _sendRequest(t,e,"Creating Form",i+"/api/media/form/"+n,"POST",null,r,a)}async function _getContentCookies(t,e,i,n,r){return _sendRequest(t,e,"Get Content Cookies",i+"/api/media/set-cookies/"+n,"GET",null,null,r)}async function _getDefaultSiteConfig(t,e,i,n){return _sendRequest(t,e,"Get Default Site Config",i+"/api/media/config","GET",null,null,n)}async function _getDynamicContent(t,e,i,n,r){return _sendRequest(t,e,"Get Dynamic Content",i+"/api/media/content/"+n,"GET",null,null,r)}async function _getDynamicContents(t,e,i,n){return _sendRequest(t,e,"Get Dynamic Contents",i+"/api/media/content","GET",null,null,n)}async function _getMediaGroup(t,e,i,n,r){return _sendRequest(t,e,"Get Media Group",i+"/api/media/group/"+n,"GET",null,null,r)}async function _getMediaItem(t,e,i,n,r){return _sendRequest(t,e,"Get Media Item",i+"/api/media/item/"+n,"GET",null,null,r)}async function _getMyContent(t,e,i,n){return _sendRequest(t,e,"Get My Content",i+"/api/media/my-content","GET",null,null,n)}async function _getMyGroup(t,e,i,n,r){return _sendRequest(t,e,"Get My Group",i+"/api/media/my-group/"+n,"GET",null,null,r)}async function _getSiteConfig(t,e,i,n,r){return _sendRequest(t,e,"Get Site Config",i+"/api/media/config/"+n,"GET",null,null,r)}async function _mediaSearch(t,e,i,n,r,a,o,s,c){i+="/api/media/search",n={searchQuery:n,ids:r,sortFields:a};return n.pageOffset=o||0,n.pageSize=s||100,_sendRequest(t,e,"Media Search",i,"POST",null,n,c)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nomad-media/public",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "Nomad Media NPM",
5
5
  "main": "dist/nomad-media-sdk.min.js",
6
6
  "type": "module",