UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

127 lines 5.49 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as logger from "../logger"; import { decodeValue } from "../gtx/serialization"; import { Method } from "./enums"; export function handleRequest(method, path, endpoint, postObject) { return __awaiter(this, void 0, void 0, function* () { if (method == Method.GET) { return yield get(path, endpoint); } else { return yield post(path, endpoint, postObject); } }); } /** * Sends request to get data from a given API endpoint. * @param path API endpoint of Rell backend * @param endpoint */ function get(path, endpoint) { return __awaiter(this, void 0, void 0, function* () { logger.debug(`GET URL ${new URL(path, endpoint).href}`); try { const response = yield fetch(new URL(path, endpoint).href); const contentType = response.headers.get("Content-Type"); const transactionTimestamp = response.headers.get("X-Transaction-Timestamp"); let rspBody = null; const error = null; if (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/json")) { rspBody = yield response.json(); } else if (contentType === null || contentType === void 0 ? void 0 : contentType.includes("text/plain")) { rspBody = yield response.text(); } else if (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/octet-stream")) { rspBody = yield constructBufferResponseBody(response); } return createResponseObject(error, response.status, rspBody, transactionTimestamp); } catch (error) { logger.error(error.message); return createResponseObject(error, null, null); } }); } /** * Sends request to post data to a given API endpoint. * @param path API endpoint of Rell backend * @param endpoint * @param requestBody request body */ function post(path, endpoint, requestBody) { return __awaiter(this, void 0, void 0, function* () { logger.debug(`POST URL ${new URL(path, endpoint).href}`); logger.debug(`POST body ${JSON.stringify(requestBody)}`); if (Buffer.isBuffer(requestBody)) { try { const requestOptions = { method: "post", body: requestBody, headers: { Accept: "application/octet-stream", "Content-Type": "application/octet-stream", }, }; const response = yield fetch(new URL(path, endpoint).href, requestOptions); const transactionTimestamp = response.headers.get("X-Transaction-Timestamp"); return createResponseObject(null, response.status, yield constructBufferResponseBody(response), transactionTimestamp); } catch (error) { return createResponseObject(error, null, null); } } else { try { const response = yield fetch(new URL(path, endpoint).href, { method: "post", body: JSON.stringify(requestBody), headers: { Accept: "application/json", "Content-Type": "application/json", }, }); const transactionTimestamp = response.headers.get("X-Transaction-Timestamp"); return createResponseObject(null, response.status, yield constructBufferResponseBody(response), transactionTimestamp); } catch (error) { return createResponseObject(error, null, null); } } }); } function constructBufferResponseBody(response) { return __awaiter(this, void 0, void 0, function* () { const contentType = response.headers.get("content-type"); if (contentType === "application/octet-stream") { const responseBuffer = yield response.arrayBuffer(); const buffer = Buffer.from(responseBuffer); return decodeValue(buffer); } if (contentType === "application/json") { return yield response.json(); } const responseText = yield response.text(); return responseText ? responseText : response.statusText; }); } function createResponseObject(error, statusCode, rspBody, transactionTimestamp) { const responseObject = { error, statusCode, rspBody, }; if (transactionTimestamp) { responseObject.transactionTimestamp = transactionTimestamp; } return responseObject; } //# sourceMappingURL=httpUtil.js.map