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 16.4 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.USER_PREFERENCE_GET_REQUEST="USER_PREFERENCE_GET_REQUEST",e.USER_PREFERENCE_GET_RESPONSE="USER_PREFERENCE_GET_RESPONSE",e.USER_PREFERENCE_SET_REQUEST="USER_PREFERENCE_SET_REQUEST",e.USER_PREFERENCE_SET_RESPONSE="USER_PREFERENCE_SET_RESPONSE"}(e||(e={})),function(e){e.HOME="home",e.DIRECT_ACCESS="directaccess",e.DASHBOARD="dashboard",e.DEVICE_ACTIVATE="device-activate",e.DEVICE_MANAGE="device-manage",e.DEVICE_VPN="device-vpn",e.LICENSING="licensing",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",e.TRIGGER="trigger"}(t||(t={}));const a="appHref";var n;!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"}(n||(n={}));class i{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 n=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)n(this._window.location.hash+"?"+a+"="+encodeURIComponent(e));else{const i=new URLSearchParams(this._window.location.hash.slice(t));i.set(a,e);n(this._window.location.hash.slice(0,t+1)+i)}}else{const t=new URL(this._window.location.href);t.searchParams.set(a,e),n(t.toString())}}}var o=function(e,t,a,n){return new(a||(a=Promise))((function(i,o){function s(e){try{E(n.next(e))}catch(e){o(e)}}function r(e){try{E(n.throw(e))}catch(e){o(e)}}function E(e){var t;e.done?i(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(s,r)}E((n=n.apply(e,t||[])).next())}))};class s{constructor({jwtApp:e,username:t,organizationId:a,organizationResourceId:n,corvinaHost:i,corvinaDomain:o,theme:s,defaultStandardTime:r,brandName:E}){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=E,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}onUserPreferenceSetRequest(e){this._onUserPreferenceSetRequestCallback=e}onUserPreferenceGetRequest(e){this._onUserPreferenceGetRequestCallback=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 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){return o(this,void 0,void 0,(function*(){var a,n,i,o;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===(n=this._onPreauthorizedTransactionAuthorizationRequestCallback)||void 0===n||n.call(this,t);break;case e.USER_PREFERENCE_SET_REQUEST:try{yield null===(i=this._onUserPreferenceSetRequestCallback)||void 0===i?void 0:i.call(this,t)}catch(e){console.error("CorvinaHost: Error in onUserPreferenceSetRequest callback",e)}finally{const a={type:e.USER_PREFERENCE_SET_RESPONSE,payload:{key:t.data.payload.key}};t.source?t.source.postMessage(a,{targetOrigin:t.origin}):console.warn("CorvinaHost: Event source is not defined",t)}break;case e.USER_PREFERENCE_GET_REQUEST:let s;try{s=yield null===(o=this._onUserPreferenceGetRequestCallback)||void 0===o?void 0:o.call(this,t)}catch(e){console.error("CorvinaHost: Error in onUserPreferenceGetRequest callback",e)}finally{const a={type:e.USER_PREFERENCE_GET_RESPONSE,payload:s};t.source?t.source.postMessage(a,{targetOrigin:t.origin}):console.warn("CorvinaHost: Event source is not defined",t)}}}))}onCorvinaConnectInit(t){var a;const n={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(n,{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:n,corvinaHost:i,corvinaDomain:o,theme:r,defaultStandardTime:E,brandName:_}){return new s({jwtApp:e,username:t,organizationId:a,organizationResourceId:n,corvinaHost:i,corvinaDomain:o,theme:r,defaultStandardTime:E,brandName:_})}))}enableNavigationSync(){this._appHref=i.extractAppHref(),this._urlWatcher=new i((({type:t})=>{const a=i.extractAppHref();if(a!==this._appHref){const n={type:e.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 r,E=function(e,t,a,n){return new(a||(a=Promise))((function(i,o){function s(e){try{E(n.next(e))}catch(e){o(e)}}function r(e){try{E(n.throw(e))}catch(e){o(e)}}function E(e){var t;e.done?i(e.value):(t=e.value,t instanceof a?t:new a((function(e){e(t)}))).then(s,r)}E((n=n.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",e.USER_PREFERENCE_GET_REQUEST="USER_PREFERENCE_GET_REQUEST",e.USER_PREFERENCE_GET_RESPONSE="USER_PREFERENCE_GET_RESPONSE",e.USER_PREFERENCE_SET_REQUEST="USER_PREFERENCE_SET_REQUEST",e.USER_PREFERENCE_SET_RESPONSE="USER_PREFERENCE_SET_RESPONSE"}(r||(r={}));class _{constructor({jwt:e,username:t,organizationId:a,organizationResourceId:n,corvinaHost:i,corvinaDomain:o,theme:s,brandName:E,defaultStandardTime:_,currentWindow:d,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(!n)throw new Error("OrganizationResourceId is required");if(!i)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=n,this._corvinaHost=i,this._corvinaDomain=o,this._theme=s,this._defaultStandardTime=_,this._corvinaHostWindow=c,this._brandName=E,this._window=d||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(){_._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.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);break;case e.USER_PREFERENCE_GET_RESPONSE:this.onUserPreferenceGetResponse(t);break;case e.USER_PREFERENCE_SET_RESPONSE:this.onUserPreferenceSetResponse(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)}onUserPreferenceGetResponse(e){for(const t of this._eventCallback[r.USER_PREFERENCE_GET_RESPONSE])t(e.data.payload)}onUserPreferenceSetResponse(e){for(const t of this._eventCallback[r.USER_PREFERENCE_SET_RESPONSE])t(e.data.payload)}off(e,t){if(!e)throw new Error("Event name is required");this._eventCallback[e]=t?this._eventCallback[e].filter((e=>e!==t)):[]}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");return this._eventCallback[e].push(t),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 E(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.CORVINA_CONNECT_INIT},n);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:E,organizationId:d,organizationResourceId:c,defaultStandardTime:h,theme:R,brandName:N}=r.payload;t.removeEventListener("message",o,!1),i(new _({jwt:s,username:E,organizationId:d,organizationResourceId:c,corvinaHost:n,corvinaDomain:e,theme:R,defaultStandardTime:h,currentWindow:t,corvinaHostWindow:a,brandName:N}))}};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 i((({type:a})=>{const n=t(this._window.location.href);if(n!==this._href){const t={type:e.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.TRANSACTIONS_AUTHORIZATION_REQUEST,payload:t};this._corvinaHostWindow.postMessage(a,this._corvinaHost)}setUserPreference(t,a,n=1e4){const i={type:e.USER_PREFERENCE_SET_REQUEST,payload:{key:t,value:a}},o=new Promise(((e,a)=>{const i=this.on(r.USER_PREFERENCE_SET_RESPONSE,(a=>{a.key===t&&(this.off(r.USER_PREFERENCE_SET_RESPONSE,i),e())}));n&&setTimeout((()=>{a(new Error(`Timeout reached waiting for user preference ${t} response`))}),n)}));return this._corvinaHostWindow.postMessage(i,this._corvinaHost),o}getUserPreference(t,a=1e4){const n={type:e.USER_PREFERENCE_GET_REQUEST,payload:{key:t}},i=new Promise(((e,n)=>{const i=this.on(r.USER_PREFERENCE_GET_RESPONSE,(a=>{a.key===t&&(this.off(r.USER_PREFERENCE_GET_RESPONSE,i),e(a.value))}));a&&setTimeout((()=>{n(new Error(`Timeout reached waiting for user preference ${t} response`))}),a)}));return this._corvinaHostWindow.postMessage(n,this._corvinaHost),i}}export{_ as CorvinaConnect,r as CorvinaConnectEventType,s as CorvinaHost,t as CorvinaPages,e as MessageType};