Author: https://github.com/kaiquye
npm i decorators-controller-express
import express from "express";
import { applyDecoratorsControllers } from "decorators-controller-express";
import { UserController } from "./src/modules/controller";
const server = express();
server.use(express.json())
const app = applyDecoratorsControllers(server, [UserController, ProfileController, ...others]);
app.listen(3000, console.log("Bomb Has Been Planted..."))
your class must extend from the ControllerBase
// CONTROLLER
import UserDto from "./dto/user-dto.ts";
import { ControllerBase, Post, ValidateBody } from "decorators-controller-express";
@Controller("/v1/user")
class UserController extends ControllerBase {
@Post("/login")
login(req, res) {
res.send(req.body);
}
}
Use @GlobalMiddleware
decorator to add middleware to your class
your class must extend from the ControllerBase
// CONTROLLER
import UserDto from "./dto/user-dto.ts";
import { ControllerBase, Post, ValidateBody } from "decorators-controller-express";
@Controller("/v1/user")
@GlobalMiddleware([AuthMiddlewareExample])
class UserController extends ControllerBase {
@Post("/login")
login(req, res) {
res.send(req.body);
}
}
Use @Middleware
decorator to add middleware to your route
- Your class must extend from the
ControllerBase
// CONTROLLER
import UserDto from "./dto/user-dto.ts";
import { ControllerBase, Post, ValidateBody } from "decorators-controller-express";
@Controller("/v1/user")
class UserController extends ControllerBase {
@Middleware(VerifyStoreMiddlewareExample)
@Post("/login")
login(req, res) {
res.send(req.body);
}
}
class-validator
npm i class-validator
your class must extend from the base DtoBase
// DTO BODY
import { IsEmail, IsString } from "class-validator";
import { DtoBase } from "decorators-controller-express";
export class UserDto extends DtoBase {
@IsEmail()
email: string;
@IsString()
password: string;
constructor({ email, password }) {
super();
this.email = email;
this.password = password;
}
}
you must call the @ValidateBody()
decorator and pass your DTO as a parameter
- your class must extend from the
ControllerBase
// CONTROLLER
import UserDto from "./dto/user-dto.ts";
import { ControllerBase, Post, ValidateBody } from "decorators-controller-express";
@Controller("/v1/user")
class UserController extends ControllerBase {
@ValidateBody(UserDto)
@ValidateParam(searchDto)
@Post("/login")
login(req, res) {
res.send(req.body);
}
}
you must call the @ValidateParam()
decorator and pass your DTO as a parameter
- your class must extend from the
ControllerBase
// CONTROLLER
import UserDto from "./dto/user-dto.ts";
import { ControllerBase, Post, ValidateBody } from "decorators-controller-express";
@Controller("/v1/user")
class UserController extends ControllerBase {
@ValidateParam(UserDto)
@Get("/:user_id")
login(req, res) {
res.send(req.body);
}
}
you must call the @ValidateQuery()
decorator and pass your DTO as a parameter
- your class must extend from the
ControllerBase
// CONTROLLER
import UserDto from "./dto/user-dto.ts";
import { ControllerBase, Post, ValidateBody } from "decorators-controller-express";
@Controller("/v1/user")
class UserController extends ControllerBase {
@ValidateQuery(UserDto)
@Get("/")
findAll(req, res) {
res.send(req.body);
}
}