Update next-connect middleware to return next() to fix error handling

This commit is contained in:
Aaron William Po
2023-07-17 15:44:32 -04:00
parent 8ed798ce60
commit bfbf7c3cf3
9 changed files with 167 additions and 50 deletions

View File

@@ -38,7 +38,7 @@ const checkIfCommentOwner = async (
throw new ServerError('You are not authorized to modify this comment', 403);
}
await next();
return next();
};
const editComment = async (
@@ -53,7 +53,7 @@ const editComment = async (
id,
});
return res.status(200).json({
res.status(200).json({
success: true,
message: 'Comment updated successfully',
statusCode: 200,

View File

@@ -1,7 +1,6 @@
import getCurrentUser from '@/config/nextConnect/middleware/getCurrentUser';
import getBeerPostById from '@/services/BeerPost/getBeerPostById';
import { UserExtendedNextApiRequest } from '@/config/auth/types';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
import editBeerPostById from '@/services/BeerPost/editBeerPostById';
import EditBeerPostValidationSchema from '@/services/BeerPost/schema/EditBeerPostValidationSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
@@ -10,6 +9,8 @@ import { createRouter, NextHandler } from 'next-connect';
import { z } from 'zod';
import ServerError from '@/config/util/ServerError';
import DBClient from '@/prisma/DBClient';
import validateRequest from '@/config/nextConnect/middleware/validateRequest';
import NextConnectOptions from '@/config/nextConnect/NextConnectOptions';
interface BeerPostRequest extends UserExtendedNextApiRequest {
query: { id: string };
@@ -37,7 +38,7 @@ const checkIfBeerPostOwner = async (
throw new ServerError('You cannot edit that beer post.', 403);
}
next();
return next();
};
const editBeerPost = async (
@@ -82,8 +83,21 @@ const router = createRouter<
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
>();
router.put(getCurrentUser, checkIfBeerPostOwner, editBeerPost);
router.delete(getCurrentUser, checkIfBeerPostOwner, deleteBeerPost);
router.put(
validateRequest({
bodySchema: EditBeerPostValidationSchema,
querySchema: z.object({ id: z.string() }),
}),
getCurrentUser,
checkIfBeerPostOwner,
editBeerPost,
);
router.delete(
validateRequest({ querySchema: z.object({ id: z.string() }) }),
getCurrentUser,
checkIfBeerPostOwner,
deleteBeerPost,
);
const handler = router.handler(NextConnectOptions);

View File

@@ -35,7 +35,7 @@ const checkIfBreweryPostOwner = async (
throw new ServerError('You are not the owner of this brewery post', 403);
}
next();
return next();
};
const editBreweryPost = async (
@@ -64,9 +64,7 @@ const deleteBreweryPost = async (req: BreweryPostRequest, res: NextApiResponse)
query: { id },
} = req;
const deleted = await DBClient.instance.beerPost.delete({
where: { id },
});
const deleted = await DBClient.instance.breweryPost.delete({ where: { id } });
if (!deleted) {
throw new ServerError('Brewery post not found', 404);

View File

@@ -39,7 +39,7 @@ const checkIfCommentOwner = async (
throw new ServerError('You are not authorized to modify this comment', 403);
}
await next();
return next();
};
const editComment = async (

View File

@@ -46,7 +46,7 @@ const checkIfUserCanEditUser = async (
throw new ServerError('You are not permitted to modify this user', 403);
}
await next();
return next();
};
const editUser = async (