@jorsek/ezd-client
Version:
100 lines • 4.36 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const Resource_1 = require("./Resource");
const Types_1 = require("../Types");
const class_transformer_validator_1 = require("class-transformer-validator");
class Search extends Resource_1.Resource {
constructor({ rootMapId, axios, config }) {
super({ axios, config });
// private search_filters: Array<{
// label: string;
// key: string;
// parent_taxon: string;
// }>;
this.search_facets = {};
this.rootMapId = rootMapId;
}
// public async get_filters(): Promise<IFilter[]> {
// await this.setup_search();
// return this.search_filters;
// }
get_search_facets() {
return __awaiter(this, void 0, void 0, function* () {
yield this.setup_search();
return this.search_facets;
});
}
get_results_per_page() {
return __awaiter(this, void 0, void 0, function* () {
yield this.setup_search();
return this.rows_per_page;
});
}
executeSearch({ queryString, page = 0, sectionPath }) {
return __awaiter(this, void 0, void 0, function* () {
yield this.setup_search();
// const drilldowns = [[["dita-class", "topic"]]];
// if (filter_facets) {
// const drilldown_facets = [];
// for (const facet_id of Object.keys(filter_facets)) {
// const or_filters = [];
// for (const facet of filter_facets[facet_id]) {
// or_filters.push([facet_id, facet]);
// }
// if (or_filters.length > 0) {
// drilldown_facets.push(or_filters);
// }
// }
// drilldowns.push(...drilldown_facets);
// }
const request_params = {
queryString,
refineToPaths: sectionPath ? [sectionPath] : undefined,
startOffset: page * this.rows_per_page,
endOffset: (page + 1) * this.rows_per_page - 1,
};
const response = yield this.axios.post("/search", JSON.stringify(request_params), {
headers: {
"Content-Type": "text/plain",
},
});
if (response.data.totalResults == null) {
throw "Search response missing total results";
}
let transformed = response.data.hits;
transformed = transformed.map(obj => {
obj.breadcrumbs = obj.breadcrumbs.filter(b => b.href !== "");
class_transformer_validator_1.transformAndValidateSync(Types_1.ISearchHit, obj);
return obj;
});
return { results: transformed, total_count: response.data.totalResults };
});
}
setup_search() {
return __awaiter(this, void 0, void 0, function* () {
const config = yield this.config.get();
this.rows_per_page = config.search.results_per_page;
// this.search_filters = config.search.filters;
// Adhering to unintuitive search query protocol here
// this.searchFolders = config.search.folders.reduce((acc, obj) => {
// acc[obj] = true;
// return acc;
// }, {});
// const transformer = el => ({
// facet_name: el.navtitle,
// id: el.value,
// children: el.children.length > 0 ? el.children.map(transformer) : [],
// });
});
}
}
exports.Search = Search;
//# sourceMappingURL=Search.js.map