Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

events.js 890 B

You have to be logged in to leave a comment. Sign In
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
  1. const express = require('express')
  2. const { omit } = require('lodash')
  3. const Ajv = require('ajv')
  4. const schema = require('../lib/schema-event')
  5. const OMIT_FIELDS = ['type', 'token']
  6. const ajv = new Ajv()
  7. const router = express.Router()
  8. router.post('/', async function postEvents (req, res, next) {
  9. const isDev = process.env.NODE_ENV === 'development'
  10. const fields = omit(req.body, '_csrf')
  11. if (!ajv.validate(schema, fields)) {
  12. if (isDev) console.log(ajv.errorsText())
  13. return res.status(400).json({})
  14. }
  15. if (req.hydro.maySend()) {
  16. // intentionally don't await this async request
  17. // so that the http response afterwards is sent immediately
  18. req.hydro.publish(
  19. req.hydro.schemas[fields.type],
  20. omit(fields, OMIT_FIELDS)
  21. ).catch((e) => {
  22. if (isDev) console.error(e)
  23. })
  24. }
  25. return res.status(200).json({})
  26. })
  27. module.exports = router
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...