UNPKG

@klevu/core

Version:

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

130 lines (129 loc) 6.12 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.KlevuIpResolver = exports.USER_UUID_STORAGE_KEY = exports.USER_IP_EXPIRY_STORAGE_KEY = exports.USER_IPV6_STORAGE_KEY = exports.USER_IPV4_STORAGE_KEY = void 0; const index_js_1 = require("../index.js"); const fetch_js_1 = require("../connection/fetch.js"); const index_js_2 = require("../utils/index.js"); exports.USER_IPV4_STORAGE_KEY = "klevu-user-v4-id"; exports.USER_IPV6_STORAGE_KEY = "klevu-user-v6-id"; exports.USER_IP_EXPIRY_STORAGE_KEY = "klevu-user-v4-v6-expiry"; exports.USER_UUID_STORAGE_KEY = "klevu-user-uuid"; const ONE_HOUR = 60 * 60 * 1000; class KlevuIpResolver { constructor() { this.timer = null; this.getIPData = () => __awaiter(this, void 0, void 0, function* () { let ipV4Response = undefined; let ipV6Response = undefined; try { const ipV4Payload = { klevu_uuid: this.uuid || "", }; ipV4Response = yield (0, fetch_js_1.post)(index_js_1.KlevuConfig.getDefault().ipv4ServiceUrl, ipV4Payload); } catch (err) { console.info(err); } try { const ipV6Payload = { klevu_uuid: (ipV4Response === null || ipV4Response === void 0 ? void 0 : ipV4Response.klevu_uuid) || "", }; ipV6Response = yield (0, fetch_js_1.post)(index_js_1.KlevuConfig.getDefault().ipv6ServiceUrl, ipV6Payload); } catch (err) { console.info(err); } return { uuid: (ipV4Response === null || ipV4Response === void 0 ? void 0 : ipV4Response.klevu_uuid) || (ipV6Response === null || ipV6Response === void 0 ? void 0 : ipV6Response.klevu_uuid) || "", ipv4: (ipV4Response === null || ipV4Response === void 0 ? void 0 : ipV4Response.ip_address) || "", ipv6: (ipV6Response === null || ipV6Response === void 0 ? void 0 : ipV6Response.ip_address) || "", }; }); index_js_2.KlevuStorage.addKey(exports.USER_IPV4_STORAGE_KEY); index_js_2.KlevuStorage.addKey(exports.USER_IPV6_STORAGE_KEY); index_js_2.KlevuStorage.addKey(exports.USER_IP_EXPIRY_STORAGE_KEY); index_js_2.KlevuStorage.addKey(exports.USER_UUID_STORAGE_KEY); this.expiry = index_js_2.KlevuStorage.getItem(exports.USER_IP_EXPIRY_STORAGE_KEY); this.uuid = index_js_2.KlevuStorage.getItem(exports.USER_UUID_STORAGE_KEY); this.ipv4 = index_js_2.KlevuStorage.getItem(exports.USER_IPV4_STORAGE_KEY); this.ipv6 = index_js_2.KlevuStorage.getItem(exports.USER_IPV6_STORAGE_KEY); } static init() { if (!KlevuIpResolver.default) KlevuIpResolver.default = new KlevuIpResolver(); } static getDefault() { if (!KlevuIpResolver.default) { throw new Error("KlevuIpResolver not initialized."); } return KlevuIpResolver.default; } hasIPInfoExpired() { if (!this.uuid || !this.ipv4 || !this.ipv6 || !this.expiry) return true; return Date.now() > +this.expiry; } generateIPData() { return __awaiter(this, void 0, void 0, function* () { try { const ipData = yield this.getIPData(); index_js_2.KlevuStorage.setItem(exports.USER_IPV4_STORAGE_KEY, ipData.ipv4); index_js_2.KlevuStorage.setItem(exports.USER_IPV6_STORAGE_KEY, ipData.ipv6); index_js_2.KlevuStorage.setItem(exports.USER_UUID_STORAGE_KEY, ipData.uuid); this.uuid = ipData.uuid; this.ipv4 = ipData.ipv4; this.ipv6 = ipData.ipv6; const expiry = Date.now() + ONE_HOUR; index_js_2.KlevuStorage.setItem(exports.USER_IP_EXPIRY_STORAGE_KEY, expiry.toString()); this.expiry = expiry.toString(); /** * Regenerate ip on expiry */ if (this.timer) { // To ensure only one timer is active clearTimeout(this.timer); } this.timer = setTimeout(() => { this.generateIPData(); }, ONE_HOUR); } catch (err) { console.error("Failed to generate ip data", err); this.timer = setTimeout(() => { this.generateIPData(); }, ONE_HOUR); } }); } 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.generateIPData(); }, expiry); } getUserData() { if (index_js_1.KlevuConfig.getDefault().isConsentDisallowed()) { return {}; } return { ipv4: index_js_2.KlevuStorage.getItem(exports.USER_IPV4_STORAGE_KEY) || "", ipv6: index_js_2.KlevuStorage.getItem(exports.USER_IPV6_STORAGE_KEY) || "", uuid: index_js_2.KlevuStorage.getItem(exports.USER_UUID_STORAGE_KEY) || "", }; } } exports.KlevuIpResolver = KlevuIpResolver;