2020-10-23 05:48:32 +13:00
|
|
|
const { generateWebhookID, getWebhookParams } = require("../../db/utils")
|
|
|
|
const toJsonSchema = require("to-json-schema")
|
|
|
|
const validate = require("jsonschema").validate
|
|
|
|
const triggers = require("../../automations/triggers")
|
2022-02-01 06:42:51 +13:00
|
|
|
const { getProdAppID } = require("@budibase/backend-core/db")
|
2022-01-28 13:05:39 +13:00
|
|
|
const { getAppDB, updateAppId } = require("@budibase/backend-core/context")
|
2020-10-23 05:48:32 +13:00
|
|
|
|
|
|
|
const AUTOMATION_DESCRIPTION = "Generated from Webhook Schema"
|
|
|
|
|
|
|
|
function Webhook(name, type, target) {
|
|
|
|
this.live = true
|
|
|
|
this.name = name
|
|
|
|
this.action = {
|
|
|
|
type,
|
|
|
|
target,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.Webhook = Webhook
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.fetch = async ctx => {
|
2022-01-28 13:05:39 +13:00
|
|
|
const db = getAppDB()
|
2020-10-23 05:48:32 +13:00
|
|
|
const response = await db.allDocs(
|
|
|
|
getWebhookParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 22:32:22 +12:00
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
2020-10-23 05:48:32 +13:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.save = async ctx => {
|
2022-01-28 13:05:39 +13:00
|
|
|
const db = getAppDB()
|
2020-10-23 05:48:32 +13:00
|
|
|
const webhook = ctx.request.body
|
2021-03-30 05:32:05 +13:00
|
|
|
webhook.appId = ctx.appId
|
2020-10-23 05:48:32 +13:00
|
|
|
|
|
|
|
// check that the webhook exists
|
|
|
|
if (webhook._id) {
|
|
|
|
await db.get(webhook._id)
|
|
|
|
} else {
|
|
|
|
webhook._id = generateWebhookID()
|
|
|
|
}
|
|
|
|
const response = await db.put(webhook)
|
2021-03-09 04:57:19 +13:00
|
|
|
webhook._rev = response.rev
|
2020-10-23 05:48:32 +13:00
|
|
|
ctx.body = {
|
|
|
|
message: "Webhook created successfully",
|
2021-03-09 04:57:19 +13:00
|
|
|
webhook,
|
2020-10-23 05:48:32 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.destroy = async ctx => {
|
2022-01-28 13:05:39 +13:00
|
|
|
const db = getAppDB()
|
2020-10-23 05:48:32 +13:00
|
|
|
ctx.body = await db.remove(ctx.params.id, ctx.params.rev)
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.buildSchema = async ctx => {
|
2022-01-28 13:05:39 +13:00
|
|
|
updateAppId(ctx.params.instance)
|
|
|
|
const db = getAppDB()
|
2020-10-23 05:48:32 +13:00
|
|
|
const webhook = await db.get(ctx.params.id)
|
|
|
|
webhook.bodySchema = toJsonSchema(ctx.request.body)
|
|
|
|
// update the automation outputs
|
|
|
|
if (webhook.action.type === exports.WebhookType.AUTOMATION) {
|
|
|
|
let automation = await db.get(webhook.action.target)
|
|
|
|
const autoOutputs = automation.definition.trigger.schema.outputs
|
|
|
|
let properties = webhook.bodySchema.properties
|
2021-12-08 23:14:30 +13:00
|
|
|
// reset webhook outputs
|
|
|
|
autoOutputs.properties = {
|
|
|
|
body: autoOutputs.properties.body,
|
|
|
|
}
|
2020-10-23 05:48:32 +13:00
|
|
|
for (let prop of Object.keys(properties)) {
|
|
|
|
autoOutputs.properties[prop] = {
|
|
|
|
type: properties[prop].type,
|
|
|
|
description: AUTOMATION_DESCRIPTION,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await db.put(automation)
|
|
|
|
}
|
|
|
|
ctx.body = await db.put(webhook)
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:32:22 +12:00
|
|
|
exports.trigger = async ctx => {
|
2022-02-01 06:42:51 +13:00
|
|
|
const prodAppId = getProdAppID(ctx.params.instance)
|
|
|
|
updateAppId(prodAppId)
|
2021-11-04 03:08:47 +13:00
|
|
|
try {
|
2022-01-28 13:05:39 +13:00
|
|
|
const db = getAppDB()
|
2021-11-04 03:08:47 +13:00
|
|
|
const webhook = await db.get(ctx.params.id)
|
|
|
|
// validate against the schema
|
|
|
|
if (webhook.bodySchema) {
|
|
|
|
validate(ctx.request.body, webhook.bodySchema)
|
|
|
|
}
|
|
|
|
const target = await db.get(webhook.action.target)
|
|
|
|
if (webhook.action.type === exports.WebhookType.AUTOMATION) {
|
|
|
|
// trigger with both the pure request and then expand it
|
|
|
|
// incase the user has produced a schema to bind to
|
|
|
|
await triggers.externalTrigger(target, {
|
|
|
|
body: ctx.request.body,
|
|
|
|
...ctx.request.body,
|
2022-02-01 06:42:51 +13:00
|
|
|
appId: prodAppId,
|
2021-11-04 03:08:47 +13:00
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message: "Webhook trigger fired successfully",
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.status === 404) {
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message: "Application not deployed yet.",
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 04:57:19 +13:00
|
|
|
}
|
2020-10-23 05:48:32 +13:00
|
|
|
}
|