Clean and Reusable Code in NestJS: Custom Parameter Decorators with ExecutionContext
When building real-world NestJS applications, especially e-commerce platforms, controllers can get messy if you repeatedly extract data from requests. For example, getting the current logged-in user, client IP, or session info in every route can lead to repetitive code.
NestJS provides a powerful tool called Custom Parameter Decorators. Combined with ExecutionContext, they allow you to extract, transform, and reuse data across your controllers in a clean and type-safe way.
This blog shows how to create custom decorators, why they’re useful, and practical examples for an e-commerce backend.
Why Use Custom Parameter Decorators
Avoid repeating req.user or req.headers extraction
Make controllers cleaner and easier to maintain
Work seamlessly with middleware, guards, and interceptors
Support HTTP, WebSockets, and GraphQL routes
Enhance readability for teams and code reviewers
ExecutionContext in a Nutshell
ExecutionContext gives your decorator access to:
The request and response objects
The handler (controller method) and class
The context type (HTTP, GraphQL, WebSocket)
This allows you to build versatile decorators that work in multiple NestJS modules.
GraphQL has changed how modern APIs are built. Instead of fixed REST endpoints, GraphQL allows clients to ask for exactly the data they need — no more and no less. When combined with NestJS and MongoDB, GraphQL becomes extremely powerful, scalable, and production-ready.
Optimizing the performance of a NestJS application is critical for building scalable, fast, and production-ready APIs. Even though NestJS is a high-performance framework, improper coding practices, unoptimized database queries, and lack of caching can slow down your application.
NestJS interceptors are one of the most powerful tools in the framework, enabling developers to transform responses, cache results, log performance, and optimize requests. For large-scale applications, building high-performance interceptors is essential to improve speed, maintainability, and scalability.