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

View File

@ -24,7 +24,11 @@ exports.definition = {
properties: { properties: {
stdout: { stdout: {
type: "string", 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 { try {
const command = processStringSync(inputs.code, context) const command = processStringSync(inputs.code, context)
let stdout let stdout,
success = true
try { try {
stdout = execSync(command, { timeout: 500 }) stdout = execSync(command, { timeout: 500 })
} catch (err) { } catch (err) {
stdout = err.message stdout = err.message
success = false
} }
return { return {
stdout, stdout,
success,
} }
} catch (err) { } catch (err) {
console.error(err)
return { return {
success: false, success: false,
response: err, response: err,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -58,7 +58,6 @@ exports.run = async function ({ inputs }) {
response, response,
} }
} catch (err) { } catch (err) {
console.error(err)
return { return {
success: false, success: false,
response: err, 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 }) { exports.run = async function ({ inputs, appId }) {
console.log(`App ${appId} - ${inputs.text}`) console.log(`App ${appId} - ${inputs.text}`)
return {
success: true,
}
} }

View File

@ -1,4 +1,5 @@
const fetch = require("node-fetch") const fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
exports.definition = { exports.definition = {
name: "Slack Message", name: "Slack Message",
@ -32,6 +33,10 @@ exports.definition = {
type: "boolean", type: "boolean",
description: "Whether the message sent successfully", 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 { return {
httpStatus: response.status, httpStatus: status,
success: response.status === 200, response: message,
success: status === 200,
} }
} }

View File

@ -100,7 +100,6 @@ exports.run = async function ({ inputs, appId, emitter }) {
success: ctx.status === 200, success: ctx.status === 200,
} }
} catch (err) { } catch (err) {
console.error(err)
return { return {
success: false, success: false,
response: err, 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 fetch = require("node-fetch")
const { getFetchResponse } = require("./utils")
exports.definition = { exports.definition = {
name: "Zapier Webhook", name: "Zapier Webhook",
@ -43,9 +44,9 @@ exports.definition = {
type: "number", type: "number",
description: "The HTTP status code of the request", description: "The HTTP status code of the request",
}, },
zapierStatus: { response: {
type: "string", 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 const { status, message } = await getFetchResponse(response)
if (response.status === 200) {
try {
data = await response.json()
} catch (err) {
data = null
}
}
return { return {
httpStatus: response.status, success: status === 200,
zapierStatus: data && data.status ? data.status : data, httpStatus: status,
response: message,
} }
} }