UNPKG

sqs-consumer

Version:

Build SQS-based Node applications without the boilerplate

77 lines (69 loc) • 2.29 kB
import type { Message, ReceiveMessageCommandOutput } from "@aws-sdk/client-sqs"; import type { ConsumerOptions } from "./types.js"; function validateOption( option: string, value: any, allOptions: { [key: string]: any }, strict?: boolean, ): void { switch (option) { case "batchSize": if (value > 10 || value < 1) { throw new Error("batchSize must be between 1 and 10."); } break; case "heartbeatInterval": if (!allOptions.visibilityTimeout || value >= allOptions.visibilityTimeout) { throw new Error("heartbeatInterval must be less than visibilityTimeout."); } break; case "visibilityTimeout": if (allOptions.heartbeatInterval && value <= allOptions.heartbeatInterval) { throw new Error("heartbeatInterval must be less than visibilityTimeout."); } break; case "waitTimeSeconds": if (value < 1 || value > 20) { throw new Error("waitTimeSeconds must be between 0 and 20."); } break; case "pollingWaitTimeMs": if (value < 0) { throw new Error("pollingWaitTimeMs must be greater than 0."); } break; default: if (strict) { throw new Error(`The update ${option} cannot be updated`); } break; } } /** * Ensure that the required options have been set. * @param options The options that have been set by the application. */ function assertOptions(options: ConsumerOptions): void { if (!options.queueUrl) { throw new Error("Missing SQS consumer option [ queueUrl ]."); } if (!options.handleMessage && !options.handleMessageBatch) { throw new Error("Missing SQS consumer option [ handleMessage or handleMessageBatch ]."); } if (options.batchSize) { validateOption("batchSize", options.batchSize, options); } if (options.heartbeatInterval) { validateOption("heartbeatInterval", options.heartbeatInterval, options); } } /** * Determine if the response from SQS has messages in it. * @param response The response from SQS. */ function hasMessages( response: ReceiveMessageCommandOutput, ): response is ReceiveMessageCommandOutput & { Messages: Message[] } { return Boolean(response.Messages?.length); } export { hasMessages, assertOptions, validateOption };