Merge pull request #1510 from automatisch/routes-js

feat: Convert routes folder to the js files
This commit is contained in:
Ömer Faruk Aydın
2024-01-04 17:48:26 +01:00
committed by GitHub
4 changed files with 37 additions and 33 deletions

View File

@@ -0,0 +1,16 @@
import { Router } from 'express';
import webhooksHandler from '../controllers/paddle/webhooks.ee';
const router = Router();
const exposeError = (handler) => async (req, res, next) => {
try {
await handler(req, res, next);
} catch (err) {
next(err);
}
};
router.post('/webhooks', exposeError(webhooksHandler));
export default router;

View File

@@ -1,19 +0,0 @@
import { Response, Router, NextFunction, RequestHandler } from 'express';
import { IRequest } from '@automatisch/types';
import webhooksHandler from '../controllers/paddle/webhooks.ee';
const router = Router();
const exposeError =
(handler: RequestHandler) =>
async (req: IRequest, res: Response, next: NextFunction) => {
try {
await handler(req, res, next);
} catch (err) {
next(err);
}
};
router.post('/webhooks', exposeError(webhooksHandler));
export default router;

View File

@@ -1,7 +1,6 @@
import express, { Response, Router, NextFunction, RequestHandler } from 'express';
import express, { Router } from 'express';
import multer from 'multer';
import { IRequest } from '@automatisch/types';
import appConfig from '../config/app';
import webhookHandlerByFlowId from '../controllers/webhooks/handler-by-flow-id';
import webhookHandlerByConnectionIdAndRefValue from '../controllers/webhooks/handler-by-connection-id-and-ref-value';
@@ -11,22 +10,24 @@ const upload = multer();
router.use(upload.none());
router.use(express.text({
router.use(
express.text({
limit: appConfig.requestBodySizeLimit,
verify(req, res, buf) {
(req as IRequest).rawBody = buf;
req.rawBody = buf;
},
}));
})
);
const exposeError = (handler: RequestHandler) => async (req: IRequest, res: Response, next: NextFunction) => {
const exposeError = (handler) => async (req, res, next) => {
try {
await handler(req, res, next);
} catch (err) {
next(err);
}
}
};
function createRouteHandler(path: string, handler: (req: IRequest, res: Response, next: NextFunction) => void) {
function createRouteHandler(path, handler) {
const wrappedHandler = exposeError(handler);
router
@@ -35,10 +36,16 @@ function createRouteHandler(path: string, handler: (req: IRequest, res: Response
.put(wrappedHandler)
.patch(wrappedHandler)
.post(wrappedHandler);
};
}
createRouteHandler('/connections/:connectionId/:refValue', webhookHandlerByConnectionIdAndRefValue);
createRouteHandler('/connections/:connectionId', webhookHandlerByConnectionIdAndRefValue);
createRouteHandler(
'/connections/:connectionId/:refValue',
webhookHandlerByConnectionIdAndRefValue
);
createRouteHandler(
'/connections/:connectionId',
webhookHandlerByConnectionIdAndRefValue
);
createRouteHandler('/flows/:flowId', webhookHandlerByFlowId);
createRouteHandler('/:flowId', webhookHandlerByFlowId);