UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

184 lines 9.22 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var operators_1 = require("rxjs/operators"); var ResourcesConversionUtil_1 = require("../../../models/v2/resources/ResourcesConversionUtil"); var endpoint_1 = require("../../endpoint"); var jsonld = require("jsonld/dist/jsonld.js"); /** * Handles requests to the search route of the Knora API. */ var SearchEndpoint = /** @class */ (function (_super) { __extends(SearchEndpoint, _super); function SearchEndpoint(knoraApiConfig, path, v2Endpoint) { var _this = _super.call(this, knoraApiConfig, path) || this; _this.knoraApiConfig = knoraApiConfig; _this.path = path; _this.v2Endpoint = v2Endpoint; return _this; } /** * URL encodes fulltext search params. * * @param offset offset to be used for paging, zero-based. * @param params parameters for fulltext search. */ SearchEndpoint.encodeFulltextParams = function (offset, params) { var paramsString = "?offset=" + offset; if (params !== undefined) { if (params.limitToResourceClass !== undefined) { paramsString += "&limitToResourceClass=" + encodeURIComponent(params.limitToResourceClass); } if (params.limitToProject !== undefined) { paramsString += "&limitToProject=" + encodeURIComponent(params.limitToProject); } if (params.limitToStandoffClass !== undefined) { paramsString += "&limitToStandoffClass=" + encodeURIComponent(params.limitToStandoffClass); } } return paramsString; }; /** * URL encodes search by label params. * * @param offset offset to be used for paging, zero-based. * @param params parameters for search by label. */ SearchEndpoint.encodeLabelParams = function (offset, params) { var paramsString = "?offset=" + offset; if (params !== undefined) { if (params.limitToResourceClass !== undefined) { paramsString += "&limitToResourceClass=" + encodeURIComponent(params.limitToResourceClass); } if (params.limitToProject !== undefined) { paramsString += "&limitToProject=" + encodeURIComponent(params.limitToProject); } } return paramsString; }; /** * Performs a fulltext search. * * @param searchTerm the term to search for. * @param offset offset to be used for paging, zero-based. * @param params parameters for fulltext search, if any. */ SearchEndpoint.prototype.doFulltextSearch = function (searchTerm, offset, params) { // TODO: Do not hard-code the URL and http call params, generate this from Knora var _this = this; if (offset === void 0) { offset = 0; } return this.httpGet("/search/" + encodeURIComponent(searchTerm) + SearchEndpoint.encodeFulltextParams(offset, params)).pipe(operators_1.mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), operators_1.mergeMap(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil_1.ResourcesConversionUtil.createReadResourceSequence(jsonldobj, _this.v2Endpoint.ontologyCache, _this.v2Endpoint.listNodeCache, _this.jsonConvert); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a fulltext search count query. * * @param searchTerm the term to search for. * @param offset offset to be used for paging, zero-based. * @param params parameters for fulltext search, if any. */ SearchEndpoint.prototype.doFulltextSearchCountQuery = function (searchTerm, offset, params) { // TODO: Do not hard-code the URL and http call params, generate this from Knora var _this = this; if (offset === void 0) { offset = 0; } return this.httpGet("/search/count/" + encodeURIComponent(searchTerm) + SearchEndpoint.encodeFulltextParams(offset, params)).pipe(operators_1.mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), operators_1.map(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil_1.ResourcesConversionUtil.createCountQueryResponse(jsonldobj, _this.jsonConvert); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a Gravsearch query. * * @param gravsearchQuery the given Gravsearch query. */ SearchEndpoint.prototype.doExtendedSearch = function (gravsearchQuery) { // TODO: Do not hard-code the URL and http call params, generate this from Knora var _this = this; // TODO: check if content-type have to be set to text/plain return this.httpPost("/searchextended", gravsearchQuery, "sparql").pipe(operators_1.mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), operators_1.mergeMap(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil_1.ResourcesConversionUtil.createReadResourceSequence(jsonldobj, _this.v2Endpoint.ontologyCache, _this.v2Endpoint.listNodeCache, _this.jsonConvert); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a Gravsearch count query. * * @param gravsearchQuery the given Gravsearch query. */ SearchEndpoint.prototype.doExtendedSearchCountQuery = function (gravsearchQuery) { // TODO: Do not hard-code the URL and http call params, generate this from Knora var _this = this; return this.httpPost("/searchextended/count", gravsearchQuery, "sparql").pipe(operators_1.mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), operators_1.map(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil_1.ResourcesConversionUtil.createCountQueryResponse(jsonldobj, _this.jsonConvert); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a search by label. * * @param searchTerm the label to search for. * @param offset offset to be used for paging, zero-based. * @param params parameters for fulltext search, if any. */ SearchEndpoint.prototype.doSearchByLabel = function (searchTerm, offset, params) { // TODO: Do not hard-code the URL and http call params, generate this from Knora var _this = this; if (offset === void 0) { offset = 0; } return this.httpGet("/searchbylabel/" + encodeURIComponent(searchTerm) + SearchEndpoint.encodeLabelParams(offset, params)).pipe(operators_1.mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), operators_1.mergeMap(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil_1.ResourcesConversionUtil.createReadResourceSequence(jsonldobj, _this.v2Endpoint.ontologyCache, _this.v2Endpoint.listNodeCache, _this.jsonConvert); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; return SearchEndpoint; }(endpoint_1.Endpoint)); exports.SearchEndpoint = SearchEndpoint; //# sourceMappingURL=search-endpoint.js.map