@nomad-media/public 1.1.11 → 1.1.13
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.
|
@@ -286,6 +286,11 @@ class NomadMediaSDK {
|
|
|
286
286
|
this.id = null
|
|
287
287
|
this.debugMode = config.debugMode || false;
|
|
288
288
|
|
|
289
|
+
if (this.config.serviceApiUrl[this.config.serviceApiUrl.length - 1] === "/")
|
|
290
|
+
{
|
|
291
|
+
this.config.serviceApiUrl = this.config.serviceApiUrl.slice(0, -1);
|
|
292
|
+
}
|
|
293
|
+
|
|
289
294
|
NomadMediaSDK.instance = this;
|
|
290
295
|
|
|
291
296
|
return this;
|
|
@@ -303,9 +308,6 @@ class NomadMediaSDK {
|
|
|
303
308
|
async _init()
|
|
304
309
|
{
|
|
305
310
|
await this.login();
|
|
306
|
-
|
|
307
|
-
// Schedule token refresh
|
|
308
|
-
this.__scheduleTokenRefresh();
|
|
309
311
|
}
|
|
310
312
|
|
|
311
313
|
// account functions
|
|
@@ -328,7 +330,7 @@ class NomadMediaSDK {
|
|
|
328
330
|
this.config.serviceApiUrl, this.debugMode);
|
|
329
331
|
this.token = LOGIN_INFO["token"];
|
|
330
332
|
this.refreshTokenVal = LOGIN_INFO["refreshToken"];
|
|
331
|
-
this.expirationSeconds =
|
|
333
|
+
this.expirationSeconds = LOGIN_INFO["expirationSeconds"];
|
|
332
334
|
this.userSessionId = LOGIN_INFO["userSessionId"];
|
|
333
335
|
this.id = LOGIN_INFO["id"];
|
|
334
336
|
_printDatetime(`Login successful`);
|
|
@@ -388,10 +390,9 @@ class NomadMediaSDK {
|
|
|
388
390
|
|
|
389
391
|
try
|
|
390
392
|
{
|
|
391
|
-
const
|
|
393
|
+
const REFRESHED = await _refreshToken(this, this.token, this.config.serviceApiUrl,
|
|
392
394
|
this.refreshTokenVal, this.debugMode);
|
|
393
|
-
|
|
394
|
-
_printDatetime(`Token refresh successful`);
|
|
395
|
+
if (REFRESHED) _printDatetime(`Token refresh successful`);
|
|
395
396
|
}
|
|
396
397
|
catch (error)
|
|
397
398
|
{
|
|
@@ -400,31 +401,6 @@ class NomadMediaSDK {
|
|
|
400
401
|
}
|
|
401
402
|
}
|
|
402
403
|
|
|
403
|
-
/**
|
|
404
|
-
* @function scheduleTokenRefresh
|
|
405
|
-
* @private
|
|
406
|
-
* @description Schedules a token refresh for the duration set by the value returned in the login.
|
|
407
|
-
* @returns {Promise<void>} - A promise that resolves when the token is refreshed.
|
|
408
|
-
* Updates the token class variable.
|
|
409
|
-
* @throws {Error} - An error is thrown if the token refresh fails.
|
|
410
|
-
*/
|
|
411
|
-
__scheduleTokenRefresh() {
|
|
412
|
-
if (this.token && this.expirationSeconds) {
|
|
413
|
-
const remainingTime = this.expirationSeconds - Date.now();
|
|
414
|
-
if (remainingTime > 0) {
|
|
415
|
-
// Clear existing timer (if any)
|
|
416
|
-
if (this.refreshTimer) {
|
|
417
|
-
clearTimeout(this.refreshTimer);
|
|
418
|
-
}
|
|
419
|
-
// Schedule a refresh slightly before token expiration
|
|
420
|
-
this.refreshTimer = setTimeout(async () => {
|
|
421
|
-
await this.refreshToken();
|
|
422
|
-
this.__scheduleTokenRefresh();
|
|
423
|
-
}, remainingTime - 5000); // Refresh 5 seconds before expiration
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
404
|
/**
|
|
429
405
|
* @function forgotPassword
|
|
430
406
|
* @async
|
|
@@ -591,7 +567,7 @@ class NomadMediaSDK {
|
|
|
591
567
|
{
|
|
592
568
|
const GET_FORMS_INFO = await _createForm(this, this.token, this.config.serviceApiUrl,
|
|
593
569
|
CONTENT_DEFINITION_ID, FORM_INFO, this.debugMode);
|
|
594
|
-
_printDatetime(`Form created`);
|
|
570
|
+
if (GET_FORMS_INFO) _printDatetime(`Form created`);
|
|
595
571
|
return GET_FORMS_INFO;
|
|
596
572
|
}
|
|
597
573
|
catch (error)
|
|
@@ -631,7 +607,7 @@ class NomadMediaSDK {
|
|
|
631
607
|
{
|
|
632
608
|
const GET_CONTENT_COOKIES_INFO = await _getContentCookies(this, this.token,
|
|
633
609
|
this.config.serviceApiUrl, CONTENT_ID, this.debugMode);
|
|
634
|
-
_printDatetime(`Content cookies gotten`);
|
|
610
|
+
if (GET_CONTENT_COOKIES_INFO) _printDatetime(`Content cookies gotten`);
|
|
635
611
|
return GET_CONTENT_COOKIES_INFO;
|
|
636
612
|
}
|
|
637
613
|
catch (error)
|
|
@@ -668,7 +644,7 @@ class NomadMediaSDK {
|
|
|
668
644
|
{
|
|
669
645
|
const GET_DYNAMIC_CONTENTS_INFO = await _getDefaultSiteConfig(this, this.token,
|
|
670
646
|
this.config.serviceApiUrl, this.debugMode);
|
|
671
|
-
_printDatetime(`Default site config gotten`);
|
|
647
|
+
if (GET_DYNAMIC_CONTENTS_INFO) _printDatetime(`Default site config gotten`);
|
|
672
648
|
return GET_DYNAMIC_CONTENTS_INFO;
|
|
673
649
|
}
|
|
674
650
|
catch (error)
|
|
@@ -701,7 +677,7 @@ class NomadMediaSDK {
|
|
|
701
677
|
{
|
|
702
678
|
const GET_DYNAMIC_CONTENT_INFO = await _getDynamicContent(this, this.token,
|
|
703
679
|
this.config.serviceApiUrl, DYNAMIC_CONTENT_RECORD_ID, this.debugMode);
|
|
704
|
-
_printDatetime(`Dynamic content gotten: ${DYNAMIC_CONTENT_RECORD_ID}`);
|
|
680
|
+
if (GET_DYNAMIC_CONTENT_INFO) _printDatetime(`Dynamic content gotten: ${DYNAMIC_CONTENT_RECORD_ID}`);
|
|
705
681
|
return GET_DYNAMIC_CONTENT_INFO;
|
|
706
682
|
}
|
|
707
683
|
catch (error)
|
|
@@ -738,7 +714,7 @@ class NomadMediaSDK {
|
|
|
738
714
|
{
|
|
739
715
|
const GET_DYNAMIC_CONTENTS_INFO = await _getDynamicContents(this, this.token,
|
|
740
716
|
this.config.serviceApiUrl, this.debugMode);
|
|
741
|
-
_printDatetime(`Dynamic contents gotten`);
|
|
717
|
+
if (GET_DYNAMIC_CONTENTS_INFO) _printDatetime(`Dynamic contents gotten`);
|
|
742
718
|
return GET_DYNAMIC_CONTENTS_INFO;
|
|
743
719
|
}
|
|
744
720
|
catch (error)
|
|
@@ -776,7 +752,7 @@ class NomadMediaSDK {
|
|
|
776
752
|
{
|
|
777
753
|
const GET_MEDIA_GROUP_INFO = await _getMediaGroup(this, this.token, this.config.serviceApiUrl,
|
|
778
754
|
MEDIA_GROUP_ID, this.debugMode);
|
|
779
|
-
_printDatetime(`Media group gotten: ${MEDIA_GROUP_ID}`);
|
|
755
|
+
if (GET_MEDIA_GROUP_INFO) _printDatetime(`Media group gotten: ${MEDIA_GROUP_ID}`);
|
|
780
756
|
return GET_MEDIA_GROUP_INFO;
|
|
781
757
|
}
|
|
782
758
|
catch (error)
|
|
@@ -814,7 +790,7 @@ class NomadMediaSDK {
|
|
|
814
790
|
{
|
|
815
791
|
const GET_MEDIA_ITEM_INFO = await _getMediaItem(this, this.token, this.config.serviceApiUrl,
|
|
816
792
|
MEDIA_ITEM_ID, this.debugMode);
|
|
817
|
-
_printDatetime(`Media item gotten: ${MEDIA_ITEM_ID}`);
|
|
793
|
+
if (GET_MEDIA_ITEM_INFO) _printDatetime(`Media item gotten: ${MEDIA_ITEM_ID}`);
|
|
818
794
|
return GET_MEDIA_ITEM_INFO;
|
|
819
795
|
}
|
|
820
796
|
catch (error)
|
|
@@ -853,7 +829,7 @@ class NomadMediaSDK {
|
|
|
853
829
|
{
|
|
854
830
|
const GET_MY_CONTENT_INFO = await _getMyContent(this, this.token, this.config.serviceApiUrl,
|
|
855
831
|
this.debugMode);
|
|
856
|
-
_printDatetime(`My content gotten`);
|
|
832
|
+
if (GET_MY_CONTENT_INFO) _printDatetime(`My content gotten`);
|
|
857
833
|
return GET_MY_CONTENT_INFO;
|
|
858
834
|
}
|
|
859
835
|
catch (error)
|
|
@@ -886,7 +862,7 @@ class NomadMediaSDK {
|
|
|
886
862
|
{
|
|
887
863
|
const GET_MY_GROUP_INFO = await _getMyGroup(this, this.token, this.config.serviceApiUrl,
|
|
888
864
|
GROUP_ID, this.debugMode);
|
|
889
|
-
_printDatetime(`My group gotten: ${GROUP_ID}`);
|
|
865
|
+
if (GET_MY_GROUP_INFO) _printDatetime(`My group gotten: ${GROUP_ID}`);
|
|
890
866
|
return GET_MY_GROUP_INFO;
|
|
891
867
|
}
|
|
892
868
|
catch (error)
|
|
@@ -919,7 +895,7 @@ class NomadMediaSDK {
|
|
|
919
895
|
{
|
|
920
896
|
const GET_DYNAMIC_CONTENT_INFO = await _getSiteConfig(this, this.token,
|
|
921
897
|
this.config.serviceApiUrl, SITE_CONFIG_RECORD_ID, this.debugMode);
|
|
922
|
-
_printDatetime(`Site config gotten: ${SITE_CONFIG_RECORD_ID}`);
|
|
898
|
+
if (GET_DYNAMIC_CONTENT_INFO) _printDatetime(`Site config gotten: ${SITE_CONFIG_RECORD_ID}`);
|
|
923
899
|
return GET_DYNAMIC_CONTENT_INFO;
|
|
924
900
|
}
|
|
925
901
|
catch (error)
|
|
@@ -962,7 +938,7 @@ class NomadMediaSDK {
|
|
|
962
938
|
{
|
|
963
939
|
const MEDIA_SEARCH_INFO = await _mediaSearch(this, this.token, this.config.serviceApiUrl,
|
|
964
940
|
QUERY, IDS, SORT_FIELDS, OFFSET, SIZE, this.debugMode);
|
|
965
|
-
_printDatetime(`Media search complete`);
|
|
941
|
+
if (MEDIA_SEARCH_INFO) _printDatetime(`Media search complete`);
|
|
966
942
|
return MEDIA_SEARCH_INFO;
|
|
967
943
|
}
|
|
968
944
|
catch (error)
|
|
@@ -1117,6 +1093,7 @@ async function _logout(instance, AUTH_TOKEN, URL, DEBUG_MODE)
|
|
|
1117
1093
|
|
|
1118
1094
|
|
|
1119
1095
|
|
|
1096
|
+
|
|
1120
1097
|
async function _refreshToken(instance, AUTH_TOKEN, URL, REFRESH_TOKEN, DEBUG_MODE)
|
|
1121
1098
|
{
|
|
1122
1099
|
const API_URL = `${URL}/api/account/refresh-token`;
|
|
@@ -1126,7 +1103,9 @@ async function _refreshToken(instance, AUTH_TOKEN, URL, REFRESH_TOKEN, DEBUG_MOD
|
|
|
1126
1103
|
};
|
|
1127
1104
|
|
|
1128
1105
|
const TOKEN_INFO = await _sendRequest(instance, AUTH_TOKEN, "Refreshing Token", API_URL, "POST", null, BODY, DEBUG_MODE);
|
|
1129
|
-
|
|
1106
|
+
instance.token = TOKEN_INFO.token;
|
|
1107
|
+
|
|
1108
|
+
return true;
|
|
1130
1109
|
}
|
|
1131
1110
|
|
|
1132
1111
|
|
|
@@ -1150,12 +1129,12 @@ async function _resetPassword(instance, URL, USERNAME, TOKEN, NEW_PASSWORD, DEBU
|
|
|
1150
1129
|
function _apiExceptionHandler(error, message) {
|
|
1151
1130
|
// Check if we have a response object and error message
|
|
1152
1131
|
if (!error) {
|
|
1153
|
-
|
|
1132
|
+
console.error(message);
|
|
1154
1133
|
}
|
|
1155
1134
|
|
|
1156
1135
|
// Check if error is a string
|
|
1157
1136
|
if (typeof error === "string") {
|
|
1158
|
-
|
|
1137
|
+
console.error(`${message}: ${error}`);
|
|
1159
1138
|
}
|
|
1160
1139
|
else if (error.errors)
|
|
1161
1140
|
{
|
|
@@ -1165,7 +1144,7 @@ function _apiExceptionHandler(error, message) {
|
|
|
1165
1144
|
{
|
|
1166
1145
|
errorString += `${error.errors[errorIdx].code}: ${error.errors[errorIdx].field}: ${error.errors[errorIdx].message}\n`;
|
|
1167
1146
|
}
|
|
1168
|
-
|
|
1147
|
+
console.error(`${message}: ${errorString}`);
|
|
1169
1148
|
}
|
|
1170
1149
|
else if (typeof error.errors === "object") {
|
|
1171
1150
|
let errorString = "";
|
|
@@ -1174,13 +1153,15 @@ function _apiExceptionHandler(error, message) {
|
|
|
1174
1153
|
errorString += `${key}: ${error.errors[key]}\n`;
|
|
1175
1154
|
}
|
|
1176
1155
|
}
|
|
1177
|
-
|
|
1156
|
+
console.error(`${message}: ${errorString}`);
|
|
1178
1157
|
}
|
|
1179
1158
|
}
|
|
1180
1159
|
else
|
|
1181
1160
|
{
|
|
1182
|
-
|
|
1161
|
+
console.error(`${message}: ${error.message}`);
|
|
1183
1162
|
}
|
|
1163
|
+
|
|
1164
|
+
return null;
|
|
1184
1165
|
}
|
|
1185
1166
|
|
|
1186
1167
|
|
|
@@ -1216,7 +1197,7 @@ import axios from 'axios';
|
|
|
1216
1197
|
|
|
1217
1198
|
const MAX_RETRIES = 5;
|
|
1218
1199
|
const TIMEOUT = 30000;
|
|
1219
|
-
const SLEEP_TIME =
|
|
1200
|
+
const SLEEP_TIME = .5;
|
|
1220
1201
|
|
|
1221
1202
|
async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYPE, PARAMS, BODY, DEBUG)
|
|
1222
1203
|
{
|
|
@@ -1226,20 +1207,22 @@ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYP
|
|
|
1226
1207
|
|
|
1227
1208
|
if (AUTH_TOKEN) HEADERS["Authorization"] = `Bearer ${AUTH_TOKEN}`;
|
|
1228
1209
|
|
|
1229
|
-
if (DEBUG) console.log(`URL: ${URL}\nMETHOD: ${METHOD_TYPE}` + (PARAMS && PARAMS.size === 0 ? `\nPARAMS: ${JSON.stringify(PARAMS, null, 4)}` : "") + (BODY ? `\nBODY: ${JSON.stringify(BODY, null, 4)}` : ""));
|
|
1210
|
+
if (DEBUG && FUNCTION_NAME !== "Login") console.log(`URL: ${URL}\nMETHOD: ${METHOD_TYPE}` + (PARAMS && PARAMS.size === 0 ? `\nPARAMS: ${JSON.stringify(PARAMS, null, 4)}` : "") + (BODY ? `\nBODY: ${JSON.stringify(BODY, null, 4)}` : ""));
|
|
1230
1211
|
|
|
1231
1212
|
let retries = 0;
|
|
1232
1213
|
let RESPONSE = null;
|
|
1214
|
+
let refreshedToken = false;
|
|
1233
1215
|
do
|
|
1234
1216
|
{
|
|
1235
1217
|
try
|
|
1236
1218
|
{
|
|
1219
|
+
refreshedToken = true;
|
|
1237
1220
|
RESPONSE = await axios({
|
|
1238
1221
|
method: METHOD_TYPE,
|
|
1239
1222
|
url: URL,
|
|
1240
1223
|
headers: HEADERS,
|
|
1241
|
-
params: PARAMS
|
|
1242
|
-
data: BODY
|
|
1224
|
+
params: PARAMS,
|
|
1225
|
+
data: BODY,
|
|
1243
1226
|
timeout: TIMEOUT
|
|
1244
1227
|
});
|
|
1245
1228
|
|
|
@@ -1248,29 +1231,27 @@ async function _sendRequest(instance, AUTH_TOKEN, FUNCTION_NAME, URL, METHOD_TYP
|
|
|
1248
1231
|
break;
|
|
1249
1232
|
}
|
|
1250
1233
|
|
|
1251
|
-
|
|
1252
|
-
{
|
|
1253
|
-
await instance.refreshToken();
|
|
1254
|
-
}
|
|
1255
|
-
else
|
|
1256
|
-
{
|
|
1257
|
-
throw new Error();
|
|
1258
|
-
}
|
|
1259
|
-
|
|
1234
|
+
throw new Error();
|
|
1260
1235
|
}
|
|
1261
1236
|
catch (error)
|
|
1262
1237
|
{
|
|
1263
|
-
if (error
|
|
1238
|
+
if (error?.response?.status === 403 && FUNCTION_NAME !== "Refresh Token" && !refreshedToken)
|
|
1264
1239
|
{
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1240
|
+
await instance.refreshToken();
|
|
1241
|
+
HEADERS["Authorization"] = `Bearer ${instance.token}`;
|
|
1242
|
+
refreshedToken = true;
|
|
1243
|
+
}
|
|
1244
|
+
else if (error.code === 'ECONNABORTED')
|
|
1245
|
+
{
|
|
1246
|
+
await _sleep(SLEEP_TIME * 2 ** (retries - 1));
|
|
1247
|
+
}
|
|
1268
1248
|
else
|
|
1269
1249
|
{
|
|
1270
|
-
_apiExceptionHandler(error.response?.data.message ? error.response.data.message : error.message, `${FUNCTION_NAME} failed`);
|
|
1250
|
+
return _apiExceptionHandler(error.response?.data.message ? error.response.data.message : error.message, `${FUNCTION_NAME} failed`);
|
|
1271
1251
|
}
|
|
1252
|
+
++retries;
|
|
1272
1253
|
}
|
|
1273
|
-
} while (!RESPONSE || RESPONSE.status < 200 || RESPONSE.status >= 300);
|
|
1254
|
+
} while ((!RESPONSE || RESPONSE.status < 200 || RESPONSE.status >= 300) && retries < MAX_RETRIES);
|
|
1274
1255
|
try { return RESPONSE.data; }
|
|
1275
1256
|
catch (error) { return; }
|
|
1276
1257
|
}
|
|
@@ -1351,7 +1332,7 @@ async function _getContentCookies(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
|
|
|
1351
1332
|
{
|
|
1352
1333
|
const API_URL = `${URL}/api/media/set-cookies/${ID}`;
|
|
1353
1334
|
|
|
1354
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Content Cookies", API_URL, "GET",
|
|
1335
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Content Cookies", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1355
1336
|
}
|
|
1356
1337
|
|
|
1357
1338
|
|
|
@@ -1361,7 +1342,7 @@ async function _getDefaultSiteConfig(instance, AUTH_TOKEN, URL, DEBUG_MODE)
|
|
|
1361
1342
|
{
|
|
1362
1343
|
const API_URL = `${URL}/api/media/config`;
|
|
1363
1344
|
|
|
1364
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Default Site Config", API_URL, "GET",
|
|
1345
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Default Site Config", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1365
1346
|
}
|
|
1366
1347
|
|
|
1367
1348
|
|
|
@@ -1371,7 +1352,7 @@ async function _getDynamicContent(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
|
|
|
1371
1352
|
{
|
|
1372
1353
|
const API_URL = `${URL}/api/media/content/${ID}`;
|
|
1373
1354
|
|
|
1374
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Dynamic Content", API_URL, "GET",
|
|
1355
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Dynamic Content", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1375
1356
|
}
|
|
1376
1357
|
|
|
1377
1358
|
|
|
@@ -1381,7 +1362,7 @@ async function _getDynamicContents(instance, AUTH_TOKEN, URL, DEBUG_MODE)
|
|
|
1381
1362
|
{
|
|
1382
1363
|
const API_URL = `${URL}/api/media/content`;
|
|
1383
1364
|
|
|
1384
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Dynamic Contents", API_URL, "GET",
|
|
1365
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Dynamic Contents", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1385
1366
|
}
|
|
1386
1367
|
|
|
1387
1368
|
|
|
@@ -1391,7 +1372,7 @@ async function _getMediaGroup(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
|
|
|
1391
1372
|
{
|
|
1392
1373
|
const API_URL = `${URL}/api/media/group/${ID}`;
|
|
1393
1374
|
|
|
1394
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Media Group", API_URL, "GET",
|
|
1375
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Media Group", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1395
1376
|
}
|
|
1396
1377
|
|
|
1397
1378
|
|
|
@@ -1401,7 +1382,7 @@ async function _getMediaItem(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
|
|
|
1401
1382
|
{
|
|
1402
1383
|
const API_URL = `${URL}/api/media/item/${ID}`;
|
|
1403
1384
|
|
|
1404
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Media Item", API_URL, "GET",
|
|
1385
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Media Item", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1405
1386
|
}
|
|
1406
1387
|
|
|
1407
1388
|
|
|
@@ -1411,7 +1392,7 @@ async function _getMyContent(instance, AUTH_TOKEN, URL, DEBUG_MODE)
|
|
|
1411
1392
|
{
|
|
1412
1393
|
const API_URL = `${URL}/api/media/my-content`;
|
|
1413
1394
|
|
|
1414
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get My Content", API_URL, "GET",
|
|
1395
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get My Content", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1415
1396
|
}
|
|
1416
1397
|
|
|
1417
1398
|
|
|
@@ -1421,7 +1402,7 @@ async function _getMyGroup(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
|
|
|
1421
1402
|
{
|
|
1422
1403
|
const API_URL = `${URL}/api/media/my-group/${ID}`;
|
|
1423
1404
|
|
|
1424
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get My Group", API_URL, "GET",
|
|
1405
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get My Group", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1425
1406
|
}
|
|
1426
1407
|
|
|
1427
1408
|
|
|
@@ -1431,7 +1412,7 @@ async function _getSiteConfig(instance, AUTH_TOKEN, URL, ID, DEBUG_MODE)
|
|
|
1431
1412
|
{
|
|
1432
1413
|
const API_URL = `${URL}/api/media/config/${ID}`;
|
|
1433
1414
|
|
|
1434
|
-
return await _sendRequest(instance, AUTH_TOKEN, "Get Site Config", API_URL, "GET",
|
|
1415
|
+
return await _sendRequest(instance, AUTH_TOKEN, "Get Site Config", API_URL, "GET", undefined, undefined, DEBUG_MODE);
|
|
1435
1416
|
}
|
|
1436
1417
|
|
|
1437
1418
|
|
|
@@ -1,5 +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()
|
|
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)),c=await s.text();if(!s.ok)try{throw JSON.parse(c)}catch(t){throw c}try{return JSON.parse(c)}catch(t){return c}}catch(t){_apiExceptionHandler(t,"API call failed")}}}export default NomadMediaSDK;async function _forgotPassword(t,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){n+="/api/account/login",e={userName:e,password:i};try{return await _sendRequest(t,null,"Login",n,"POST",null,e,r)}catch(t){throw t.message.includes("409")?new Error("Invalid username or password"):t}}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){
|
|
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,"/"===this.config.serviceApiUrl[this.config.serviceApiUrl.length-1]&&(this.config.serviceApiUrl=this.config.serviceApiUrl.slice(0,-1)),NomadMediaSDK.instance=this)}async _init(){await this.login()}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=t.expirationSeconds,this.userSessionId=t.userSessionId,this.id=t.id,_printDatetime("Login successful")}catch(t){throw _printDatetime("Login failed"),t}}async logout(){null===this.token&&this.config.username&&await this._init(),_printDatetime("Logging out");try{await _logout(this,this.token,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{await _refreshToken(this,this.token,this.config.serviceApiUrl,this.refreshTokenVal,this.debugMode)&&_printDatetime("Token refresh successful")}catch(t){throw _printDatetime("Token refresh failed"),t}}async forgotPassword(){var e=this.config.username;_printDatetime("Sending code to "+e);try{await _forgotPassword(this,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&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Clearing continue watching");try{await _clearContinueWatching(this,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&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Clearing watchlist");try{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&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Creating form");try{var i=await _createForm(this,this.token,this.config.serviceApiUrl,t,e,this.debugMode);return i&&_printDatetime("Form created"),i}catch(t){throw _printDatetime("Form failed to create"),t}}async getContentCookies(t){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting content cookies");try{var e=await _getContentCookies(this,this.token,this.config.serviceApiUrl,t,this.debugMode);return e&&_printDatetime("Content cookies gotten"),e}catch(t){throw _printDatetime("Content cookies failed to get"),t}}async getDefaultSiteConfig(){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting default site config");try{var t=await _getDefaultSiteConfig(this,this.token,this.config.serviceApiUrl,this.debugMode);return t&&_printDatetime("Default site config gotten"),t}catch(t){throw _printDatetime("Default site config failed to get"),t}}async getDynamicContent(e){null===this.token&&this.config.username&&await this._init(),_printDatetime("Getting dynamic content: "+e);try{var t=await _getDynamicContent(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return t&&_printDatetime("Dynamic content gotten: "+e),t}catch(t){throw _printDatetime("Dynamic content failed to get: "+e),t}}async getDynamicContents(){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting dynamic contents");try{var t=await _getDynamicContents(this,this.token,this.config.serviceApiUrl,this.debugMode);return t&&_printDatetime("Dynamic contents gotten"),t}catch(t){throw _printDatetime("Dynamic contents failed to get"),t}}async getMediaGroup(e){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting media group: "+e);try{var t=await _getMediaGroup(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return t&&_printDatetime("Media group gotten: "+e),t}catch(t){throw _printDatetime("Media group failed to get: "+e),t}}async getMediaItem(e){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Getting media item: "+e);try{var t=await _getMediaItem(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return t&&_printDatetime("Media item gotten: "+e),t}catch(t){throw _printDatetime("Media item failed to get: "+e),t}}async getMyContent(){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is not available for portal API type.");_printDatetime("Getting my content");try{var t=await _getMyContent(this,this.token,this.config.serviceApiUrl,this.debugMode);return t&&_printDatetime("My content gotten"),t}catch(t){throw _printDatetime("My content failed to get"),t}}async getMyGroup(e){null===this.token&&this.config.username&&await this._init(),_printDatetime("Getting my group: "+e);try{var t=await _getMyGroup(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return t&&_printDatetime("My group gotten: "+e),t}catch(t){throw _printDatetime("My group failed to get: "+e),t}}async getSiteConfig(e){null===this.token&&this.config.username&&await this._init(),_printDatetime("Getting site config: "+e);try{var t=await _getSiteConfig(this,this.token,this.config.serviceApiUrl,e,this.debugMode);return t&&_printDatetime("Site config gotten: "+e),t}catch(t){throw _printDatetime("Site config failed to get: "+e),t}}async mediaSearch(t,e,i,n,r){if(null===this.token&&this.config.username&&await this._init(),"portal"!==this.config.apiType)throw new Error("This function is only available for portal API type.");_printDatetime("Searching media...");try{var a=await _mediaSearch(this,this.token,this.config.serviceApiUrl,t,e,i,n,r,this.debugMode);return a&&_printDatetime("Media search complete"),a}catch(t){throw _printDatetime("Media search failed"),t}}async miscFunctions(e,i,n,r){null===this.token&&this.config.username&&await this._init(),_printDatetime("Calling function: "+e);try{let t=this.config.serviceApiUrl+"/"+e;r&&(t=(t=(t=t.replace("/api","")).replace("app-api.","")).replace("admin-app.",""));var a=new Headers,o=(a.append("Content-Type","application/json"),null!==this.token&&a.append("Authorization","Bearer "+this.token),this.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)),c=await s.text();if(!s.ok)try{throw JSON.parse(c)}catch(t){throw c}try{return JSON.parse(c)}catch(t){return c}}catch(t){_apiExceptionHandler(t,"API call failed")}}}export default NomadMediaSDK;async function _forgotPassword(t,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){n+="/api/account/login",e={userName:e,password:i};try{return await _sendRequest(t,null,"Login",n,"POST",null,e,r)}catch(t){throw t.message.includes("409")?new Error("Invalid username or password"):t}}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);return t.token=e.token,!0}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||console.error(n),"string"==typeof i)console.error(n+": "+i);else if(i.errors){if(Array.isArray(i.errors)){let e="";for(let t=0;t<i.errors.length;t++)e+=`${i.errors[t].code}: ${i.errors[t].field}: ${i.errors[t].message}\n`;console.error(n+": "+e)}else if("object"==typeof i.errors){let t="";for(var e in i.errors)Object.prototype.hasOwnProperty.call(i.errors,e)&&(t+=`${e}: ${i.errors[e]}\n`);console.error(n+": "+t)}}else console.error(n+": "+i.message);return null}import{getRandomValues}from"crypto";function _newGuid(){return([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,t=>(t^getRandomValues(new Uint8Array(1))[0]&15>>t/4).toString(16))}function _printDatetime(t){var e=(new Date).toLocaleTimeString();console.log(e+": "+t)}import axios from"axios";let MAX_RETRIES=5,TIMEOUT=3e4,SLEEP_TIME=.5;async function _sendRequest(e,t,i,n,r,a,o,s){var c={"Content-Type":"application/json"};t&&(c.Authorization="Bearer "+t),s&&"Login"!==i&&console.log(`URL: ${n}
|
|
3
3
|
METHOD: `+r+(a&&0===a.size?`
|
|
4
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
|
|
5
|
+
BODY: `+JSON.stringify(o,null,4):""));let l=0,h=null,u=!1;do{try{if(u=!0,200<=(h=await axios({method:r,url:n,headers:c,params:a,data:o,timeout:TIMEOUT})).status&&h.status<300)break;throw new Error}catch(t){if(403!==t?.response?.status||"Refresh Token"===i||u){if("ECONNABORTED"!==t.code)return _apiExceptionHandler((t.response?.data.message?t.response.data:t).message,i+" failed");await _sleep(SLEEP_TIME*2**(l-1))}else await e.refreshToken(),c.Authorization="Bearer "+e.token,u=!0;++l}}while((!h||h.status<200||300<=h.status)&&l<MAX_RETRIES);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",void 0,void 0,r)}async function _getDefaultSiteConfig(t,e,i,n){return _sendRequest(t,e,"Get Default Site Config",i+"/api/media/config","GET",void 0,void 0,n)}async function _getDynamicContent(t,e,i,n,r){return _sendRequest(t,e,"Get Dynamic Content",i+"/api/media/content/"+n,"GET",void 0,void 0,r)}async function _getDynamicContents(t,e,i,n){return _sendRequest(t,e,"Get Dynamic Contents",i+"/api/media/content","GET",void 0,void 0,n)}async function _getMediaGroup(t,e,i,n,r){return _sendRequest(t,e,"Get Media Group",i+"/api/media/group/"+n,"GET",void 0,void 0,r)}async function _getMediaItem(t,e,i,n,r){return _sendRequest(t,e,"Get Media Item",i+"/api/media/item/"+n,"GET",void 0,void 0,r)}async function _getMyContent(t,e,i,n){return _sendRequest(t,e,"Get My Content",i+"/api/media/my-content","GET",void 0,void 0,n)}async function _getMyGroup(t,e,i,n,r){return _sendRequest(t,e,"Get My Group",i+"/api/media/my-group/"+n,"GET",void 0,void 0,r)}async function _getSiteConfig(t,e,i,n,r){return _sendRequest(t,e,"Get Site Config",i+"/api/media/config/"+n,"GET",void 0,void 0,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)}
|