@dasch-swiss/dsp-js
Version:
TypeScript client library for DSP-API
109 lines • 5.17 kB
JavaScript
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 { 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';
const jsonld = require('jsonld/dist/jsonld.js');
/**
* Handles requests to the values route of the Knora API.
*
* @category Endpoint V2
*/
export class ValuesEndpointV2 extends Endpoint {
/**
* @category Internal
* @param knoraApiConfig the config object.
* @param path this endpoint's base path.
* @param v2Endpoint a reference to the v2 endpoint.
*/
constructor(knoraApiConfig, path, v2Endpoint) {
super(knoraApiConfig, path);
this.knoraApiConfig = knoraApiConfig;
this.path = path;
this.v2Endpoint = v2Endpoint;
}
/**
* Reads a value from Knora.
*
* @param resourceIri the Iri of the resource the value belongs to.
* @param valueUuid the value's UUID.
*/
getValue(resourceIri, valueUuid) {
// Create temporary caches for this request only
// These will be garbage collected after the request completes
const tempOntologyCache = new OntologyCache(this.knoraApiConfig, this.v2Endpoint);
const tempListNodeCache = new ListNodeV2Cache(this.v2Endpoint);
return this.httpGet(`/${encodeURIComponent(resourceIri)}/${encodeURIComponent(valueUuid)}`).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), mergeMap((jsonldobj) => {
// console.log(JSON.stringify(jsonldobj));
return ResourcesConversionUtil.createReadResourceSequence(jsonldobj, tempOntologyCache, tempListNodeCache, this.jsonConvert);
}), map((resources) => resources.resources[0]), catchError(error => {
return this.handleError(error);
}));
}
/**
* Updates an existing value.
*
* @param resource The resource with the value to be updated.
*/
updateValue(resource) {
const res = this.jsonConvert.serializeObject(resource);
const val = this.jsonConvert.serializeObject(resource.value);
res[resource.property] = val;
return this.httpPut('', res, 'json', { 'X-Asset-Ingested': 'true' }).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, WriteValueResponse);
}), catchError(error => this.handleError(error)));
}
/**
* Creates a new value.
*
* @param resource The resource with the value to be created.
*/
createValue(resource) {
const 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');
}
const val = this.jsonConvert.serializeObject(resource.value);
res[resource.property] = val;
return this.httpPost('', res, 'json', { 'X-Asset-Ingested': 'true' }).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, WriteValueResponse);
}), catchError(error => this.handleError(error)));
}
/**
* Deletes a value.
*
* @param resource The resource with the value to be deleted.
*/
deleteValue(resource) {
const res = this.jsonConvert.serializeObject(resource);
const val = this.jsonConvert.serializeObject(resource.value);
res[resource.property] = val;
return this.httpPost('/delete', res).pipe(mergeMap(ajaxResponse => {
// console.log(JSON.stringify(ajaxResponse.response));
// TODO: @rosenth Adapt context object
// TODO: adapt getOntologyIriFromEntityIri
return jsonld.compact(ajaxResponse.response, {});
}), map(jsonldobj => {
return this.jsonConvert.deserializeObject(jsonldobj, DeleteValueResponse);
}), catchError(error => this.handleError(error)));
}
}
//# sourceMappingURL=values-endpoint-v2.js.map