Skip to content

Commit

Permalink
Producer.send message input type added
Browse files Browse the repository at this point in the history
  • Loading branch information
qvantor committed Jul 3, 2020
1 parent de781f2 commit 40aa9de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/producer.ts
Original file line number Diff line number Diff line change
@@ -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'
];
Expand Down Expand Up @@ -38,17 +38,13 @@ export class Producer {
return Number(result && result.Attributes && result.Attributes.ApproximateNumberOfMessages);
}

async send(messages: string | string[]): Promise<SendMessageBatchResultEntryList> {
async send(messages: string | Message | (string | Message)[]): Promise<SendMessageBatchResultEntryList> {
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 {
Expand All @@ -62,7 +58,7 @@ export class Producer {
}
}

private async sendBatch(failedMessages?: string[], successfulMessages?: SendMessageBatchResultEntryList, messages?: string[], startIndex?: number): Promise<SendMessageBatchResultEntryList> {
private async sendBatch(failedMessages?: string[], successfulMessages?: SendMessageBatchResultEntryList, messages?: (string | Message)[], startIndex?: number): Promise<SendMessageBatchResultEntryList> {
const endIndex = startIndex + this.batchSize;
const batch = messages.slice(startIndex, endIndex);
const params = {
Expand Down
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 40aa9de

Please sign in to comment.