@jorsek/ezd-client
Version:
115 lines • 4.76 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;
});
}
get_facets(facetRequests) {
return __awaiter(this, void 0, void 0, function* () {
const request_params = {
queryString: "*",
facetRequests: facetRequests.map(facetRequest => ([facetRequest])),
startOffset: 0,
endOffset: 1,
hierarchical_facets: true
};
const response = yield this.axios.post("/search", JSON.stringify(request_params), {
headers: {
"Content-Type": "text/plain",
},
});
const returnedFacets = response.data.hierarchicalFacets;
return returnedFacets;
});
}
executeSearch({ queryString, page = 0, sectionPath, facetRequests, drilldowns }) {
return __awaiter(this, void 0, void 0, function* () {
yield this.setup_search();
let refineToPaths;
if (Array.isArray(sectionPath)) {
refineToPaths = sectionPath;
}
else if (sectionPath) {
refineToPaths = [sectionPath];
}
;
const request_params = {
queryString,
refineToPaths,
startOffset: page * this.rows_per_page,
endOffset: (page + 1) * this.rows_per_page - 1,
facetRequests: facetRequests,
drilldowns: drilldowns
};
console.log(request_params);
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