UNPKG

@arrowhealth/bridge-sdk

Version:

Bridge SDK provides web applications the ability to integrate with the Bridge Platform.

504 lines (503 loc) 18 kB
/* Bridge SDK provided by Arrow Health 2021-present. All rights reserved. */ (function(global, factory) { typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.bridge = {})); })(this, (function(exports) { "use strict"; const EVENT_PREFIX = "bridge::"; const WINDOW_PREFIX = "bridge_"; const inPopout$1 = !!(window.opener && window.opener !== window); const inIframe$1 = !inPopout$1 && window.parent !== window; const inBridge$1 = (window.name + "").includes(WINDOW_PREFIX); const version$1 = "2.7.1"; const EVENTS = { CAPTURE_USER_EVENTS: EVENT_PREFIX + "capture_user_events", CLEAR_CACHE: EVENT_PREFIX + "clear_cache", CLOSE_APP: EVENT_PREFIX + "close_app", DISABLE_TILE: EVENT_PREFIX + "disable_tile", ENABLE_TILE: EVENT_PREFIX + "enable_tile", GET_AUTH_STATUS: EVENT_PREFIX + "get_auth_status", GET_AUTH_USER: EVENT_PREFIX + "get_auth_user", GET_ORG: EVENT_PREFIX + "get_org", GET_PAGE: EVENT_PREFIX + "get_page", GET_PAGE_DETAILS: EVENT_PREFIX + "get_page_details", GET_PATIENT_INFO: EVENT_PREFIX + "get_patient", GET_PLATFORM: EVENT_PREFIX + "get_platform", GET_USER_SESSION: EVENT_PREFIX + "get_user_session", HIDE_TILE: EVENT_PREFIX + "hide_tile", LOGIN: EVENT_PREFIX + "login", LOGOUT: EVENT_PREFIX + "logout", OPEN_APP: EVENT_PREFIX + "open_app", PROXY_READY: EVENT_PREFIX + "proxy_ready", PUSH_NOTIFICATION: EVENT_PREFIX + "push_notification", RELEASE_USER_EVENTS: EVENT_PREFIX + "release_user_events", SET_AUTH_STATUS: EVENT_PREFIX + "set_auth_status", SET_AUTH_USER: EVENT_PREFIX + "set_auth_user", SET_BADGE_COUNT: EVENT_PREFIX + "set_badge_count", SET_ORG: EVENT_PREFIX + "set_org", SET_PATIENT_INFO: EVENT_PREFIX + "set_patient", SHOW_TILE: EVENT_PREFIX + "show_tile" }; exports.PlatformKind = void 0; (function(PlatformKind) { PlatformKind["Other"] = "Other"; PlatformKind["Athena"] = "Athena"; PlatformKind["eCW"] = "eCW"; })(exports.PlatformKind || (exports.PlatformKind = {})); function __awaiter(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()); })); } typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; const PARENT_ORIGIN = "*"; const CHILD_ORIGIN = "*"; const EVENT_TYPE = "BRIDGE_EVENT"; const emitToChild = (win, event, data) => { if (win && win.postMessage) { win.postMessage(JSON.stringify({ data: data, event: event, eventType: EVENT_TYPE }), CHILD_ORIGIN); } else { console.warn('[ BRIDGE SDK ] :: emitToChild is missing required "win" param'); } }; const emitToParent = (event, data) => { let win = window.parent; if (window.opener) { win = window.opener; } if (window === win) { console.warn("Cannot post message to self. No parent window found."); return; } if (!window.name) { console.warn("No app id assigned. Cannot post request."); return; } win.postMessage(JSON.stringify({ appId: window.name, data: data, event: event, eventType: EVENT_TYPE }), PARENT_ORIGIN); }; const on = (event, handle) => { const eventHandler = evt => { handle(evt.detail || {}); }; window.addEventListener(event, eventHandler); return () => window.removeEventListener(event, eventHandler); }; window.addEventListener("message", (evt => { let payload; try { if (typeof evt.data === "string") { payload = JSON.parse(evt.data); payload.win = evt.source; if (payload.eventType === EVENT_TYPE) { window.dispatchEvent(new CustomEvent(payload.event, { detail: payload })); } } } catch (e) {} }), false); function getAuthUser() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_AUTH_USER, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_AUTH_USER); })); } function getPage(deep = false) { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_PAGE, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_PAGE, { deep: deep }); })); } function getPatient() { return __awaiter(this, void 0, void 0, (function*() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_PATIENT_INFO, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_PATIENT_INFO); })); })); } function getPlatform() { return __awaiter(this, void 0, void 0, (function*() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_PLATFORM, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_PLATFORM); })); })); } function setBadgeCount(count = 0) { emitToParent(EVENTS.SET_BADGE_COUNT, count); } function showTile() { emitToParent(EVENTS.SHOW_TILE); } function hideTile() { emitToParent(EVENTS.HIDE_TILE); } function enableTile() { emitToParent(EVENTS.ENABLE_TILE); } function disableTile() { emitToParent(EVENTS.DISABLE_TILE); } function getAuthStatus() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_AUTH_STATUS, (({data: data}) => { off(); resolve(data || "ready"); })); emitToParent(EVENTS.GET_AUTH_STATUS); })); } function setAuthStatus(status) { emitToParent(EVENTS.SET_AUTH_STATUS, status); } function captureUserEvents() { emitToParent(EVENTS.CAPTURE_USER_EVENTS); } function releaseUserEvents() { emitToParent(EVENTS.RELEASE_USER_EVENTS); } function closeApp() { emitToParent(EVENTS.CLOSE_APP); } function pushNotification(notification) { emitToParent(EVENTS.PUSH_NOTIFICATION, notification); } function onPatientChanged(handle) { return on(EVENTS.SET_PATIENT_INFO, (payload => handle(payload.data))); } function clearCache() { emitToParent(EVENTS.CLEAR_CACHE); } function getOrg() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_ORG, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_ORG); })); } function getRuntimeDetails() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_PAGE_DETAILS, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_PAGE_DETAILS); })); } function getUserSession() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.GET_USER_SESSION, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.GET_USER_SESSION); })); } function login(realm, user, pw) { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.LOGIN, (({data: data}) => { off(); resolve(data); })); emitToParent(EVENTS.LOGIN, { realm: realm, user: user, pw: pw }); })); } function logout() { return new Promise((resolve => { if (!inBridge$1) resolve(null); const off = on(EVENTS.LOGOUT, (() => { off(); resolve(); })); emitToParent(EVENTS.LOGOUT); })); } function openApp() { emitToParent(EVENTS.OPEN_APP); } function setAuthUser(authUser) { emitToParent(EVENTS.SET_AUTH_USER, authUser); } function setOrg(org) { emitToParent(EVENTS.SET_ORG, org); } var internal = Object.freeze({ __proto__: null, clearCache: clearCache, getOrg: getOrg, getRuntimeDetails: getRuntimeDetails, getUserSession: getUserSession, login: login, logout: logout, openApp: openApp, setAuthUser: setAuthUser, setOrg: setOrg }); function onCaptureUserEventsRequest(handle) { return on(EVENTS.CAPTURE_USER_EVENTS, (request => { handle(request.appId); })); } function onClearCacheRequest(handle) { return on(EVENTS.CLEAR_CACHE, (request => { handle(request.appId); })); } function onCloseAppRequest(handle) { return on(EVENTS.CLOSE_APP, (request => { handle(request.appId); })); } function onDisableTileRequest(handle) { return on(EVENTS.DISABLE_TILE, (request => { handle(request.appId); })); } function onEnableTileRequest(handle) { return on(EVENTS.ENABLE_TILE, (request => { handle(request.appId); })); } function onGetAuthStatusRequest(handle) { return on(EVENTS.GET_AUTH_STATUS, (request => { handle(request.appId, (authStatus => { emitToChild(request.win, request.event, authStatus); }), request.data); })); } function onGetAuthUserRequest(handle) { return on(EVENTS.GET_AUTH_USER, (request => { handle(request.appId, (authUser => { emitToChild(request.win, request.event, authUser); })); })); } function onGetUserSessionRequest(handle) { return on(EVENTS.GET_USER_SESSION, (request => { handle(request.appId, (userSession => { emitToChild(request.win, request.event, userSession); })); })); } function onGetOrgRequest(handle) { return on(EVENTS.GET_ORG, (request => { handle(request.appId, (org => { emitToChild(request.win, request.event, org); })); })); } function onGetPageRequest(handle) { return on(EVENTS.GET_PAGE, (request => { var _a; handle(request.appId, !!((_a = request.data) === null || _a === void 0 ? void 0 : _a.deep), (page => { emitToChild(request.win, request.event, page); })); })); } function onGetPatientRequest(handle) { return on(EVENTS.GET_PATIENT_INFO, (request => { handle(request.appId, (patient => { emitToChild(request.win, request.event, patient); })); })); } function onGetPlatformRequest(handle) { return on(EVENTS.GET_PLATFORM, (request => { handle(request.appId, (platform => { emitToChild(request.win, request.event, platform); })); })); } function onGetRuntimeDetailsRequest(handle) { return on(EVENTS.GET_PAGE_DETAILS, (request => { handle(request.appId, (runtimeDetails => { emitToChild(request.win, request.event, runtimeDetails); })); })); } function onHideTileRequest(handle) { return on(EVENTS.HIDE_TILE, (request => { handle(request.appId); })); } function onLoginRequest(handle) { return on(EVENTS.LOGIN, (request => { handle(request.appId, request.data.realm, request.data.user, request.data.pw, (loginResult => { emitToChild(request.win, request.event, loginResult); })); })); } function onLogoutRequest(handle) { return on(EVENTS.LOGOUT, (request => { handle(request.appId, (() => emitToChild(request.win, request.event))); })); } function onOpenAppRequest(handle) { return on(EVENTS.OPEN_APP, (request => { handle(request.appId); })); } function onProxyReady(handle) { return on(EVENTS.PROXY_READY, (request => { handle(request.appId); })); } function onPushNotificationRequest(handle) { return on(EVENTS.PUSH_NOTIFICATION, (request => { handle(request.appId, request.data); })); } function onReleaseUserEventsRequest(handle) { return on(EVENTS.RELEASE_USER_EVENTS, (request => { handle(request.appId); })); } function onSetAuthStatusRequest(handle) { return on(EVENTS.SET_AUTH_STATUS, (request => { handle(request.appId, request.data); })); } function onSetAuthUserRequest(handle) { return on(EVENTS.SET_AUTH_USER, (request => { handle(request.appId, request.data); })); } function onSetBadgeCountRequest(handle) { return on(EVENTS.SET_BADGE_COUNT, (request => { const count = request.data; handle(request.appId, count || 0); })); } function onSetOrgRequest(handle) { return on(EVENTS.SET_ORG, (request => { handle(request.appId, request.data); })); } function onShowTileRequest(handle) { return on(EVENTS.SHOW_TILE, (request => { handle(request.appId); })); } function setPatient(win, patient) { emitToChild(win, EVENTS.SET_PATIENT_INFO, patient); } var platform = Object.freeze({ __proto__: null, onCaptureUserEventsRequest: onCaptureUserEventsRequest, onClearCacheRequest: onClearCacheRequest, onCloseAppRequest: onCloseAppRequest, onDisableTileRequest: onDisableTileRequest, onEnableTileRequest: onEnableTileRequest, onGetAuthStatusRequest: onGetAuthStatusRequest, onGetAuthUserRequest: onGetAuthUserRequest, onGetOrgRequest: onGetOrgRequest, onGetPageRequest: onGetPageRequest, onGetPatientRequest: onGetPatientRequest, onGetPlatformRequest: onGetPlatformRequest, onGetRuntimeDetailsRequest: onGetRuntimeDetailsRequest, onGetUserSessionRequest: onGetUserSessionRequest, onHideTileRequest: onHideTileRequest, onLoginRequest: onLoginRequest, onLogoutRequest: onLogoutRequest, onOpenAppRequest: onOpenAppRequest, onProxyReady: onProxyReady, onPushNotificationRequest: onPushNotificationRequest, onReleaseUserEventsRequest: onReleaseUserEventsRequest, onSetAuthStatusRequest: onSetAuthStatusRequest, onSetAuthUserRequest: onSetAuthUserRequest, onSetBadgeCountRequest: onSetBadgeCountRequest, onSetOrgRequest: onSetOrgRequest, onShowTileRequest: onShowTileRequest, setPatient: setPatient }); const inPopout = inPopout$1; const inIframe = inIframe$1; const inBridge = inBridge$1; const version = version$1; exports.$ = platform; exports.$$ = internal; exports.captureUserEvents = captureUserEvents; exports.closeApp = closeApp; exports.disableTile = disableTile; exports.enableTile = enableTile; exports.getAuthStatus = getAuthStatus; exports.getAuthUser = getAuthUser; exports.getPage = getPage; exports.getPatient = getPatient; exports.getPlatform = getPlatform; exports.hideTile = hideTile; exports.inBridge = inBridge; exports.inIframe = inIframe; exports.inPopout = inPopout; exports.onPatientChanged = onPatientChanged; exports.pushNotification = pushNotification; exports.releaseUserEvents = releaseUserEvents; exports.setAuthStatus = setAuthStatus; exports.setBadgeCount = setBadgeCount; exports.showTile = showTile; exports.version = version; }));