postchain-client
Version:
Client library for accessing a Postchain node through REST.
104 lines • 4.08 kB
JavaScript
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 { Buffer } from "buffer";
import * as logger from "../logger";
import { decodeValue } from "../gtx/serialization";
import { Method } from "./enums";
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);
}
});
}
export default handleRequest;
/**
* 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");
let rspBody;
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();
}
return {
error: null,
statusCode: response.status,
rspBody,
};
}
catch (error) {
logger.error(error.message);
return { error, statusCode: null, rspBody: 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);
return {
error: null,
statusCode: response.status,
rspBody: decodeValue(Buffer.from(yield response.arrayBuffer())),
};
}
catch (error) {
return { error, statusCode: null, rspBody: null };
}
}
else {
try {
const response = yield fetch(new URL(path, endpoint).href, {
method: "post",
body: JSON.stringify(requestBody),
});
return {
error: null,
statusCode: response.status,
rspBody: yield response.json(),
};
}
catch (error) {
return { error, statusCode: null, rspBody: null };
}
}
});
}
//# sourceMappingURL=httpUtil.js.map