@yext/search-core
Version:
Typescript Networking Library for the Yext Search API
77 lines • 6.48 kB
JavaScript
"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.AutocompleteServiceImpl = void 0;
const createAutocompleteResponse_1 = require("../transformers/autocompleteservice/createAutocompleteResponse");
const provideEndpoints_1 = require("../provideEndpoints");
const getClientSdk_1 = require("../utils/getClientSdk");
const serializeFilterSearch_1 = require("../serializers/serializeFilterSearch");
/**
* A service that performs query suggestions.
*/
class AutocompleteServiceImpl {
constructor(config, httpRequester, apiResponseValidator) {
this.config = config;
this.httpService = httpRequester;
this.apiResponseValidator = apiResponseValidator;
this.universalEndpoint = this.config.endpoints.universalAutocomplete;
this.verticalEndpoint = this.config.endpoints.verticalAutocomplete;
this.filterEndpoint = this.config.endpoints.filterSearch;
}
universalAutocomplete(request) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const queryParams = Object.assign(Object.assign(Object.assign({ input: request.input, experienceKey: this.config.experienceKey }, ('apiKey' in this.config && { api_key: this.config.apiKey })), { v: provideEndpoints_1.defaultApiVersion, version: this.config.experienceVersion, locale: this.config.locale, sessionTrackingEnabled: request.sessionTrackingEnabled, visitorId: (_a = this.config.visitor) === null || _a === void 0 ? void 0 : _a.id, visitorIdMethod: (_b = this.config.visitor) === null || _b === void 0 ? void 0 : _b.idMethod, limit: request.limit }), (_c = this.config) === null || _c === void 0 ? void 0 : _c.additionalQueryParams);
const response = 'token' in this.config
? yield this.httpService.get(this.universalEndpoint, queryParams, (0, getClientSdk_1.getClientSdk)(request.additionalHttpHeaders), this.config.token)
: yield this.httpService.get(this.universalEndpoint, queryParams, (0, getClientSdk_1.getClientSdk)(request.additionalHttpHeaders));
const validationResult = this.apiResponseValidator.validate(response);
if (validationResult instanceof Error) {
return Promise.reject(validationResult);
}
return (0, createAutocompleteResponse_1.createAutocompleteResponse)(response);
});
}
verticalAutocomplete(request) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const queryParams = Object.assign(Object.assign(Object.assign({ input: request.input, experienceKey: this.config.experienceKey }, ('apiKey' in this.config && { api_key: this.config.apiKey })), { v: provideEndpoints_1.defaultApiVersion, version: this.config.experienceVersion, locale: this.config.locale, verticalKey: request.verticalKey, sessionTrackingEnabled: request.sessionTrackingEnabled, visitorId: (_a = this.config.visitor) === null || _a === void 0 ? void 0 : _a.id, visitorIdMethod: (_b = this.config.visitor) === null || _b === void 0 ? void 0 : _b.idMethod, limit: request.limit }), (_c = this.config) === null || _c === void 0 ? void 0 : _c.additionalQueryParams);
const response = 'token' in this.config
? yield this.httpService.get(this.verticalEndpoint, queryParams, (0, getClientSdk_1.getClientSdk)(request.additionalHttpHeaders), this.config.token)
: yield this.httpService.get(this.verticalEndpoint, queryParams, (0, getClientSdk_1.getClientSdk)(request.additionalHttpHeaders));
const validationResult = this.apiResponseValidator.validate(response);
if (validationResult instanceof Error) {
return Promise.reject(validationResult);
}
return (0, createAutocompleteResponse_1.createAutocompleteResponse)(response);
});
}
filterSearch(request) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const searchParams = {
sectioned: request.sectioned,
fields: (0, serializeFilterSearch_1.serializeSearchParameterFields)(request.fields)
};
const queryParams = Object.assign(Object.assign(Object.assign({ input: request.input, experienceKey: this.config.experienceKey }, ('apiKey' in this.config && { api_key: this.config.apiKey })), { v: provideEndpoints_1.defaultApiVersion, version: this.config.experienceVersion, locale: this.config.locale, search_parameters: JSON.stringify(searchParams), verticalKey: request.verticalKey, sessionTrackingEnabled: request.sessionTrackingEnabled, visitorId: (_a = this.config.visitor) === null || _a === void 0 ? void 0 : _a.id, visitorIdMethod: (_b = this.config.visitor) === null || _b === void 0 ? void 0 : _b.idMethod, excluded: JSON.stringify((0, serializeFilterSearch_1.serializeExcludedFields)(request.excluded)) }), (_c = this.config) === null || _c === void 0 ? void 0 : _c.additionalQueryParams);
const response = 'token' in this.config
? yield this.httpService.get(this.filterEndpoint, queryParams, (0, getClientSdk_1.getClientSdk)(request.additionalHttpHeaders), this.config.token)
: yield this.httpService.get(this.filterEndpoint, queryParams, (0, getClientSdk_1.getClientSdk)(request.additionalHttpHeaders));
const validationResult = this.apiResponseValidator.validate(response);
if (validationResult instanceof Error) {
return Promise.reject(validationResult);
}
return (0, createAutocompleteResponse_1.createFilterSearchResponse)(response);
});
}
}
exports.AutocompleteServiceImpl = AutocompleteServiceImpl;
//# sourceMappingURL=AutocompleteServiceImpl.js.map