Last active
June 30, 2021 20:11
-
-
Save hoangsetup/b51671027a3103954068e09a44b06493 to your computer and use it in GitHub Desktop.
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
import middy from '@middy/core'; | |
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'; | |
import { formatJSONResponse } from './apiGateway'; | |
import { AppError } from './appError'; | |
import MiddlewareFunction = middy.MiddlewareFunction; | |
export const apiGatewayResponseMiddleware = (options: { enableErrorLogger?: boolean } = {}) => { | |
const after: MiddlewareFunction<APIGatewayProxyEvent, any> = async (request) => { | |
if (!request.event?.httpMethod || request.response === undefined || request.response === null) { | |
return; | |
} | |
const existingKeys = Object.keys(request.response); | |
const isHttpResponse = existingKeys.includes('statusCode') | |
&& existingKeys.includes('headers') | |
&& existingKeys.includes('body'); | |
if (isHttpResponse) { | |
return; | |
} | |
request.response = formatJSONResponse(request.response); | |
} | |
const onError: MiddlewareFunction<APIGatewayProxyEvent, APIGatewayProxyResult> = async (request) => { | |
const { error } = request; | |
let statusCode = 500; | |
if (error instanceof AppError) { | |
statusCode = error.statusCode; | |
} | |
if (options.enableErrorLogger) { | |
console.error(error); | |
} | |
request.response = formatJSONResponse({ message: error.message }, statusCode); | |
} | |
return { | |
after, | |
onError, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment