diff --git a/package.json b/package.json index 02e77e9..eecb8d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-message-bus", - "version": "3.3.1", + "version": "3.3.2", "description": "Minimalistic and complete AMQP message bus implementation", "main": "lib/index.js", "files": [ diff --git a/src/Types/index.ts b/src/Types/index.ts index dd6a8e7..de4db90 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -2,3 +2,4 @@ export * from './config'; export * from './handler'; export * from './logger'; export * from './message'; +export * from './publisher'; diff --git a/src/Types/publisher.ts b/src/Types/publisher.ts new file mode 100644 index 0000000..eccbd69 --- /dev/null +++ b/src/Types/publisher.ts @@ -0,0 +1,7 @@ +import { Options } from 'amqplib'; +import { IMessage } from './message'; + +export interface PublishMessageOptions extends IMessage { + exchangeName?: string; + options?: Options.Publish; +} diff --git a/src/Utils/testing.ts b/src/Utils/testing.ts index 501f84c..eec5f31 100644 --- a/src/Utils/testing.ts +++ b/src/Utils/testing.ts @@ -1,5 +1,5 @@ import { LAST_MESSAGES_BUFFER_SIZE, isTestEnv } from 'Const'; -import { IMessage } from 'Types'; +import { IMessage, PublishMessageOptions } from 'Types'; import { error } from './logger'; interface LastMessagesFactory { @@ -10,7 +10,9 @@ interface LastMessagesFactory { get: () => T[]; } -const lastMessagesFactory = (): LastMessagesFactory => { +const lastMessagesFactory = < + T extends IMessage & PublishMessageOptions +>(): LastMessagesFactory => { const array: Array<{ message: T; promise?: Promise }> = []; return { push: (m: T, promise?: Promise) => { diff --git a/src/publisher.ts b/src/publisher.ts index 426f4c4..72c6c4d 100644 --- a/src/publisher.ts +++ b/src/publisher.ts @@ -1,4 +1,4 @@ -import { IMessage } from 'Types'; +import { IMessage, PublishMessageOptions } from 'Types'; import { error, log, @@ -9,19 +9,16 @@ import { Options } from 'amqplib'; import { DEFAULT_CONFIG, DEFAULT_EXCHANGE_NAME } from './Const'; import { getDefaultChannel } from './channel'; -interface Message extends IMessage { - exchangeName?: string; - options?: Options.Publish; -} - interface DirectMessage { queueName: string; options?: Options.Publish; body: MessageType['body']; } -export const publishMessage = async ( - message: Message & DataType +export const publishMessage = async < + MessageType extends IMessage = PublishMessageOptions +>( + message: PublishMessageOptions & MessageType ) => { const channel = await getDefaultChannel(); const exchangeName = @@ -52,7 +49,7 @@ export const publishMessage = async ( }; export const publishMessageToQueue = async < - DataType extends IMessage = Message + DataType extends IMessage = PublishMessageOptions >({ body, queueName,