diff --git a/src/core/operations/CustomCode.mjs b/src/core/operations/CustomCode.mjs new file mode 100644 index 00000000..e485d2e0 --- /dev/null +++ b/src/core/operations/CustomCode.mjs @@ -0,0 +1,54 @@ +/** + * @author kjcain [kyler@kylercain.com] + * @copyright Crown Copyright 2022 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Custom Code operation + */ +class CustomCode extends Operation { + + /** + * CustomCode constructor + */ + constructor() { + super(); + + this.name = "Custom Code"; + this.module = "Default"; + this.description = "Execute custom javascript code."; + this.infoURL = ""; + this.inputType = "JSON"; + this.outputType = "JSON"; + this.args = [ + { + name: "Code", + type: "text", + value: "return input;" + } + ]; + } + + /** + * @param {JSON} input + * @param {Object[]} args + * @returns {JSON} + */ + run(input, args) { + const [customCode] = args; + + try { + const wrappedCode = new Function("input", customCode); + return wrappedCode(input); + } catch (error) { + throw new OperationError(error); + } + } + +} + +export default CustomCode;