From 40aa9de9c38c435f624b2631a22a0d218e129585 Mon Sep 17 00:00:00 2001 From: SergNikolaev Date: Fri, 3 Jul 2020 16:25:24 +0300 Subject: [PATCH] Producer.send message input type added --- src/producer.ts | 14 +++++--------- src/types.ts | 10 +++++----- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/producer.ts b/src/producer.ts index 9956aa1..03c1342 100644 --- a/src/producer.ts +++ b/src/producer.ts @@ -1,6 +1,6 @@ import { SQS } from 'aws-sdk'; import { SendMessageBatchResultEntryList } from 'aws-sdk/clients/sqs'; -import { toEntry } from './types'; +import { Message, toEntry } from './types'; const requiredOptions = [ 'queueUrl' ]; @@ -38,17 +38,13 @@ export class Producer { return Number(result && result.Attributes && result.Attributes.ApproximateNumberOfMessages); } - async send(messages: string | string[]): Promise { + async send(messages: string | Message | (string | Message)[]): Promise { const failedMessages = []; const successfulMessages = []; const startIndex = 0; + const messagesArr = !Array.isArray(messages) ? [messages] : messages; - if (!Array.isArray(messages)) { - // tslint:disable-next-line: no-parameter-reassignment - messages = [messages]; - } - - return this.sendBatch(failedMessages, successfulMessages, messages, startIndex); + return this.sendBatch(failedMessages, successfulMessages, messagesArr, startIndex); } private validate(options: ProducerOptions): void { @@ -62,7 +58,7 @@ export class Producer { } } - private async sendBatch(failedMessages?: string[], successfulMessages?: SendMessageBatchResultEntryList, messages?: string[], startIndex?: number): Promise { + private async sendBatch(failedMessages?: string[], successfulMessages?: SendMessageBatchResultEntryList, messages?: (string | Message)[], startIndex?: number): Promise { const endIndex = startIndex + this.batchSize; const batch = messages.slice(startIndex, endIndex); const params = { diff --git a/src/types.ts b/src/types.ts index 65539cb..c80ef35 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,13 +2,13 @@ import { SQS } from 'aws-sdk'; import { SendMessageBatchRequestEntry } from 'aws-sdk/clients/sqs'; const { isObject, isString, isMessageAttributeValid } = require('./validation'); -interface Message { +export interface Message { id: string; body: string; - groupId: string; - deduplicationId: string; - delaySeconds: number; - messageAttributes: SQS.MessageBodyAttributeMap; + groupId?: string; + deduplicationId?: string; + delaySeconds?: number; + messageAttributes?: SQS.MessageBodyAttributeMap; } function entryFromObject(message: Message): SendMessageBatchRequestEntry {