UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

149 lines 7.61 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 delete_resource_response_1 = require("../../../models/v2/resources/delete/delete-resource-response"); var ResourcesConversionUtil_1 = require("../../../models/v2/resources/ResourcesConversionUtil"); var update_resource_metadata_response_1 = require("../../../models/v2/resources/update/update-resource-metadata-response"); var endpoint_1 = require("../../endpoint"); var jsonld = require("jsonld/dist/jsonld.js"); /** * Handles requests to the resources route of the Knora API. */ var ResourcesEndpoint = /** @class */ (function (_super) { __extends(ResourcesEndpoint, _super); function ResourcesEndpoint(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. */ ResourcesEndpoint.prototype.getResources = function (resourceIris) { // 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); }).reduce(function (acc, currentValue) { return acc + currentValue; }); return this.httpGet(resIris).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); })); }; /** * Given a resource IRI, gets the resource from Knora. * * @param resourceIri Iri of the resource to get. */ ResourcesEndpoint.prototype.getResource = function (resourceIri) { var _this = this; return this.getResources([resourceIri]).pipe(operators_1.map(function (resources) { return resources[0]; }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Creates a new resource. * * @param resource the resource to be created. */ ResourcesEndpoint.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]); } }); return this.httpPost("", res).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.map(function (resources) { return resources[0]; }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Updates a resource's metadata. * * @param resourceMetadata the new metadata. */ ResourcesEndpoint.prototype.updateResourceMetadata = function (resourceMetadata) { var _this = this; // check that at least one of the following properties is updated: label, hasPermissions, newModificationDateDate if (resourceMetadata.label === undefined && resourceMetadata.hasPermissions === undefined && resourceMetadata.newModificationDateDate === undefined) { throw new Error("At least one of the following properties has to be updated: label, hasPermissions, newModificationDateDate"); } var res = this.jsonConvert.serializeObject(resourceMetadata); return this.httpPut("", res).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) { return _this.jsonConvert.deserializeObject(jsonldobj, update_resource_metadata_response_1.UpdateResourceMetadataResponse); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Deletes a resource. * * @param resource the resource to be deleted. */ ResourcesEndpoint.prototype.deleteResource = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); return this.httpPost("/delete", res).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) { return _this.jsonConvert.deserializeObject(jsonldobj, delete_resource_response_1.DeleteResourceResponse); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; return ResourcesEndpoint; }(endpoint_1.Endpoint)); exports.ResourcesEndpoint = ResourcesEndpoint; //# sourceMappingURL=resources-endpoint.js.map