Skip to content

Commit

Permalink
fix typo for consumer and create consumer and producer type for core …
Browse files Browse the repository at this point in the history
…package
  • Loading branch information
zgid123 committed Apr 7, 2023
1 parent d5fb6c7 commit 081fc69
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 72 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.d.ts
6 changes: 3 additions & 3 deletions examples/nestjs/src/consumer/consumer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller } from '@nestjs/common';
import {
Ctx,
Payload,
Subcribe,
Subscribe,
type RmqContext,
} from '@rabbitmq-ts/nestjs-consumer';

Expand All @@ -14,7 +14,7 @@ interface IPayloadProps {

@Controller()
export class ConsumerController {
@Subcribe({
@Subscribe({
routingKey: ROUTE,
queue: {
exclusive: true,
Expand All @@ -30,7 +30,7 @@ export class ConsumerController {
noAck: true,
},
})
public async handleSubcribe(
public async handleSubscribe(
@Payload() data: IPayloadProps,
@Ctx() context: RmqContext,
): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rabbitmq-ts/core",
"version": "2.0.1",
"version": "2.0.2",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class Connection {
return url;
}

const { host, port, username, password, virtualHost } = url;
const { host, port = '', username, password, virtualHost } = url;

return combine(
{ joinWith: '/' },
Expand Down
18 changes: 10 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import type { Options, Replies } from 'amqplib';
import type { Replies } from 'amqplib';
import type { Options as ManagerOptions } from 'amqp-connection-manager';

export type TAssertExchange = Options.AssertExchange;

export type TRepliesAssertExchange = Replies.AssertExchange;

export type TAssertQueue = Options.AssertQueue;

export type TRepliesEmpty = Replies.Empty;

export type TPublish = ManagerOptions.Publish;

export type TConsumeOptions = ManagerOptions.Consume;

export type { Channel, ConsumeMessage } from 'amqplib';

export type {
Expand All @@ -22,4 +16,12 @@ export type {

export * from './Connection';

export type { IConnectionProps } from './interface';
export type {
TAssertQueue,
TExchangeType,
IProducerProps,
TAssertExchange,
TConsumeOptions,
ISubscribeParams,
IConnectionProps,
} from './interface';
44 changes: 43 additions & 1 deletion packages/core/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { AmqpConnectionManagerOptions } from 'amqp-connection-manager';
import type { Options } from 'amqplib';
import type {
Options as ManagerOptions,
AmqpConnectionManagerOptions,
} from 'amqp-connection-manager';

export interface IConnectionAtomProps {
host: string;
Expand All @@ -11,3 +15,41 @@ export interface IConnectionAtomProps {
export interface IConnectionProps extends AmqpConnectionManagerOptions {
urls: string | IConnectionAtomProps | (string | IConnectionAtomProps)[];
}

export type TAssertExchange = Options.AssertExchange;

export type TAssertQueue = Options.AssertQueue;

export type TConsumeOptions = ManagerOptions.Consume;

type TExchangeBaseType = 'direct' | 'topic' | 'headers' | 'fanout' | 'match';

export type TExchangeType = TExchangeBaseType | Omit<string, TExchangeBaseType>;

interface IExchangeProps extends Partial<TAssertExchange> {
name: string;
type?: TExchangeType;
}

interface IQueueProps extends Partial<TAssertQueue> {
name: string;
}

export interface ISubscribeParams {
queue: IQueueProps;
routingKey?: string;
exchange?: IExchangeProps;
consumerOptions?: TConsumeOptions;
}

export interface IProducerProps {
urls: IConnectionProps['urls'];
connectionOptions?: AmqpConnectionManagerOptions;
configurations?: {
exchanges?: {
exchange: string;
type: TExchangeType;
options?: TAssertExchange;
}[];
};
}
4 changes: 2 additions & 2 deletions packages/nestjs/consumer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ bootstrap();
```ts
// rabbit.controller.ts
import { Controller } from '@nestjs/common';
import { Ctx, Payload, Subcribe } from '@rabbitmq-ts/nestjs-consumer';
import { Ctx, Payload, Subscribe } from '@rabbitmq-ts/nestjs-consumer';

import type { RmqContext } from '@rabbitmq-ts/nestjs-consumer';

@Controller()
export class TestController {
@Subcribe({
@Subscribe({
routingKey: 'routing_key',
queue: {
name: 'queue_name',
Expand Down
2 changes: 1 addition & 1 deletion packages/nestjs/consumer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rabbitmq-ts/nestjs-consumer",
"version": "2.0.0",
"version": "2.0.1",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down
11 changes: 5 additions & 6 deletions packages/nestjs/consumer/src/RabbitMQConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import {
type ChannelWrapper,
type ConsumeMessage,
type IConnectionProps,
type ISubscribeParams,
type TRepliesAssertExchange,
} from '@rabbitmq-ts/core';

import { omit } from './utils';
import { RmqContext } from './Context';

import type { ISubcribeParams } from './decorators';

type TCallback = () => void;

interface ICreateServiceReturnProps {
Expand All @@ -29,7 +28,7 @@ export class RabbitMQConsumer
implements CustomTransportStrategy
{
#connection: Connection;
#patterns: ISubcribeParams[] = [];
#patterns: ISubscribeParams[] = [];
#channel: ChannelWrapper | undefined;

constructor(props: IConnectionProps) {
Expand All @@ -47,13 +46,13 @@ export class RabbitMQConsumer
}

public addHandler(
pattern: ISubcribeParams & { isRabbitMQ: boolean },
pattern: ISubscribeParams & { isRabbitMQ: boolean },
callback: MessageHandler,
isEventHandler = false,
extras: Record<string, any> = {},
) {
if (typeof pattern === 'object' && pattern.isRabbitMQ) {
pattern = omit(pattern, ['isRabbitMQ']) as ISubcribeParams & {
pattern = omit(pattern, ['isRabbitMQ']) as ISubscribeParams & {
isRabbitMQ: boolean;
};

Expand Down Expand Up @@ -131,7 +130,7 @@ export class RabbitMQConsumer
async #handleMessage(
message: ConsumeMessage,
channel: Channel,
pattern: ISubcribeParams,
pattern: ISubscribeParams,
): Promise<void> {
const { content } = message;
let rawMessage = content.toString();
Expand Down
30 changes: 3 additions & 27 deletions packages/nestjs/consumer/src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
import type {
TAssertQueue,
TAssertExchange,
TConsumeOptions,
} from '@rabbitmq-ts/core';
import type { ISubscribeParams } from '@rabbitmq-ts/core';

const PATTERN_METADATA = 'microservices:pattern';
const PATTERN_HANDLER_METADATA = 'microservices:handler_type';

type TExchangeBaseType = 'direct' | 'topic' | 'headers' | 'fanout' | 'match';

type TExchangeType = TExchangeBaseType | Omit<string, TExchangeBaseType>;

interface IExchangeProps extends Partial<TAssertExchange> {
name: string;
type?: TExchangeType;
}

interface IQueueProps extends Partial<TAssertQueue> {
name: string;
}

export interface ISubcribeParams {
queue: IQueueProps;
routingKey?: string;
exchange?: IExchangeProps;
consumerOptions?: TConsumeOptions;
}

export function Subcribe({
export function Subscribe({
queue,
exchange,
routingKey,
consumerOptions = {},
}: ISubcribeParams): MethodDecorator {
}: ISubscribeParams): MethodDecorator {
return (
_target: object,
_key: string | symbol,
Expand Down
2 changes: 1 addition & 1 deletion packages/nestjs/consumer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { Ctx, Payload } from '@nestjs/microservices';

export { Subcribe } from './decorators';
export { Subscribe } from './decorators';

export * from './Context';
export * from './RabbitMQConsumer';
2 changes: 1 addition & 1 deletion packages/nestjs/producer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rabbitmq-ts/nestjs-producer",
"version": "2.0.0",
"version": "2.0.1",
"license": "MIT",
"directories": {
"lib": "lib"
Expand Down
22 changes: 2 additions & 20 deletions packages/nestjs/producer/src/RabbitMQProducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,21 @@ import {
Connection,
type Channel,
type ChannelWrapper,
type TAssertExchange,
type IConnectionProps,
type AmqpConnectionManagerOptions,
type IProducerProps,
} from '@rabbitmq-ts/core';

import { CHANNEL_WRAPPER } from './constants';
import { RabbitMQModel } from './RabbitMQModel';

import type { IChannelProps } from './RabbitMQModel';

type TExchangeBaseType = 'direct' | 'topic' | 'headers' | 'fanout' | 'match';

type TExchangeType = TExchangeBaseType | Omit<string, TExchangeBaseType>;

interface IRegisterParams {
urls: IConnectionProps['urls'];
connectionOptions?: AmqpConnectionManagerOptions;
configurations?: {
exchanges?: {
exchange: string;
type: TExchangeType;
options?: TAssertExchange;
}[];
};
}

@Module({})
export class RabbitMQProducer implements OnApplicationShutdown {
public static register({
urls,
configurations = {},
connectionOptions = {},
}: IRegisterParams): DynamicModule {
}: IProducerProps): DynamicModule {
const { exchanges = [] } = configurations;

return {
Expand Down

0 comments on commit 081fc69

Please sign in to comment.