init: init project
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { configs } from "../configs";
|
||||
import { AppError } from "../utils/app_error";
|
||||
import { jwtHelpers, JwtPayloadType } from "../utils/JWT";
|
||||
|
||||
type Role = "ADMIN" | "USER";
|
||||
|
||||
const auth = (...roles: Role[]) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const token = req.headers.authorization || req.cookies.access_token;
|
||||
if (!token) {
|
||||
throw new AppError("You are not authorize!!", 401);
|
||||
}
|
||||
const verifiedUser = jwtHelpers.verifyToken(
|
||||
token,
|
||||
configs.jwt.access_token as string,
|
||||
);
|
||||
if (!roles.length || !roles.includes(verifiedUser.role)) {
|
||||
throw new AppError("You are not authorize!!", 401);
|
||||
}
|
||||
req.user = verifiedUser as JwtPayloadType;
|
||||
next();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default auth;
|
||||
@@ -0,0 +1,51 @@
|
||||
import { ErrorRequestHandler } from "express";
|
||||
import { ZodError } from "zod";
|
||||
import { configs } from "../configs";
|
||||
import handleZodError from "../errors/zodError";
|
||||
import { TErrorSources } from "../types/error";
|
||||
import { AppError } from "../utils/app_error";
|
||||
|
||||
const globalErrorHandler: ErrorRequestHandler = (err, req, res, next) => {
|
||||
let statusCode = 500;
|
||||
let message = "Something went wrong!";
|
||||
let errorSources: TErrorSources = [
|
||||
{
|
||||
path: "",
|
||||
message: "Something went wrong",
|
||||
},
|
||||
];
|
||||
|
||||
if (err instanceof ZodError) {
|
||||
const simplifiedError = handleZodError(err);
|
||||
statusCode = simplifiedError?.statusCode;
|
||||
message = simplifiedError?.message;
|
||||
errorSources = simplifiedError?.errorSources;
|
||||
} else if (err instanceof AppError) {
|
||||
statusCode = err?.statusCode;
|
||||
message = err.message;
|
||||
errorSources = [
|
||||
{
|
||||
path: "",
|
||||
message: err?.message,
|
||||
},
|
||||
];
|
||||
} else if (err instanceof Error) {
|
||||
message = err.message;
|
||||
errorSources = [
|
||||
{
|
||||
path: "",
|
||||
message: err?.message,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
res.status(statusCode).json({
|
||||
success: false,
|
||||
message,
|
||||
errorSources,
|
||||
err,
|
||||
stack: configs.env === "development" ? err?.stack : null,
|
||||
});
|
||||
};
|
||||
|
||||
export default globalErrorHandler;
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
|
||||
const notFound = (req: Request, res: Response, next: NextFunction) => {
|
||||
res.status(404).json({
|
||||
message: 'Sorry Route is not found!! 😴😴😴',
|
||||
success: false,
|
||||
error: '',
|
||||
});
|
||||
};
|
||||
export default notFound;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
|
||||
const RequestValidator = (schema: any) => {
|
||||
return async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
req.body = await schema.parseAsync(req.body);
|
||||
next();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default RequestValidator;
|
||||
@@ -0,0 +1,16 @@
|
||||
import multer from "multer";
|
||||
import path from "path";
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, path.join(process.cwd(), "uploads"))
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
|
||||
cb(null, file.originalname)
|
||||
}
|
||||
})
|
||||
|
||||
const uploader = multer({ storage: storage })
|
||||
|
||||
export default uploader;
|
||||
Reference in New Issue
Block a user