UNPKG

@klevu/core

Version:

Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.

145 lines (144 loc) 5.19 kB
import { runPendingAnalyticalRequests } from "./events/eventRequests.js"; import { isBrowser } from "./utils/index.js"; import { KlevuUserSession } from "./resolvers/usersession.js"; import { Klaviyo } from "./connectors/klaviyo.js"; import { KlevuDomEvents } from "./events/KlevuDomEvents.js"; import { KlevuIpResolver } from "./resolvers/ipresolver.js"; export class KlevuConfig { constructor(config) { var _a; this.cacheMaxTTL = 600000; this.eventsApiV1Url = "https://stats.ksearchnet.com/analytics/"; this.eventsApiV2Url = "https://stats.ksearchnet.com/analytics/collect"; this.recommendationsApiUrl = "https://config-cdn.ksearchnet.com/recommendations/"; this.visitorServiceUrl = "https://visitor.service.ksearchnet.com/public/1.0"; this.ipv6ServiceUrl = "https://ipv6check.ksearchnet.com"; this.ipv4ServiceUrl = "https://ipv4check.ksearchnet.com"; this.moiApiUrl = "https://moi-ai.ksearchnet.com/"; this.disableClickTracking = false; this.enableKlaviyoConnector = false; this.useConsent = false; this.consentGiven = false; this.disableUserSession = false; this.apiKey = config.apiKey; this.url = config.url; if (config.moiApiUrl) { this.moiApiUrl = config.moiApiUrl; } if (config.cacheMaxTTL) { this.cacheMaxTTL = config.cacheMaxTTL; } if (config.axios) { this.axios = config.axios.create(); } if (config.eventsApiV1Url) { this.eventsApiV1Url = config.eventsApiV1Url; } if (config.eventsApiV2Url) { this.eventsApiV2Url = config.eventsApiV2Url; } if (config.visitorServiceUrl) { this.visitorServiceUrl = config.visitorServiceUrl; } if (config.ipv4ServiceUrl) { this.ipv4ServiceUrl = config.ipv4ServiceUrl; } if (config.ipv6ServiceUrl) { this.ipv6ServiceUrl = config.ipv6ServiceUrl; } if (config.recommendationsApiUrl) { this.recommendationsApiUrl = config.recommendationsApiUrl; } this.disableClickTracking = (_a = config.disableClickTrackStoring) !== null && _a !== void 0 ? _a : false; this.useConsent = config.useConsent || false; this.consentGiven = config.consentGiven || false; this.enableKlaviyoConnector = config.enableKlaviyoConnector || false; this.disableUserSession = config.disableUserSession || false; } static init(config) { KlevuConfig.default = new KlevuConfig(config); runPendingAnalyticalRequests(); if (!config.disableUserSession) { this.initializeUserSession(); } this.initializeIPResolver(); } static initializeIPResolver() { KlevuIpResolver.init(); if (KlevuIpResolver.getDefault().hasIPInfoExpired()) { KlevuIpResolver.getDefault().generateIPData(); } else { KlevuIpResolver.getDefault().setExpiryTimer(); } } static initializeUserSession() { KlevuUserSession.init(); if (KlevuUserSession.getDefault().hasSessionExpired()) { KlevuUserSession.getDefault() .generateSession() .then(() => { if (this.getDefault().enableKlaviyoConnector) { Klaviyo.init(); } }); } else { KlevuUserSession.getDefault().setExpiryTimer(); if (this.getDefault().enableKlaviyoConnector) { Klaviyo.init(); } } } static getDefault() { if (!KlevuConfig.default) { throw new Error("Configuration missing"); } return KlevuConfig.default; } isConsentDisallowed() { return this.useConsent && !this.consentGiven; } setUseConsent(useConsent) { this.useConsent = useConsent; if (typeof document !== "undefined") { document.dispatchEvent(new CustomEvent(KlevuDomEvents.UseConsentChanged, { detail: { useConsent, }, })); } } setEnableKlaviyoConnector(val) { this.enableKlaviyoConnector = val; if (val) { Klaviyo.init(); } } setConsentGiven(userConsent) { this.consentGiven = userConsent; if (userConsent) { KlevuUserSession.getDefault().generateSession(); KlevuIpResolver.getDefault().generateIPData(); } if (typeof document !== "undefined") { document.dispatchEvent(new CustomEvent(KlevuDomEvents.UserConsentGivenChanged, { detail: { userConsent, }, })); } } } KlevuConfig.default = loadFromGlobal(); function loadFromGlobal() { if (!isBrowser()) { return undefined; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const cfg = window.KlevuConfig; if (!cfg) { return undefined; } return new KlevuConfig(cfg); }