@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
52 lines • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseVariables = parseVariables;
exports.parseVariablesAndCustomHeadersToJSON = parseVariablesAndCustomHeadersToJSON;
exports.stringifyVariables = stringifyVariables;
const lib_1 = require("../../lib");
function parseVariables(input) {
// Null guard the input to avoid issues with undefined input
// This is a run-time guard. The type system disallows passing undefined, but runtime checks are necessary.
// We had a failing test that hit this condition. I'm doing this as a workaround. There is probably a deeper issue that needs to be fixed.
// See: https://github.com/camunda/camunda-8-js-sdk/issues/565
return Object.assign({}, input, {
variables: (0, lib_1.losslessParse)(input?.variables || '{}'),
});
}
/**
* Parse an incoming job and convert its variables and custom headers to JSON.
*/
function parseVariablesAndCustomHeadersToJSON(response,
/* eslint-disable @typescript-eslint/no-explicit-any */
inputVariableDto, customHeadersDto) {
try {
return Object.assign({}, response, {
customHeaders: (0, lib_1.losslessParse)(response.customHeaders, customHeadersDto),
variables: (0, lib_1.losslessParse)(response.variables, inputVariableDto),
});
}
catch (e) {
console.error(`Error parsing variables ${e}`);
console.error('Job', response);
throw e;
}
}
/**
* Turn the `variables` field of a request from a JS object to a JSON string
* This should be a key:value object where the keys will be variable names in Zeebe and the values are the corresponding values.
* This function is used when sending a job back to Zeebe.
*/
function stringifyVariables(request) {
const variables = request.variables || {};
/**
* This is a run-time guard. The type system disallows passing an array, but type erasure and dynamic programming can override that.
* If you pass an array as the variables to a CompleteJob RPC call, it will report success, but fail on the broker, stalling the process.
* See: https://github.com/camunda/camunda-8-js-sdk/issues/247
*/
if (Array.isArray(variables)) {
throw new Error('Unable to parse Array into variables');
}
const variablesString = (0, lib_1.losslessStringify)(variables);
return Object.assign({}, request, { variables: variablesString });
}
//# sourceMappingURL=stringifyVariables.js.map