You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB

import tokenHandler from './../../handler/token.js'
import bearerHandler from './../../handler/bearer.js'
import flowHandler from './../../handler/flow.js'
import logger from './../../helper/logger.js'
/**
* handle webhook
*
* @author Björn Hase <me@herr-hase.wtf>
* @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)
/**
*
*
* @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}$)', { preHandler: [ bearerHandler ] }, 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)
}
})
/**
*
*
* @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}$)/:token(^[A-Za-z0-9+\/]+={0,2}$)', { preHandler: [ tokenHandler ] }, 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)
}
})
}