forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
38 lines (30 loc) · 1020 Bytes
/
events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require('express')
const { omit } = require('lodash')
const Ajv = require('ajv')
const schema = require('../lib/schema-event')
const OMIT_FIELDS = ['type', 'token']
const ajv = new Ajv()
const router = express.Router()
router.post('/', async (req, res, next) => {
const fields = omit(req.body, '_csrf')
if (!ajv.validate(schema, fields)) {
if (process.env.NODE_ENV === 'development') console.log(ajv.errorsText())
return res.status(400).json({})
}
// Don't depend on Hydro on local development
if (process.env.NODE_ENV === 'development' && !req.hydro.maySend()) {
return res.status(200).json({})
}
try {
const hydroRes = await req.hydro.publish(
req.hydro.schemas[fields.type],
omit(fields, OMIT_FIELDS)
)
if (!hydroRes.ok) return res.status(502).json({})
return res.status(201).json(fields)
} catch (err) {
if (process.env.NODE_ENV === 'development') console.log(err)
return res.status(502).json({})
}
})
module.exports = router