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