Move asyncController calls to controller files
This commit is contained in:
parent
ff640451be
commit
9e62d4c114
@ -5,6 +5,7 @@ import express from 'express';
|
||||
import * as userService from '../services/user';
|
||||
import * as passwordResetService from '../services/passwordReset';
|
||||
import { TokenVerificationError } from '../services/passwordReset';
|
||||
import asyncController from '../routes/asyncController';
|
||||
|
||||
function createUser(req, res) {
|
||||
const user = req.body;
|
||||
@ -64,7 +65,7 @@ function deleteCurrentUser(req, res) {
|
||||
});
|
||||
}
|
||||
|
||||
async function resetPassword(req: express.Request, res: express.Response) {
|
||||
const resetPassword = asyncController(async function(req: express.Request, res: express.Response) {
|
||||
if(req.body == undefined || (req.body.email == undefined && req.body.token == undefined)) {
|
||||
return res.send({ error: 'Expected an email address or password reset token in the request body' });
|
||||
}
|
||||
@ -94,7 +95,7 @@ async function resetPassword(req: express.Request, res: express.Response) {
|
||||
|
||||
return res.send({ success: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export default {
|
||||
createUser,
|
||||
|
8
app/src/api/routes/asyncController.ts
Normal file
8
app/src/api/routes/asyncController.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
export default function asyncController(fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
Promise.resolve(fn(req, res, next))
|
||||
.catch(next);
|
||||
};
|
||||
}
|
@ -2,12 +2,6 @@ import express from 'express';
|
||||
|
||||
import userController from '../controllers/userController';
|
||||
|
||||
const asyncMiddleware = fn =>
|
||||
(req, res, next) => {
|
||||
Promise.resolve(fn(req, res, next))
|
||||
.catch(next);
|
||||
};
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/', userController.createUser);
|
||||
@ -16,6 +10,6 @@ router.route('/me')
|
||||
.get(userController.getCurrentUser)
|
||||
.delete(userController.deleteCurrentUser);
|
||||
|
||||
router.put('/password', asyncMiddleware(userController.resetPassword));
|
||||
router.put('/password', userController.resetPassword);
|
||||
|
||||
export default router;
|
Loading…
Reference in New Issue
Block a user