Initial work to make a new JS script block in automations.
This commit is contained in:
parent
9ae483ca30
commit
0e3689548c
|
@ -15,6 +15,7 @@ export const ActionStepID = {
|
|||
DELETE_ROW: "DELETE_ROW",
|
||||
OUTGOING_WEBHOOK: "OUTGOING_WEBHOOK",
|
||||
EXECUTE_SCRIPT: "EXECUTE_SCRIPT",
|
||||
EXECUTE_SCRIPT_V2: "EXECUTE_SCRIPT_V2",
|
||||
EXECUTE_QUERY: "EXECUTE_QUERY",
|
||||
SERVER_LOG: "SERVER_LOG",
|
||||
DELAY: "DELAY",
|
||||
|
|
|
@ -676,7 +676,10 @@ const automationActions = (store: AutomationStore) => ({
|
|||
runtimeName = `loop.${name}`
|
||||
} else if (idx === 0) {
|
||||
runtimeName = `trigger.${name}`
|
||||
} else if (currentBlock?.stepId === AutomationActionStepId.EXECUTE_SCRIPT) {
|
||||
} else if (
|
||||
currentBlock?.stepId === AutomationActionStepId.EXECUTE_SCRIPT ||
|
||||
currentBlock?.stepId === AutomationActionStepId.EXECUTE_SCRIPT_V2
|
||||
) {
|
||||
const stepId = pathSteps[idx].id
|
||||
if (!stepId) {
|
||||
notifications.error("Error generating binding: Step ID not found.")
|
||||
|
|
|
@ -3,6 +3,7 @@ import * as createRow from "./steps/createRow"
|
|||
import * as updateRow from "./steps/updateRow"
|
||||
import * as deleteRow from "./steps/deleteRow"
|
||||
import * as executeScript from "./steps/executeScript"
|
||||
import * as executeScriptV2 from "./steps/executeScriptV2"
|
||||
import * as executeQuery from "./steps/executeQuery"
|
||||
import * as outgoingWebhook from "./steps/outgoingWebhook"
|
||||
import * as serverLog from "./steps/serverLog"
|
||||
|
@ -42,6 +43,7 @@ const ACTION_IMPLS: ActionImplType = {
|
|||
DELETE_ROW: deleteRow.run,
|
||||
OUTGOING_WEBHOOK: outgoingWebhook.run,
|
||||
EXECUTE_SCRIPT: executeScript.run,
|
||||
EXECUTE_SCRIPT_V2: executeScriptV2.run,
|
||||
EXECUTE_QUERY: executeQuery.run,
|
||||
SERVER_LOG: serverLog.run,
|
||||
DELAY: delay.run,
|
||||
|
@ -68,6 +70,7 @@ export const BUILTIN_ACTION_DEFINITIONS: Record<
|
|||
DELETE_ROW: deleteRow.definition,
|
||||
OUTGOING_WEBHOOK: outgoingWebhook.definition,
|
||||
EXECUTE_SCRIPT: executeScript.definition,
|
||||
EXECUTE_SCRIPT_V2: executeScriptV2.definition,
|
||||
EXECUTE_QUERY: executeQuery.definition,
|
||||
SERVER_LOG: serverLog.definition,
|
||||
DELAY: delay.definition,
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
import * as automationUtils from "../automationUtils"
|
||||
import {
|
||||
AutomationActionStepId,
|
||||
AutomationCustomIOType,
|
||||
AutomationFeature,
|
||||
AutomationIOType,
|
||||
AutomationStepDefinition,
|
||||
AutomationStepType,
|
||||
ExecuteScriptStepInputs,
|
||||
ExecuteScriptStepOutputs,
|
||||
} from "@budibase/types"
|
||||
import { processStringSync } from "@budibase/string-templates"
|
||||
|
||||
export const definition: AutomationStepDefinition = {
|
||||
name: "JS Scripting",
|
||||
tagline: "Execute JavaScript Code",
|
||||
icon: "Code",
|
||||
description: "Run a piece of JavaScript code in your automation",
|
||||
type: AutomationStepType.ACTION,
|
||||
internal: true,
|
||||
stepId: AutomationActionStepId.EXECUTE_SCRIPT_V2,
|
||||
inputs: {},
|
||||
features: {
|
||||
[AutomationFeature.LOOPING]: true,
|
||||
},
|
||||
schema: {
|
||||
inputs: {
|
||||
properties: {
|
||||
code: {
|
||||
type: AutomationIOType.STRING,
|
||||
customType: AutomationCustomIOType.CODE,
|
||||
title: "Code",
|
||||
},
|
||||
},
|
||||
required: ["code"],
|
||||
},
|
||||
outputs: {
|
||||
properties: {
|
||||
value: {
|
||||
type: AutomationIOType.STRING,
|
||||
description: "The result of the return statement",
|
||||
},
|
||||
success: {
|
||||
type: AutomationIOType.BOOLEAN,
|
||||
description: "Whether the action was successful",
|
||||
},
|
||||
},
|
||||
required: ["success"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export async function run({
|
||||
inputs,
|
||||
context,
|
||||
}: {
|
||||
inputs: ExecuteScriptStepInputs
|
||||
context: Record<string, any>
|
||||
}): Promise<ExecuteScriptStepOutputs> {
|
||||
if (inputs.code == null) {
|
||||
return {
|
||||
success: false,
|
||||
response: {
|
||||
message: "Invalid inputs",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const js = Buffer.from(inputs.code, "utf-8").toString("base64")
|
||||
|
||||
try {
|
||||
return {
|
||||
success: true,
|
||||
value: processStringSync(`{{ js "${js}" }}`, context),
|
||||
}
|
||||
} catch (err) {
|
||||
return {
|
||||
success: false,
|
||||
response: automationUtils.getError(err),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -208,6 +208,9 @@ class BaseStepBuilder {
|
|||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use `executeScriptV2` instead
|
||||
*/
|
||||
executeScript(
|
||||
input: ExecuteScriptStepInputs,
|
||||
opts?: { stepName?: string; stepId?: string }
|
||||
|
@ -220,6 +223,18 @@ class BaseStepBuilder {
|
|||
)
|
||||
}
|
||||
|
||||
executeScriptV2(
|
||||
input: ExecuteScriptStepInputs,
|
||||
opts?: { stepName?: string; stepId?: string }
|
||||
): this {
|
||||
return this.step(
|
||||
AutomationActionStepId.EXECUTE_SCRIPT_V2,
|
||||
BUILTIN_ACTION_DEFINITIONS.EXECUTE_SCRIPT_V2,
|
||||
input,
|
||||
opts
|
||||
)
|
||||
}
|
||||
|
||||
filter(input: FilterStepInputs): this {
|
||||
return this.step(
|
||||
AutomationActionStepId.FILTER,
|
||||
|
|
|
@ -63,6 +63,7 @@ export enum AutomationActionStepId {
|
|||
EXECUTE_BASH = "EXECUTE_BASH",
|
||||
OUTGOING_WEBHOOK = "OUTGOING_WEBHOOK",
|
||||
EXECUTE_SCRIPT = "EXECUTE_SCRIPT",
|
||||
EXECUTE_SCRIPT_V2 = "EXECUTE_SCRIPT_V2",
|
||||
EXECUTE_QUERY = "EXECUTE_QUERY",
|
||||
SERVER_LOG = "SERVER_LOG",
|
||||
DELAY = "DELAY",
|
||||
|
|
|
@ -79,6 +79,10 @@ export type ActionImplementations<T extends Hosting> = {
|
|||
ExecuteScriptStepInputs,
|
||||
ExecuteScriptStepOutputs
|
||||
>
|
||||
[AutomationActionStepId.EXECUTE_SCRIPT_V2]: ActionImplementation<
|
||||
ExecuteScriptStepInputs,
|
||||
ExecuteScriptStepOutputs
|
||||
>
|
||||
[AutomationActionStepId.FILTER]: ActionImplementation<
|
||||
FilterStepInputs,
|
||||
FilterStepOutputs
|
||||
|
@ -190,6 +194,8 @@ export type AutomationStepInputs<T extends AutomationActionStepId> =
|
|||
? ExecuteQueryStepInputs
|
||||
: T extends AutomationActionStepId.EXECUTE_SCRIPT
|
||||
? ExecuteScriptStepInputs
|
||||
: T extends AutomationActionStepId.EXECUTE_SCRIPT_V2
|
||||
? ExecuteScriptStepInputs
|
||||
: T extends AutomationActionStepId.FILTER
|
||||
? FilterStepInputs
|
||||
: T extends AutomationActionStepId.QUERY_ROWS
|
||||
|
@ -247,6 +253,9 @@ export type ExecuteQueryStep =
|
|||
export type ExecuteScriptStep =
|
||||
AutomationStepSchema<AutomationActionStepId.EXECUTE_SCRIPT>
|
||||
|
||||
export type ExecuteScriptV2Step =
|
||||
AutomationStepSchema<AutomationActionStepId.EXECUTE_SCRIPT_V2>
|
||||
|
||||
export type FilterStep = AutomationStepSchema<AutomationActionStepId.FILTER>
|
||||
|
||||
export type QueryRowsStep =
|
||||
|
@ -293,6 +302,7 @@ export type AutomationStep =
|
|||
| DeleteRowStep
|
||||
| ExecuteQueryStep
|
||||
| ExecuteScriptStep
|
||||
| ExecuteScriptV2Step
|
||||
| FilterStep
|
||||
| QueryRowsStep
|
||||
| SendEmailSmtpStep
|
||||
|
|
Loading…
Reference in New Issue