diff --git a/src/utils/index.ts b/src/utils/index.ts index 6df4094..841cfeb 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1 +1 @@ -export { middyfyAPIGateway } from './middyfy'; +export { middyfy } from './middyfy'; diff --git a/src/utils/middyfy.ts b/src/utils/middyfy.ts index baadf80..64468ee 100644 --- a/src/utils/middyfy.ts +++ b/src/utils/middyfy.ts @@ -1,19 +1,22 @@ -import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda'; -import { ObjectSchema } from 'joi'; -import middy from '@middy/core'; -import httpEventNormalizer from '@middy/http-event-normalizer'; -import jsonBodyParser from '@middy/http-json-body-parser'; +import { Handler } from 'aws-lambda'; +import middy, { MiddlewareObj, MiddyfiedHandler } from '@middy/core'; -type ApiGatewayHandlerFn = ( - event: APIGatewayProxyEvent, - context: Context, -) => Promise; - -export interface MiddyfyOptions { - handler: THandler; - eventSchema?: ObjectSchema; -} - -export const middyfyAPIGateway = (options: MiddyfyOptions) => { - return middy(options.handler).use(httpEventNormalizer()).use(jsonBodyParser()); +/** + * + * @param lambdaHandler - The AWS Lambda handler function. + * @param middlewares - Optional. An array of middlewares which will be attached + * to the handler in the order in which they appear in the array. + * @returns A handler function wrapped in middleware to be used as the handler + * function for the AWS Lambda function. + */ +export const middyfy = ( + lambdaHandler: Handler, + middlewares?: MiddlewareObj[], +): MiddyfiedHandler => { + // prepare the handler so that it may have middleware(s) attached + const handler = middy(lambdaHandler); + // attach middleware(s) + middlewares?.forEach((middleware) => handler.use(middleware)); + // return the "middyfied" handler + return handler; };