Snippets/elasticmq/receive-test.ts

53 lines
No EOL
1.5 KiB
TypeScript

import {
SQSClient,
ReceiveMessageCommand,
DeleteMessageCommand,
DeleteMessageBatchCommand,
ReceiveMessageCommandInput,
DeleteMessageCommandInput,
DeleteMessageBatchCommandInput
} from "@aws-sdk/client-sqs";
import { v4 as uuid } from "uuid";
import { SqsConfig } from "./sqs.config";
const client: SQSClient = new SQSClient(
{
region: SqsConfig.region,
endpoint: SqsConfig.endpoint,
credentials: SqsConfig.credentials,
}
);
const params: ReceiveMessageCommandInput = {
AttributeNames: ["SentTimestamp"],
MaxNumberOfMessages: 10,
MessageAttributeNames: ["All"],
QueueUrl: `${SqsConfig.endpoint}/${SqsConfig.accountId}/${SqsConfig.queueName}`,
VisibilityTimeout: 20,
WaitTimeSeconds: 0,
};
const command: ReceiveMessageCommand = new ReceiveMessageCommand(params);
client.send(command).then((response) => {
if (response.Messages) {
console.log(JSON.stringify(response.Messages, null, 2));
const deleteParams: DeleteMessageBatchCommandInput = {
QueueUrl: `${SqsConfig.endpoint}/${SqsConfig.accountId}/${SqsConfig.queueName}`,
Entries: response.Messages.map((message) => ({
ReceiptHandle: message.ReceiptHandle,
Id: uuid(),
}))
};
client.send(new DeleteMessageBatchCommand(deleteParams)).then(() => {
console.log("Message deleted");
}).catch((error) => {
console.log(error);
});
}
}).catch((error) => {
console.error(error);
});