UNPKG

coveo-search-ui-extensions

Version:

Small generic components to extend the functionality of Coveo's Search UI framework.

83 lines 3.51 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 { EndpointCaller, Assert } from 'coveo-search-ui'; /** * User Actions posible type. */ export var UserActionType; (function (UserActionType) { UserActionType["Search"] = "SEARCH"; UserActionType["Click"] = "CLICK"; UserActionType["PageView"] = "VIEW"; UserActionType["Custom"] = "CUSTOM"; UserActionType["TicketCreated"] = "TICKET_CREATED"; })(UserActionType || (UserActionType = {})); /** * Class that handle interaction with the endpoint. */ export class UserProfilingEndpoint { /** * Create a `UserProfilingEndpoint` instance. * Create [`EndpointCaller`]{@link EndpointCaller} instance and uses it to communicate with the endpoint internally. * * @param options The options to initialize the component. */ constructor(options) { this.options = options; Assert.exists(this.options.accessToken); Assert.exists(this.options.organization); this.options.uri = this.options.uri ? this.options.uri : UserProfilingEndpoint.DEFAULT_URI; this.options.accessToken.subscribeToRenewal(this.buildEndpointCaller.bind(this)); this.buildEndpointCaller(this.options.accessToken.token); } buildEndpointCaller(token) { this.caller = new EndpointCaller({ accessToken: token }); } /** * Get the list of actions a user has performed. * * @param userId Id from which action history will be retrieve. (either visitId or user email). */ getActions(userId) { return __awaiter(this, void 0, void 0, function* () { Assert.exists(userId); const response = yield this.caller.call({ method: 'POST', url: `${this.options.uri}/rest/organizations/${this.options.organization}/machinelearning/user/actions`, queryString: [], responseType: 'json', requestDataType: 'application/json', requestData: { objectId: userId }, errorsAsSuccess: false, }); if (this.isResponseEmpty(response)) { throw new Error(`Response has no values: ${JSON.stringify(response)}`); } return this.parseResponse(response.data); }); } parseResponse(response) { return response.value.map((v) => { return { time: parseInt(v.time), value: JSON.parse(v.value), name: v.name, }; }); } isResponseEmpty(response) { return !response || !response.data || !response.data.value || !Array.isArray(response.data.value) || !(response.data.value.length > 0); } } /** * Default platform uri. */ UserProfilingEndpoint.DEFAULT_URI = 'https://platform.cloud.coveo.com'; //# sourceMappingURL=UserProfilingEndpoint.js.map