UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

296 lines 15.2 kB
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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { catchError, map, mergeMap } from "rxjs"; import { ListNodeV2Cache } from "../../../cache/ListNodeV2Cache"; import { OntologyCache } from "../../../cache/ontology-cache/OntologyCache"; import { ResourcesConversionUtil } from "../../../models/v2/resources/ResourcesConversionUtil"; import { Endpoint } from "../../endpoint"; var jsonld = require("jsonld/dist/jsonld.js"); /** * Handles requests to the search route of the Knora API. * * @category Endpoint V2 */ var SearchEndpointV2 = /** @class */ (function (_super) { __extends(SearchEndpointV2, _super); function SearchEndpointV2(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. */ SearchEndpointV2.encodeFulltextParams = function (offset, params) { var paramsString = "?offset=".concat(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. */ SearchEndpointV2.encodeLabelParams = function (offset, params) { var paramsString = "?offset=".concat(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. */ SearchEndpointV2.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; } // Create temporary caches for this request only // These will be garbage collected after the request completes var tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint); var tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint); return this.httpGet("/search/" + encodeURIComponent(searchTerm) + SearchEndpointV2.encodeFulltextParams(offset, params)).pipe(mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), mergeMap(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil.createReadResourceSequence(jsonldobj, tempOntologyCache, tempListNodeCache, _this.jsonConvert); }), 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. */ SearchEndpointV2.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) + SearchEndpointV2.encodeFulltextParams(offset, params)).pipe(mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), map(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil.createCountQueryResponse(jsonldobj, _this.jsonConvert); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a Gravsearch query. * * @param gravsearchQuery the given Gravsearch query. */ SearchEndpointV2.prototype.doExtendedSearch = function (gravsearchQuery) { // TODO: Do not hard-code the URL and http call params, generate this from Knora // TODO: check if content-type have to be set to text/plain var _this = this; // Create temporary caches for this request only // These will be garbage collected after the request completes var tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint); var tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint); return this.httpPost("/searchextended", gravsearchQuery, "sparql").pipe(mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), mergeMap(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil.createReadResourceSequence(jsonldobj, tempOntologyCache, tempListNodeCache, _this.jsonConvert); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a Gravsearch count query. * * @param gravsearchQuery the given Gravsearch query. */ SearchEndpointV2.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(mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), map(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil.createCountQueryResponse(jsonldobj, _this.jsonConvert); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a Gravsearch in order to get incoming links of queried resource * * @param resourceIri resource that is queried for incoming links * @param offset the offset to be used for paging */ SearchEndpointV2.prototype.doSearchIncomingLinks = function (resourceIri, offset) { var _this = this; if (offset === void 0) { offset = 0; } // Create temporary caches for this request only // These will be garbage collected after the request completes var tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint); var tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint); return this.httpGet("/searchIncomingLinks/".concat(encodeURIComponent(resourceIri), "?offset=").concat(offset)).pipe(mergeMap(function (response) { return jsonld.compact(response.response, {}); }), mergeMap(function (jsonld) { return ResourcesConversionUtil.createReadResourceSequence(jsonld, tempOntologyCache, tempListNodeCache, _this.jsonConvert); }), catchError(function (err) { return _this.handleError(err); })); }; /** * Performs a Gravsearch in order to get StillImageRepresentations of queried resource * * @param resourceIri resource that is queried for incoming links * @param offset the offset to be used for paging */ SearchEndpointV2.prototype.doSearchStillImageRepresentations = function (resourceIri, offset) { var _this = this; if (offset === void 0) { offset = 0; } // Create temporary caches for this request only // These will be garbage collected after the request completes var tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint); var tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint); return this.httpGet("/searchStillImageRepresentations/".concat(encodeURIComponent(resourceIri), "?offset=").concat(offset)).pipe(mergeMap(function (response) { return jsonld.compact(response.response, {}); }), mergeMap(function (jsonld) { return ResourcesConversionUtil.createReadResourceSequence(jsonld, tempOntologyCache, tempListNodeCache, _this.jsonConvert); }), catchError(function (err) { return _this.handleError(err); })); }; /** * Performs a Gravsearch in order to get StillImageRepresentations count of queried resource * * @param resourceIri resource that is queried for incoming links */ SearchEndpointV2.prototype.doSearchStillImageRepresentationsCount = function (resourceIri) { var _this = this; return this.httpGet("/searchStillImageRepresentationsCount/".concat(encodeURIComponent(resourceIri))).pipe(mergeMap(function (response) { return jsonld.compact(response.response, {}); }), map(function (jsonld) { return ResourcesConversionUtil.createCountQueryResponse(jsonld, _this.jsonConvert); }), catchError(function (err) { return _this.handleError(err); })); }; /** * Performs a Gravsearch to get incoming regions of queried resource * * @param resourceIri resource that is queried for incoming links * @param offset the offset to be used for paging */ SearchEndpointV2.prototype.doSearchIncomingRegions = function (resourceIri, offset) { var _this = this; if (offset === void 0) { offset = 0; } // Create temporary caches for this request only // These will be garbage collected after the request completes var tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint); var tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint); return this.httpGet("/searchIncomingRegions/".concat(encodeURIComponent(resourceIri), "?offset=").concat(offset)).pipe(mergeMap(function (response) { return jsonld.compact(response.response, {}); }), mergeMap(function (jsonld) { return ResourcesConversionUtil.createReadResourceSequence(jsonld, tempOntologyCache, tempListNodeCache, _this.jsonConvert); }), catchError(function (err) { return _this.handleError(err); })); }; /** * 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. */ SearchEndpointV2.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; } // Create temporary caches for this request only // These will be garbage collected after the request completes var tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint); var tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint); return this.httpGet("/searchbylabel/" + encodeURIComponent(searchTerm) + SearchEndpointV2.encodeLabelParams(offset, params)).pipe(mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); // TODO: @rosenth Adapt context object // TODO: adapt getOntologyIriFromEntityIri return jsonld.compact(ajaxResponse.response, {}); }), mergeMap(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil.createReadResourceSequence(jsonldobj, tempOntologyCache, tempListNodeCache, _this.jsonConvert); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Performs a query to get the count of results when performing a search by label. * * @param searchTerm the label to search for. * @param params parameters for fulltext search, if any. */ SearchEndpointV2.prototype.doSearchByLabelCountQuery = function (searchTerm, params) { var _this = this; return this.httpGet("/searchbylabel/count/" + encodeURIComponent(searchTerm) + SearchEndpointV2.encodeLabelParams(0, params)).pipe(mergeMap(function (ajaxResponse) { // console.log(JSON.stringify(ajaxResponse.response)); return jsonld.compact(ajaxResponse.response, {}); }), map(function (jsonldobj) { // console.log(JSON.stringify(jsonldobj)); return ResourcesConversionUtil.createCountQueryResponse(jsonldobj, _this.jsonConvert); }), catchError(function (error) { return _this.handleError(error); })); }; return SearchEndpointV2; }(Endpoint)); export { SearchEndpointV2 }; //# sourceMappingURL=search-endpoint-v2.js.map