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 1.3 KB

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  1. const express = require('express')
  2. const { omit } = require('lodash')
  3. const Ajv = require('ajv')
  4. const schema = require('../lib/schema-event')
  5. const FailBot = require('../lib/failbot')
  6. const OMIT_FIELDS = ['type', 'token']
  7. const ajv = new Ajv()
  8. const router = express.Router()
  9. router.post('/', async (req, res, next) => {
  10. const isDev = process.env.NODE_ENV === 'development'
  11. const fields = omit(req.body, '_csrf')
  12. if (!ajv.validate(schema, fields)) {
  13. if (isDev) console.log(ajv.errorsText())
  14. return res.status(400).json({})
  15. }
  16. // Don't depend on Hydro on local development
  17. if (isDev && !req.hydro.maySend()) {
  18. return res.status(200).json({})
  19. }
  20. try {
  21. const hydroRes = await req.hydro.publish(
  22. req.hydro.schemas[fields.type],
  23. omit(fields, OMIT_FIELDS)
  24. )
  25. if (!hydroRes.ok) {
  26. const err = new Error('Hydro request failed')
  27. err.status = hydroRes.status
  28. err.path = fields.path
  29. await FailBot.report(err, {
  30. path: fields.path,
  31. hydroStatus: hydroRes.status,
  32. hydroText: await hydroRes.text()
  33. })
  34. throw err
  35. }
  36. return res.status(201).json(fields)
  37. } catch (err) {
  38. if (isDev) console.error(err)
  39. return res.status(502).json({})
  40. }
  41. })
  42. module.exports = router
Tip!

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

Comments

Loading...