import tokenHandler from './../../handler/token.js' import flowHandler from './../../handler/flow.js' import logger from './../../helper/logger.js' /** * handle webhook * * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 * @link https://git.node001.net/HerrHase/signpost.git * */ export default async function(fastify, options) { fastify.addHook('preHandler', flowHandler) fastify.addHook('preHandler', tokenHandler) /** * * * @param {object} request * @param {object} response * */ fastify.post('/:uuid(^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$)', async function (request, response) { if (response.locals.schema && !request.validateInput(request.body, response.locals.schema)) { return response .code(400) .send() } // running actions const action = new response.locals.action.default(response.locals.flow, request.body) response .code(204) .send() // try run from action try { await action.run() } catch(error) { logger(response.locals.flow.uuid).error('webhook / run / ' + error) } }) }