Skip to main content
Back to blog// web development

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

NK

Nicanor Korir

Author

July 31, 2024
6 min read
nestjsdecoratorsjavascripttypescriptbackend

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

TypeScript
typescript
1
2
3
4
5
6
7
8
9

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.

  1. Define the Decorator:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
  1. Use the Decorator:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
10

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.

  1. Define the Decorator:
TypeScript
typescript
1
2
3
4
  1. Use the Decorator:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
10
11
  1. Create a Guard to Use the Metadata:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  1. Apply the Guard:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

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.

  1. Define the Decorator:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  1. Use the Decorator:
TypeScript
typescript
1
2
3
4
5
6
7
8
9
10
11
12

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.