UNPKG

@klevu/core

Version:

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

206 lines (205 loc) 7.96 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()); }); }; var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; import { KlevuConfig, } from "../index.js"; import { get, post } from "../connection/fetch.js"; import { isBrowser } from "../utils/isBrowser.js"; import { KlevuStorage, StorageType } from "../utils/storage.js"; const KEY_PENDING_REQUESTS = "klevu-pending-analytics"; export function KlevuEventV1Search(event) { return __awaiter(this, void 0, void 0, function* () { const url = `${KlevuConfig.getDefault().eventsApiV1Url}n-search/search`; const id = addPendingRequest(url, event); const res = yield sendGenericPostEvent(url, event); if (id) { removePendingRequest(id); } return res; }); } export function KlevuEventV1ProductTracking(event) { return __awaiter(this, void 0, void 0, function* () { const url = `${KlevuConfig.getDefault().eventsApiV1Url}productTracking`; const id = addPendingRequest(url, event); const res = yield sendGenericPostEvent(url, event); if (id) { removePendingRequest(id); } return res; }); } export function KlevuEventV2CheckedOutProducts(event) { return __awaiter(this, void 0, void 0, function* () { const url = `${KlevuConfig.getDefault().eventsApiV2Url}`; const id = addPendingRequest(url, event); const res = yield post(url, [event], true); if (id) { removePendingRequest(id); } return res; }); } export function KlevuEventV1CategoryView(event) { return __awaiter(this, void 0, void 0, function* () { const url = `${KlevuConfig.getDefault().eventsApiV1Url}categoryProductViewTracking`; const id = addPendingRequest(url, event); const res = yield sendGenericPostEvent(url, event); if (id) { removePendingRequest(id); } return res; }); } export function KlevuEventV1CategoryProductClick(event) { return __awaiter(this, void 0, void 0, function* () { const url = `${KlevuConfig.getDefault().eventsApiV1Url}categoryProductClickTracking`; const id = addPendingRequest(url, event); const res = yield sendGenericPostEvent(url, event); if (id) { removePendingRequest(id); } return res; }); } export function KlevuEventV1BannerClick(event) { return __awaiter(this, void 0, void 0, function* () { const url = `${KlevuConfig.getDefault().eventsApiV1Url}dataTracking`; const id = addPendingRequest(url, event); const res = yield sendGenericPostEvent(url, event); if (id) { removePendingRequest(id); } return res; }); } export function KlevuEventV2(data) { return __awaiter(this, void 0, void 0, function* () { const url = KlevuConfig.getDefault().eventsApiV2Url; const id = addPendingRequest(url, data); const res = yield post(url, data, true); if (id) { removePendingRequest(id); } return res; }); } // eslint-disable-next-line @typescript-eslint/no-explicit-any function sendGenericPostEvent(url, data) { return __awaiter(this, void 0, void 0, function* () { const formData = new FormData(); for (const key in data) { if (data[key] !== undefined) { if (Array.isArray(data[key])) { data[key].forEach((d) => { formData.append(key, d); }); } else { formData.append(key, data[key]); } } } if (isBrowser() && navigator.sendBeacon) { if (navigator.sendBeacon(url, formData)) { return; } } return yield post(url, formData, true); }); } function addPendingRequest(url, data) { if (!isBrowser()) { return; } let requests = []; const old = KlevuStorage.getItem(KEY_PENDING_REQUESTS, StorageType.SESSION); if (old) { try { requests = JSON.parse(old); } catch (_a) { KlevuStorage.removeItem(KEY_PENDING_REQUESTS, StorageType.SESSION); } } const id = generateUID(); requests.push({ id, url, data }); KlevuStorage.setItem(KEY_PENDING_REQUESTS, JSON.stringify(requests), StorageType.SESSION); return id; } function removePendingRequest(id) { if (!isBrowser()) { return; } const data = KlevuStorage.getItem(KEY_PENDING_REQUESTS, StorageType.SESSION); if (!data) { console.error("No pending requests!"); return; } try { const requests = JSON.parse(data); requests.splice(requests.findIndex((r) => r.id === id), 1); KlevuStorage.setItem(KEY_PENDING_REQUESTS, JSON.stringify(requests), StorageType.SESSION); } catch (e) { console.error("Failed to save pending request"); } } export function runPendingAnalyticalRequests() { var _a, e_1, _b, _c; return __awaiter(this, void 0, void 0, function* () { if (!isBrowser()) { return; } const data = KlevuStorage.getItem(KEY_PENDING_REQUESTS, StorageType.SESSION); if (!data) { return; } const requests = JSON.parse(data); try { for (var _d = true, requests_1 = __asyncValues(requests), requests_1_1; requests_1_1 = yield requests_1.next(), _a = requests_1_1.done, !_a; _d = true) { _c = requests_1_1.value; _d = false; const request = _c; try { if (request.data) { yield sendGenericPostEvent(request.url, request.data); } else { yield get(request.url, true); } removePendingRequest(request.id); } catch (e) { // we ignore errors here } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = requests_1.return)) yield _b.call(requests_1); } finally { if (e_1) throw e_1.error; } } }); } function generateUID() { let firstPart = (Math.random() * 46656) | 0; let secondPart = (Math.random() * 46656) | 0; firstPart = ("000" + firstPart.toString(36)).slice(-3); secondPart = ("000" + secondPart.toString(36)).slice(-3); return firstPart + secondPart; }