npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestx-amqp

v2.0.6

Published

nestjs x amqp-connection-manager module

Downloads

36

Readme

nestx-amqp

NPM Github Workflow Status Codecov Semantic-Release

Provide an AMQP connection as NestJS Module. Internally use amqp-connection-manager.

Features

  • Provide an AmqpModule create AmqpConnectionManager async
  • Provide an injectable amqp connection manager at global
  • Provide decorators like @PublishQueue and @SubscribeQueue as method decorator for simple usage

Installation

yarn add nestx-amqp

Examples

Register Module Async

import { Module } from '@nestjs/common'
import { AmqpModule } from 'nestx-amqp'

@Module({
  imports: [
    AmqpModule.forRootAsync({
      useFactory: () => ({
        urls: ['amqp://guest:guest@localhost:5672?heartbeat=60'],
      }),
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

Inject AmqpConnectionManager

Use Symbol AMQP_CONNECTION for Injection:

Below is an abstract producer code sample.

import { Inject, OnModuleInit } from '@nestjs/common'
import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager'
import { Options } from 'amqplib'
import { AMQP_CONNECTION } from 'nestx-amqp'

export abstract class SimpleAbstractProducer implements OnModuleInit {
  channel: ChannelWrapper;

  abstract getQueue(): string;
  abstract getQueueOptions(): Options.AssertQueue;

  constructor(
    @Inject(AMQP_CONNECTION)
    readonly connectionManager: AmqpConnectionManager
  ) {}

  async onModuleInit() {
    this.channel = this.connectionManager.createChannel({
      json: true,
      setup: (channel) => channel.assertQueue(this.queue),
    })
    await this.channel.waitForConnect();
  }

  async send(message, options?: Options.Publish) {
    await this.channel.sendToQueue(this.queue, message, options);
  }
}

Advanced Usage with Decorators

Currently, only support direct queue publish and subscribe

Interface Queue

export interface Queue {
  name: string;
  options?: Options.AssertQueue;
}

export interface RetryOptions {
  maxAttempts: number;
}

export interface BaseConsumeOptions {
  prefetch: number;
  exceptionQueue?: string;
}

export type PublishQueueOptions = Options.Publish;
export type ConsumeQueueOptions = BaseConsumeOptions & Partial<RetryOptions> & Options.Consume;

@PublishQueue()

Provide a MethodDecorator easily publishing message to queue

Options:

@PublishQueue(queue: string | Queue, options?: amqplib.Options.Publish)
yourPublishQueueMethod(content:any, options?: amqplib.Options.Publish){}

Example:

(You must register and enable AmqpModule)

@Injectable()
class TestMessageService {
  queue = 'TEST.QUEUE';

  @PublishQueue(queue)
  async testPublishQueue(content) {
    console.log(`call test publish queue with ${JSON.stringify(content)}`);
  }
}

@SubscribeQueue()

Provide a MethodDecorator easily consuming message and support simply requeue logic

Options:

@SubscribeQueue(nameOrQueue: string | Queue, options?: ConsumeQueueOptions)
yourSubscribeQueueMethod(content){}

ConsumeQueueOptions:

export interface RetryOptions {
  maxAttempts: number;
}

export interface BaseConsumeOptions {
  prefetch: number;
  exceptionQueue?: string;
}

export type ConsumeQueueOptions = BaseConsumeOptions & Partial<RetryOptions>;

Example:

You must register and enable AmqpModule

@Injectable()
class TestMessageService {
  queue = 'TEST.QUEUE';

  @SubscribeQueue(queue)
  async testSubscribeQueue(content) {
    // do your business handling code
    // save db? send email?
    console.log(`handling content ${JSON.stringify(content)}`);
  }
}

Interface Exchange

import { Options } from 'amqplib'

/**
 * @desc simply wrap amqp exchange definitions as interface
 * */
export interface Exchange {
  name: string
  type: string | 'direct' | 'fanout' | 'topic' | 'headers'
  options?: Options.AssertExchange
}

/**
 * @desc wrap amqp.Channel.publish(exchange: string, routingKey: string, content, options?: Publish): boolean
 *       as interface
 * */
export interface PublishExchangeOptions {
  routingKey: string
  options?: Options.Publish
}

@PublishExchange()

Not Stable

Provide a MethodDecorator easily publishing message to exchange

Options:

@PublishExchange(exchange: string | Exchange, options?: PublishExchangeOptions)
yourPublishExchangeMethod(content:any, options?: PublishExchangeOptions){}

Example:

No Example for stable usage, you can refer to unit test case (or submit PR)

@UseAmqpConnection(name?:string)

Provide a MethodDecorator easily spec connection (when you register AmqpModule) with @PublisQueue() and @SubscribeQueue)

Recommend if you want to develop npm package using spec named connection

Example:

@Injectable()
class AmqpLoggerService {
  queue = 'LOGGER.QUEUE'

  @UseAmqpConnection('logger')
  @PublishQueue(queue)
  async logSideEffect(content) {
    // just do nothing here and auto send to LOGGER.QUEUE with spec `logger` connection
  }
}

for more details, you can refer unittest cases.

Change Log

See CHANGELOG.md

LICENSE

Released under MIT License.