UNPKG

@knora/api

Version:

JavaScript library that handles API requests to Knora

164 lines 5.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var json2typescript_1 = require("json2typescript"); var json_convert_enums_1 = require("json2typescript/src/json2typescript/json-convert-enums"); var rxjs_1 = require("rxjs"); var ajax_1 = require("rxjs/ajax"); var api_response_error_1 = require("../models/api-response-error"); var data_error_1 = require("../models/data-error"); var Endpoint = /** @class */ (function () { // </editor-fold> ///////////////// // CONSTRUCTOR // ///////////////// // <editor-fold desc=""> /** * Constructor. */ function Endpoint(knoraApiConfig, path) { this.knoraApiConfig = knoraApiConfig; this.path = path; /////////////// // CONSTANTS // /////////////// // <editor-fold desc=""> // </editor-fold> //////////////// // PROPERTIES // //////////////// // <editor-fold desc=""> /** * JsonConvert instance */ this.jsonConvert = new json2typescript_1.JsonConvert(json2typescript_1.OperationMode.ENABLE, json2typescript_1.ValueCheckingMode.DISALLOW_NULL, false, json_convert_enums_1.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: true, configurable: true }); // </editor-fold> ///////////// // METHODS // ///////////// // <editor-fold desc=""> // </editor-fold> /** * Performs a general GET request. * * @param path the relative URL for the request */ Endpoint.prototype.httpGet = function (path) { if (path === undefined) path = ""; return ajax_1.ajax.get(this.knoraApiConfig.apiUrl + this.path + path, this.constructHeader()); }; /** * 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. */ Endpoint.prototype.httpPost = function (path, body, contentType) { if (contentType === void 0) { contentType = "json"; } if (path === undefined) path = ""; return ajax_1.ajax.post(this.knoraApiConfig.apiUrl + this.path + path, body, this.constructHeader(contentType)); }; /** * 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. */ Endpoint.prototype.httpPut = function (path, body, contentType) { if (contentType === void 0) { contentType = "json"; } if (path === undefined) path = ""; return ajax_1.ajax.put(this.knoraApiConfig.apiUrl + this.path + path, body, this.constructHeader(contentType)); }; /** * 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. */ Endpoint.prototype.httpPatch = function (path, body, contentType) { if (contentType === void 0) { contentType = "json"; } if (path === undefined) path = ""; return ajax_1.ajax.patch(this.knoraApiConfig.apiUrl + this.path + path, body, this.constructHeader(contentType)); }; /** * Performs a general PUT request. * * @param path the relative URL for the request */ Endpoint.prototype.httpDelete = function (path) { if (path === undefined) path = ""; return ajax_1.ajax.delete(this.knoraApiConfig.apiUrl + this.path + path, this.constructHeader()); }; /** * 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 data_error_1.DataError) { responseError = error.response; if (this.knoraApiConfig.logErrors) { console.error("Parse Error in Knora API request: " + responseError.error); } } else { responseError = api_response_error_1.ApiResponseError.fromAjaxError(error); if (this.knoraApiConfig.logErrors) { console.error("Ajax Error in Knora API request: " + responseError.method + " " + responseError.url); } } return rxjs_1.throwError(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. */ Endpoint.prototype.constructHeader = function (contentType) { var header = {}; if (this.jsonWebToken !== "") { 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"; } } return header; }; return Endpoint; }()); exports.Endpoint = Endpoint; //# sourceMappingURL=endpoint.js.map