mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Update next-connect, begin work on cloud img upload
This commit is contained in:
@@ -1,22 +1,15 @@
|
||||
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { Options } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
import ServerError from '../util/ServerError';
|
||||
|
||||
const NextConnectConfig: Options<
|
||||
NextApiRequest,
|
||||
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
||||
> = {
|
||||
onNoMatch(req, res) {
|
||||
const NextConnectOptions = {
|
||||
onNoMatch(req: NextApiRequest, res: NextApiResponse) {
|
||||
res.status(405).json({
|
||||
message: 'Method not allowed.',
|
||||
statusCode: 405,
|
||||
success: false,
|
||||
});
|
||||
},
|
||||
onError(error, req, res) {
|
||||
onError(error: unknown, req: NextApiRequest, res: NextApiResponse) {
|
||||
const message = error instanceof Error ? error.message : 'Internal server error.';
|
||||
const statusCode = error instanceof ServerError ? error.statusCode : 500;
|
||||
res.status(statusCode).json({
|
||||
@@ -27,4 +20,4 @@ const NextConnectConfig: Options<
|
||||
},
|
||||
};
|
||||
|
||||
export default NextConnectConfig;
|
||||
export default NextConnectOptions;
|
||||
25
config/nextConnect/middleware/getCurrentUser.ts
Normal file
25
config/nextConnect/middleware/getCurrentUser.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import findUserById from '@/services/User/findUserById';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import { getLoginSession } from '../../auth/session';
|
||||
import { UserExtendedNextApiRequest } from '../../auth/types';
|
||||
|
||||
/** Get the current user from the session. Adds the user to the request object. */
|
||||
const getCurrentUser = async (
|
||||
req: UserExtendedNextApiRequest,
|
||||
res: NextApiResponse,
|
||||
next: NextHandler,
|
||||
) => {
|
||||
const session = await getLoginSession(req);
|
||||
const user = await findUserById(session?.id);
|
||||
|
||||
if (!user) {
|
||||
throw new ServerError('Could not get user.', 401);
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
return next();
|
||||
};
|
||||
|
||||
export default getCurrentUser;
|
||||
48
config/nextConnect/middleware/validateRequest.ts
Normal file
48
config/nextConnect/middleware/validateRequest.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Middleware to validate the request body and/or query against a zod schema.
|
||||
*
|
||||
* @example
|
||||
* const handler = nextConnect(NextConnectConfig).post(
|
||||
* validateRequest({ bodySchema: BeerPostValidationSchema }),
|
||||
* getCurrentUser,
|
||||
* createBeerPost,
|
||||
* );
|
||||
*
|
||||
* @param args
|
||||
* @param args.bodySchema The body schema to validate against.
|
||||
* @param args.querySchema The query schema to validate against.
|
||||
* @throws ServerError with status code 400 if the request body or query is invalid.
|
||||
*/
|
||||
const validateRequest =
|
||||
({
|
||||
bodySchema,
|
||||
querySchema,
|
||||
}: {
|
||||
bodySchema?: z.ZodSchema<any>;
|
||||
querySchema?: z.ZodSchema<any>;
|
||||
}) =>
|
||||
async (req: NextApiRequest, res: NextApiResponse, next: NextHandler) => {
|
||||
if (bodySchema) {
|
||||
const parsed = bodySchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
throw new ServerError('Invalid request body.', 400);
|
||||
}
|
||||
}
|
||||
|
||||
if (querySchema) {
|
||||
const parsed = querySchema.safeParse(req.query);
|
||||
if (!parsed.success) {
|
||||
throw new ServerError('Invalid request query.', 400);
|
||||
}
|
||||
req.query = parsed.data;
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
export default validateRequest;
|
||||
Reference in New Issue
Block a user