UNPKG

@adyen/adyen-platform-experience-web

Version:

![Platform Experience header](https://github.com/Adyen/adyen-platform-experience-web/assets/7926613/18094965-9e01-450e-8dc9-ea84e6b22c2b)

82 lines (81 loc) 3.95 kB
import { FALLBACK_ENV as e, resolveEnvironment as r, getConfigFromCdn as l, getDatasetFromCdn as h } from "./utils.js"; import { AuthSession as d } from "./ConfigContext/session/AuthSession.js"; import { Assets as a } from "./Assets/Assets.js"; import { getCustomTranslationsAnalyticsPayload as c } from "./Analytics/analytics/customTranslations.js"; import { shouldWarnAboutServerSideInitialization as m, SERVER_SIDE_INITIALIZATION_WARNING as u } from "./runtime.js"; import g from "./Localization/Localization.js"; import { EMPTY_OBJECT as p } from "../utils/value/constants.js"; class S { static version = "1.11.0"; components = []; options; localization; loadingContext; analyticsEnabled; session = new d(); onError; getImageAsset; getDatasetAsset; getCdnConfig; getCdnDataset; hasWarnedAboutServerSideInitialization = !1; readyCustomTranslationsAnalytics; // [TODO]: Change the error handling strategy. constructor(t) { this.options = { environment: e, ...t }; const { cdnTranslationsUrl: s, cdnAssetsUrl: i, cdnConfigUrl: n, apiUrl: o } = r(this.options.environment); this.localization = new g(t.locale, t.availableTranslations, s, n), this.loadingContext = t.loadingContext || null || o, this.getImageAsset = new a(i).getAsset({ extension: "svg", subFolder: "images" }), this.getDatasetAsset = new a(i).getAsset({ extension: "json", mainFolder: "datasets" }), this.getCdnConfig = l({ url: n }), this.getCdnDataset = h({ url: `${i}/datasets` }), this.readyCustomTranslationsAnalytics = !1, this.analyticsEnabled = t?.analytics?.enabled ?? !0, this.session.analyticsEnabled = this.analyticsEnabled, this.setOptions(t); } async initialize() { return !this.hasWarnedAboutServerSideInitialization && m() && (console.warn(u), this.hasWarnedAboutServerSideInitialization = !0), Promise.all([this.localization.ready]).then(() => { if (!this.readyCustomTranslationsAnalytics && this.analyticsEnabled) { const t = this.setTranslationsPayload(); t.length > 0 && (this.session.analyticsPayload = t, this.readyCustomTranslationsAnalytics = !0); } return this; }); } /** * Updates global configurations, resets the internal state and remounts each element. * @param options - props to update * @returns this - the element instance */ update = async (t = p) => (this.setOptions(t), await this.initialize(), this.components.forEach((s) => { s.props.core === this && s.update(this.getPropsForComponent(this.options)); }), this); /** * Remove the reference of a component * @param component - reference to the component to be removed * @returns this - the element instance */ remove = (t) => (this.components = this.components.filter((s) => s._id !== t._id), t.unmount(), this); /** * @internal * Register components in core to be able to update them all at once */ registerComponent = (t) => { t.props.core === this && this.components.push(t); }; /** * @internal * Enhances the config object passed when AdyenPlatformExperience is initialised (environment, clientKey, etc...) * (Re)Initializes core properties & processes (i18n, etc...) * @param options - the config object passed when AdyenPlatformExperience is initialised * @returns this */ setOptions = (t) => (this.options = { ...this.options, ...t }, this.localization.locale = this.options?.locale, this.localization.customTranslations = this.options?.translations, this.session.loadingContext = this.loadingContext, this.session.onSessionCreate = this.options.onSessionCreate, this); setTranslationsPayload() { return this.localization ? c(this.localization.i18n.customTranslations) : []; } /** * @internal * @param options - options that will be merged to the global Checkout props * @returns props for a new UIElement */ getPropsForComponent(t) { return { ...t }; } } export { S as default };