sqs-producer
Version:
Enqueues messages onto a given SQS queue
83 lines (82 loc) • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toEntry = toEntry;
const validation_js_1 = require("./validation.js");
/**
* Converts a message object to a SendMessageBatchRequestEntry
* @param message - The message to convert
* @returns The SendMessageBatchRequestEntry
* @throws Will throw an error if the message is invalid
*/
function entryFromObject(message) {
if (!message.body) {
throw new Error(`Object messages must have 'body' prop`);
}
if (!message.groupId && !message.deduplicationId && !message.id) {
throw new Error(`Object messages must have 'id' prop`);
}
if (message.deduplicationId && !message.groupId) {
throw new Error(`FIFO Queue messages must have 'groupId' prop`);
}
if (message.id) {
if (!(0, validation_js_1.isString)(message.id)) {
throw new Error("Message.id value must be a string");
}
}
const entry = {
Id: message.id,
MessageBody: message.body,
};
if (message.delaySeconds) {
if (typeof message.delaySeconds !== "number" ||
message.delaySeconds < 0 ||
message.delaySeconds > 900) {
throw new Error("Message.delaySeconds value must be a number contained within [0 - 900]");
}
entry.DelaySeconds = message.delaySeconds;
}
if (message.messageAttributes) {
if (!(0, validation_js_1.isObject)(message.messageAttributes)) {
throw new Error("Message.messageAttributes must be an object");
}
Object.values(message.messageAttributes).every(validation_js_1.isMessageAttributeValid);
entry.MessageAttributes = message.messageAttributes;
}
if (message.groupId) {
if (!(0, validation_js_1.isString)(message.groupId)) {
throw new Error("Message.groupId value must be a string");
}
entry.MessageGroupId = message.groupId;
}
if (message.deduplicationId) {
if (!(0, validation_js_1.isString)(message.deduplicationId)) {
throw new Error("Message.deduplicationId value must be a string");
}
entry.MessageDeduplicationId = message.deduplicationId;
}
return entry;
}
/**
* Converts a message string to a SendMessageBatchRequestEntry
* @param message The message to convert
*/
function entryFromString(message) {
return {
Id: message,
MessageBody: message,
};
}
/**
* Converts a message to a SendMessageBatchRequestEntry using the appropriate method
* depending on if the message is a string or an object
* @param message The message to convert
*/
function toEntry(message) {
if ((0, validation_js_1.isString)(message)) {
return entryFromString(message);
}
if ((0, validation_js_1.isObject)(message)) {
return entryFromObject(message);
}
throw new Error("A message can either be an object or a string");
}