2020-10-28 03:16:57 +13:00
|
|
|
const fetch = require("node-fetch")
|
2020-10-28 01:33:25 +13:00
|
|
|
|
|
|
|
const RequestType = {
|
|
|
|
POST: "POST",
|
|
|
|
GET: "GET",
|
|
|
|
PUT: "PUT",
|
|
|
|
DELETE: "DELETE",
|
|
|
|
PATCH: "PATCH",
|
|
|
|
}
|
|
|
|
|
|
|
|
const BODY_REQUESTS = [RequestType.POST, RequestType.PUT, RequestType.PATCH]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Note, there is some functionality in this that is not currently exposed as it
|
|
|
|
* is complex and maybe better to be opinionated here.
|
|
|
|
* GET/DELETE requests cannot handle body elements so they will not be sent if configured.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports.definition = {
|
|
|
|
name: "Outgoing webhook",
|
|
|
|
tagline: "Send a {{inputs.requestMethod}} request",
|
|
|
|
icon: "ri-send-plane-line",
|
|
|
|
description: "Send a request of specified method to a URL",
|
|
|
|
type: "ACTION",
|
|
|
|
stepId: "OUTGOING_WEBHOOK",
|
|
|
|
inputs: {
|
|
|
|
requestMethod: "POST",
|
|
|
|
url: "http://",
|
|
|
|
requestBody: "{}",
|
|
|
|
},
|
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
requestMethod: {
|
|
|
|
type: "string",
|
|
|
|
enum: Object.values(RequestType),
|
|
|
|
title: "Request method",
|
|
|
|
},
|
|
|
|
url: {
|
|
|
|
type: "string",
|
|
|
|
title: "URL",
|
|
|
|
},
|
|
|
|
requestBody: {
|
|
|
|
type: "string",
|
|
|
|
title: "JSON Body",
|
|
|
|
customType: "wide",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["requestMethod", "url"],
|
|
|
|
},
|
|
|
|
outputs: {
|
|
|
|
properties: {
|
|
|
|
response: {
|
|
|
|
type: "object",
|
|
|
|
description: "The response from the webhook",
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
type: "boolean",
|
|
|
|
description: "Whether the action was successful",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["response", "success"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
module.exports.run = async function ({ inputs }) {
|
2020-10-28 01:33:25 +13:00
|
|
|
let { requestMethod, url, requestBody } = inputs
|
|
|
|
if (!url.startsWith("http")) {
|
|
|
|
url = `http://${url}`
|
|
|
|
}
|
|
|
|
const request = {
|
|
|
|
method: requestMethod,
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
requestBody &&
|
|
|
|
requestBody.length !== 0 &&
|
|
|
|
BODY_REQUESTS.indexOf(requestMethod) !== -1
|
|
|
|
) {
|
2020-10-28 03:16:57 +13:00
|
|
|
request.body = JSON.parse(requestBody)
|
2020-10-28 01:33:25 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-10-28 03:16:57 +13:00
|
|
|
const response = await fetch(url, request)
|
2020-10-28 01:33:25 +13:00
|
|
|
return {
|
2020-10-28 03:16:57 +13:00
|
|
|
response: await response.json(),
|
2020-10-28 01:33:25 +13:00
|
|
|
success: response.status === 200,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-03-13 04:32:16 +13:00
|
|
|
/* istanbul ignore next */
|
2020-10-28 01:33:25 +13:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
response: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|