1
0
mirror of synced 2025-12-25 02:17:36 -05:00
Files
docs/middleware/events.js
2021-04-06 10:53:14 -07:00

51 lines
1.3 KiB
JavaScript

const express = require('express')
const { omit } = require('lodash')
const Ajv = require('ajv')
const schema = require('../lib/schema-event')
const FailBot = require('../lib/failbot')
const OMIT_FIELDS = ['type', 'token']
const ajv = new Ajv()
const router = express.Router()
router.post('/', async function postEvents (req, res, next) {
const isDev = process.env.NODE_ENV === 'development'
const fields = omit(req.body, '_csrf')
if (!ajv.validate(schema, fields)) {
if (isDev) console.log(ajv.errorsText())
return res.status(400).json({})
}
if (req.hydro.maySend()) {
// intentionally don't await this async request
// so that the http response afterwards is sent immediately
req.hydro.publish(
req.hydro.schemas[fields.type],
omit(fields, OMIT_FIELDS)
).then(async (hydroRes) => {
if (!hydroRes.ok) {
const err = new Error('Hydro request failed')
err.status = hydroRes.status
err.path = fields.path
await FailBot.report(err, {
path: fields.path,
hydroStatus: hydroRes.status,
hydroText: await hydroRes.text()
})
throw err
}
}).catch((e) => {
if (isDev) console.error(e)
})
}
return res.status(201).json(fields)
})
module.exports = router