@nomad-media/public 1.1.6 → 1.1.8
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.
|
@@ -1024,18 +1024,26 @@ class NomadMediaSDK {
|
|
|
1024
1024
|
|
|
1025
1025
|
const RESPONSE = await fetch(API_URL, REQUEST);
|
|
1026
1026
|
|
|
1027
|
+
const RESPONSE_BODY = await RESPONSE.text();
|
|
1027
1028
|
if (!RESPONSE.ok)
|
|
1028
1029
|
{
|
|
1029
|
-
|
|
1030
|
+
try
|
|
1031
|
+
{
|
|
1032
|
+
throw JSON.parse(RESPONSE_BODY);
|
|
1033
|
+
}
|
|
1034
|
+
catch (error)
|
|
1035
|
+
{
|
|
1036
|
+
throw RESPONSE_BODY;
|
|
1037
|
+
}
|
|
1030
1038
|
}
|
|
1031
1039
|
|
|
1032
1040
|
try
|
|
1033
1041
|
{
|
|
1034
|
-
return
|
|
1042
|
+
return JSON.parse(RESPONSE_BODY);
|
|
1035
1043
|
}
|
|
1036
1044
|
catch (error)
|
|
1037
1045
|
{
|
|
1038
|
-
return
|
|
1046
|
+
return RESPONSE_BODY;
|
|
1039
1047
|
}
|
|
1040
1048
|
}
|
|
1041
1049
|
catch (error)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
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)throw
|
|
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){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
3
|
METHOD: `+r+(a?`
|
|
4
4
|
PARAMS: `+JSON.stringify(a,null,4):"")+(o?`
|
|
5
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)}
|