Skip to content

Commit

Permalink
SLSCMN-5 initial middyfy function
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarman committed Nov 14, 2023
1 parent 2ed940d commit 3abd0ca
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { middyfyAPIGateway } from './middyfy';
export { middyfy } from './middyfy';
37 changes: 20 additions & 17 deletions src/utils/middyfy.ts
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;
};

0 comments on commit 3abd0ca

Please sign in to comment.