From 94daa3d4b511a95aa6ded8858b279a029d48510c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 18 Jun 2021 18:07:51 +0100 Subject: [PATCH] Adding a server logging script for testing and updating automation script functionality so that you can use 'return trigger.row.firstName' and it'll function as expected. --- packages/server/src/api/controllers/script.js | 6 ++- packages/server/src/automations/actions.js | 3 ++ .../server/src/automations/steps/serverLog.js | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 packages/server/src/automations/steps/serverLog.js diff --git a/packages/server/src/api/controllers/script.js b/packages/server/src/api/controllers/script.js index 3f110a2764..f312472801 100644 --- a/packages/server/src/api/controllers/script.js +++ b/packages/server/src/api/controllers/script.js @@ -3,13 +3,15 @@ const vm = require("vm") class ScriptExecutor { constructor(body) { - this.script = new vm.Script(body.script) + const code = `let fn = () => {\n${body.script}\n}; out = fn();` + this.script = new vm.Script(code) this.context = vm.createContext(body.context) this.context.fetch = fetch } execute() { - return this.script.runInContext(this.context) + this.script.runInContext(this.context) + return this.context.out } } diff --git a/packages/server/src/automations/actions.js b/packages/server/src/automations/actions.js index 762ce677a7..b0b5f9f1ba 100644 --- a/packages/server/src/automations/actions.js +++ b/packages/server/src/automations/actions.js @@ -7,6 +7,7 @@ const executeScript = require("./steps/executeScript") const bash = require("./steps/bash") const executeQuery = require("./steps/executeQuery") const outgoingWebhook = require("./steps/outgoingWebhook") +const serverLog = require("./steps/serverLog") const env = require("../environment") const Sentry = require("@sentry/node") const { @@ -24,6 +25,7 @@ const BUILTIN_ACTIONS = { EXECUTE_SCRIPT: executeScript.run, EXECUTE_BASH: bash.run, EXECUTE_QUERY: executeQuery.run, + SERVER_LOG: serverLog.run, } const BUILTIN_DEFINITIONS = { SEND_EMAIL: sendgridEmail.definition, @@ -35,6 +37,7 @@ const BUILTIN_DEFINITIONS = { EXECUTE_SCRIPT: executeScript.definition, EXECUTE_QUERY: executeQuery.definition, EXECUTE_BASH: bash.definition, + SERVER_LOG: serverLog.definition, } let MANIFEST = null diff --git a/packages/server/src/automations/steps/serverLog.js b/packages/server/src/automations/steps/serverLog.js new file mode 100644 index 0000000000..a98e87235c --- /dev/null +++ b/packages/server/src/automations/steps/serverLog.js @@ -0,0 +1,41 @@ +/** + * Note, there is some functionality in this that is not currently exposed as it + * is complex and maybe better to be opinionated here. + * GET/DELETE requests cannot handle body elements so they will not be sent if configured. + */ + +module.exports.definition = { + name: "Backend log", + tagline: "Console log a value in the backend", + icon: "ri-server-line", + description: "Logs the given text to the server (using console.log)", + type: "ACTION", + stepId: "SERVER_LOG", + inputs: { + text: "", + }, + schema: { + inputs: { + properties: { + text: { + type: "string", + title: "URL", + }, + }, + required: ["text"], + }, + outputs: { + properties: { + success: { + type: "boolean", + description: "Whether the action was successful", + }, + }, + required: ["success"], + }, + }, +} + +module.exports.run = async function ({ inputs, appId }) { + console.log(`App ${appId} - ${inputs.text}`) +} \ No newline at end of file