Skip to content

Commit

Permalink
Extend typings for testing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaeverywhere committed Sep 4, 2023
1 parent c0c922b commit 9d42c8c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
1 change: 1 addition & 0 deletions src/Types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './config';
export * from './handler';
export * from './logger';
export * from './message';
export * from './publisher';
7 changes: 7 additions & 0 deletions src/Types/publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Options } from 'amqplib';
import { IMessage } from './message';

export interface PublishMessageOptions extends IMessage {
exchangeName?: string;
options?: Options.Publish;
}
6 changes: 4 additions & 2 deletions src/Utils/testing.ts
Original file line number Diff line number Diff line change
@@ -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<T extends IMessage> {
Expand All @@ -10,7 +10,9 @@ interface LastMessagesFactory<T extends IMessage> {
get: () => T[];
}

const lastMessagesFactory = <T extends IMessage>(): LastMessagesFactory<T> => {
const lastMessagesFactory = <
T extends IMessage & PublishMessageOptions
>(): LastMessagesFactory<T> => {
const array: Array<{ message: T; promise?: Promise<any> }> = [];
return {
push: (m: T, promise?: Promise<any>) => {
Expand Down
15 changes: 6 additions & 9 deletions src/publisher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IMessage } from 'Types';
import { IMessage, PublishMessageOptions } from 'Types';
import {
error,
log,
Expand All @@ -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<MessageType extends IMessage> {
queueName: string;
options?: Options.Publish;
body: MessageType['body'];
}

export const publishMessage = async <DataType extends IMessage = Message>(
message: Message & DataType
export const publishMessage = async <
MessageType extends IMessage = PublishMessageOptions
>(
message: PublishMessageOptions & MessageType
) => {
const channel = await getDefaultChannel();
const exchangeName =
Expand Down Expand Up @@ -52,7 +49,7 @@ export const publishMessage = async <DataType extends IMessage = Message>(
};

export const publishMessageToQueue = async <
DataType extends IMessage = Message
DataType extends IMessage = PublishMessageOptions
>({
body,
queueName,
Expand Down

0 comments on commit 9d42c8c

Please sign in to comment.