UNPKG

@skbkontur/cassandra-distributed-task-queue-ui

Version:

.NET library implementing distributed task queue machinery using Apache Cassandra

121 lines 4.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const ApiError_1 = require("./ApiError"); require("whatwg-fetch"); function getExceptionInfo(exception, path) { return (exception["Exception" + path] || exception["exception" + path] || exception[path] || exception[path[0].toLowerCase() + path.substr(1)]); } class ApiBase { constructor(prefix) { Object.defineProperty(this, "prefix", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.prefix = prefix; } checkStatus(response) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!(response.status >= 200 && response.status < 300)) { const errorText = yield response.text(); let serverResponse; try { serverResponse = JSON.parse(errorText); } catch (e) { serverResponse = null; } if (serverResponse != null) { throw new ApiError_1.ApiError(errorText, getExceptionInfo(serverResponse, "Message"), response.status, getExceptionInfo(serverResponse, "Type"), serverResponse.stackTrace || serverResponse.StackTrace, serverResponse.innerErrors); } throw new ApiError_1.ApiError(errorText, "Unexpected error format", response.status); } }); } post(url, body) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield fetch(this.prefix + url, Object.assign(Object.assign({}, ApiBase.additionalHeaders), { method: "POST", body: JSON.stringify(body) })); yield this.checkStatus(response); const textResult = yield response.text(); if (textResult !== "") { return JSON.parse(textResult); } return undefined; }); } createQueryString(params) { if (params == null) { return ""; } const params2 = params; let result = Object.keys(params) .map(key => { const value = params2[key]; if (typeof value === "string") { return encodeURIComponent(key) + "=" + encodeURIComponent(value); } if (typeof value === "number" || typeof value === "boolean") { return encodeURIComponent(key) + "=" + encodeURIComponent(value.toString()); } if (Array.isArray(value)) { return value .map(item => { if (typeof item === "string") { return encodeURIComponent(key) + "=" + encodeURIComponent(item); } if (typeof item === "number") { return encodeURIComponent(key) + "=" + encodeURIComponent(item.toString()); } return null; }) .filter(x => x !== null) .join("&"); } return null; }) .filter(x => x !== null) .join("&"); result = result ? "?" + result : ""; return result; } get(url, params) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield fetch(this.prefix + url + this.createQueryString(params), Object.assign(Object.assign({}, ApiBase.additionalHeaders), { method: "GET" })); yield this.checkStatus(response); const result = yield response.json(); return result; }); } delete(url, body) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const response = yield fetch(this.prefix + url, Object.assign(Object.assign({}, ApiBase.additionalHeaders), { method: "DELETE", body: JSON.stringify(body) })); yield this.checkStatus(response); const textResult = yield response.text(); if (textResult !== "") { return JSON.parse(textResult); } return undefined; }); } } exports.default = ApiBase; Object.defineProperty(ApiBase, "additionalHeaders", { enumerable: true, configurable: true, writable: true, value: { headers: { ["Cache-Control"]: "no-cache, no-store", ["Pragma"]: "no-cache", ["Expires"]: "0", ["Content-Type"]: "application/json", }, ["credentials"]: "same-origin", } }); //# sourceMappingURL=ApiBase.js.map