UNPKG

@descope/sdk-mixins

Version:
112 lines (109 loc) 6.64 kB
import { __classPrivateFieldGet, __classPrivateFieldSet } from 'tslib'; import { createSingletonMixin, compose } from '@descope/sdk-helpers'; import { configMixin } from '../configMixin/configMixin.js'; import { injectNpmLibMixin } from '../injectNpmLibMixin/injectNpmLibMixin.js'; import { loggerMixin } from '../loggerMixin/loggerMixin.js'; import { getDescopeUiComponentsList } from './helpers.js'; import { LOCAL_STORAGE_OVERRIDE, WEB_COMPONENTS_UI_LIB_NAME, JS_FILE_PATH } from './constants.js'; const descopeUiMixin = createSingletonMixin((superclass) => { var _DescopeUiMixinClass_instances, _DescopeUiMixinClass_getComponentsVersion, _DescopeUiMixinClass_getComponentsVersionSri, _DescopeUiMixinClass_descopeUi, _DescopeUiMixinClass_loadDescopeUiComponent, _DescopeUiMixinClass_getDescopeUi, _a; const BaseClass = compose(loggerMixin, configMixin, injectNpmLibMixin)(superclass); return _a = class DescopeUiMixinClass extends BaseClass { constructor() { super(...arguments); _DescopeUiMixinClass_instances.add(this); _DescopeUiMixinClass_descopeUi.set(this, void 0); } get descopeUi() { if (!__classPrivateFieldGet(this, _DescopeUiMixinClass_descopeUi, "f")) { __classPrivateFieldSet(this, _DescopeUiMixinClass_descopeUi, __classPrivateFieldGet(this, _DescopeUiMixinClass_instances, "m", _DescopeUiMixinClass_getDescopeUi).call(this), "f"); } return __classPrivateFieldGet(this, _DescopeUiMixinClass_descopeUi, "f"); } async loadDescopeUiComponents(templateOrComponentNames) { const descopeUiComponentsList = Array.isArray(templateOrComponentNames) ? templateOrComponentNames : getDescopeUiComponentsList(templateOrComponentNames); return Promise.all(descopeUiComponentsList.map((componentName) => __classPrivateFieldGet(this, _DescopeUiMixinClass_instances, "m", _DescopeUiMixinClass_loadDescopeUiComponent).call(this, componentName))); } }, _DescopeUiMixinClass_descopeUi = new WeakMap(), _DescopeUiMixinClass_instances = new WeakSet(), _DescopeUiMixinClass_getComponentsVersion = // eslint-disable-next-line class-methods-use-this async function _DescopeUiMixinClass_getComponentsVersion() { var _b; const config = await this.config; const componentsVersion = (_b = config === null || config === void 0 ? void 0 : config.projectConfig) === null || _b === void 0 ? void 0 : _b.componentsVersion; if (!componentsVersion) { this.logger.error('Could not get components version'); } else { this.logger.debug(`Got component version "${componentsVersion}"`); } return componentsVersion; }, _DescopeUiMixinClass_getComponentsVersionSri = async function _DescopeUiMixinClass_getComponentsVersionSri() { var _b; const config = await this.config; const componentsVersionSri = (_b = config === null || config === void 0 ? void 0 : config.projectConfig) === null || _b === void 0 ? void 0 : _b.componentsVersionSri; if (componentsVersionSri) { this.logger.debug('SRI hash available for components'); } return componentsVersionSri; }, _DescopeUiMixinClass_loadDescopeUiComponent = async function _DescopeUiMixinClass_loadDescopeUiComponent(componentName) { const isComponentAlreadyDefined = !!customElements.get(componentName); if (isComponentAlreadyDefined) { this.logger.debug(`Loading component "${componentName}" is skipped as it is already defined`); return undefined; } const descopeUI = await this.descopeUi; if (!descopeUI[componentName]) { this.logger.error(`Cannot load UI component "${componentName}"`, `Descope UI does not have a component named "${componentName}", available components are: "${Object.keys(descopeUI).join(', ')}"`); return undefined; } try { // eslint-disable-next-line @typescript-eslint/return-await return await descopeUI[componentName](); } catch (e) { // this error is thrown when trying to register a component which is already registered // when running 2 flows on the same page, it might happen that the register fn is called twice // in case it happens, we are silently ignore the error if (e.name === 'NotSupportedError') { // eslint-disable-next-line no-console console.debug(`Encountered an error while attempting to define the "${componentName}" component, it is likely that this component is already defined`); } else { throw e; } } return undefined; }, _DescopeUiMixinClass_getDescopeUi = async function _DescopeUiMixinClass_getDescopeUi() { if (globalThis.DescopeUI) { return globalThis.DescopeUI; } try { const componentsVersion = await __classPrivateFieldGet(this, _DescopeUiMixinClass_instances, "m", _DescopeUiMixinClass_getComponentsVersion).call(this); // When the dev-panel override is set, the URL points at a locally // built bundle whose bytes won't match the CDN-published SRI hash. // Skip SRI in that case so the override actually loads instead of // failing the integrity check and falling back to the CDN. const componentsVersionSri = LOCAL_STORAGE_OVERRIDE ? undefined : await __classPrivateFieldGet(this, _DescopeUiMixinClass_instances, "m", _DescopeUiMixinClass_getComponentsVersionSri).call(this); await this.injectNpmLib(WEB_COMPONENTS_UI_LIB_NAME, componentsVersion, JS_FILE_PATH, [LOCAL_STORAGE_OVERRIDE], componentsVersionSri); this.logger.debug('DescopeUI was loaded'); return globalThis.DescopeUI; } catch (error) { this.logger.error(error); throw new Error('DescopeUI was not loaded'); } }, _a; }); export { descopeUiMixin }; //# sourceMappingURL=descopeUiMixin.js.map