@descope/sdk-mixins
Version:
Descope JavaScript SDK mixins
69 lines (66 loc) • 3.84 kB
JavaScript
import { __classPrivateFieldGet } from 'tslib';
import { createSingletonMixin, compose } from '@descope/sdk-helpers';
import { localeMixin } from '../localeMixin/localeMixin.js';
import { staticResourcesMixin } from '../staticResourcesMixin/staticResourcesMixin.js';
import { widgetConfigMixin } from '../widgetConfigMixin/widgetConfigMixin.js';
import { widgetIdMixin } from '../widgetIdMixin.js';
/**
* Builds a widget's fetchWidgetPagesMixin. Every widget shares identical logic and differs only by
* its published base directory (the widget type/name under which its screens live in S3), so each
* widget calls this with its own dir:
*
* export const fetchWidgetPagesMixin = createFetchWidgetPagesMixin('user-profile-widget');
*
* `fetchWidgetPage` fetches `<base>/<widgetId>/<file>`, transparently preferring a localized
* `<file>-<locale>.html` variant when the widget publishes that locale (config.json targetLocales),
* and falling back to the default page otherwise.
*/
const createFetchWidgetPagesMixin = (widgetPagesBaseDir) => createSingletonMixin((superclass) => {
var _FetchWidgetPagesMixinClass_instances, _FetchWidgetPagesMixinClass_tryFetchLocalized, _a;
const BaseClass = compose(staticResourcesMixin, widgetIdMixin, widgetConfigMixin, localeMixin)(superclass);
return _a = class FetchWidgetPagesMixinClass extends BaseClass {
constructor() {
super(...arguments);
_FetchWidgetPagesMixinClass_instances.add(this);
}
// Keep `init` typed as a method across the package boundary (mirrors widgetConfigMixin):
// without it, the emitted .d.ts widens the inherited `init` to a property, and a widget
// composing this alongside another init-providing mixin (e.g. initLifecycleMixin) hits TS2425.
async init() {
var _b;
await ((_b = super.init) === null || _b === void 0 ? void 0 : _b.call(this));
}
async fetchWidgetPage(filename) {
const localized = await __classPrivateFieldGet(this, _FetchWidgetPagesMixinClass_instances, "m", _FetchWidgetPagesMixinClass_tryFetchLocalized).call(this, filename);
if (localized !== undefined)
return localized;
const res = await this.fetchStaticResource(`${widgetPagesBaseDir}/${this.widgetId}/${filename}`, 'text');
return res === null || res === void 0 ? void 0 : res.body;
}
},
_FetchWidgetPagesMixinClass_instances = new WeakSet(),
_FetchWidgetPagesMixinClass_tryFetchLocalized =
// Fetch <page>-<locale>.html when the widget publishes that locale (config.json
// targetLocales); otherwise return undefined so the caller falls back to the default page.
async function _FetchWidgetPagesMixinClass_tryFetchLocalized(filename) {
var _b;
const widgetConfig = await this.getWidgetConfig();
const userLocale = this.firstAvailableLocale((_b = widgetConfig === null || widgetConfig === void 0 ? void 0 : widgetConfig.targetLocales) !== null && _b !== void 0 ? _b : []);
if (!userLocale)
return undefined;
const ext = '.html';
const base = filename.endsWith(ext)
? filename.slice(0, -ext.length)
: filename;
try {
const res = await this.fetchStaticResource(`${widgetPagesBaseDir}/${this.widgetId}/${base}-${userLocale}${ext}`, 'text');
return res === null || res === void 0 ? void 0 : res.body;
}
catch (_c) {
return undefined;
}
},
_a;
});
export { createFetchWidgetPagesMixin };
//# sourceMappingURL=createFetchWidgetPagesMixin.js.map