NestJS Decorators
Decorators in NestJS play a crucial role in enhancing the functionality and readability of the code. They are a powerful feature that allows you to modify the behaviour of classes, methods, and proper
Nicanor Korir
Author
Decorators in NestJS play a crucial role in enhancing the functionality and readability of the code. They are a powerful feature that allows you to modify the behaviour of classes, methods, and properties in a declarative way.
Origin of Decorators
Decorators are a design pattern that originated in the world of object-oriented programming (OOP). They allow behavior to be added to individual objects, without modifying the class code. In JavaScript, decorators were proposed and moved to stage 2 as of 2021. NestJS leverages TypeScript, which supports decorators as an experimental feature, to provide this powerful functionality.
How Decorators Work in NestJS
In NestJS, decorators are used extensively to define and configure its components e.g. routes, inject dependencies, apply guards, interceptors, and much more. They are functions that add metadata to the class and its members, which NestJS uses to create the desired behaviour.
Built-in Decorators in NestJS
NestJS comes with a set of built-in decorators that cover a wide range of functionalities:
- @Controller: Defines a controller class.
- @Get, @Post, @Put, @Delete: Define route handlers for various HTTP methods.
- @Injectable: Marks a class as a provider that can be injected into other classes.
- @Param, @Query, @Body, @Headers: Extract parameters from the request.
- @UseGuards: Apply guards to a route or controller.
- @UseInterceptors: Apply interceptors to a route or controller.
- @UsePipes: Apply pipes for data transformation and validation.
Example: Using Built-in Decorators
Creating Custom Decorators
Custom decorators in NestJS are simple to create and can be very powerful. They allow you to encapsulate repetitive logic and reuse it across your application.
Creating a Custom Parameter Decorator
Custom parameter decorators can be used to extract specific data from the request.
Example: Current User Decorator
Let's create a custom decorator to extract the current user from the request.
- Define the Decorator:
- Use the Decorator:
Creating a Custom Class Decorator
Custom class decorators can be used to add metadata to classes.
Example: Roles Decorator
Let's create a custom decorator to specify roles for a controller.
- Define the Decorator:
- Use the Decorator:
- Create a Guard to Use the Metadata:
- Apply the Guard:
Creating a Custom Method Decorator
Custom method decorators can modify the behaviour of methods.
Example: Log Execution Time Decorator
Let's create a custom decorator to log the execution time of a method.
- Define the Decorator:
- Use the Decorator:
Decorators in NestJS are a powerful feature that enhances the readability and functionality of your code. Understanding built-in decorators and creating custom ones can help you build more modular, reusable, and maintainable applications. Whether extracting parameters from requests, adding metadata to classes, or modifying method behaviour, decorators provide a flexible and declarative way to extend your application's capabilities.
Stay in the Loop
Get occasional updates on AI engineering, robotics projects, and lessons from building startups. No spam, unsubscribe anytime.