UNPKG

@klevu/core

Version:

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

113 lines (112 loc) 4.93 kB
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()); }); }; import { KlevuConfig } from "../index.js"; import { post } from "../connection/fetch.js"; import { Klaviyo } from "../connectors/klaviyo.js"; import { KlevuStorage } from "../utils/index.js"; export const USER_SESSION_ID_STORAGE_KEY = "klevu-user-sessionId"; export const USER_SESSION_EXPIRY_STORAGE_KEY = "klevu-user-session_expiry"; export const USER_SEGMENT_INFO_STORAGE_KEY = "klevu-user-segmentInfo"; const connectors = [ { name: "klaviyo", connector: Klaviyo }, ]; export class KlevuUserSession { constructor() { this.timer = null; this.getSessionId = () => __awaiter(this, void 0, void 0, function* () { const apiKey = 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 post(`${KlevuConfig.getDefault().visitorServiceUrl}/${apiKey}/session`, payload); }); KlevuStorage.addKey(USER_SESSION_ID_STORAGE_KEY); KlevuStorage.addKey(USER_SESSION_EXPIRY_STORAGE_KEY); KlevuStorage.addKey(USER_SEGMENT_INFO_STORAGE_KEY); this.expiry = KlevuStorage.getItem(USER_SESSION_EXPIRY_STORAGE_KEY); const segmentInfo = KlevuStorage.getItem(USER_SEGMENT_INFO_STORAGE_KEY); this.segmentInfo = segmentInfo ? JSON.parse(segmentInfo) : null; this.sessionId = KlevuStorage.getItem(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) { KlevuStorage.setItem(USER_SESSION_ID_STORAGE_KEY, sessionInfo.sessionId); this.sessionId = sessionInfo.sessionId; if (sessionInfo.segmentInfo) { KlevuStorage.setItem(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); KlevuStorage.setItem(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) || []; } }