UNPKG

@klevu/core

Version:

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

149 lines (148 loc) 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KlevuConfig = void 0; const eventRequests_js_1 = require("./events/eventRequests.js"); const index_js_1 = require("./utils/index.js"); const usersession_js_1 = require("./resolvers/usersession.js"); const klaviyo_js_1 = require("./connectors/klaviyo.js"); const KlevuDomEvents_js_1 = require("./events/KlevuDomEvents.js"); const ipresolver_js_1 = require("./resolvers/ipresolver.js"); 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); (0, eventRequests_js_1.runPendingAnalyticalRequests)(); if (!config.disableUserSession) { this.initializeUserSession(); } this.initializeIPResolver(); } static initializeIPResolver() { ipresolver_js_1.KlevuIpResolver.init(); if (ipresolver_js_1.KlevuIpResolver.getDefault().hasIPInfoExpired()) { ipresolver_js_1.KlevuIpResolver.getDefault().generateIPData(); } else { ipresolver_js_1.KlevuIpResolver.getDefault().setExpiryTimer(); } } static initializeUserSession() { usersession_js_1.KlevuUserSession.init(); if (usersession_js_1.KlevuUserSession.getDefault().hasSessionExpired()) { usersession_js_1.KlevuUserSession.getDefault() .generateSession() .then(() => { if (this.getDefault().enableKlaviyoConnector) { klaviyo_js_1.Klaviyo.init(); } }); } else { usersession_js_1.KlevuUserSession.getDefault().setExpiryTimer(); if (this.getDefault().enableKlaviyoConnector) { klaviyo_js_1.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_js_1.KlevuDomEvents.UseConsentChanged, { detail: { useConsent, }, })); } } setEnableKlaviyoConnector(val) { this.enableKlaviyoConnector = val; if (val) { klaviyo_js_1.Klaviyo.init(); } } setConsentGiven(userConsent) { this.consentGiven = userConsent; if (userConsent) { usersession_js_1.KlevuUserSession.getDefault().generateSession(); ipresolver_js_1.KlevuIpResolver.getDefault().generateIPData(); } if (typeof document !== "undefined") { document.dispatchEvent(new CustomEvent(KlevuDomEvents_js_1.KlevuDomEvents.UserConsentGivenChanged, { detail: { userConsent, }, })); } } } exports.KlevuConfig = KlevuConfig; KlevuConfig.default = loadFromGlobal(); function loadFromGlobal() { if (!(0, index_js_1.isBrowser)()) { return undefined; } // eslint-disable-next-line @typescript-eslint/no-explicit-any const cfg = window.KlevuConfig; if (!cfg) { return undefined; } return new KlevuConfig(cfg); }