-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
21 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { middyfyAPIGateway } from './middyfy'; | ||
export { middyfy } from './middyfy'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<APIGatewayProxyResult>; | ||
|
||
export interface MiddyfyOptions<THandler> { | ||
handler: THandler; | ||
eventSchema?: ObjectSchema; | ||
} | ||
|
||
export const middyfyAPIGateway = (options: MiddyfyOptions<ApiGatewayHandlerFn>) => { | ||
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; | ||
}; |