@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, ...
347 lines • 14.4 kB
JavaScript
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 var CorvinaConnectEventType;
(function (CorvinaConnectEventType) {
CorvinaConnectEventType["ORGANIZATION_ID_CHANGED"] = "ORGANIZATION_ID_CHANGED";
CorvinaConnectEventType["ORGANIZATION_RESOURCE_ID_CHANGED"] = "ORGANIZATION_RESOURCE_ID_CHANGED";
CorvinaConnectEventType["JWT_CHANGED"] = "JWT_CHANGED";
CorvinaConnectEventType["USER_CHANGED"] = "USER_CHANGED";
CorvinaConnectEventType["THEME_CHANGED"] = "THEME_CHANGED";
CorvinaConnectEventType["BRAND_NAME_CHANGED"] = "BRAND_NAME_CHANGED";
CorvinaConnectEventType["IFRAME_HREF_CHANGED"] = "IFRAME_HREF_CHANGED";
CorvinaConnectEventType["TRANSACTIONS_AUTHORIZATION_RESPONSE"] = "TRANSACTIONS_AUTHORIZATION_RESPONSE";
CorvinaConnectEventType["USER_PREFERENCE_GET_REQUEST"] = "USER_PREFERENCE_GET_REQUEST";
CorvinaConnectEventType["USER_PREFERENCE_GET_RESPONSE"] = "USER_PREFERENCE_GET_RESPONSE";
CorvinaConnectEventType["USER_PREFERENCE_SET_REQUEST"] = "USER_PREFERENCE_SET_REQUEST";
CorvinaConnectEventType["USER_PREFERENCE_SET_RESPONSE"] = "USER_PREFERENCE_SET_RESPONSE";
})(CorvinaConnectEventType || (CorvinaConnectEventType = {}));
const initHandshake = ({ currentWindow, corvinaHostWindow, corvinaHost }) => {
return new Promise((resolve, reject) => {
try {
corvinaHostWindow === null || corvinaHostWindow === void 0 ? void 0 : corvinaHostWindow.postMessage({ type: MessageType.CORVINA_CONNECT_INIT }, corvinaHost);
const handleInitResponse = (event) => {
console.debug("CorvinaConnect: onMessage", event.data);
let message = event.data;
if (message.type === MessageType.CORVINA_CONNECT_INIT_RESPONSE) {
let { corvinaDomain, jwt, username, organizationId, organizationResourceId, defaultStandardTime, theme, brandName } = message.payload;
currentWindow.removeEventListener("message", handleInitResponse, false);
resolve(new CorvinaConnect({ jwt, username, organizationId, organizationResourceId, corvinaHost, corvinaDomain, theme, defaultStandardTime, currentWindow, corvinaHostWindow, brandName }));
}
};
currentWindow.addEventListener('message', handleInitResponse);
}
catch (error) {
reject(error);
}
});
};
export class CorvinaConnect {
constructor({ jwt, username, organizationId, organizationResourceId, corvinaHost, corvinaDomain, theme, brandName, defaultStandardTime, currentWindow, corvinaHostWindow }) {
this._eventCallback = {};
if (!jwt) {
throw new Error('JWT is required');
}
if (!username) {
throw new Error('Username is required');
}
if (!organizationId) {
throw new Error('OrganizationId is required');
}
if (!organizationResourceId) {
throw new Error('OrganizationResourceId is required');
}
if (!corvinaHost) {
throw new Error('CorvinaHost is required');
}
if (!corvinaDomain) {
throw new Error('CorvinaDomain is required');
}
if (!corvinaHostWindow) {
throw new Error('CorvinaHostWindow is required');
}
this._jwt = jwt;
this._username = username;
this._organizationId = organizationId;
this._organizationResourceId = organizationResourceId;
this._corvinaHost = corvinaHost;
this._corvinaDomain = corvinaDomain;
this._theme = theme;
this._defaultStandardTime = defaultStandardTime;
this._corvinaHostWindow = corvinaHostWindow;
this._brandName = brandName;
this._window = currentWindow || window;
this._eventCallback = Object.keys(CorvinaConnectEventType).reduce((acc, key) => {
acc[key] = [];
return acc;
}, {});
this._onMessageRef = this.onMessage.bind(this);
this._window.addEventListener("message", this._onMessageRef);
}
static dispose() {
if (CorvinaConnect._instance) {
CorvinaConnect._instance.dispose();
CorvinaConnect._instance = undefined;
}
}
dispose() {
var _a;
this._window.removeEventListener("message", this._onMessageRef);
(_a = this._urlWatcher) === null || _a === void 0 ? void 0 : _a.dispose();
this._urlWatcher = undefined;
}
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(event) {
console.debug("CorvinaConnect: onMessage", event.data);
switch (event.data.type) {
case MessageType.USER_CHANGED:
this.onUserChanged(event);
break;
case MessageType.JWT_CHANGED:
this.onJwtChanged(event);
break;
case MessageType.ORGANIZATION_ID_CHANGED:
this.onOrganizationIdChanged(event);
break;
case MessageType.ORGANIZATION_RESOURCE_ID_CHANGED:
this.onOrganizationResourceIdChanged(event);
break;
case MessageType.THEME_CHANGED:
this.onThemeChanged(event);
break;
case MessageType.BRAND_NAME_CHANGED:
this.onBrandNameChanged(event);
break;
case MessageType.IFRAME_HREF_CHANGED:
this.onIframeHrefChanged(event);
break;
case MessageType.TRANSACTIONS_AUTHORIZATION_RESPONSE:
this.onPreauthorizedTransactionResponse(event);
break;
case MessageType.USER_PREFERENCE_GET_RESPONSE:
this.onUserPreferenceGetResponse(event);
break;
case MessageType.USER_PREFERENCE_SET_RESPONSE:
this.onUserPreferenceSetResponse(event);
break;
default:
break;
}
}
onThemeChanged(event) {
this._theme = event.data.payload.theme;
for (const callback of this._eventCallback[CorvinaConnectEventType.THEME_CHANGED]) {
callback(this._theme);
}
}
onBrandNameChanged(event) {
this._brandName = event.data.payload.brandName;
for (const callback of this._eventCallback[CorvinaConnectEventType.BRAND_NAME_CHANGED]) {
callback(this._brandName);
}
}
onUserChanged(event) {
this._username = event.data.payload.username;
for (const callback of this._eventCallback[CorvinaConnectEventType.USER_CHANGED]) {
callback(this._username);
}
}
onJwtChanged(event) {
this._jwt = event.data.payload.jwt;
for (const callback of this._eventCallback[CorvinaConnectEventType.JWT_CHANGED]) {
callback(this._jwt);
}
}
onOrganizationIdChanged(event) {
this._organizationId = event.data.payload.organizationId;
for (const callback of this._eventCallback[CorvinaConnectEventType.ORGANIZATION_ID_CHANGED]) {
callback(this._organizationId);
}
}
onOrganizationResourceIdChanged(event) {
this._organizationResourceId = event.data.payload.organizationResourceId;
for (const callback of this._eventCallback[CorvinaConnectEventType.ORGANIZATION_RESOURCE_ID_CHANGED]) {
callback(this._organizationResourceId);
}
}
onIframeHrefChanged(event) {
for (const callback of this._eventCallback[CorvinaConnectEventType.IFRAME_HREF_CHANGED]) {
callback(event.data.payload);
}
}
onPreauthorizedTransactionResponse(event) {
for (const callback of this._eventCallback[CorvinaConnectEventType.TRANSACTIONS_AUTHORIZATION_RESPONSE]) {
callback(event.data.payload);
}
}
onUserPreferenceGetResponse(event) {
for (const callback of this._eventCallback[CorvinaConnectEventType.USER_PREFERENCE_GET_RESPONSE]) {
callback(event.data.payload);
}
}
onUserPreferenceSetResponse(event) {
for (const callback of this._eventCallback[CorvinaConnectEventType.USER_PREFERENCE_SET_RESPONSE]) {
callback(event.data.payload);
}
}
off(event, callback) {
if (!event) {
throw new Error("Event name is required");
}
if (callback) {
this._eventCallback[event] = this._eventCallback[event].filter((cb) => cb !== callback);
}
else {
this._eventCallback[event] = [];
}
}
on(event, callback) {
if (!event) {
throw new Error("Event name is required");
}
if (!Object.values(CorvinaConnectEventType).includes(event)) {
throw new Error("Event name is not valid");
}
if (!callback) {
throw new Error("Callback is required");
}
this._eventCallback[event].push(callback);
return callback;
}
navigateTo(page) {
if (!page) {
throw new Error("Path is required");
}
const message = {
type: MessageType.CORVINA_NAVIGATE,
payload: {
page
}
};
this._corvinaHostWindow.postMessage(message, this._corvinaHost);
}
static create(_a) {
return __awaiter(this, arguments, void 0, function* ({ corvinaHost, currentWindow, corvinaHostWindow, timeoutMs }) {
currentWindow = currentWindow || window;
if (!this._instance) {
corvinaHostWindow = corvinaHostWindow || currentWindow.parent.window;
const initHandshakePromise = initHandshake({ currentWindow, corvinaHostWindow, corvinaHost });
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(reject, timeoutMs !== null && timeoutMs !== void 0 ? timeoutMs : 5000, 'Create timeout reached');
});
this._instance = (yield Promise.race([initHandshakePromise, timeoutPromise]));
}
return this._instance;
});
}
enableNavigationSync(filterNewHref = (newHref) => newHref) {
this._href = this._window.location.href;
this._urlWatcher = new UrlWatcher(({ type }) => {
const newHref = filterNewHref(this._window.location.href);
if (newHref !== this._href) {
const message = {
type: MessageType.IFRAME_HREF_CHANGED,
payload: {
href: newHref,
type
},
};
this._corvinaHostWindow.postMessage(message, this._corvinaHost);
}
}, this._window);
}
disableNavigationSync() {
var _a;
(_a = this._urlWatcher) === null || _a === void 0 ? void 0 : _a.dispose();
this._urlWatcher = undefined;
}
promptPreauthorizedTransactionAuthorization(dto) {
const message = {
type: MessageType.TRANSACTIONS_AUTHORIZATION_REQUEST,
payload: dto
};
this._corvinaHostWindow.postMessage(message, this._corvinaHost);
}
setUserPreference(key, value, timeoutMs = 10000) {
const message = {
type: MessageType.USER_PREFERENCE_SET_REQUEST,
payload: {
key,
value
}
};
const p = new Promise((resolve, reject) => {
const callback = this.on(CorvinaConnectEventType.USER_PREFERENCE_SET_RESPONSE, (response) => {
if (response.key === key) {
this.off(CorvinaConnectEventType.USER_PREFERENCE_SET_RESPONSE, callback);
resolve();
}
});
if (timeoutMs) {
setTimeout(() => {
reject(new Error(`Timeout reached waiting for user preference ${key} response`));
}, timeoutMs);
}
});
this._corvinaHostWindow.postMessage(message, this._corvinaHost);
return p;
}
getUserPreference(key, timeoutMs = 10000) {
const message = {
type: MessageType.USER_PREFERENCE_GET_REQUEST,
payload: {
key
}
};
const p = new Promise((resolve, reject) => {
const callback = this.on(CorvinaConnectEventType.USER_PREFERENCE_GET_RESPONSE, (response) => {
if (response.key === key) {
this.off(CorvinaConnectEventType.USER_PREFERENCE_GET_RESPONSE, callback);
resolve(response.value);
}
});
if (timeoutMs) {
setTimeout(() => {
reject(new Error(`Timeout reached waiting for user preference ${key} response`));
}, timeoutMs);
}
});
this._corvinaHostWindow.postMessage(message, this._corvinaHost);
return p;
}
}
//# sourceMappingURL=CorvinaConnect.js.map