mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Update next-connect, begin work on cloud img upload
This commit is contained in:
27
config/cloudinary/index.ts
Normal file
27
config/cloudinary/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { v2 as cloudinary } from 'cloudinary';
|
||||
import { CloudinaryStorage } from 'multer-storage-cloudinary';
|
||||
import ServerError from '../util/ServerError';
|
||||
|
||||
const { CLOUDINARY_CLOUD_NAME, CLOUDINARY_KEY, CLOUDINARY_SECRET } = process.env;
|
||||
|
||||
if (!(CLOUDINARY_CLOUD_NAME && CLOUDINARY_KEY && CLOUDINARY_SECRET)) {
|
||||
throw new ServerError(
|
||||
'The cloudinary credentials were not found in the environment variables.',
|
||||
500,
|
||||
);
|
||||
}
|
||||
|
||||
cloudinary.config({
|
||||
cloud_name: CLOUDINARY_CLOUD_NAME,
|
||||
api_key: CLOUDINARY_KEY,
|
||||
api_secret: CLOUDINARY_SECRET,
|
||||
});
|
||||
|
||||
// @ts-expect-error
|
||||
const storage = new CloudinaryStorage({ cloudinary, params: { folder: 'BeerApp' } });
|
||||
|
||||
/** Configuration object for Cloudinary image upload. */
|
||||
const cloudinaryConfig = { cloudinary, storage };
|
||||
|
||||
export default cloudinaryConfig;
|
||||
@@ -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;
|
||||
@@ -2,8 +2,8 @@ import { NextApiResponse } from 'next';
|
||||
import { NextHandler } from 'next-connect';
|
||||
import findUserById from '@/services/User/findUserById';
|
||||
import ServerError from '@/config/util/ServerError';
|
||||
import { getLoginSession } from '../session';
|
||||
import { UserExtendedNextApiRequest } from '../types';
|
||||
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 (
|
||||
@@ -19,7 +19,7 @@ const getCurrentUser = async (
|
||||
}
|
||||
|
||||
req.user = user;
|
||||
next();
|
||||
return next();
|
||||
};
|
||||
|
||||
export default getCurrentUser;
|
||||
@@ -42,7 +42,7 @@ const validateRequest =
|
||||
req.query = parsed.data;
|
||||
}
|
||||
|
||||
next();
|
||||
return next();
|
||||
};
|
||||
|
||||
export default validateRequest;
|
||||
Reference in New Issue
Block a user