@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
126 lines • 6.14 kB
JavaScript
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/operators";
import { ResourcesConversionUtil } from "../../../models/v2/resources/ResourcesConversionUtil";
import { CreateFileValue } from "../../../models/v2/resources/values/create/create-file-value";
import { DeleteValueResponse } from "../../../models/v2/resources/values/delete/delete-value-response";
import { WriteValueResponse } from "../../../models/v2/resources/values/write-value-response";
import { Endpoint } from "../../endpoint";
var jsonld = require("jsonld/dist/jsonld.js");
/**
* Handles requests to the values route of the Knora API.
*
* @category Endpoint V2
*/
var ValuesEndpointV2 = /** @class */ (function (_super) {
__extends(ValuesEndpointV2, _super);
/**
* @category Internal
* @param knoraApiConfig the config object.
* @param path this endpoint's base path.
* @param v2Endpoint a reference to the v2 endpoint.
*/
function ValuesEndpointV2(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.
*/
ValuesEndpointV2.prototype.getValue = function (resourceIri, valueUuid) {
var _this = this;
return this.httpGet("/" + encodeURIComponent(resourceIri) + "/" + encodeURIComponent(valueUuid)).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, _this.v2Endpoint.ontologyCache, _this.v2Endpoint.listNodeCache, _this.jsonConvert);
}), map(function (resources) { return resources.resources[0]; }), catchError(function (error) {
return _this.handleError(error);
}));
};
/**
* Updates an existing value.
*
* @param resource The resource with the value to be updated.
*/
ValuesEndpointV2.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, "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, {});
}), map(function (jsonldobj) {
return _this.jsonConvert.deserializeObject(jsonldobj, WriteValueResponse);
}), catchError(function (error) { return _this.handleError(error); }));
};
/**
* Creates a new value.
*
* @param resource The resource with the value to be created.
*/
ValuesEndpointV2.prototype.createValue = function (resource) {
var _this = this;
var res = this.jsonConvert.serializeObject(resource);
if (resource.value instanceof 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, "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, {});
}), map(function (jsonldobj) {
return _this.jsonConvert.deserializeObject(jsonldobj, WriteValueResponse);
}), catchError(function (error) { return _this.handleError(error); }));
};
/**
* Deletes a value.
*
* @param resource The resource with the value to be deleted.
*/
ValuesEndpointV2.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(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, DeleteValueResponse);
}), catchError(function (error) { return _this.handleError(error); }));
};
return ValuesEndpointV2;
}(Endpoint));
export { ValuesEndpointV2 };
//# sourceMappingURL=values-endpoint-v2.js.map