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

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

Comments

Loading...