@dasch-swiss/dsp-js
Version:
JavaScript library that handles API requests to Knora
192 lines • 7.53 kB
JavaScript
import { JsonConvert, OperationMode, ValueCheckingMode } from "json2typescript";
import { PropertyMatchingRule } from "json2typescript/src/json2typescript/json-convert-enums";
import { throwError } from "rxjs";
import { ajax } from "rxjs/ajax";
import { ApiResponseError } from "../models/api-response-error";
import { DataError } from "../models/data-error";
import { retryOnError } from "./operators/retryOnError";
/**
* @category Internal
*/
var Endpoint = /** @class */ (function () {
/**
* Constructor.
*/
function Endpoint(knoraApiConfig, path) {
this.knoraApiConfig = knoraApiConfig;
this.path = path;
this.maxRetries = 0;
this.delay = 500;
this.retryOnErrorStatus = [0, 500];
/**
* JsonConvert instance
*/
this.jsonConvert = new JsonConvert(OperationMode.ENABLE, ValueCheckingMode.DISALLOW_NULL, false, PropertyMatchingRule.CASE_STRICT);
}
Object.defineProperty(Endpoint.prototype, "jsonWebToken", {
/**
* The session token
*/
get: function () {
return this.knoraApiConfig.jsonWebToken;
},
/**
* The session token
*/
set: function (value) {
this.knoraApiConfig.jsonWebToken = value;
},
enumerable: false,
configurable: true
});
/**
* Performs a general GET request.
*
* @param path the relative URL for the request
* @param headerOpts additional headers, if any.
*/
Endpoint.prototype.httpGet = function (path, headerOpts) {
if (path === undefined)
path = "";
return ajax(this.setAjaxRequest(path, "GET", undefined, this.constructHeader(undefined, headerOpts)))
.pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
};
/**
* Performs a general POST request.
*
* @param path the relative URL for the request
* @param body the body of the request, if any.
* @param contentType content content type of body, if any.
* @param headerOpts additional headers, if any.
*/
Endpoint.prototype.httpPost = function (path, body, contentType, headerOpts) {
if (contentType === void 0) { contentType = "json"; }
if (path === undefined)
path = "";
return ajax(this.setAjaxRequest(path, "POST", body, this.constructHeader(contentType, headerOpts)))
.pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
};
/**
* Performs a general PUT request.
*
* @param path the relative URL for the request
* @param body the body of the request
* @param contentType content content type of body, if any.
* @param headerOpts additional headers, if any.
*/
Endpoint.prototype.httpPut = function (path, body, contentType, headerOpts) {
if (contentType === void 0) { contentType = "json"; }
if (path === undefined)
path = "";
return ajax(this.setAjaxRequest(path, "PUT", body, this.constructHeader(contentType, headerOpts)))
.pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
};
/**
* Performs a general PATCH request.
*
* @param path the relative URL for the request
* @param body the body of the request
* @param contentType content content type of body, if any.
* @param headerOpts additional headers, if any.
*/
Endpoint.prototype.httpPatch = function (path, body, contentType, headerOpts) {
if (contentType === void 0) { contentType = "json"; }
if (path === undefined)
path = "";
return ajax(this.setAjaxRequest(path, "PATCH", body, this.constructHeader(contentType, headerOpts)))
.pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
};
/**
* Performs a general PUT request.
*
* @param path the relative URL for the request.
* @param headerOpts additional headers, if any.
*/
Endpoint.prototype.httpDelete = function (path, headerOpts) {
if (path === undefined)
path = "";
return ajax(this.setAjaxRequest(path, "DELETE", undefined, this.constructHeader(undefined, headerOpts)))
.pipe(retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors));
};
/**
* Handles parsing errors.
* @param error the error class provided by us
*/
Endpoint.prototype.handleError = function (error) {
var responseError;
if (this.knoraApiConfig.logErrors) {
console.error(error);
}
// Check the type of error and save it to the responseError
if (error instanceof DataError) {
responseError = error.response;
if (this.knoraApiConfig.logErrors) {
console.error("Parse Error in Knora API request: " + responseError.error);
}
}
else {
responseError = ApiResponseError.fromAjaxError(error);
if (this.knoraApiConfig.logErrors) {
console.error("Ajax Error in Knora API request: " + responseError.method + " " + responseError.url);
}
}
return throwError(function () { return responseError; });
};
/**
* Creates a header for a HTTP request.
* If the client has obtained a token, it is included.
*
* @param contentType Sets the content type, if any.
* @param headerOpts additional headers, if any.
*/
Endpoint.prototype.constructHeader = function (contentType, headerOpts) {
var header = {};
if (this.jsonWebToken !== "") {
// NOTE: I think this is not needed anymore because with the `withCredentials = true`
// the cookie will always be sent with each request.
// But for the moment I'll keep it
header["Authorization"] = "Bearer " + this.jsonWebToken;
}
if (contentType !== undefined) {
if (contentType === "json") {
header["Content-Type"] = "application/json; charset=utf-8";
}
else if (contentType === "sparql") {
header["Content-Type"] = "application/sparql-query; charset=utf-8";
}
}
if (headerOpts !== undefined) {
var headerProps = Object.keys(headerOpts);
headerProps.forEach(function (prop) {
header[prop] = headerOpts[prop];
});
}
return header;
};
/**
* Sets ajax request
* @param path string
* @param method 'GET', 'POST', 'PUT', 'PATCH' or 'DELETE'
* @param [body] any
* @param [headers] IHeaderOptions
* @returns AjaxRequest object
*/
Endpoint.prototype.setAjaxRequest = function (path, method, body, headers) {
var apiUrl = this.knoraApiConfig.apiUrl;
var ajaxRequest = {
url: apiUrl + this.path + path,
method: method,
body: body,
async: true,
withCredentials: true,
headers: headers || {},
timeout: 0,
crossDomain: false,
responseType: 'json'
};
return ajaxRequest;
};
return Endpoint;
}());
export { Endpoint };
//# sourceMappingURL=endpoint.js.map