@klevu/core
Version:
Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.
126 lines (125 loc) • 5.5 kB
JavaScript
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 { KlevuStorage } from "../utils/index.js";
export const USER_IPV4_STORAGE_KEY = "klevu-user-v4-id";
export const USER_IPV6_STORAGE_KEY = "klevu-user-v6-id";
export const USER_IP_EXPIRY_STORAGE_KEY = "klevu-user-v4-v6-expiry";
export const USER_UUID_STORAGE_KEY = "klevu-user-uuid";
const ONE_HOUR = 60 * 60 * 1000;
export 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 post(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 post(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) || "",
};
});
KlevuStorage.addKey(USER_IPV4_STORAGE_KEY);
KlevuStorage.addKey(USER_IPV6_STORAGE_KEY);
KlevuStorage.addKey(USER_IP_EXPIRY_STORAGE_KEY);
KlevuStorage.addKey(USER_UUID_STORAGE_KEY);
this.expiry = KlevuStorage.getItem(USER_IP_EXPIRY_STORAGE_KEY);
this.uuid = KlevuStorage.getItem(USER_UUID_STORAGE_KEY);
this.ipv4 = KlevuStorage.getItem(USER_IPV4_STORAGE_KEY);
this.ipv6 = KlevuStorage.getItem(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();
KlevuStorage.setItem(USER_IPV4_STORAGE_KEY, ipData.ipv4);
KlevuStorage.setItem(USER_IPV6_STORAGE_KEY, ipData.ipv6);
KlevuStorage.setItem(USER_UUID_STORAGE_KEY, ipData.uuid);
this.uuid = ipData.uuid;
this.ipv4 = ipData.ipv4;
this.ipv6 = ipData.ipv6;
const expiry = Date.now() + ONE_HOUR;
KlevuStorage.setItem(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 (KlevuConfig.getDefault().isConsentDisallowed()) {
return {};
}
return {
ipv4: KlevuStorage.getItem(USER_IPV4_STORAGE_KEY) || "",
ipv6: KlevuStorage.getItem(USER_IPV6_STORAGE_KEY) || "",
uuid: KlevuStorage.getItem(USER_UUID_STORAGE_KEY) || "",
};
}
}