1
0
Fork 0
mirror of synced 2024-09-30 00:57:16 +13:00
budibase/packages/server/src/automations/steps/executeScript.js

73 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-03-26 00:17:04 +13:00
const scriptController = require("../../api/controllers/script")
exports.definition = {
2021-05-27 22:11:44 +12:00
name: "JS Scripting",
2021-05-01 02:44:37 +12:00
tagline: "Execute JavaScript Code",
2021-03-26 00:17:04 +13:00
icon: "ri-terminal-box-line",
description: "Run a piece of JavaScript code in your automation",
type: "ACTION",
stepId: "EXECUTE_SCRIPT",
inputs: {},
schema: {
inputs: {
properties: {
code: {
2021-03-27 03:56:34 +13:00
type: "string",
customType: "code",
2021-03-26 00:17:04 +13:00
title: "Code",
},
},
required: ["code"],
},
outputs: {
properties: {
2021-05-01 02:44:37 +12:00
value: {
type: "string",
description:
"The result of the last statement of the executed script.",
},
2021-03-26 00:17:04 +13:00
success: {
type: "boolean",
description: "Whether the action was successful",
},
},
},
2021-03-27 03:56:34 +13:00
required: ["success"],
2021-03-26 00:17:04 +13:00
},
}
exports.run = async function ({ inputs, appId, context, emitter }) {
2021-03-26 00:17:04 +13:00
if (inputs.code == null) {
return {
success: false,
response: {
message: "Invalid inputs",
},
}
}
const ctx = {
request: {
body: {
2021-03-27 03:56:34 +13:00
script: inputs.code,
context,
2021-03-26 00:17:04 +13:00
},
},
user: { appId },
eventEmitter: emitter,
}
try {
await scriptController.execute(ctx)
return {
success: ctx.status === 200,
2021-05-01 02:44:37 +12:00
value: ctx.body,
2021-03-26 00:17:04 +13:00
}
} catch (err) {
return {
success: false,
response: err,
}
}
}