@descope/sdk-mixins
Version:
Descope JavaScript SDK mixins
114 lines (110 loc) • 6.69 kB
JavaScript
'use strict';
var tslib = require('tslib');
var sdkHelpers = require('@descope/sdk-helpers');
var configMixin = require('../configMixin/configMixin.js');
var injectNpmLibMixin = require('../injectNpmLibMixin/injectNpmLibMixin.js');
var loggerMixin = require('../loggerMixin/loggerMixin.js');
var helpers = require('./helpers.js');
var constants = require('./constants.js');
const descopeUiMixin = sdkHelpers.createSingletonMixin((superclass) => {
var _DescopeUiMixinClass_instances, _DescopeUiMixinClass_getComponentsVersion, _DescopeUiMixinClass_getComponentsVersionSri, _DescopeUiMixinClass_descopeUi, _DescopeUiMixinClass_loadDescopeUiComponent, _DescopeUiMixinClass_getDescopeUi, _a;
const BaseClass = sdkHelpers.compose(loggerMixin.loggerMixin, configMixin.configMixin, injectNpmLibMixin.injectNpmLibMixin)(superclass);
return _a = class DescopeUiMixinClass extends BaseClass {
constructor() {
super(...arguments);
_DescopeUiMixinClass_instances.add(this);
_DescopeUiMixinClass_descopeUi.set(this, void 0);
}
get descopeUi() {
if (!tslib.__classPrivateFieldGet(this, _DescopeUiMixinClass_descopeUi, "f")) {
tslib.__classPrivateFieldSet(this, _DescopeUiMixinClass_descopeUi, tslib.__classPrivateFieldGet(this, _DescopeUiMixinClass_instances, "m", _DescopeUiMixinClass_getDescopeUi).call(this), "f");
}
return tslib.__classPrivateFieldGet(this, _DescopeUiMixinClass_descopeUi, "f");
}
async loadDescopeUiComponents(templateOrComponentNames) {
const descopeUiComponentsList = Array.isArray(templateOrComponentNames)
? templateOrComponentNames
: helpers.getDescopeUiComponentsList(templateOrComponentNames);
return Promise.all(descopeUiComponentsList.map((componentName) => tslib.__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 tslib.__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 = constants.LOCAL_STORAGE_OVERRIDE
? undefined
: await tslib.__classPrivateFieldGet(this, _DescopeUiMixinClass_instances, "m", _DescopeUiMixinClass_getComponentsVersionSri).call(this);
await this.injectNpmLib(constants.WEB_COMPONENTS_UI_LIB_NAME, componentsVersion, constants.JS_FILE_PATH, [constants.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;
});
exports.descopeUiMixin = descopeUiMixin;
//# sourceMappingURL=descopeUiMixin.js.map