feat: Implement async handler for routes

This commit is contained in:
Faruk AYDIN
2024-02-26 01:02:21 +01:00
parent 51abd74304
commit 8b4aee1afa
14 changed files with 151 additions and 30 deletions

View File

@@ -38,6 +38,7 @@
"debug": "~2.6.9",
"dotenv": "^10.0.0",
"express": "~4.18.2",
"express-async-handler": "^1.2.0",
"express-basic-auth": "^1.2.1",
"express-graphql": "^0.12.0",
"fast-xml-parser": "^4.0.11",
@@ -94,6 +95,7 @@
"url": "https://github.com/automatisch/automatisch/issues"
},
"devDependencies": {
"@typescript-eslint/utils": "^7.0.2",
"nodemon": "^2.0.13",
"supertest": "^6.3.3",
"vitest": "^1.1.3"

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../../helpers/authentication.js';
import { authorizeAdmin } from '../../../../helpers/authorization.js';
import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
@@ -11,7 +12,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getAdminAppAuthClientsAction
asyncHandler(getAdminAppAuthClientsAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../../helpers/authentication.js';
import { authorizeAdmin } from '../../../../helpers/authorization.js';
import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
@@ -11,7 +12,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getPermissionsCatalogAction
asyncHandler(getPermissionsCatalogAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../../helpers/authentication.js';
import { authorizeAdmin } from '../../../../helpers/authorization.js';
import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
@@ -12,7 +13,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getRolesAction
asyncHandler(getRolesAction)
);
router.get(
@@ -20,7 +21,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getRoleAction
asyncHandler(getRoleAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../../helpers/authentication.js';
import { authorizeAdmin } from '../../../../helpers/authorization.js';
import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
@@ -12,7 +13,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getSamlAuthProvidersAction
asyncHandler(getSamlAuthProvidersAction)
);
router.get(
@@ -20,7 +21,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getSamlAuthProviderAction
asyncHandler(getSamlAuthProviderAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../../helpers/authentication.js';
import { authorizeAdmin } from '../../../../helpers/authorization.js';
import { checkIsEnterprise } from '../../../../helpers/check-is-enterprise.js';
@@ -12,7 +13,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getUsersAction
asyncHandler(getUsersAction)
);
router.get(
@@ -20,7 +21,7 @@ router.get(
authenticateUser,
authorizeAdmin,
checkIsEnterprise,
getUserAction
asyncHandler(getUserAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import { checkIsEnterprise } from '../../../helpers/check-is-enterprise.js';
import getAppAuthClientAction from '../../../controllers/api/v1/app-auth-clients/get-app-auth-client.js';
@@ -9,7 +10,7 @@ router.get(
'/:appAuthClientId',
authenticateUser,
checkIsEnterprise,
getAppAuthClientAction
asyncHandler(getAppAuthClientAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import versionAction from '../../../controllers/api/v1/automatisch/version.js';
import notificationsAction from '../../../controllers/api/v1/automatisch/notifications.js';
import infoAction from '../../../controllers/api/v1/automatisch/info.js';
@@ -6,9 +7,9 @@ import licenseAction from '../../../controllers/api/v1/automatisch/license.js';
const router = Router();
router.get('/version', versionAction);
router.get('/notifications', notificationsAction);
router.get('/info', infoAction);
router.get('/license', licenseAction);
router.get('/version', asyncHandler(versionAction));
router.get('/notifications', asyncHandler(notificationsAction));
router.get('/info', asyncHandler(infoAction));
router.get('/license', asyncHandler(licenseAction));
export default router;

View File

@@ -1,10 +1,16 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import { authorizeUser } from '../../../helpers/authorization.js';
import getFlowAction from '../../../controllers/api/v1/flows/get-flow.js';
const router = Router();
router.get('/:flowId', authenticateUser, authorizeUser, getFlowAction);
router.get(
'/:flowId',
authenticateUser,
authorizeUser,
asyncHandler(getFlowAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import checkIsCloud from '../../../helpers/check-is-cloud.js';
import getPlansAction from '../../../controllers/api/v1/payment/get-plans.ee.js';
@@ -6,7 +7,18 @@ import getPaddleInfoAction from '../../../controllers/api/v1/payment/get-paddle-
const router = Router();
router.get('/plans', authenticateUser, checkIsCloud, getPlansAction);
router.get('/paddle-info', authenticateUser, checkIsCloud, getPaddleInfoAction);
router.get(
'/plans',
authenticateUser,
checkIsCloud,
asyncHandler(getPlansAction)
);
router.get(
'/paddle-info',
authenticateUser,
checkIsCloud,
asyncHandler(getPaddleInfoAction)
);
export default router;

View File

@@ -1,4 +1,5 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import { authenticateUser } from '../../../helpers/authentication.js';
import checkIsCloud from '../../../helpers/check-is-cloud.js';
import getCurrentUserAction from '../../../controllers/api/v1/users/get-current-user.js';
@@ -7,14 +8,19 @@ import getInvoicesAction from '../../../controllers/api/v1/users/get-invoices.ee
const router = Router();
router.get('/me', authenticateUser, getCurrentUserAction);
router.get('/invoices', authenticateUser, checkIsCloud, getInvoicesAction);
router.get('/me', authenticateUser, asyncHandler(getCurrentUserAction));
router.get(
'/invoices',
authenticateUser,
checkIsCloud,
asyncHandler(getInvoicesAction)
);
router.get(
'/:userId/trial',
authenticateUser,
checkIsCloud,
getUserTrialAction
asyncHandler(getUserTrialAction)
);
export default router;

View File

@@ -1,8 +1,9 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import indexAction from '../controllers/healthcheck/index.js';
const router = Router();
router.get('/', indexAction);
router.get('/', asyncHandler(indexAction));
export default router;

View File

@@ -1,16 +1,9 @@
import { Router } from 'express';
import asyncHandler from 'express-async-handler';
import webhooksHandler from '../controllers/paddle/webhooks.ee.js';
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));
router.post('/webhooks', asyncHandler(webhooksHandler));
export default router;