groupby-api
Version:
Client for the GroupBy Searchandiser API.
188 lines • 6.33 kB
JavaScript
import * as clone from 'clone';
import * as deepEqual from 'deep-equal';
import * as filterObject from 'filter-object';
import * as qs from 'qs';
import { NavigationConverter } from '../utils';
const REFINEMENT_MASK = '{navigationName,value,low,high}';
export class Query {
constructor(query = '') {
this.request = {};
this.unprocessedNavigations = [];
this.queryParams = {};
this.request.query = query;
this.request.sort = [];
this.request.fields = [];
this.request.orFields = [];
this.request.refinements = [];
this.request.customUrlParams = [];
this.request.includedNavigations = [];
this.request.excludedNavigations = [];
this.request.wildcardSearchEnabled = false;
this.request.pruneRefinements = true;
}
withQuery(query) {
this.request.query = query;
return this;
}
withConfiguration(configuration, mask = '*') {
Object.assign(this.request, filterObject(configuration, mask));
return this;
}
withSelectedRefinements(...refinements) {
refinements.forEach((ref) => this.addRefinement(ref, this.request.refinements));
return this;
}
withoutSelectedRefinements(...refinements) {
refinements.forEach((refinement) => {
const index = this.request.refinements.findIndex((ref) => deepEqual(ref, refinement));
if (index > -1)
this.request.refinements.splice(index, 1);
});
return this;
}
withRefinements(navigationName, ...refinements) {
const convert = (refinement) => Object.assign(refinement, { navigationName });
refinements.map(convert).forEach((ref) => this.addRefinement(ref, this.request.refinements));
return this;
}
withNavigations(...navigations) {
this.unprocessedNavigations.push(...navigations);
return this;
}
withCustomUrlParams(customUrlParams) {
if (typeof customUrlParams === 'string') {
this.request.customUrlParams.push(...this.convertParamString(customUrlParams));
}
else if (Array.isArray(customUrlParams)) {
this.request.customUrlParams.push(...customUrlParams);
}
return this;
}
withFields(...fields) {
this.request.fields.push(...fields);
return this;
}
withOrFields(...orFields) {
this.request.orFields.push(...orFields);
return this;
}
withSorts(...sorts) {
this.request.sort.push(...sorts);
return this;
}
withoutSorts(...sorts) {
this.request.sort = this.request.sort.filter((oldSort) => !sorts.find((sort) => sort.type === 'ByIds'
? oldSort.type === 'ByIds' && sort.ids.toString() === oldSort.ids.toString()
: sort.field === oldSort.field));
return this;
}
withIncludedNavigations(...navigationNames) {
this.request.includedNavigations.push(...navigationNames);
return this;
}
withExcludedNavigations(...navigationNames) {
this.request.excludedNavigations.push(...navigationNames);
return this;
}
withQueryParams(queryParams) {
switch (typeof queryParams) {
case 'string':
return Object.assign(this, { queryParams: this.convertQueryString(queryParams) });
case 'object':
return Object.assign(this, { queryParams });
}
}
refineByValue(navigationName, value, exclude = false) {
return this.withSelectedRefinements({
navigationName,
value,
exclude,
type: 'Value'
});
}
refineByRange(navigationName, low, high, exclude = false) {
return this.withSelectedRefinements({
navigationName,
low,
high,
exclude,
type: 'Range'
});
}
restrictNavigation(restrictNavigation) {
this.request.restrictNavigation = restrictNavigation;
return this;
}
skip(skip) {
this.request.skip = skip;
return this;
}
withPageSize(pageSize) {
this.request.pageSize = pageSize;
return this;
}
withMatchStrategy(matchStrategy) {
this.request.matchStrategy = matchStrategy;
return this;
}
withBiasing(biasing) {
this.request.biasing = biasing;
return this;
}
withPreFilterExpression(preFilterExpression) {
this.request['pre-filter'] = preFilterExpression;
return this;
}
enableWildcardSearch() {
this.request.wildcardSearchEnabled = true;
return this;
}
disableAutocorrection() {
this.request.disableAutocorrection = true;
return this;
}
disableBinaryPayload() {
this.request.returnBinary = false;
return this;
}
allowPrunedRefinements() {
this.request.pruneRefinements = false;
return this;
}
build() {
const builtRequest = this.raw;
NavigationConverter.convert(this.unprocessedNavigations)
.forEach((ref) => this.addRefinement(ref, builtRequest.refinements));
return this.clearEmptyArrays(builtRequest);
}
get raw() {
return clone(this.request, false);
}
get rawNavigations() {
return Object.create(this.unprocessedNavigations);
}
addRefinement(refinement, refinements) {
if (!refinements.find((ref) => this.refinementMatches(ref, refinement))) {
refinements.push(refinement);
}
}
refinementMatches(target, original) {
return deepEqual(filterObject(target, REFINEMENT_MASK), filterObject(original, REFINEMENT_MASK));
}
convertParamString(customUrlParams) {
const parsed = qs.parse(customUrlParams);
return Object.keys(parsed).reduce((converted, key) => converted.concat({ key, value: parsed[key] }), []);
}
convertQueryString(queryParams) {
return qs.parse(queryParams);
}
clearEmptyArrays(request) {
for (let key in request) {
if (Array.isArray(request[key]) && request[key].length === 0) {
delete request[key];
}
}
return request;
}
}
//# sourceMappingURL=query.js.map