UNPKG

@klevu/core

Version:

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

117 lines (116 loc) 5.42 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KlevuUserSession = exports.USER_SEGMENT_INFO_STORAGE_KEY = exports.USER_SESSION_EXPIRY_STORAGE_KEY = exports.USER_SESSION_ID_STORAGE_KEY = void 0; const index_js_1 = require("../index.js"); const fetch_js_1 = require("../connection/fetch.js"); const klaviyo_js_1 = require("../connectors/klaviyo.js"); const index_js_2 = require("../utils/index.js"); exports.USER_SESSION_ID_STORAGE_KEY = "klevu-user-sessionId"; exports.USER_SESSION_EXPIRY_STORAGE_KEY = "klevu-user-session_expiry"; exports.USER_SEGMENT_INFO_STORAGE_KEY = "klevu-user-segmentInfo"; const connectors = [ { name: "klaviyo", connector: klaviyo_js_1.Klaviyo }, ]; class KlevuUserSession { constructor() { this.timer = null; this.getSessionId = () => __awaiter(this, void 0, void 0, function* () { const apiKey = index_js_1.KlevuConfig.getDefault().apiKey; const sessionInfo = { connectorInfo: connectors .map(({ connector }) => { try { return connector.generatePayload(); } catch (_a) { return undefined; } }) .filter((connector) => connector), }; const payload = { apiKey, sessionId: this.sessionId || undefined, sessionInfo, }; return (0, fetch_js_1.post)(`${index_js_1.KlevuConfig.getDefault().visitorServiceUrl}/${apiKey}/session`, payload); }); index_js_2.KlevuStorage.addKey(exports.USER_SESSION_ID_STORAGE_KEY); index_js_2.KlevuStorage.addKey(exports.USER_SESSION_EXPIRY_STORAGE_KEY); index_js_2.KlevuStorage.addKey(exports.USER_SEGMENT_INFO_STORAGE_KEY); this.expiry = index_js_2.KlevuStorage.getItem(exports.USER_SESSION_EXPIRY_STORAGE_KEY); const segmentInfo = index_js_2.KlevuStorage.getItem(exports.USER_SEGMENT_INFO_STORAGE_KEY); this.segmentInfo = segmentInfo ? JSON.parse(segmentInfo) : null; this.sessionId = index_js_2.KlevuStorage.getItem(exports.USER_SESSION_ID_STORAGE_KEY); } static init() { if (!KlevuUserSession.default) KlevuUserSession.default = new KlevuUserSession(); } static getDefault() { if (!KlevuUserSession.default) { throw new Error("KlevuUserSession not initialized."); } return KlevuUserSession.default; } hasSessionExpired() { if (!this.sessionId || !this.expiry) return true; return Date.now() > +this.expiry; } generateSession() { return __awaiter(this, void 0, void 0, function* () { const sessionInfo = yield this.getSessionId(); if (sessionInfo) { index_js_2.KlevuStorage.setItem(exports.USER_SESSION_ID_STORAGE_KEY, sessionInfo.sessionId); this.sessionId = sessionInfo.sessionId; if (sessionInfo.segmentInfo) { index_js_2.KlevuStorage.setItem(exports.USER_SEGMENT_INFO_STORAGE_KEY, JSON.stringify(sessionInfo.segmentInfo)); this.segmentInfo = sessionInfo.segmentInfo; const expiry = Date.now() + (sessionInfo.segmentInfo.ttl ? sessionInfo.segmentInfo.ttl * 1000 : 0); index_js_2.KlevuStorage.setItem(exports.USER_SESSION_EXPIRY_STORAGE_KEY, expiry.toString()); this.expiry = expiry.toString(); /** * Regenerate session on expiry */ if (this.timer) { // To ensure only one timer is active clearTimeout(this.timer); } this.timer = setTimeout(() => { this.generateSession(); }, sessionInfo.segmentInfo.ttl * 1000); } } else { console.warn("Failed to generate user session"); } }); } setExpiryTimer() { const currentTime = Date.now(); const expiry = +(this.expiry || currentTime) - currentTime; if (this.timer) { // To ensure only one timer is active clearTimeout(this.timer); } this.timer = setTimeout(() => { this.generateSession(); }, expiry); } getSegments() { var _a; return ((_a = this.segmentInfo) === null || _a === void 0 ? void 0 : _a.segments) || []; } } exports.KlevuUserSession = KlevuUserSession;