2020-10-22 18:48:32 +02:00
|
|
|
const Router = require("@koa/router")
|
|
|
|
const controller = require("../controllers/webhook")
|
|
|
|
const authorized = require("../../middleware/authorized")
|
|
|
|
const joiValidator = require("../../middleware/joi-validator")
|
2021-05-14 16:43:41 +02:00
|
|
|
const { BUILDER } = require("@budibase/auth/permissions")
|
2020-10-22 18:48:32 +02:00
|
|
|
const Joi = require("joi")
|
|
|
|
|
|
|
|
const router = Router()
|
|
|
|
|
|
|
|
function generateSaveValidator() {
|
|
|
|
// prettier-ignore
|
|
|
|
return joiValidator.body(Joi.object({
|
|
|
|
live: Joi.bool(),
|
|
|
|
_id: Joi.string().optional(),
|
|
|
|
_rev: Joi.string().optional(),
|
|
|
|
name: Joi.string().required(),
|
|
|
|
bodySchema: Joi.object().optional(),
|
|
|
|
action: Joi.object({
|
|
|
|
type: Joi.string().required().valid(controller.WebhookType.AUTOMATION),
|
|
|
|
target: Joi.string().required(),
|
|
|
|
}).required(),
|
|
|
|
}).unknown(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
router
|
|
|
|
.get("/api/webhooks", authorized(BUILDER), controller.fetch)
|
|
|
|
.put(
|
|
|
|
"/api/webhooks",
|
|
|
|
authorized(BUILDER),
|
|
|
|
generateSaveValidator(),
|
|
|
|
controller.save
|
|
|
|
)
|
|
|
|
.delete("/api/webhooks/:id/:rev", authorized(BUILDER), controller.destroy)
|
|
|
|
.post(
|
|
|
|
"/api/webhooks/schema/:instance/:id",
|
|
|
|
authorized(BUILDER),
|
|
|
|
controller.buildSchema
|
|
|
|
)
|
2021-02-27 11:15:05 +01:00
|
|
|
// this shouldn't have authorisation, right now its always public
|
|
|
|
.post("/api/webhooks/trigger/:instance/:id", controller.trigger)
|
2020-10-22 18:48:32 +02:00
|
|
|
|
|
|
|
module.exports = router
|