UNPKG

@dasch-swiss/dsp-js

Version:

JavaScript library that handles API requests to Knora

201 lines 10.1 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 { CanDoResponse } from "../../../models/v2/ontologies/read/can-do-response"; import { DeleteResourceResponse } from "../../../models/v2/resources/delete/delete-resource-response"; import { ResourcesConversionUtil } from "../../../models/v2/resources/ResourcesConversionUtil"; import { UpdateResourceMetadataResponse } from "../../../models/v2/resources/update/update-resource-metadata-response"; import { Endpoint } from "../../endpoint"; var jsonld = require("jsonld/dist/jsonld.js"); /** * Handles requests to the resources route of the Knora API. * * @category Endpoint V2 */ var ResourcesEndpointV2 = /** @class */ (function (_super) { __extends(ResourcesEndpointV2, _super); /** * @category Internal * @param knoraApiConfig the config object. * @param path this endpoint's base path. * @param v2Endpoint a reference to the v2 endpoint. */ function ResourcesEndpointV2(knoraApiConfig, path, v2Endpoint) { var _this = _super.call(this, knoraApiConfig, path) || this; _this.knoraApiConfig = knoraApiConfig; _this.path = path; _this.v2Endpoint = v2Endpoint; return _this; } /** * Given a sequence of resource IRIs, gets the resources from Knora. * * @param resourceIris Iris of the resources to get. * @param version Timestamp of the resource version. */ ResourcesEndpointV2.prototype.getResources = function (resourceIris, version) { // TODO: Do not hard-code the URL and http call params, generate this from Knora var _this = this; // make URL containing resource Iris as segments var resIris = resourceIris.map(function (resIri) { return "/" + encodeURIComponent(resIri) + (version ? "?version=" + version : ""); }).reduce(function (acc, currentValue) { return acc + currentValue; }); // 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(resIris).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); })); }; /** * Given a resource IRI, gets the resource from Knora. * * @param resourceIri Iri of the resource to get. * @param version Timestamp of the resource version. */ ResourcesEndpointV2.prototype.getResource = function (resourceIri, version) { var _this = this; return this.getResources([resourceIri], version).pipe(map(function (resources) { return resources.resources[0]; }), catchError(function (error) { return _this.handleError(error); })); }; /** * Creates a new resource. * * @param resource the resource to be created. */ ResourcesEndpointV2.prototype.createResource = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); // get property Iris var propIris = Object.keys(resource.properties); // for each property, serialize its values // and assign them to the resource propIris.forEach(function (propIri) { // check that array contains least one value if (resource.properties[propIri].length === 0) { throw new Error("No values defined for " + propIri); } // if array contains only one element, serialize as an object if (resource.properties[propIri].length === 1) { res[propIri] = _this.jsonConvert.serializeObject(resource.properties[propIri][0]); } else { res[propIri] = _this.jsonConvert.serializeArray(resource.properties[propIri]); } }); // 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("", res, "json", { "X-Asset-Ingested": "true" }).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); }), map(function (resources) { return resources.resources[0]; }), catchError(function (error) { return _this.handleError(error); })); }; /** * Updates a resource's metadata. * * @param resourceMetadata the new metadata. */ ResourcesEndpointV2.prototype.updateResourceMetadata = function (resourceMetadata) { var _this = this; // check that at least one of the following properties is updated: label, hasPermissions, newModificationDate if (resourceMetadata.label === undefined && resourceMetadata.hasPermissions === undefined && resourceMetadata.newModificationDate === undefined) { throw new Error("At least one of the following properties has to be updated: label, hasPermissions, newModificationDate"); } var res = this.jsonConvert.serializeObject(resourceMetadata); return this.httpPut("", res).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) { return _this.jsonConvert.deserializeObject(jsonldobj, UpdateResourceMetadataResponse); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Checks whether a resource can be deleted or not. * * @param resource the resource to check. */ ResourcesEndpointV2.prototype.canDeleteResource = function (resource) { var _this = this; var res = JSON.stringify(this.jsonConvert.serializeObject(resource)); return this.httpGet("/candelete?jsonLd=".concat(encodeURIComponent(res))).pipe(mergeMap(function (ajaxResponse) { return jsonld.compact(ajaxResponse.response, {}); }), map(function (jsonldobj) { return _this.jsonConvert.deserializeObject(jsonldobj, CanDoResponse); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Deletes a resource. * * @param resource the resource to be deleted. */ ResourcesEndpointV2.prototype.deleteResource = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); return this.httpPost("/delete", res).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) { return _this.jsonConvert.deserializeObject(jsonldobj, DeleteResourceResponse); }), catchError(function (error) { return _this.handleError(error); })); }; /** * Erases a resource. * * @param resource the resource to be deleted. */ ResourcesEndpointV2.prototype.eraseResource = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); return this.httpPost("/erase", res).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) { return _this.jsonConvert.deserializeObject(jsonldobj, DeleteResourceResponse); }), catchError(function (error) { return _this.handleError(error); })); }; return ResourcesEndpointV2; }(Endpoint)); export { ResourcesEndpointV2 }; //# sourceMappingURL=resources-endpoint-v2.js.map