diff --git a/src/utils/__tests__/middyfy.test.ts b/src/utils/__tests__/middyfy.test.ts index 8507d95..32524ad 100644 --- a/src/utils/__tests__/middyfy.test.ts +++ b/src/utils/__tests__/middyfy.test.ts @@ -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 ( @@ -26,3 +32,13 @@ describe('middyfyScheduled', () => { expect(handler).toBeDefined(); }); }); + +describe('middyfySNS', () => { + const handlerFn = async (event: SNSEvent, context: Context): Promise => {}; + + it('should middyfySNS', () => { + const handler = middyfySNS({ handler: handlerFn }); + + expect(handler).toBeDefined(); + }); +}); diff --git a/src/utils/index.ts b/src/utils/index.ts index 61ed44e..f6a0d3d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,9 +3,12 @@ export { APIGatewayMiddyfyOptions, middyfyAPIGateway, middyfyScheduled, + middyfySNS, MiddyfyOptions, ScheduledHandlerFn, ScheduledMiddyfyOptions, + SNSHandlerFn, + SNSMiddyfyOptions, } from './middyfy'; export { default as ID } from './id'; diff --git a/src/utils/middyfy.ts b/src/utils/middyfy.ts index 538a760..1e2b8a8 100644 --- a/src/utils/middyfy.ts +++ b/src/utils/middyfy.ts @@ -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'; @@ -21,6 +27,11 @@ export type APIGatewayHandlerFn = ( */ export type ScheduledHandlerFn = (event: ScheduledEvent, context: Context) => Promise; +/** + * The AWS Lambda handler function signature for SNS events. + */ +export type SNSHandlerFn = (event: SNSEvent, Context: Context) => Promise; + /** * Base options for `middyfy` functions. */ @@ -45,6 +56,13 @@ export type ScheduledMiddyfyOptions = MiddyfyOptions & { eventSchema?: ObjectSchema; }; +/** + * Options for middyfied SNS event handler functions. + */ +export type SNSMiddyfyOptions = MiddyfyOptions & { + eventSchema?: ObjectSchema; +}; + /** * Wraps an AWS Gateway proxy event handler function in middleware, returning the * AWS Lambda handler function. @@ -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 }), + ); +};