* Start a basic exit event * Combine to one performance function * Lint * Fancy operators * Update events.js * Update events.js * Update cookie-settings.js * Add scroll tracking * Tell "standard" to use babel-eslint * Throttle scroll tracking * Lint * Use sendBeacon * Update index.js
32 lines
857 B
JavaScript
32 lines
857 B
JavaScript
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({})
|
|
}
|
|
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
|