@villedemontreal/workit-bpm-client
Version:
Camunda BPM client for WorkIt that works with Camunda platform powered by TypeScript
162 lines • 5.21 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
/* eslint @typescript-eslint/no-unsafe-assignment: 0 */
/* eslint @typescript-eslint/ban-types: 0 */
/* eslint @typescript-eslint/restrict-template-expressions: 0 */
/* eslint @typescript-eslint/no-unsafe-call: 0 */
/* eslint @typescript-eslint/no-unsafe-member-access: 0 */
/* eslint @typescript-eslint/no-unsafe-return: 0 */
const workit_core_1 = require("@villedemontreal/workit-core");
const camunda_external_task_client_js_1 = require("camunda-external-task-client-js");
const identifiers_1 = require("../config/constants/identifiers");
const variables_1 = require("../variables");
const GLOBAL_TIMEOUT_PULL = 60000;
const CONSTANTS_INTEGER_VALUE = 2 ** 31;
/**
* Checks if parameter is undefined or null
*/
const isUndefinedOrNull = (a) => typeof a === 'undefined' || a === null;
const typeMatchers = {
null: isUndefinedOrNull,
/**
* @returns {boolean} true if value is Integer
*/
integer(a) {
return Number.isInteger(a) && a >= -CONSTANTS_INTEGER_VALUE && a <= CONSTANTS_INTEGER_VALUE - 1;
},
/**
* @returns {boolean} true if value is Long
*/
long(a) {
return Number.isInteger(a) && !typeMatchers.integer(a);
},
/**
* @returns {boolean} true if value is Double
*/
double(a) {
return typeof a === 'number' && !Number.isInteger(a);
},
/**
* @returns {boolean} true if value is Boolean
*/
boolean(a) {
return typeof a === 'boolean';
},
/**
* @returns {boolean} true if value is String
*/
string(a) {
return typeof a === 'string';
},
/**
* @returns {boolean} true if value is File
*/
// file(a) {
// return a instanceof File;
// },
/**
* @returns {boolean} true if value is Date.
*
*/
date(a) {
return a instanceof Date;
},
/**
* @returns {boolean} true if value is JSON
*/
json(a) {
return typeof a === 'object';
},
};
class Utils {
static assign(target, object) {
Object.entries(object).forEach((keyVal) => target.set(keyVal[0], keyVal[1]));
return target;
}
/**
* Not a deep copy
*/
static copyVariables(variables) {
/* eslint @typescript-eslint/no-unsafe-argument: 0 */
return Utils.assign(new variables_1.Variables(), variables.getAll());
}
/**
* Helper for building config with default settings
*
* @static
* @param {ICamundaConfig} [config]
* @returns {ICamundaConfig}
* @memberof Utils
*/
static buildConfig(config) {
return {
maxTasks: 1,
baseUrl: 'http://localhost:8080/engine-rest',
workerId: `worker-${config ? config.topicName : 'demo'}`,
interceptors: Utils.defaultInterceptors(),
use: Utils.getLogger(),
topicName: 'topic_demo',
...config,
};
}
static serializeVariable({ typedValue, }) {
let { value, type } = { ...typedValue };
type = type.toLowerCase();
if (type === 'json' && typeof value !== 'string') {
value = JSON.stringify(value);
}
if (type === 'date' && value instanceof Date) {
value = value.toISOString().replace(/Z$/, 'UTC+00:00');
}
return { ...typedValue, value, type };
}
static serializeVariables(variables, local = false) {
if (!variables) {
return undefined;
}
const dirtyVariables = {};
Object.entries(variables).forEach(([key, value]) => {
const type = Utils.getVariableType(value);
const typedValue = { type, value, valueInfo: {}, local };
dirtyVariables[key] = Utils.serializeVariable({ typedValue });
});
return dirtyVariables;
}
static defaultInterceptors() {
const interceptors = [];
try {
const basicOauth = workit_core_1.IoC.get(identifiers_1.SERVICE_IDENTIFIER.camunda_oauth_info);
interceptors.push(new camunda_external_task_client_js_1.BasicAuthInterceptor(basicOauth));
}
catch (error) {
//
}
// add default timeout for polling
interceptors.push((config) => ({ timeout: GLOBAL_TIMEOUT_PULL, ...config }));
return interceptors;
}
static getLogger() {
try {
return workit_core_1.IoC.get(identifiers_1.SERVICE_IDENTIFIER.logger, process.env.NODE_ENV);
}
catch (error) {
return workit_core_1.IoC.get(identifiers_1.SERVICE_IDENTIFIER.logger);
}
}
}
exports.Utils = Utils;
/*
* @returns the type of the variable
* @param variable: external task variable
*/
Utils.getVariableType = (variable) => {
const match = Object.entries(typeMatchers).filter(([, matcherFunction]) => matcherFunction(variable))[0];
return match[0];
};
//# sourceMappingURL=utils.js.map
;