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

71 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-03-26 00:17:04 +13:00
const scriptController = require("../../api/controllers/script")
const { buildCtx } = require("./utils")
const automationUtils = require("../automationUtils")
2021-03-26 00:17:04 +13:00
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",
icon: "Code",
2021-03-26 00:17:04 +13:00
description: "Run a piece of JavaScript code in your automation",
type: "ACTION",
internal: true,
2021-03-26 00:17:04 +13:00
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 return statement",
2021-05-01 02:44:37 +12:00
},
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 = buildCtx(appId, emitter, {
body: {
script: inputs.code,
context,
2021-03-26 00:17:04 +13:00
},
})
2021-03-26 00:17:04 +13:00
try {
await scriptController.execute(ctx)
return {
success: true,
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: automationUtils.getError(err),
2021-03-26 00:17:04 +13:00
}
}
}