Re-working all of the actions to have a success output, and make sure everything has useful outputs.

This commit is contained in:
mike12345567 2021-09-07 17:31:54 +01:00
parent f429bc1a08
commit 1c24e3f520
17 changed files with 87 additions and 129 deletions

View File

@ -1,4 +1,3 @@
const sendgridEmail = require("./steps/sendgridEmail")
const sendSmtpEmail = require("./steps/sendSmtpEmail")
const createRow = require("./steps/createRow")
const updateRow = require("./steps/updateRow")
@ -14,7 +13,6 @@ const zapier = require("./steps/zapier")
const integromat = require("./steps/integromat")
const ACTION_IMPLS = {
SEND_EMAIL: sendgridEmail.run,
SEND_EMAIL_SMTP: sendSmtpEmail.run,
CREATE_ROW: createRow.run,
UPDATE_ROW: updateRow.run,
@ -31,7 +29,6 @@ const ACTION_IMPLS = {
integromat: integromat.run,
}
const ACTION_DEFINITIONS = {
SEND_EMAIL: sendgridEmail.definition,
SEND_EMAIL_SMTP: sendSmtpEmail.definition,
CREATE_ROW: createRow.definition,
UPDATE_ROW: updateRow.definition,

View File

@ -24,7 +24,11 @@ exports.definition = {
properties: {
stdout: {
type: "string",
description: "Standard output of your bash command or script.",
description: "Standard output of your bash command or script",
},
success: {
type: "boolean",
description: "Whether the command was successful",
},
},
},
@ -42,18 +46,20 @@ exports.run = async function ({ inputs, context }) {
try {
const command = processStringSync(inputs.code, context)
let stdout
let stdout,
success = true
try {
stdout = execSync(command, { timeout: 500 })
} catch (err) {
stdout = err.message
success = false
}
return {
stdout,
success,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -42,7 +42,7 @@ exports.definition = {
},
success: {
type: "boolean",
description: "Whether the action was successful",
description: "Whether the row creation was successful",
},
id: {
type: "string",
@ -97,7 +97,6 @@ exports.run = async function ({ inputs, appId, apiKey, emitter }) {
success: ctx.status === 200,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -17,10 +17,22 @@ exports.definition = {
},
required: ["time"],
},
outputs: {
properties: {
success: {
type: "boolean",
description: "Whether the delay was successful",
},
},
required: ["success"],
},
},
type: "LOGIC",
}
exports.run = async function delay({ inputs }) {
await wait(inputs.time)
return {
success: true,
}
}

View File

@ -42,7 +42,7 @@ exports.definition = {
},
success: {
type: "boolean",
description: "Whether the action was successful",
description: "Whether the deletion was successful",
},
},
required: ["row", "success"],
@ -84,7 +84,6 @@ exports.run = async function ({ inputs, appId, apiKey, emitter }) {
success: ctx.status === 200,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -1,4 +1,5 @@
const fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
const DEFAULT_USERNAME = "Budibase Automate"
const DEFAULT_AVATAR_URL = "https://i.imgur.com/a1cmTKM.png"
@ -39,6 +40,10 @@ exports.definition = {
type: "number",
description: "The HTTP status code of the request",
},
response: {
type: "string",
description: "The response from the Discord Webhook",
},
success: {
type: "boolean",
description: "Whether the message sent successfully",
@ -68,8 +73,10 @@ exports.run = async function ({ inputs }) {
},
})
const { status, message } = await getFetchResponse(response)
return {
httpStatus: response.status,
success: response.status === 200,
httpStatus: status,
success: status === 200,
response: message,
}
}

View File

@ -75,7 +75,6 @@ exports.run = async function ({ inputs, appId, emitter }) {
success: ctx.status === 200,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -64,7 +64,6 @@ exports.run = async function ({ inputs, appId, context, emitter }) {
value: ctx.body,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -1,4 +1,5 @@
const fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
exports.definition = {
name: "Integromat Integration",
@ -45,6 +46,10 @@ exports.definition = {
type: "boolean",
description: "Whether call was successful",
},
httpStatus: {
type: "number",
description: "The HTTP status code returned",
},
response: {
type: "object",
description: "The webhook response - this can have properties",
@ -72,19 +77,10 @@ exports.run = async function ({ inputs }) {
},
})
let data
if (response.status === 200) {
try {
data = await response.json()
} catch (err) {
data = {}
}
} else {
data = await response.text()
}
const { status, message } = await getFetchResponse(response)
return {
success: response.status === 200,
response: data,
httpStatus: status,
success: status === 200,
response: message,
}
}

View File

@ -1,4 +1,5 @@
const fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
const RequestType = {
POST: "POST",
@ -60,6 +61,10 @@ exports.definition = {
type: "object",
description: "The response from the webhook",
},
httpStatus: {
type: "number",
description: "The HTTP status code returned",
},
success: {
type: "boolean",
description: "Whether the action was successful",
@ -107,19 +112,11 @@ exports.run = async function ({ inputs }) {
JSON.parse(request.body)
}
const response = await fetch(url, request)
const contentType = response.headers.get("content-type")
const success = response.status === 200
let resp
if (!success) {
resp = response.statusText
} else if (contentType && contentType.indexOf("application/json") !== -1) {
resp = await response.json()
} else {
resp = await response.text()
}
const { status, message } = await getFetchResponse(response)
return {
response: resp,
success: success,
httpStatus: status,
response: message,
success: status === 200,
}
} catch (err) {
/* istanbul ignore next */

View File

@ -58,7 +58,6 @@ exports.run = async function ({ inputs }) {
response,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -1,74 +0,0 @@
exports.definition = {
description: "Send an email using SendGrid",
tagline: "Send email to {{inputs.to}}",
icon: "ri-mail-open-line",
name: "Send Email (SendGrid)",
type: "ACTION",
stepId: "SEND_EMAIL",
inputs: {},
schema: {
inputs: {
properties: {
apiKey: {
type: "string",
title: "SendGrid API key",
},
to: {
type: "string",
title: "Send To",
},
from: {
type: "string",
title: "Send From",
},
subject: {
type: "string",
title: "Email Subject",
},
contents: {
type: "string",
title: "Email Contents",
},
},
required: ["to", "from", "subject", "contents"],
},
outputs: {
properties: {
success: {
type: "boolean",
description: "Whether the email was sent",
},
response: {
type: "object",
description: "A response from the email client, this may be an error",
},
},
required: ["success"],
},
},
}
exports.run = async function ({ inputs }) {
const sgMail = require("@sendgrid/mail")
sgMail.setApiKey(inputs.apiKey)
const msg = {
to: inputs.to,
from: inputs.from,
subject: inputs.subject,
text: inputs.contents ? inputs.contents : "Empty",
}
try {
let response = await sgMail.send(msg)
return {
success: true,
response,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,
}
}
}

View File

@ -38,4 +38,7 @@ exports.definition = {
exports.run = async function ({ inputs, appId }) {
console.log(`App ${appId} - ${inputs.text}`)
return {
success: true,
}
}

View File

@ -1,4 +1,5 @@
const fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
exports.definition = {
name: "Slack Message",
@ -32,6 +33,10 @@ exports.definition = {
type: "boolean",
description: "Whether the message sent successfully",
},
response: {
type: "string",
description: "The response from the Slack Webhook",
},
},
},
},
@ -49,8 +54,10 @@ exports.run = async function ({ inputs }) {
},
})
const { status, message } = await getFetchResponse(response)
return {
httpStatus: response.status,
success: response.status === 200,
httpStatus: status,
response: message,
success: status === 200,
}
}

View File

@ -100,7 +100,6 @@ exports.run = async function ({ inputs, appId, emitter }) {
success: ctx.status === 200,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,

View File

@ -0,0 +1,18 @@
exports.getFetchResponse = async fetched => {
let status = fetched.status,
message
const contentType = fetched.headers.get("content-type")
try {
if (contentType && contentType.indexOf("application/json") !== -1) {
message = await fetched.json()
} else {
message = await fetched.text()
}
} catch (err) {
message = "Failed to retrieve response"
}
if (typeof message !== "string") {
message = JSON.stringify(message)
}
return { status, message }
}

View File

@ -1,4 +1,5 @@
const fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
exports.definition = {
name: "Zapier Webhook",
@ -43,9 +44,9 @@ exports.definition = {
type: "number",
description: "The HTTP status code of the request",
},
zapierStatus: {
response: {
type: "string",
description: "The result status from Zapier",
description: "The response from Zapier",
},
},
},
@ -72,17 +73,11 @@ exports.run = async function ({ inputs }) {
},
})
let data = null
if (response.status === 200) {
try {
data = await response.json()
} catch (err) {
data = null
}
}
const { status, message } = await getFetchResponse(response)
return {
httpStatus: response.status,
zapierStatus: data && data.status ? data.status : data,
success: status === 200,
httpStatus: status,
response: message,
}
}