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, ...

217 lines 8.55 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { MessageType } from "./common"; import { UrlWatcher } from "./hrefwatcher"; export class CorvinaHost { constructor({ jwtApp, username, organizationId, organizationResourceId, corvinaHost, corvinaDomain, theme, defaultStandardTime, brandName, }) { this._jwtApp = jwtApp; this._username = username; this._organizationId = organizationId; this._organizationResourceId = organizationResourceId; this._corvinaHost = corvinaHost; this._corvinaDomain = corvinaDomain; this._theme = theme; this._defaultStandardTime = defaultStandardTime; this._onMessageRef = this.onMessage.bind(this); this._brandName = brandName; window.addEventListener("message", this._onMessageRef); } dispose() { var _a; window.removeEventListener("message", this._onMessageRef); (_a = this._urlWatcher) === null || _a === void 0 ? void 0 : _a.dispose(); this._urlWatcher = undefined; } onNavigate(callback) { this._onNavigateCallback = callback; } onPreauthorizedTransactionAuthorizationRequest(callback) { this._onPreauthorizedTransactionAuthorizationRequestCallback = callback; } setJwtApp(jwtApp) { const message = { type: MessageType.JWT_CHANGED, payload: { jwt: jwtApp.jwt, }, }; this._jwtApp.set(jwtApp.iframeOrigin, jwtApp); this.sendMessageToFrame(message, jwtApp.iframeOrigin); } set username(username) { this._username = username; const message = { type: MessageType.USER_CHANGED, payload: { username, }, }; this.sendMessageToAllFrames(message); } set organizationId(organizationId) { this._organizationId = organizationId; const message = { type: MessageType.ORGANIZATION_ID_CHANGED, payload: { organizationId, }, }; this.sendMessageToAllFrames(message); } set organizationResourceId(organizationResourceId) { this._organizationResourceId = organizationResourceId; const message = { type: MessageType.ORGANIZATION_RESOURCE_ID_CHANGED, payload: { organizationResourceId, }, }; this.sendMessageToAllFrames(message); } set theme(theme) { this._theme = theme; const message = { type: MessageType.THEME_CHANGED, payload: { theme, }, }; this.sendMessageToAllFrames(message); } set brandName(brandName) { this._brandName = brandName; const message = { type: MessageType.BRAND_NAME_CHANGED, payload: { brandName, }, }; this.sendMessageToAllFrames(message); } set defaultStandardTime(defaultStandardTime) { this._defaultStandardTime = defaultStandardTime; } get defaultStandardTime() { return this._defaultStandardTime; } get organizationId() { return this._organizationId; } get theme() { return this._theme; } get brandName() { return this._brandName; } sendMessageToAllFrames(message) { var _a; const iframes = document.getElementsByTagName("iframe"); for (const iframe of iframes) { if (iframe.id && iframe.id.startsWith("corvina-app-connect-")) { (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage(message, { targetOrigin: iframe.src, }); } } } sendMessageToFrame(message, iframeOrigin) { var _a; const iframes = document.querySelectorAll(`iframe[src^='${iframeOrigin}']`); for (const iframe of iframes) { (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.postMessage(message, { targetOrigin: iframe.src, }); } } onMessage(event) { var _a, _b; console.debug("CorvinaHost: onMessage", event.data); switch (event.data.type) { case MessageType.CORVINA_CONNECT_INIT: this.onCorvinaConnectInit(event); break; case MessageType.CORVINA_NAVIGATE: if (!this._onNavigateCallback) { console.warn("CorvinaHost: onNavigate callback is not defined"); break; } (_a = this._onNavigateCallback) === null || _a === void 0 ? void 0 : _a.call(this, event.data.payload); break; case MessageType.IFRAME_HREF_CHANGED: if (event.data.payload.href && this._appHref !== event.data.payload.href) { this._appHref = event.data.payload.href; if (this._urlWatcher && this._appHref) { this._urlWatcher.setAppHref(this._appHref, event.data.payload.type); } } break; case MessageType.TRANSACTIONS_AUTHORIZATION_REQUEST: if (!this._onPreauthorizedTransactionAuthorizationRequestCallback) { console.warn("CorvinaHost: onPaymentAuthorizationRequest callback is not defined"); break; } (_b = this._onPreauthorizedTransactionAuthorizationRequestCallback) === null || _b === void 0 ? void 0 : _b.call(this, event); break; default: break; } } onCorvinaConnectInit(event) { var _a; const response = { type: MessageType.CORVINA_CONNECT_INIT_RESPONSE, payload: { jwt: (_a = this._jwtApp.get(event.origin)) === null || _a === void 0 ? 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, }, }; if (event.source) { event.source.postMessage(response, { targetOrigin: event.origin }); } else { console.warn("CorvinaHost: Event source is not defined", event); } } static create(_a) { return __awaiter(this, arguments, void 0, function* ({ jwtApp, username, organizationId, organizationResourceId, corvinaHost, corvinaDomain, theme, defaultStandardTime, brandName, }) { return new CorvinaHost({ jwtApp, username, organizationId, organizationResourceId, corvinaHost, corvinaDomain, theme, defaultStandardTime, brandName }); }); } enableNavigationSync() { this._appHref = UrlWatcher.extractAppHref(); this._urlWatcher = new UrlWatcher(({ type }) => { const newAppHref = UrlWatcher.extractAppHref(); if (newAppHref !== this._appHref) { const message = { type: MessageType.IFRAME_HREF_CHANGED, payload: { href: newAppHref, type }, }; this.sendMessageToAllFrames(message); this._appHref = newAppHref; } }); } disableNavigationSync() { var _a; (_a = this._urlWatcher) === null || _a === void 0 ? void 0 : _a.dispose(); this._urlWatcher = undefined; } } //# sourceMappingURL=CorvinaHost.js.map