Skip to content

Commit

Permalink
refactor: abbreviations (#244)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename all `AMQP` prefix to `Amqp`.

- add docker-compose.yml for local dev
- update rabbitmq local dev url (update user/pass to default value resepect official image)
- update example in README.md
  • Loading branch information
aquariuslt authored Oct 3, 2020
1 parent 0c89b65 commit a49dad9
Show file tree
Hide file tree
Showing 17 changed files with 180 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"semanticCommits": true,
"semanticCommits": "enabled",
"packageRules": [
{
"updateTypes": ["minor", "patch", "pin", "digest"],
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ jobs:
services:
rabbitmq:
image: rabbitmq:3-management
env:
RABBITMQ_DEFAULT_PASS: devuser
RABBITMQ_DEFAULT_USER: devuser
container:
image: node:lts
env:
Expand Down
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Provide an `AMQP` connection as NestJS Module. Internally use [amqp-connection-m

## Features

- Provide an `AMQPModule` create `AmqpConnectionManager` async
- 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

Expand All @@ -31,13 +31,13 @@ yarn add nestx-amqp

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

@Module({
imports: [
AMQPModule.forRootAsync({
AmqpModule.forRootAsync({
useFactory: () => ({
urls: ['amqp://devuser:devuser@localhost:5672?heartbeat=60'],
urls: ['amqp://guest:guest@localhost:5672?heartbeat=60'],
}),
}),
],
Expand All @@ -49,7 +49,7 @@ export class AppModule {}

<br/>

### Inject AMQPConnectionManager
### Inject AmqpConnectionManager

Use Symbol `AMQP_CONNECTION` for Injection:

Expand All @@ -62,10 +62,10 @@ import { Options } from 'amqplib'
import { AMQP_CONNECTION } from 'nestx-amqp'

export abstract class SimpleAbstractProducer implements OnModuleInit {
channel: ChannelWrapper
channel: ChannelWrapper;

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

constructor(
@Inject(AMQP_CONNECTION)
Expand All @@ -77,11 +77,11 @@ export abstract class SimpleAbstractProducer implements OnModuleInit {
json: true,
setup: (channel) => channel.assertQueue(this.queue),
})
await this.channel.waitForConnect()
await this.channel.waitForConnect();
}

async send(message, options?: Options.Publish) {
await this.channel.sendToQueue(this.queue, message, options)
await this.channel.sendToQueue(this.queue, message, options);
}
}
```
Expand All @@ -96,21 +96,21 @@ export abstract class SimpleAbstractProducer implements OnModuleInit {

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

export interface RetryOptions {
maxAttempts: number
maxAttempts: number;
}

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

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

#### `@PublishQueue()`
Expand All @@ -126,16 +126,16 @@ yourPublishQueueMethod(content:any, options?: amqplib.Options.Publish){}

**Example:**

> (You must register and enable `AMQPModule`)
> (You must register and enable `AmqpModule`)
```typescript
@Injectable()
class TestMessageService {
queue = 'TEST.QUEUE'
queue = 'TEST.QUEUE';

@PublishQueue(queue)
async testPublishQueue(content) {
console.log(`call test publish queue with ${JSON.stringify(content)}`)
console.log(`call test publish queue with ${JSON.stringify(content)}`);
}
}
```
Expand All @@ -155,31 +155,31 @@ yourSubscribeQueueMethod(content){}

```typescript
export interface RetryOptions {
maxAttempts: number
maxAttempts: number;
}

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

export type ConsumeQueueOptions = BaseConsumeOptions & Partial<RetryOptions>
export type ConsumeQueueOptions = BaseConsumeOptions & Partial<RetryOptions>;
```

**Example:**

> You must register and enable `AMQPModule`
> You must register and enable `AmqpModule`
```typescript
@Injectable()
class TestMessageService {
queue = 'TEST.QUEUE'
queue = 'TEST.QUEUE';

@SubscribeQueue(queue)
async testSubscribeQueue(content) {
// do your business handling code
// save db? send email?
console.log(`handling content ${JSON.stringify(content)}`)
console.log(`handling content ${JSON.stringify(content)}`);
}
}
```
Expand Down Expand Up @@ -232,20 +232,20 @@ yourPublishExchangeMethod(content:any, options?: PublishExchangeOptions){}

<br />

#### `@UseAMQPConnection(name?:string)`
#### `@UseAmqpConnection(name?:string)`

Provide a `MethodDecorator` easily spec connection (when you register AMQPModule) with `@PublisQueue()` and `@SubscribeQueue`)
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:**

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

@UseAMQPConnection('logger')
@UseAmqpConnection('logger')
@PublishQueue(queue)
async logSideEffect(content) {
// just do nothing here and auto send to LOGGER.QUEUE with spec `logger` connection
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'

services:
rabbitmq:
image: rabbitmq:3-management
ports:
- 5672:5672
- 15672:15672
2 changes: 1 addition & 1 deletion src/__tests__/__fixtures__/amqp.test.fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const AMQP_TEST_URL = process.env.CI ? process.env.RABBITMQ_URL : 'amqp://devuser:devuser@localhost:5672';
const AMQP_TEST_URL = process.env.CI ? process.env.RABBITMQ_URL : 'amqp://guest:guest@localhost:5672';

export const AMQP_TEST_URLS = [AMQP_TEST_URL];
61 changes: 35 additions & 26 deletions src/__tests__/amqp.decorators.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Test, TestingModule } from '@nestjs/testing';
import { AMQP_CONNECTION, USE_AMQP_CONNECTION_TOKEN } from '../amqp.constants';
import { AMQPModule } from '../amqp.module';
import { UseAMQPConnection } from '../decorators/inject-connection';
import { AmqpModule } from '../amqp.module';
import { UseAmqpConnection } from '../decorators/inject-connection';
import { PublishExchange } from '../decorators/publish-exchange';
import { PublishQueue } from '../decorators/publish-queue';
import { SubscribeQueue } from '../decorators/subscribe-queue';
import { ConsumeQueueOptions } from '../interfaces/queue';
import { AMQP_TEST_URLS } from './__fixtures__/amqp.test.fixtures';
import { wait } from './__fixtures__/shared.utils';

describe('AMQP Decorators', () => {
describe('Amqp Decorators', () => {
it('# should use @PublishQueue decorator with default connection', async (done) => {
const queue = 'TEST.QUEUE';

@Injectable()
class TestPublishQueueService {
@PublishQueue(queue)
async testPublishQueue(content) {}
async testPublishQueue(content) {
}

@SubscribeQueue(queue)
async testSubscribeQueue(content) {}
async testSubscribeQueue(content) {
}
}

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand Down Expand Up @@ -56,12 +56,13 @@ describe('AMQP Decorators', () => {
}

@SubscribeQueue(queue)
async testSubscribeQueue(content) {}
async testSubscribeQueue(content) {
}
}

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand All @@ -88,18 +89,20 @@ describe('AMQP Decorators', () => {

@Injectable()
class TestPublishQueueService {
@UseAMQPConnection('log4js')
@UseAmqpConnection('log4js')
@PublishQueue(queue)
async testPublishQueue(content) {}
async testPublishQueue(content) {
}

@UseAMQPConnection('log4js')
@UseAmqpConnection('log4js')
@SubscribeQueue(queue)
async testSubscribeQueue(content) {}
async testSubscribeQueue(content) {
}
}

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
name: 'log4js',
urls: AMQP_TEST_URLS
})
Expand Down Expand Up @@ -127,7 +130,8 @@ describe('AMQP Decorators', () => {
public consumed = 0;

@PublishExchange(exchange, { routingKey })
async testPublishExchange(content) {}
async testPublishExchange(content) {
}

@SubscribeQueue(routingKey)
async testSubscribeQueue(content) {
Expand All @@ -137,7 +141,7 @@ describe('AMQP Decorators', () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand All @@ -162,7 +166,8 @@ describe('AMQP Decorators', () => {
public consumed = 0;

@PublishQueue('TEST.QUEUE.FOR.SUBSCRIBE')
async testPublishQueue(content) {}
async testPublishQueue(content) {
}

@SubscribeQueue('TEST.QUEUE.FOR.SUBSCRIBE')
async testSubscribeQueue(content) {
Expand All @@ -172,7 +177,7 @@ describe('AMQP Decorators', () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand Down Expand Up @@ -201,10 +206,12 @@ describe('AMQP Decorators', () => {
public replyCount = 0;

@PublishQueue(queue)
async testPublishQueue(content, options?) {}
async testPublishQueue(content, options?) {
}

@PublishQueue(replyQueue)
async testPublishReplyQueue(content) {}
async testPublishReplyQueue(content) {
}

@SubscribeQueue(queue)
async testSubscribeQueue(content) {
Expand All @@ -224,7 +231,7 @@ describe('AMQP Decorators', () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand Down Expand Up @@ -261,7 +268,8 @@ describe('AMQP Decorators', () => {
exceptionCount = 0;

@PublishQueue(queue)
async testPublishQueue(content, options?) {}
async testPublishQueue(content, options?) {
}

@PublishQueue(replyQueue)
async testPublishReplyQueue(content) {
Expand Down Expand Up @@ -289,7 +297,7 @@ describe('AMQP Decorators', () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand Down Expand Up @@ -329,7 +337,8 @@ describe('AMQP Decorators', () => {
exceptionCount = 0;

@PublishQueue(queue)
async testPublishQueue(content, options?) {}
async testPublishQueue(content, options?) {
}

@PublishQueue(replyQueue)
async testPublishReplyQueue(content) {
Expand Down Expand Up @@ -357,7 +366,7 @@ describe('AMQP Decorators', () => {

const module: TestingModule = await Test.createTestingModule({
imports: [
AMQPModule.register({
AmqpModule.register({
urls: AMQP_TEST_URLS
})
],
Expand Down
Loading

0 comments on commit a49dad9

Please sign in to comment.