@nomad-media/public 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -0
- package/dist/nomad-media-sdk-debug.js +1702 -0
- package/dist/nomad-media-sdk.min.js +25 -0
- package/package.json +16 -0
|
@@ -0,0 +1,25 @@
|
|
|
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")}}
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nomad-media/public",
|
|
3
|
+
"version": "1.1.4",
|
|
4
|
+
"description": "Nomad Media NPM",
|
|
5
|
+
"main": "dist/nomad-media-sdk.min.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/nomad-media-sdk.min.js",
|
|
9
|
+
"./debug": "./dist/nomad-media-sdk-debug.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist/nomad-media-sdk.min.js",
|
|
13
|
+
"dist/nomad-media-sdk-debug.js",
|
|
14
|
+
"README.md"
|
|
15
|
+
]
|
|
16
|
+
}
|