UNPKG

@descope/sdk-mixins

Version:
71 lines (67 loc) 3.94 kB
'use strict'; var tslib = require('tslib'); var sdkHelpers = require('@descope/sdk-helpers'); var localeMixin = require('../localeMixin/localeMixin.js'); var staticResourcesMixin = require('../staticResourcesMixin/staticResourcesMixin.js'); var widgetConfigMixin = require('../widgetConfigMixin/widgetConfigMixin.js'); var widgetIdMixin = require('../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) => sdkHelpers.createSingletonMixin((superclass) => { var _FetchWidgetPagesMixinClass_instances, _FetchWidgetPagesMixinClass_tryFetchLocalized, _a; const BaseClass = sdkHelpers.compose(staticResourcesMixin.staticResourcesMixin, widgetIdMixin.widgetIdMixin, widgetConfigMixin.widgetConfigMixin, localeMixin.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 tslib.__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; }); exports.createFetchWidgetPagesMixin = createFetchWidgetPagesMixin; //# sourceMappingURL=createFetchWidgetPagesMixin.js.map