mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
Adds a validation schema for the application's environment variables using the Zod library. The parsed environment variables are then exported as constants that can be imported throughout the application, replacing the direct use of process.env.
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
|
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
import type { RequestHandler } from 'next-connect/dist/types/node';
|
|
import type { HandlerOptions } from 'next-connect/dist/types/types';
|
|
import { z } from 'zod';
|
|
import logger from '../pino/logger';
|
|
|
|
import ServerError from '../util/ServerError';
|
|
import { NODE_ENV } from '../env';
|
|
|
|
type NextConnectOptionsT = HandlerOptions<
|
|
RequestHandler<
|
|
NextApiRequest,
|
|
NextApiResponse<z.infer<typeof APIResponseValidationSchema>>
|
|
>
|
|
>;
|
|
|
|
const NextConnectOptions: NextConnectOptionsT = {
|
|
onNoMatch(req, res) {
|
|
res.status(405).json({
|
|
message: 'Method not allowed.',
|
|
statusCode: 405,
|
|
success: false,
|
|
});
|
|
},
|
|
onError(error, req, res) {
|
|
if (NODE_ENV !== 'production') {
|
|
logger.error(error);
|
|
}
|
|
|
|
const message = error instanceof Error ? error.message : 'Internal server error.';
|
|
const statusCode = error instanceof ServerError ? error.statusCode : 500;
|
|
res.status(statusCode).json({
|
|
message,
|
|
statusCode,
|
|
success: false,
|
|
});
|
|
},
|
|
};
|
|
|
|
export default NextConnectOptions;
|