UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

118 lines 6 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 create_file_value_1 = require("../../../models/v2/resources/values/create/create-file-value"); var delete_value_response_1 = require("../../../models/v2/resources/values/delete/delete-value-response"); var write_value_response_1 = require("../../../models/v2/resources/values/write-value-response"); var endpoint_1 = require("../../endpoint"); var jsonld = require("jsonld/dist/jsonld.js"); /** * Handles requests to the values route of the Knora API. */ var ValuesEndpoint = /** @class */ (function (_super) { __extends(ValuesEndpoint, _super); function ValuesEndpoint(knoraApiConfig, path, v2Endpoint) { var _this = _super.call(this, knoraApiConfig, path) || this; _this.knoraApiConfig = knoraApiConfig; _this.path = path; _this.v2Endpoint = v2Endpoint; return _this; } /** * Reads a value from Knora. * * @param resourceIri the Iri of the resource the value belongs to. * @param valueUuid the value's UUID. */ ValuesEndpoint.prototype.getValue = function (resourceIri, valueUuid) { var _this = this; return this.httpGet("/" + encodeURIComponent(resourceIri) + "/" + encodeURIComponent(valueUuid)).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 an existing value. * * @param resource The resource with the value to be updated. */ ValuesEndpoint.prototype.updateValue = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); var val = this.jsonConvert.serializeObject(resource.value); res[resource.property] = val; 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, write_value_response_1.WriteValueResponse); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Creates a new value. * * @param resource The resource with the value to be created. */ ValuesEndpoint.prototype.createValue = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); if (resource.value instanceof create_file_value_1.CreateFileValue) { throw Error("A value of type CreateFileValue can only be created with a new resource"); } var val = this.jsonConvert.serializeObject(resource.value); res[resource.property] = val; 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.map(function (jsonldobj) { return _this.jsonConvert.deserializeObject(jsonldobj, write_value_response_1.WriteValueResponse); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; /** * Deletes a value. * * @param resource The resource with the value to be deleted. */ ValuesEndpoint.prototype.deleteValue = function (resource) { var _this = this; var res = this.jsonConvert.serializeObject(resource); var val = this.jsonConvert.serializeObject(resource.value); res[resource.property] = val; 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_value_response_1.DeleteValueResponse); }), operators_1.catchError(function (error) { return _this.handleError(error); })); }; return ValuesEndpoint; }(endpoint_1.Endpoint)); exports.ValuesEndpoint = ValuesEndpoint; //# sourceMappingURL=values-endpoint.js.map