UNPKG

@5minds/processcube_engine

Version:

The ProcessCube Engine. Stores and executes BPMNs.

173 lines • 8.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpServiceTaskInstanceHandler = void 0; const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk"); const index_1 = require("../../../Contracts/index"); const AbortablePromise_1 = require("../../../Tools/AbortablePromise"); const Configurator_1 = __importDefault(require("../../../Tools/Configurator")); const index_2 = require("../../../Tools/Http/index"); const ActivityInstanceHandler_1 = require("./ActivityInstanceHandler"); class HttpServiceTaskInstanceHandler extends ActivityInstanceHandler_1.ActivityInstanceHandler { httpClientConfiguration; loggerNamespace = 'http_service_task'; get serviceTask() { return this.flowNode; } async runHandler() { this.logger.warn(`The HTTP Service Task is deprecated and will be removed in a future update. From now on, this functionality will be covered by the Engine's HttpServiceTask Extension and the Studio\'s CustomServiceTask extension. Please switch at your earliest convenience. Contact ProcessCube support, if you need assistance with the migration.`); this.httpClientConfiguration = Configurator_1.default.httpClient(); if (this.httpClientConfiguration?.allowUnauthorizedCertificates === true) { this.logger.warn('Unauthorized certificates are allowed.'); } this.logger.debug(`Executing HttpServiceTask instance.`); const handlerPromise = new AbortablePromise_1.AbortablePromise(async (resolve, reject) => { try { this.logger.debug('Executing HttpServiceTask'); this.validateInvocation(); const result = await this.executeInternalServiceTask(); this.currentToken = result; return resolve(); } catch (error) { this.logger.error(error.message, { err: error, }); return reject(error); } }, { signal: this.activityHandler.abortSignal }); return handlerPromise; } async persistOnExit(dataObjectValues) { return super.persistOnExit(dataObjectValues, this.typeData); } async persistOnError(error) { return super.persistOnError(error, this.typeData); } async persistOnTerminate() { return super.persistOnTerminate(this.typeData); } async executeInternalServiceTask() { const invocation = this.serviceTask.invocation; this.ensureInvocationMethodIsSupported(); const argumentsToPassThrough = await this.getArgumentsFromInvocation(); this.setTypeData(invocation.method, argumentsToPassThrough); const httpClient = new index_2.HttpClient(this.httpClientConfiguration); const serviceMethod = httpClient[invocation.method]; const result = await serviceMethod.call(httpClient, ...argumentsToPassThrough); return result; } validateInvocation() { const isMethodInvocation = () => { const propertyNames = Object.getOwnPropertyNames(this.serviceTask.invocation); const hasModuleProperty = propertyNames.some((property) => property === 'module'); const hasMethodProperty = propertyNames.some((property) => property === 'method'); return hasModuleProperty && hasMethodProperty; }; if (!isMethodInvocation()) { const invalidInvocationError = new processcube_engine_sdk_1.UnprocessableEntityError('The Http Service Task Config is missing some required properties.', 'process'); invalidInvocationError.additionalInformation = { requiredProperties: ['module', 'method'], actualProperties: Object.keys(this.serviceTask.invocation), processInstanceId: this.processInstance.getProcessInstanceId(), correlationId: this.processInstance.getCorrelationId(), }; this.logger.error(invalidInvocationError.message); throw invalidInvocationError; } } ensureInvocationMethodIsSupported() { const invocation = this.serviceTask.invocation; const supportedMethods = ['GET', 'PUT', 'POST', 'DELETE']; const serviceMethod = supportedMethods.map((method) => method.toLowerCase()).includes(invocation.method.toLowerCase()); if (!serviceMethod) { const error = new processcube_engine_sdk_1.BadRequestError(`HTTP Method \`${invocation.method}\` is not supported.`, 'process'); error.additionalInformation = { supportedMethods: supportedMethods, configuredMethod: invocation.method, }; throw error; } } async getArgumentsFromInvocation() { const invocation = this.serviceTask.invocation; if (invocation.params?.length <= 0) { return []; } const params = await this.executeExpression(invocation.params, true); if (!Array.isArray(params)) { return [params]; } let body = invocation.method === 'post' || invocation.method === 'put' ? params.splice(1, 1)[0] : undefined; const bodyIsString = typeof body === 'string'; if (bodyIsString) { body = await this.executeExpression(body, true); } const parsedParams = await this.parseTokenExpressionsOfParams(params); if (body) { parsedParams.splice(1, 0, body); } return parsedParams; } setTypeData(method, argumentsFromInvocation) { const url = argumentsFromInvocation[0]; let body; let headers; if (argumentsFromInvocation.length > 1) { if (method !== 'post' && method !== 'put') { headers = argumentsFromInvocation[1]?.headers; } else { body = argumentsFromInvocation[1]; if (argumentsFromInvocation.length > 2) { headers = argumentsFromInvocation[2]?.headers; } } } this.typeData = { type: index_1.FlowNodeInstanceDataTypes.httpServiceTask, method: method, url: url, body: body, headers: headers, }; } async parseTokenExpressionsOfParams(params) { if (typeof params === 'string') { const expressionStartsOn = '${'; const expressionEndsOn = '}'; if (params.includes(expressionStartsOn) && params.includes(expressionEndsOn)) { return await this.executeExpression(`\`${params}\``, true); } return params; } if (Array.isArray(params)) { let parsedParams = []; for (const param of params) { let parsedParam = await this.parseTokenExpressionsOfParams(param); parsedParams.push(parsedParam); } return parsedParams; } let parsedParams = {}; for (const paramKey of Object.keys(params)) { parsedParams[paramKey] = await this.parseTokenExpressionsOfParams(params[paramKey]); } return parsedParams; } async executeExpression(expression, allowNonObjectResults) { return this.processInstance.executeRuntimeExpression({ expression: expression, currentFlowNode: this.serviceTask, currentToken: this.currentToken, previousFlowNode: this.processInstance.getProcessModelFacade().getPreviousFlowNodesFor(this.flowNode)?.pop(), allowNonObjectResults: allowNonObjectResults, }); } } exports.HttpServiceTaskInstanceHandler = HttpServiceTaskInstanceHandler; //# sourceMappingURL=HttpServiceTaskInstanceHandler.js.map