Skip to content

Commit

Permalink
SLSCMN-9 middyfy SNS events (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman authored Dec 16, 2023
1 parent a56cf82 commit 331c2cc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/utils/__tests__/middyfy.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context, ScheduledEvent } from 'aws-lambda';
import {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Context,
SNSEvent,
ScheduledEvent,
} from 'aws-lambda';

import { middyfyAPIGateway, middyfyScheduled } from '../middyfy';
import { middyfyAPIGateway, middyfySNS, middyfyScheduled } from '../middyfy';

describe('middyfyAPIGateway', () => {
const handlerFn = async (
Expand All @@ -26,3 +32,13 @@ describe('middyfyScheduled', () => {
expect(handler).toBeDefined();
});
});

describe('middyfySNS', () => {
const handlerFn = async (event: SNSEvent, context: Context): Promise<void> => {};

it('should middyfySNS', () => {
const handler = middyfySNS({ handler: handlerFn });

expect(handler).toBeDefined();
});
});
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ export {
APIGatewayMiddyfyOptions,
middyfyAPIGateway,
middyfyScheduled,
middyfySNS,
MiddyfyOptions,
ScheduledHandlerFn,
ScheduledMiddyfyOptions,
SNSHandlerFn,
SNSMiddyfyOptions,
} from './middyfy';

export { default as ID } from './id';
32 changes: 31 additions & 1 deletion src/utils/middyfy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import middy from '@middy/core';
import httpEventNormalizer from '@middy/http-event-normalizer';
import jsonBodyParser from '@middy/http-json-body-parser';
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context, ScheduledEvent } from 'aws-lambda';
import {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Context,
SNSEvent,
ScheduledEvent,
} from 'aws-lambda';
import { ObjectSchema } from 'joi';

import { validator } from '../middlewares/validator-joi';
Expand All @@ -21,6 +27,11 @@ export type APIGatewayHandlerFn = (
*/
export type ScheduledHandlerFn = (event: ScheduledEvent, context: Context) => Promise<void>;

/**
* The AWS Lambda handler function signature for SNS events.
*/
export type SNSHandlerFn = (event: SNSEvent, Context: Context) => Promise<void>;

/**
* Base options for `middyfy` functions.
*/
Expand All @@ -45,6 +56,13 @@ export type ScheduledMiddyfyOptions = MiddyfyOptions<ScheduledHandlerFn> & {
eventSchema?: ObjectSchema;
};

/**
* Options for middyfied SNS event handler functions.
*/
export type SNSMiddyfyOptions = MiddyfyOptions<SNSHandlerFn> & {
eventSchema?: ObjectSchema;
};

/**
* Wraps an AWS Gateway proxy event handler function in middleware, returning the
* AWS Lambda handler function.
Expand Down Expand Up @@ -76,3 +94,15 @@ export const middyfyScheduled = (options: ScheduledMiddyfyOptions) => {
validator({ eventSchema: options.eventSchema, logger: options.logger }),
);
};

/**
* Wraps a SNS event handler function in middleware, returning the
* AWS Lambda handler function.
* @param options - The `SNSMiddyfyOptions` object.
* @returns A middyfied handler function.
*/
export const middyfySNS = (options: SNSMiddyfyOptions) => {
return middy(options.handler).use(
validator({ eventSchema: options.eventSchema, logger: options.logger }),
);
};

0 comments on commit 331c2cc

Please sign in to comment.