File size: 1,422 Bytes
3206347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
import express, { Router } from 'express';
import multer from 'multer';

import appConfig from '../config/app.js';
import webhookHandlerByFlowId from '../controllers/webhooks/handler-by-flow-id.js';
import webhookHandlerSyncByFlowId from '../controllers/webhooks/handler-sync-by-flow-id.js';
import webhookHandlerByConnectionIdAndRefValue from '../controllers/webhooks/handler-by-connection-id-and-ref-value.js';

const router = Router();
const upload = multer();

router.use(upload.none());

router.use(
  express.text({
    limit: appConfig.requestBodySizeLimit,
    verify(req, res, buf) {
      req.rawBody = buf;
    },
  })
);

const exposeError = (handler) => async (req, res, next) => {
  try {
    await handler(req, res, next);
  } catch (err) {
    next(err);
  }
};

function createRouteHandler(path, handler) {
  const wrappedHandler = exposeError(handler);

  router
    .route(path)
    .get(wrappedHandler)
    .put(wrappedHandler)
    .patch(wrappedHandler)
    .post(wrappedHandler);
}

createRouteHandler(
  '/connections/:connectionId/:refValue',
  webhookHandlerByConnectionIdAndRefValue
);
createRouteHandler(
  '/connections/:connectionId',
  webhookHandlerByConnectionIdAndRefValue
);
createRouteHandler('/flows/:flowId/sync', webhookHandlerSyncByFlowId);
createRouteHandler('/flows/:flowId', webhookHandlerByFlowId);
createRouteHandler('/:flowId', webhookHandlerByFlowId);

export default router;