@villedemontreal/workit-bpm-client
Version:
Camunda BPM client for WorkIt that works with Camunda platform powered by TypeScript
156 lines • 6.74 kB
JavaScript
/*
* Copyright (c) 2025 Ville de Montreal. All rights reserved.
* Licensed under the MIT license.
* See LICENSE file in the project root for full license information.
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var CamundaRepository_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CamundaRepository = void 0;
const axios_1 = require("axios");
const FormData = require("form-data");
const fs = require("fs");
const inversify_1 = require("inversify");
require("reflect-metadata");
const identifiers_1 = require("../config/constants/identifiers");
const utils_1 = require("../utils/utils");
let CamundaRepository = CamundaRepository_1 = class CamundaRepository {
static _getworkflowInstanceUrl(idOrKey) {
let url = `/process-definition`;
if (idOrKey.split(':').length === 3) {
url += `/${idOrKey}/start`;
}
else {
url += `/key/${idOrKey}/start`;
}
return url;
}
static _setStaticHeaders(configs, headers) {
if (configs.interceptors) {
// TODO: improve this part.
const basicAuthFunc = configs.interceptors.find((func) => func.name === 'bound interceptor');
let camundaConfig = { headers: {} };
if (basicAuthFunc) {
camundaConfig = basicAuthFunc({});
}
return Object.assign(headers, camundaConfig.headers);
}
return headers;
}
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
constructor(configs) {
this._configs = configs;
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-APP': this._configs.workerId || 'unknow',
};
this._headers = CamundaRepository_1._setStaticHeaders(configs, headers);
this._request = axios_1.default.create({
baseURL: this._configs.baseUrl,
timeout: 30000,
headers: this._headers,
});
}
deployWorkflow(deployName, absPath) {
const formData = new FormData();
const xmlStream = fs.createReadStream(absPath);
formData.append('deployment-name', deployName);
formData.append('process', xmlStream);
return this._request.post('/deployment/create', formData, {
headers: { ...this._headers, 'content-type': `multipart/form-data; boundary=${formData.getBoundary()}` },
});
}
getWorkflows(options) {
return this._request.get('/process-definition', options);
}
getWorkflowCount(options) {
return this._request.get('/process-definition/count', options);
}
createWorkflowInstance(idOrKey, variables) {
const url = CamundaRepository_1._getworkflowInstanceUrl(idOrKey);
return this._request.post(url, {
businessKey: typeof variables === 'object' ? variables.businessKey : undefined,
variables: utils_1.Utils.serializeVariables(variables),
});
}
/**
* Message can be correlated to a message start event or an intermediate message catching event.
*/
async publishMessage({ messageName, processInstanceId, variables, correlationKeys, }) {
await this._request.post('/message', {
messageName,
processInstanceId,
correlationKeys: utils_1.Utils.serializeVariables(correlationKeys),
businessKey: typeof variables === 'object' ? variables.businessKey : undefined,
processVariables: utils_1.Utils.serializeVariables(variables),
resultEnabled: false,
all: true,
});
}
async cancelWorkflowInstance(id) {
await this._request.delete(`/process-instance/${id}?skipCustomListeners=true&skipIoMappings=true&skipSubprocesses=true`);
}
async getIncident(incidentKey) {
const response = await this._request.get(`/incident/?incidentId=${incidentKey}`);
return response.data[0];
}
async resolveIncident(incidentKey) {
const incident = await this.getIncident(incidentKey);
// check if type is for external task
await this._request.post(`/process-instance/${incident.processInstanceId}/modification`, {
skipCustomListeners: true,
skipIoMappings: true,
instructions: [
{
type: 'cancel',
activityId: incident.activityId,
},
],
});
}
async getWorkflow(idOrKey) {
if (!idOrKey) {
throw new Error('Id or Key must be specified');
}
let basePath = '/process-definition';
if (idOrKey.split(':').length !== 3) {
basePath += '/key';
}
const pDef = this._request.get(`${basePath}/${idOrKey}`);
const pDefXml = this._request.get(`${basePath}/${idOrKey}/xml`);
const results = await Promise.all([pDef, pDefXml]);
const payload = results[0].data;
payload.bpmn20Xml = results[1].data.bpmn20Xml;
return Promise.resolve(payload);
}
updateJobRetries(id, retries) {
return this._request.put(`/external-task/${id}/retries`, {
retries,
});
}
updateVariables(processInstanceId, variables, local = false) {
return this._request.post(`/process-instance/${processInstanceId}/variables`, {
modifications: utils_1.Utils.serializeVariables(variables, local),
});
}
};
CamundaRepository = CamundaRepository_1 = __decorate([
(0, inversify_1.injectable)(),
__param(0, (0, inversify_1.inject)(identifiers_1.SERVICE_IDENTIFIER.camunda_external_config)),
__metadata("design:paramtypes", [Object])
], CamundaRepository);
exports.CamundaRepository = CamundaRepository;
//# sourceMappingURL=camundaRepository.js.map
;