Decorators
Decorators are a feature of TypeScript and are used to extend the functionality of a class or method adding metadata to them. ExpressoTS uses decorators to refine the user experience and make the code more readable and maintainable.
List of decorators
Here is a complete list of all decorators available in ExpressoTS as well as a brief description of what they do.
- Class
- Controller
- Method
- Parameter
Class Decorators
Decorators available for your classes.
Decorator | Description |
---|---|
@provide | Binds a class to a dependency injection container as RequestScope. |
@provideSingleton | Binds a class to a dependency injection container as Singleton. |
@provideTransient | Binds a class to a dependency injection container as Transient. |
@injectable | Marks a class as available to be injected. |
@inject | Marks a class as available to be injected in the constructor of another class. |
Controller Decorators
Decorators available for your controllers.
Decorator | Description | Usage example |
---|---|---|
@controller | Marks a class as a controller. | @controller("/path", middlewares) |
@scope | Binds a controller to a scope. | @scope("Scope") |
Method Decorators
Decorators available for your controller methods.
Decorator | Description | Usage |
---|---|---|
@Get | Binds a controller method to a GET HTTP verb. | @Get("/path", "/middlewares") |
@Post | Binds a controller method to a POST HTTP verb. | @Post("/path", "/middlewares") |
@Put | Binds a controller method to a PUT HTTP verb. | @Put("/path", "/middlewares") |
@Patch | Binds a controller method to a PATCH HTTP verb. | @Patch("/path", "/middlewares") |
@Head | Binds a controller method to a HEAD HTTP verb. | @Head("/path", "/middlewares") |
@Delete | Binds a controller method to a DELETE HTTP verb. | @Delete("/path", "/middlewares") |
@Method | Binds a controller method to a specified HTTP verb. | @Method("verb", "/path", "/middlewares") |
@Http | Sets the HTTP status code for the response. | @Http(200) |
@FileUpload | Injects the file upload object for multer middleware. | @FileUpload('fieldOptions', "multerOptions") |
@Render | Renders a view using the specified view engine. | @Render("view", data ) |
@All | Binds a controller method to all HTTP verbs. | @All("/path", "/middlewares") |
Parameter Decorators
Here is a list of all parameter decorators available in ExpressoTS, along with their description and usage:
Decorator | Description | Usage |
---|---|---|
@request() | Injects the Express Request object. | execute(@request() req: Request) |
@response() | Injects the Express Response object. | execute(@response() res: Response) |
@param(paramName?: string) | Injects a parameter from the request URL path. | execute(@param('id') id: string) |
@query(paramName?: string) | Injects a parameter from the request URL query string. | execute(@query('searchTerm') searchTerm: string) |
@body() | Injects the request body payload. | execute(@body() body: MyDTO) |
@headers(headerName?: string) | Injects a header from the request headers. | execute(@headers('authorization') auth: string) |
@cookies(cookieName?: string) | Injects a cookie from the request cookies. | execute(@cookies('session') session: string) |
@next() | Injects the Express NextFunction object. | execute(@next() next: NextFunction) |
In a future release, we will add the capability to create custom decorators.
Support us ❤️
ExpressoTS is an MIT-licensed open source project. It's an independent project with ongoing development made possible thanks to your support. If you'd like to help, please read our support guide.