pegasys-orchestrate
Version:
The PegaSys Orchestrate library provides convenient access to the Codefi Orchestrate API from applications written in server-side JavaScript
106 lines (105 loc) • 3.89 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
const axios_1 = require("axios");
const qs = require("qs");
const HttpResponseError_1 = require("./errors/HttpResponseError");
/**
* @hidden
* @class HttpClient
*/
class HttpClient {
constructor(config) {
this.baseURL = config.host;
}
get(req, authToken, headers) {
return __awaiter(this, void 0, void 0, function* () {
let path = req.path;
if (req.query) {
path += `?${qs.stringify(req.query, { arrayFormat: 'comma' })}`;
}
try {
return HttpClient.parseResponse(yield axios_1.default.get(path, this.requestConfig(req, authToken, headers)));
}
catch (e) {
throw HttpClient.parseErrResponse(e);
}
});
}
post(path, data, authToken, headers) {
return __awaiter(this, void 0, void 0, function* () {
const req = { path, data };
try {
return HttpClient.parseResponse(yield axios_1.default.post(req.path, req.data, this.requestConfig(req, authToken, headers)))
.data;
}
catch (e) {
throw HttpClient.parseErrResponse(e);
}
});
}
patch(path, data, authToken, headers) {
return __awaiter(this, void 0, void 0, function* () {
const req = { path, data };
try {
return HttpClient.parseResponse(yield axios_1.default.patch(req.path, req.data, this.requestConfig(req, authToken, headers))).data;
}
catch (e) {
throw HttpClient.parseErrResponse(e);
}
});
}
delete(path, authToken, headers) {
return __awaiter(this, void 0, void 0, function* () {
const req = { path };
try {
return HttpClient.parseResponse(yield axios_1.default.delete(req.path, this.requestConfig(req, authToken, headers))).data;
}
catch (e) {
throw HttpClient.parseErrResponse(e);
}
});
}
requestConfig(req, authToken, headers) {
const cfg = {
baseURL: this.baseURL,
headers: headers || {}
};
if (authToken) {
cfg.headers.Authorization = `Bearer ${authToken}`;
}
const reqP = req;
if (reqP.data) {
cfg.headers['Content-Type'] = 'application/json';
}
return cfg;
}
static parseResponse(res) {
return {
status: res.status,
headers: res.headers,
data: res.data
};
}
static parseErrResponse(e) {
if (e.isAxiosError && e.response) {
return new HttpResponseError_1.HttpResponseError(e, e.response.status, e.response.headers, e.response.data);
}
else if (e.isAxiosError) {
return new HttpResponseError_1.HttpResponseError(e, 500, e.config);
}
else {
return new HttpResponseError_1.HttpResponseError(e, 500);
}
}
}
exports.HttpClient = HttpClient;