This package provides TypeScript decorators for defining controllers, route methods, and middleware for Express applications.
Install using npm:
npm install @arunvaradharajalu/common.decorators
The Controller
decorator allows you to define a controller class with route handlers:
import { Controller } from '@arunvaradharajalu/common.decorators';
@Controller('/api/users')
class UserController {
// Define route handlers here using other route decorators
}
Use route method decorators like Get
, Post
, Put
, etc., to define HTTP methods and their corresponding paths:
import { Get, Post, Controller } from '@arunvaradharajalu/common.decorators';
@Controller('/api/users')
class UserController {
@Get('/')
getAllUsers(req: Request, res: Response) {
// Handle GET request to fetch all users
}
@Post('/')
createUser(req: Request, res: Response) {
// Handle POST request to create a new user
}
}
The Use
decorator adds middleware to a route handler:
import { Get, Use, Controller } from '@arunvaradharajalu/common.decorators';
@Controller('/api/users')
class UserController {
@Get('/')
@Use(authMiddleware)
getAllUsers(req: Request, res: Response) {
// This route uses authMiddleware
}
}
Contributions are welcome! Please feel free to submit a pull request or open an issue for bug fixes, improvements, or new features.
Arun Varadharajalu
This project is licensed under the ISC License.