@rudderstack/workflow-engine
Version:
A generic workflow execution engine
52 lines • 1.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonataStepExecutor = void 0;
const jsonata_1 = __importDefault(require("jsonata"));
const errors_1 = require("../../../../../errors");
const base_1 = require("../../../executors/base");
class JsonataStepExecutor extends base_1.BaseStepExecutor {
constructor(step, template) {
super(step);
this.templateExpression = (0, jsonata_1.default)(template);
}
async execute(input, executionBindings) {
const output = await JsonataStepExecutor.evaluateJsonataExpr(this.templateExpression, input, executionBindings);
return { output: JsonataStepExecutor.cleanUpArrays(output) };
}
/**
* JSONata adds custom properties to arrays for internal processing
* hence it fails the comparison so we need to cleanup.
* Reference: https://github.com/jsonata-js/jsonata/issues/296
*/
static cleanUpArrays(obj) {
let newObj = obj;
if (Array.isArray(obj)) {
newObj = obj.map((val) => this.cleanUpArrays(val));
}
else if (newObj instanceof Object) {
Object.keys(newObj).forEach((key) => {
newObj[key] = this.cleanUpArrays(obj[key]);
});
}
return newObj;
}
static async evaluateJsonataExpr(expr, data, bindings) {
try {
return await expr.evaluate(data, bindings);
}
catch (error) {
if (error.token === 'doReturn') {
return error.result;
}
if (errors_1.ErrorUtils.isAssertError(error)) {
throw new errors_1.StatusError(error.message, 400);
}
throw error;
}
}
}
exports.JsonataStepExecutor = JsonataStepExecutor;
//# sourceMappingURL=jsonata.js.map