1.0.5 โ€ข Published 5 months ago

nestjs-microservice-auto-decorators v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

NestJS Microservice Auto Decorators


Overview

nestjs-microservice-auto-decorators eliminates boilerplate code in NestJS microservice applications by providing intelligent decorators that automatically generate message patterns and handle message sending operations. This package improves developer productivity, reduces errors, and enforces consistent communication patterns across distributed systems.


Features

  • ๐Ÿ”„ AutoMessagePattern: Automatically generates a message pattern based on the controller's metadata or class name and the decorated method name.
  • ๐Ÿ“จ AutoSend: Automatically constructs the message pattern for client methods and sends payloads via a ClientProxy.
  • ๐Ÿš€ Zero Configuration: Works out-of-the-box with sensible defaults.
  • ๐Ÿ” Type Safety: Fully typed with TypeScript, ensuring robust development.
  • ๐Ÿ“Š Performance Optimized: Minimal overhead with maximum convenience.

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

Quick Start

After installation, integrate the decorators into your NestJS microservices:

  1. Import the Decorators:

    import {
      AutoMessagePattern,
      AutoSend,
    } from "nestjs-microservice-auto-decorators";
  2. Apply AutoMessagePattern to your controller methods to automatically generate message patterns.

  3. Use AutoSend in your services to dynamically send messages through a ClientProxy.


Usage Examples

Using AutoMessagePattern

The AutoMessagePattern decorator automatically derives the message pattern from your controllerโ€™s metadata or class name.

import { Controller } from "@nestjs/common";
import { AutoMessagePattern } from "nestjs-microservice-auto-decorators";

@Controller("users")
export class UsersController {
  @AutoMessagePattern()
  findAll() {
    // Automatically resolves to the pattern "users.findAll"
  }
}

Using AutoSend

The AutoSend decorator simplifies sending messages by auto-generating the message pattern and handling payload wrapping.

Example 1: findAll Without Payload

If the method does not require a payload, calling it without arguments sends an empty object.

import { Injectable, Inject } from "@nestjs/common";
import { ClientProxy } from "@nestjs/microservices";
import { AutoSend } from "nestjs-microservice-auto-decorators";

@Injectable()
export class UsersService {
  // Optionally, set the service name explicitly.
  public serviceName = "users";

  constructor(
    @Inject("USERS_CLIENT") private readonly userClient: ClientProxy
  ) {}

  @AutoSend({ clientProperty: "userClient" })
  findAll() {
    // When invoked as findAll(), it sends {} using the message pattern "users.findAll"
  }
}

Example 2: findOne With Payload

For methods that require a payload, passing a primitive value (like an ID) will automatically be wrapped in an object.

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" })
  findOne(id: string) {
    // When called as findOne('123'), it sends { id: '123' } using the pattern "users.findOne"
  }
}

API Reference

AutoMessagePattern

  • Usage: Apply to controller methods to automatically generate a message pattern.
  • How It Works:
    • Uses the controller's metadata (@Controller('xxx')) or the class name (stripping "Controller")
    • Combines it with the method name (e.g., findAll) to produce a pattern like users.findAll

AutoSend

  • Usage: Decorate service methods to auto-generate a message pattern and send payloads via a designated ClientProxy.
  • Configuration Option:
    • clientProperty: The property name where the ClientProxy instance is stored.
  • Payload Handling:
    • If the first argument is not an object, it is wrapped as { id: value }
  • Example:
    • For a service named users and method findOne, calling findOne('123') sends a message with pattern users.findOne and payload { id: '123' }.

Contributing

Contributions are welcome! Please follow these guidelines:

  • Reporting Issues:
    Open issues for bug reports or feature requests on GitHub Issues.
  • Pull Requests:
    Fork the repository, commit your changes with clear messages, and open a pull request. Include tests and update documentation where applicable.
  • Code Style:
    Adhere to TypeScript best practices and maintain consistency with existing code.

Changelog

Keep track of changes with a detailed changelog. Consider using tools like standard-version for automated changelog generation.


License

This project is licensed under the MIT License - see the LICENSE file for details.


For further details, examples, and updates, please visit our GitHub repository.

Built with โค๏ธ for the NestJS community.

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago