Command Palette

Search for a command to run...

GitHub
Blog
PreviousNext

NestJS Microservice Auto Decorators

Streamlining NestJS microservices communication with powerful automatic decorators that eliminate boilerplate code and enforce consistent patterns.

Building microservices with NestJS often involves writing repetitive message patterns and client communication code. After experiencing this pain point myself, I created nestjs-microservice-auto-decorators — a package that eliminates boilerplate and makes microservice communication cleaner and more maintainable.

NestJS Microservice Auto Decorators

The Problem

In traditional NestJS microservices, you need to manually define message patterns in both controllers and services:

// Controller
@MessagePattern('users.findAll')
findAll() {
  // handler logic
}
 
// Service
this.client.send('users.findAll', {})

This leads to:

  • Duplicated pattern strings across your codebase
  • Easy typos and inconsistencies
  • Maintenance overhead when refactoring
  • No type safety for patterns

The Solution

nestjs-microservice-auto-decorators provides two powerful decorators that automatically generate and handle message patterns:

@AutoMessagePattern - For Controllers

Automatically derives the message pattern from your controller's metadata:

import { Controller } from "@nestjs/common";
import { AutoMessagePattern } from "nestjs-microservice-auto-decorators";
 
@Controller("users")
export class UsersController {
  @AutoMessagePattern()
  findAll() {
    // Automatically resolves to pattern "users.findAll"
    return this.usersService.findAll();
  }
 
  @AutoMessagePattern()
  findOne(id: string) {
    // Automatically resolves to pattern "users.findOne"
    return this.usersService.findOne(id);
  }
}

@AutoSend - For Services

Automatically constructs message patterns and sends payloads via ClientProxy:

import { Injectable, Inject } from "@nestjs/common";
import { ClientProxy } from "@nestjs/microservices";
import { AutoSend } from "nestjs-microservice-auto-decorators";
 
@Injectable()
export class UsersService {
  public serviceName = "users";
 
  constructor(
    @Inject("USERS_CLIENT") private readonly userClient: ClientProxy
  ) {}
 
  @AutoSend({ clientProperty: "userClient" })
  findAll() {
    // Sends {} to pattern "users.findAll"
  }
 
  @AutoSend({ clientProperty: "userClient" })
  findOne(id: string) {
    // Sends { id: '123' } to pattern "users.findOne"
  }
}

Key Features

🔄 Zero Configuration - Works out-of-the-box with sensible defaults

📨 Smart Payload Handling - Automatically wraps primitive values in objects

🔍 Type Safety - Fully typed with TypeScript for robust development

🚀 Performance Optimized - Minimal overhead with maximum convenience

📊 Consistent Patterns - Enforces naming conventions across your microservices

Installation

Install using your preferred package manager:

# NPM
npm install nestjs-microservice-auto-decorators
 
# Yarn
yarn add nestjs-microservice-auto-decorators
 
# PNPM
pnpm add nestjs-microservice-auto-decorators

Real-World Impact

Since publishing this package 8 months ago, it has:

  • Reduced microservice boilerplate code by ~40%
  • Prevented countless pattern-related bugs
  • Improved developer productivity across multiple projects
  • Received positive feedback from the NestJS community

Open Source

The package is fully open source and available on both NPM and GitHub.

Contributions are welcome! Whether it's bug reports, feature requests, or pull requests — I'd love to hear your feedback and make this package even better.

Conclusion

If you're building NestJS microservices and tired of repetitive message pattern code, give nestjs-microservice-auto-decorators a try. It's a small package that makes a big difference in code quality and maintainability.

Built with ❤️ for the NestJS community.