Skip to content

Commit

Permalink
Refactor & add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalushin committed Aug 19, 2021
1 parent ea48095 commit bc7e5f2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface EventSettings {
}

/**
* A static object with keys corresponding to event types and values holding corresponding SQS Queues and JSON Schemas
* A static singleton object with keys corresponding to event types and values holding corresponding SQS Queues
* and JSON Schemas
*/
const eventSettings: EventSettings = {
PRODUCT_UPDATED: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import eventSettings from "../eventSettings";

/**
* A lambda handler for receiving any type of message.
* If a message is successfully received, returns an empty 202 responce.
* If while processing a message a known type of error was encountered, returns
* If a message is successfully received, returns an empty 202 response.
* If while processing a message a known type of error was encountered, returns 400 response with error description.
* If an unknown error was encountered, the Error is propagated and lambda function fallbacks on default AWS behaviour.
*/
export const lambdaHandler = async (event: lambda.APIGatewayEvent): Promise<APIGatewayProxyResult> => {
try {
const { eventType, payload } = validateMessage(event.body);

if (eventSettings[eventType].runBeforeSendingToQueue) {
await eventSettings[eventType].runBeforeSendingToQueue!(payload);
}
eventSettings[eventType].runBeforeSendingToQueue?.call(null, payload);
await sendMessageToQueue(eventType, payload);

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
import eventSettings from "./eventSettings";

/**
* Pushes a payload to a corresponding queue. The queue URL is depends on the EventType and is extracted from the
* eventSettings object.
*/
const sendMessageToQueue = async (eventType: string, payload: any) => {
const sqs = new SQSClient({
region: process.env.AWS_REGION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ interface Message {
payload: any;
}

/**
* Checks that an incoming message is valid: it is of a known type and conforms to a corresponding JSON-schema.
* If there is a validation error, an error that is a subclass of ErrorWithPredefinedResponse is raised.
*/
export const validateMessage = (incomingMessage: string | null) => {
let message: Message;

Expand All @@ -17,7 +21,7 @@ export const validateMessage = (incomingMessage: string | null) => {
throw new ErrorBodyIsNotAJSON();
}

if (!message || message == null) {
if (!message) {
throw new ErrorBodyIsNotAJSON();
}

Expand Down

0 comments on commit bc7e5f2

Please sign in to comment.