UNPKG

@klevu/core

Version:

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

99 lines (98 loc) 5.14 kB
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _KlevuResponseObject_instances, _KlevuResponseObject_functions, _KlevuResponseObject_queryObjects, _KlevuResponseObject_buildQueryObjects; import { KlevuResponseQueryObject } from "./responseQueryObject.js"; /** * This class is used to access the response data from Klevu API * It builds up the state of the result and it can be used to do various things with the data */ export class KlevuResponseObject { constructor(response, functions, options, checkProcessedFunctions) { _KlevuResponseObject_instances.add(this); _KlevuResponseObject_functions.set(this, void 0); _KlevuResponseObject_queryObjects.set(this, {}); this.apiResponse = response; __classPrivateFieldSet(this, _KlevuResponseObject_functions, functions, "f"); if (checkProcessedFunctions && this.packProcessedFunctionsToString() !== checkProcessedFunctions) { throw new Error("Processed functions are not the same as on the backend"); } __classPrivateFieldGet(this, _KlevuResponseObject_instances, "m", _KlevuResponseObject_buildQueryObjects).call(this); for (const f of functions) { if (f.modifiers) { for (const modifier of f.modifiers) { if (!modifier.onResult) { continue; } if (!(options === null || options === void 0 ? void 0 : options.params.isSSR) || (options.params.isSSR && !modifier.ssrOnResultFE)) { modifier.onResult(this, f); } else if ((options === null || options === void 0 ? void 0 : options.params.FEHydrate) && modifier.ssrOnResultFE) { modifier.onResult(this, f); } } } } } /** * Get suggestions by id * * @param id * @returns */ suggestionsById(id) { var _a; return (_a = this.apiResponse.suggestionResults) === null || _a === void 0 ? void 0 : _a.find((q) => q.id === id); } /** * Get query results by id * @param id query id used * @returns */ queriesById(id) { if (!__classPrivateFieldGet(this, _KlevuResponseObject_queryObjects, "f")[id]) { throw new Error(`Query with id ${id} not found`); } return __classPrivateFieldGet(this, _KlevuResponseObject_queryObjects, "f")[id]; } /** * Check if query exists. This should be used as queriesById can throw an error * * @param id query id used * @returns */ queryExists(id) { return !!__classPrivateFieldGet(this, _KlevuResponseObject_queryObjects, "f")[id]; } /** * * @returns String that can be used to compare if the same functions were used to create this response */ packProcessedFunctionsToString() { return __classPrivateFieldGet(this, _KlevuResponseObject_functions, "f") .map((f) => { var _a, _b; return `${f.klevuFunctionId},${(_a = f.queries) === null || _a === void 0 ? void 0 : _a.map((q) => q.id)},${(_b = f.modifiers) === null || _b === void 0 ? void 0 : _b.map((m) => m.klevuModifierId)}`; }) .join(";"); } } _KlevuResponseObject_functions = new WeakMap(), _KlevuResponseObject_queryObjects = new WeakMap(), _KlevuResponseObject_instances = new WeakSet(), _KlevuResponseObject_buildQueryObjects = function _KlevuResponseObject_buildQueryObjects() { var _a; for (const q of (_a = this.apiResponse.queryResults) !== null && _a !== void 0 ? _a : []) { const func = __classPrivateFieldGet(this, _KlevuResponseObject_functions, "f").find((f) => { var _a; return (_a = f.queries) === null || _a === void 0 ? void 0 : _a.some((qu) => q.id == qu.id || q.id == `${qu.id}-fallback`); }); if (func) { __classPrivateFieldGet(this, _KlevuResponseObject_queryObjects, "f")[q.id] = new KlevuResponseQueryObject(this, q, func); } } };