Express Controller Decorator
About |
Features |
Technologies |
Requirements |
Starting |
License |
Author
:dart: About
A simple, lightweit npm package to bind your controllers to express
:sparkles: Features
:heavy_check_mark: uses modern TS decorators;
:heavy_check_mark: support of Middleware;
:heavy_check_mark: No need of instantiating your controllers with the
new keyword;
:rocket: Technologies
The following tools were used in this project:
:white_check_mark: Requirements
Before starting :checkered_flag:, you need to have
express and
Typescript v.4.4 or above installed.
:checkered_flag: Installing
$ npm i --save express-controller-decorator
$ yarn add express-controller-decorator
$ pnpm add express-controller-decorator
:arrow_forward: Usage
First of all, add a @Controller
decorator to your Controller. Then,
add @HTTPMethod
decorators to the methods you wish to be invoked for
each http method.
NOTE: All methods that are marked with HTTP method decorators must
return ControllerResponse
or Promise<ControllerResponse>
instance.
Request and Response args will be injected automatically
@Controller('/user')
export class SomeController {
@Post('/:id')
public getUser(req: Request, res: Response): ControllerResponse {
return new ControllerResponse(body, status, headers)
}
@Post()
public createUser(req: Request, res: Response): ControllerResponse {
return new ControllerResponse(null, 200)
}
}
The following decorators are available:
Controller(path: string)
- Decorator to mark classes that are
controllersPost(path: string = '/', ...middlewares: Middleware[])
- Method
decoratorGet(path: string = '/', ...middlewares: Middleware[])
- Method
decoratorDelete(path: string = '/', ...middlewares: Middleware[])
- Method
decoratorPut(path: string = '/', ...middlewares: Middleware[])
- Method
decoratorPatch(path: string = '/', ...middlewares: Middleware[])
- Method
decoratorHead(path: string = '/', ...middlewares: Middleware[])
- Method
decoratorFallback(...middlewares: Middleware[])
- Method decorator to mark a
fallback method. It will be invoked when no other route/method passes
Then, you need to add your controllers to the express app instance in
your main.ts
file:
const app = express()
injectControllers(app)
app.listen(3010)
NEW in 1.3: You can create a custom Context
class and make this
lib use it instead of express' Request
and Response
classes. To do
so, you need to pass your custom Context
class to setContextClass
function BEFORE calling injectControllers
function. Example:
const app = express()
class MyContext {
constructor(req: Request, res: Response, next: NextFunction) {
}
}
setContextClass(MyContext)
injectControllers(app)
app.listen(3010)
If you did specify a custom Context
class, you MUST use it instead of
express' Request
and Response
types in your controller methods.
Example:
@Controller('/user')
export class SomeController {
@Post('/:id')
public getUser(ctx: MyContext): ControllerResponse {
return new ControllerResponse(body, status, headers)
}
@Post('/:id')
public getUser(req: Request, res: Response): ControllerResponse {
return new ControllerResponse(body, status, headers)
}
}
There's also a Middleware
interface. If you wish to create a
Middleware and then use it in your decorators, you must create each
Middleware as a class implementing this interface. It has only one
method: use()
that will be invoked while using the route the
middleware sits in. Example:
interface Middleware {
use(
request: Request,
response: Response,
next: NextFunction
): void | Promise<void>
}
If you wish to add some middlewares to your Controller or to a specific
method:
@Controller('/', new Middleware1(), new Middleware2(), ...)
or
@Get('', new Middleware1(), new Middleware2(), ...)
Note! The middlewares you pass are executed before your method. THe must
implement the Middleware
interface
Example:
class SomeMiddleware implements Middleware {
use(
request: Request,
response: Response,
next: NextFunction
): void | Promise<void> {
}
}
@Controller('/auth', new SomeMiddleware())
class MyController {
@Get('/', new SomeMiddleware())
public foo(req: Request, res: Response): ControllerResponse {
}
}
:white_check_mark: Todo
- :white_check_mark: Middleware support
- :white_check_mark: Fallback route
- Generate swagger file
:memo: License
This project is under license from MIT. For more details, see the
LICENSE file
Made with :heart: by
sannnekk
Back to top