@5minds/processcube_engine
Version:
The ProcessCube Engine. Stores and executes BPMNs.
194 lines • 7.43 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const https_1 = __importDefault(require("https"));
const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk");
const EngineErrors = __importStar(require("@5minds/processcube_engine_sdk"));
class HttpClient {
config;
httpSuccessResponseCode = 200;
httpRedirectResponseCode = 300;
constructor(config) {
this.config = config;
}
async get(url, options) {
const prefixedUrl = this.addPrefixToUrl(url);
const headers = this.buildHeaders(options);
const request = this.decorateSSLVerification(url, {
method: 'GET',
mode: 'cors',
referrer: 'no-referrer',
headers: headers,
});
const response = await (0, cross_fetch_1.default)(prefixedUrl, request);
const parsedResponse = await this.evaluateResponse(response);
return parsedResponse;
}
async post(url, data, options) {
const prefixedUrl = this.addPrefixToUrl(url);
const headers = this.buildHeaders(options);
const request = this.decorateSSLVerification(url, {
method: 'POST',
mode: 'cors',
referrer: 'no-referrer',
headers: headers,
body: JSON.stringify(data),
});
const response = await (0, cross_fetch_1.default)(prefixedUrl, request);
const parsedResponse = await this.evaluateResponse(response);
return parsedResponse;
}
async put(url, data, options) {
const prefixedUrl = this.addPrefixToUrl(url);
const headers = this.buildHeaders(options);
const request = this.decorateSSLVerification(url, {
method: 'PUT',
mode: 'cors',
referrer: 'no-referrer',
headers: headers,
body: JSON.stringify(data),
});
const response = await (0, cross_fetch_1.default)(prefixedUrl, request);
const parsedResponse = await this.evaluateResponse(response);
return parsedResponse;
}
async delete(url, options) {
const prefixedUrl = this.addPrefixToUrl(url);
const headers = this.buildHeaders(options);
const request = this.decorateSSLVerification(url, {
method: 'DELETE',
mode: 'cors',
referrer: 'no-referrer',
headers: headers,
});
const response = await (0, cross_fetch_1.default)(prefixedUrl, request);
const parsedResponse = await this.evaluateResponse(response);
return parsedResponse;
}
decorateSSLVerification(url, request) {
if (url.startsWith('https') && this.config?.allowUnauthorizedCertificates === true) {
const agent = new https_1.default.Agent({
rejectUnauthorized: false,
});
return {
...request,
agent: agent,
};
}
return request;
}
addPrefixToUrl(url) {
return this.config?.url ? `${this.config.url}/${url}` : url;
}
buildHeaders(options) {
const headers = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
};
if (options?.headers != undefined) {
const optionHeaders = Object.keys(options.headers);
for (const header of optionHeaders) {
headers[header] = options.headers[header];
}
}
return headers;
}
async evaluateResponse(response) {
const body = await response.text();
if (this.responseIsAnError(response)) {
this.createAndThrowError(response.status, body);
}
const parsedResponse = {
result: this.tryParseStringtoJson(body),
status: response.status,
};
return parsedResponse;
}
responseIsAnError(response) {
return response.status < this.httpSuccessResponseCode || response.status >= this.httpRedirectResponseCode;
}
createAndThrowError(statusCode, body) {
const errorName = EngineErrors.ErrorCodes[statusCode];
const errorInfo = this.tryParseStringtoJson(body);
if (typeof errorInfo === 'string') {
this.throwErrorFromString(errorName, statusCode, errorInfo);
}
this.throwErrorFromObject(errorName, statusCode, errorInfo);
}
tryParseStringtoJson(result) {
try {
return JSON.parse(result);
}
catch (error) {
return result;
}
}
throwErrorFromString(errorName, statusCode, message) {
if (this.isEngineError(errorName)) {
throw new EngineErrors[errorName](message);
}
if ((errorName = processcube_engine_sdk_1.BpmnError.name)) {
throw new processcube_engine_sdk_1.BpmnError(errorName, statusCode, message);
}
throw new EngineErrors.InternalServerError(message);
}
throwErrorFromObject(errorName, statusCode, errorInfo) {
if (this.isEngineError(errorName)) {
this.throwEngineError(errorName, errorInfo);
}
if ((errorName = processcube_engine_sdk_1.BpmnError.name)) {
throw new processcube_engine_sdk_1.BpmnError(errorName, statusCode, errorInfo.message);
}
this.throwNonEngineError(errorInfo);
}
throwEngineError(errorName, errorInfo) {
const error = new EngineErrors[errorName](errorInfo.message);
error.additionalInformation = errorInfo.additionalInformation;
throw error;
}
throwNonEngineError(error) {
throw new Error(error.message);
}
isEngineError(errorName) {
return errorName in EngineErrors;
}
}
exports.HttpClient = HttpClient;
//# sourceMappingURL=HttpClient.js.map