UNPKG

@corvina/corvina-app-connect

Version:

This library enables an application embedded as an iframe in Corvina to retrieve some information such as JWT, organization id, ...

1 lines 13.6 kB
var e,t;!function(e){e.CORVINA_CONNECT_INIT="CORVINA_CONNECT_INIT",e.CORVINA_CONNECT_INIT_RESPONSE="CORVINA_CONNECT_INIT_RESPONSE",e.ORGANIZATION_ID_CHANGED="ORGANIZATION_ID_CHANGED",e.ORGANIZATION_RESOURCE_ID_CHANGED="ORGANIZATION_RESOURCE_ID_CHANGED",e.JWT_CHANGED="JWT_CHANGED",e.USER_CHANGED="USER_CHANGED",e.THEME_CHANGED="THEME_CHANGED",e.CORVINA_NAVIGATE="CORVINA_NAVIGATE",e.BRAND_NAME_CHANGED="BRAND_NAME_CHANGED",e.IFRAME_HREF_CHANGED="IFRAME_HREF_CHANGED",e.TRANSACTIONS_AUTHORIZATION_REQUEST="TRANSACTIONS_AUTHORIZATION_REQUEST",e.TRANSACTIONS_AUTHORIZATION_RESPONSE="TRANSACTIONS_AUTHORIZATION_RESPONSE"}(e||(e={})),function(e){e.HOME="home",e.DASHBOARD="dashboard",e.DEVICE_ACTIVATE="device-activate",e.DEVICE_MANAGE="device-manage",e.DEVICE_VPN="device-vpn",e.DATA_CONFIGURE="data-configure",e.DATA_EXPLORE="data-explore",e.DATA_ALARMS="data-alarms",e.DATA_NOTIFICATIONS="data-notifications",e.IAM_ORGANIZATIONS="iam-organizations",e.IAM_USERS="iam-users",e.IAM_ROLES="iam-roles",e.DEALER="dealer",e.AUDIT="audit",e.LOG="log"}(t||(t={}));const a="appHref";var i;!function(e){e.AUTHORIZED="AUTHORIZED",e.USER_REJECTED="USER_REJECTED",e.ERROR_NO_EMPTY_TRANSACTIONS="ERROR_NO_EMPTY_TRANSACTIONS",e.ERROR_NO_IN_APP_PURCHASES="ERROR_NO_IN_APP_PURCHASES"}(i||(i={}));class n{constructor(e,t){this._window=window,this._window=t||window,this._onUrlChange=e,this._originalPushState=this._window.history.pushState,this._originalReplaceState=this._window.history.replaceState,this._window.history.pushState=(...e)=>{this._originalPushState.apply(this._window.history,e),this._onUrlChange({type:"pushState"})},this._window.history.replaceState=(...e)=>{this._originalReplaceState.apply(this._window.history,e),this._onUrlChange({type:"replaceState"})},this._window.addEventListener("popstate",this._onUrlChange)}dispose(){this._window.history.pushState=this._originalPushState,this._window.history.replaceState=this._originalReplaceState,this._window.removeEventListener("popstate",this._onUrlChange)}static extractAppHref(){try{const e=new URL(window.location.href);let t=e.searchParams.get(a);if(t||(t=new URLSearchParams(e.hash.slice(e.hash.indexOf("?"))).get(a)),t)return decodeURIComponent(t)}catch(e){console.warn("CorvinaHost: Error extracting appHref from ",window.location.href)}}setAppHref(e,t){const i=e=>{"replaceState"===t?this._originalReplaceState.call(this._window.history,null,"",e):this._originalPushState.call(this._window.history,null,"",e)};if(this._window.location.hash){let t=this._window.location.hash.indexOf("?");if(t<0)i(this._window.location.hash+"?"+a+"="+encodeURIComponent(e));else{const n=new URLSearchParams(this._window.location.hash.slice(t));n.set(a,e);i(this._window.location.hash.slice(0,t+1)+n)}}else{const t=new URL(this._window.location.href);t.searchParams.set(a,e),i(t.toString())}}}var o=function(e,t,a,i){return new(a||(a=Promise))((function(n,o){function s(e){try{d(i.next(e))}catch(e){o(e)}}function r(e){try{d(i.throw(e))}catch(e){o(e)}}function d(e){var t;e.done?n(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(s,r)}d((i=i.apply(e,t||[])).next())}))};class s{constructor({jwtApp:e,username:t,organizationId:a,organizationResourceId:i,corvinaHost:n,corvinaDomain:o,theme:s,defaultStandardTime:r,brandName:d}){this._jwtApp=e,this._username=t,this._organizationId=a,this._organizationResourceId=i,this._corvinaHost=n,this._corvinaDomain=o,this._theme=s,this._defaultStandardTime=r,this._onMessageRef=this.onMessage.bind(this),this._brandName=d,window.addEventListener("message",this._onMessageRef)}dispose(){var e;window.removeEventListener("message",this._onMessageRef),null===(e=this._urlWatcher)||void 0===e||e.dispose(),this._urlWatcher=void 0}onNavigate(e){this._onNavigateCallback=e}onPreauthorizedTransactionAuthorizationRequest(e){this._onPreauthorizedTransactionAuthorizationRequestCallback=e}setJwtApp(t){const a={type:e.JWT_CHANGED,payload:{jwt:t.jwt}};this._jwtApp.set(t.iframeOrigin,t),this.sendMessageToFrame(a,t.iframeOrigin)}set username(t){this._username=t;const a={type:e.USER_CHANGED,payload:{username:t}};this.sendMessageToAllFrames(a)}set organizationId(t){this._organizationId=t;const a={type:e.ORGANIZATION_ID_CHANGED,payload:{organizationId:t}};this.sendMessageToAllFrames(a)}set organizationResourceId(t){this._organizationResourceId=t;const a={type:e.ORGANIZATION_RESOURCE_ID_CHANGED,payload:{organizationResourceId:t}};this.sendMessageToAllFrames(a)}set theme(t){this._theme=t;const a={type:e.THEME_CHANGED,payload:{theme:t}};this.sendMessageToAllFrames(a)}set brandName(t){this._brandName=t;const a={type:e.BRAND_NAME_CHANGED,payload:{brandName:t}};this.sendMessageToAllFrames(a)}set defaultStandardTime(e){this._defaultStandardTime=e}get defaultStandardTime(){return this._defaultStandardTime}get organizationId(){return this._organizationId}get theme(){return this._theme}get brandName(){return this._brandName}sendMessageToAllFrames(e){var t;const a=document.getElementsByTagName("iframe");for(const i of a)i.id&&i.id.startsWith("corvina-app-connect-")&&(null===(t=i.contentWindow)||void 0===t||t.postMessage(e,{targetOrigin:i.src}))}sendMessageToFrame(e,t){var a;const i=document.querySelectorAll(`iframe[src^='${t}']`);for(const t of i)null===(a=t.contentWindow)||void 0===a||a.postMessage(e,{targetOrigin:t.src})}onMessage(t){var a,i;switch(console.debug("CorvinaHost: onMessage",t.data),t.data.type){case e.CORVINA_CONNECT_INIT:this.onCorvinaConnectInit(t);break;case e.CORVINA_NAVIGATE:if(!this._onNavigateCallback){console.warn("CorvinaHost: onNavigate callback is not defined");break}null===(a=this._onNavigateCallback)||void 0===a||a.call(this,t.data.payload);break;case e.IFRAME_HREF_CHANGED:t.data.payload.href&&this._appHref!==t.data.payload.href&&(this._appHref=t.data.payload.href,this._urlWatcher&&this._appHref&&this._urlWatcher.setAppHref(this._appHref,t.data.payload.type));break;case e.TRANSACTIONS_AUTHORIZATION_REQUEST:if(!this._onPreauthorizedTransactionAuthorizationRequestCallback){console.warn("CorvinaHost: onPaymentAuthorizationRequest callback is not defined");break}null===(i=this._onPreauthorizedTransactionAuthorizationRequestCallback)||void 0===i||i.call(this,t)}}onCorvinaConnectInit(t){var a;const i={type:e.CORVINA_CONNECT_INIT_RESPONSE,payload:{jwt:null===(a=this._jwtApp.get(t.origin))||void 0===a?void 0:a.jwt,username:this._username,organizationId:this._organizationId,organizationResourceId:this._organizationResourceId,corvinaHost:this._corvinaHost,corvinaDomain:this._corvinaDomain,theme:this._theme,defaultStandardTime:this._defaultStandardTime,brandName:this._brandName}};t.source?t.source.postMessage(i,{targetOrigin:t.origin}):console.warn("CorvinaHost: Event source is not defined",t)}static create(e){return o(this,arguments,void 0,(function*({jwtApp:e,username:t,organizationId:a,organizationResourceId:i,corvinaHost:n,corvinaDomain:o,theme:r,defaultStandardTime:d,brandName:h}){return new s({jwtApp:e,username:t,organizationId:a,organizationResourceId:i,corvinaHost:n,corvinaDomain:o,theme:r,defaultStandardTime:d,brandName:h})}))}enableNavigationSync(){this._appHref=n.extractAppHref(),this._urlWatcher=new n((({type:t})=>{const a=n.extractAppHref();if(a!==this._appHref){const i={type:e.IFRAME_HREF_CHANGED,payload:{href:a,type:t}};this.sendMessageToAllFrames(i),this._appHref=a}}))}disableNavigationSync(){var e;null===(e=this._urlWatcher)||void 0===e||e.dispose(),this._urlWatcher=void 0}}var r,d=function(e,t,a,i){return new(a||(a=Promise))((function(n,o){function s(e){try{d(i.next(e))}catch(e){o(e)}}function r(e){try{d(i.throw(e))}catch(e){o(e)}}function d(e){var t;e.done?n(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(s,r)}d((i=i.apply(e,t||[])).next())}))};!function(e){e.ORGANIZATION_ID_CHANGED="ORGANIZATION_ID_CHANGED",e.ORGANIZATION_RESOURCE_ID_CHANGED="ORGANIZATION_RESOURCE_ID_CHANGED",e.JWT_CHANGED="JWT_CHANGED",e.USER_CHANGED="USER_CHANGED",e.THEME_CHANGED="THEME_CHANGED",e.BRAND_NAME_CHANGED="BRAND_NAME_CHANGED",e.IFRAME_HREF_CHANGED="IFRAME_HREF_CHANGED",e.TRANSACTIONS_AUTHORIZATION_RESPONSE="TRANSACTIONS_AUTHORIZATION_RESPONSE"}(r||(r={}));class h{constructor({jwt:e,username:t,organizationId:a,organizationResourceId:i,corvinaHost:n,corvinaDomain:o,theme:s,brandName:d,defaultStandardTime:h,currentWindow:_,corvinaHostWindow:c}){if(this._eventCallback={},!e)throw new Error("JWT is required");if(!t)throw new Error("Username is required");if(!a)throw new Error("OrganizationId is required");if(!i)throw new Error("OrganizationResourceId is required");if(!n)throw new Error("CorvinaHost is required");if(!o)throw new Error("CorvinaDomain is required");if(!c)throw new Error("CorvinaHostWindow is required");this._jwt=e,this._username=t,this._organizationId=a,this._organizationResourceId=i,this._corvinaHost=n,this._corvinaDomain=o,this._theme=s,this._defaultStandardTime=h,this._corvinaHostWindow=c,this._brandName=d,this._window=_||window,this._eventCallback=Object.keys(r).reduce(((e,t)=>(e[t]=[],e)),{}),this._onMessageRef=this.onMessage.bind(this),this._window.addEventListener("message",this._onMessageRef)}static dispose(){h._instance&&(h._instance.dispose(),h._instance=void 0)}dispose(){var e;this._window.removeEventListener("message",this._onMessageRef),null===(e=this._urlWatcher)||void 0===e||e.dispose(),this._urlWatcher=void 0}get jwt(){return this._jwt}get username(){return this._username}get organizationId(){return this._organizationId}get organizationResourceId(){return this._organizationResourceId}get corvinaHost(){return this._corvinaHost}get corvinaDomain(){return this._corvinaDomain}get theme(){return this._theme}get defaultStandardTime(){return this._defaultStandardTime}get brandName(){return this._brandName}onMessage(t){switch(console.debug("CorvinaConnect: onMessage",t.data),t.data.type){case e.USER_CHANGED:this.onUserChanged(t);break;case e.JWT_CHANGED:this.onJwtChanged(t);break;case e.ORGANIZATION_ID_CHANGED:this.onOrganizationIdChanged(t);break;case e.ORGANIZATION_RESOURCE_ID_CHANGED:this.onOrganizationResourceIdChanged(t);break;case e.THEME_CHANGED:this.onThemeChanged(t);break;case e.BRAND_NAME_CHANGED:this.onBrandNameChanged(t);break;case e.IFRAME_HREF_CHANGED:this.onIframeHrefChanged(t);break;case e.TRANSACTIONS_AUTHORIZATION_RESPONSE:this.onPreauthorizedTransactionResponse(t)}}onThemeChanged(e){this._theme=e.data.payload.theme;for(const e of this._eventCallback[r.THEME_CHANGED])e(this._theme)}onBrandNameChanged(e){this._brandName=e.data.payload.brandName;for(const e of this._eventCallback[r.BRAND_NAME_CHANGED])e(this._brandName)}onUserChanged(e){this._username=e.data.payload.username;for(const e of this._eventCallback[r.USER_CHANGED])e(this._username)}onJwtChanged(e){this._jwt=e.data.payload.jwt;for(const e of this._eventCallback[r.JWT_CHANGED])e(this._jwt)}onOrganizationIdChanged(e){this._organizationId=e.data.payload.organizationId;for(const e of this._eventCallback[r.ORGANIZATION_ID_CHANGED])e(this._organizationId)}onOrganizationResourceIdChanged(e){this._organizationResourceId=e.data.payload.organizationResourceId;for(const e of this._eventCallback[r.ORGANIZATION_RESOURCE_ID_CHANGED])e(this._organizationResourceId)}onIframeHrefChanged(e){for(const t of this._eventCallback[r.IFRAME_HREF_CHANGED])t(e.data.payload)}onPreauthorizedTransactionResponse(e){for(const t of this._eventCallback[r.TRANSACTIONS_AUTHORIZATION_RESPONSE])t(e.data.payload)}off(e){if(!e)throw new Error("Event name is required");this._eventCallback[e]=[]}on(e,t){if(!e)throw new Error("Event name is required");if(!Object.values(r).includes(e))throw new Error("Event name is not valid");if(!t)throw new Error("Callback is required");this._eventCallback[e].push(t)}navigateTo(t){if(!t)throw new Error("Path is required");const a={type:e.CORVINA_NAVIGATE,payload:{page:t}};this._corvinaHostWindow.postMessage(a,this._corvinaHost)}static create(t){return d(this,arguments,void 0,(function*({corvinaHost:t,currentWindow:a,corvinaHostWindow:i,timeoutMs:n}){if(a=a||window,!this._instance){const o=(({currentWindow:t,corvinaHostWindow:a,corvinaHost:i})=>new Promise(((n,o)=>{try{null==a||a.postMessage({type:e.CORVINA_CONNECT_INIT},i);const o=s=>{console.debug("CorvinaConnect: onMessage",s.data);let r=s.data;if(r.type===e.CORVINA_CONNECT_INIT_RESPONSE){let{corvinaDomain:e,jwt:s,username:d,organizationId:_,organizationResourceId:c,defaultStandardTime:A,theme:N,brandName:l}=r.payload;t.removeEventListener("message",o,!1),n(new h({jwt:s,username:d,organizationId:_,organizationResourceId:c,corvinaHost:i,corvinaDomain:e,theme:N,defaultStandardTime:A,currentWindow:t,corvinaHostWindow:a,brandName:l}))}};t.addEventListener("message",o)}catch(e){o(e)}})))({currentWindow:a,corvinaHostWindow:i=i||a.parent.window,corvinaHost:t}),s=new Promise(((e,t)=>{setTimeout(t,null!=n?n:5e3,"Create timeout reached")}));this._instance=yield Promise.race([o,s])}return this._instance}))}enableNavigationSync(t=e=>e){this._href=this._window.location.href,this._urlWatcher=new n((({type:a})=>{const i=t(this._window.location.href);if(i!==this._href){const t={type:e.IFRAME_HREF_CHANGED,payload:{href:i,type:a}};this._corvinaHostWindow.postMessage(t,this._corvinaHost)}}),this._window)}disableNavigationSync(){var e;null===(e=this._urlWatcher)||void 0===e||e.dispose(),this._urlWatcher=void 0}promptPreauthorizedTransactionAuthorization(t){const a={type:e.TRANSACTIONS_AUTHORIZATION_REQUEST,payload:t};this._corvinaHostWindow.postMessage(a,this._corvinaHost)}}export{h as CorvinaConnect,r as CorvinaConnectEventType,s as CorvinaHost,t as CorvinaPages,e as MessageType};