-
-
Save wenindoubt/f940ece29a43fa5c28da67054fefc2b0 to your computer and use it in GitHub Desktop.
Revisions
-
hoangsetup revised this gist
Apr 25, 2021 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -30,9 +30,10 @@ export const apiGatewayResponseMiddleware = (options: { enableErrorLogger?: bool if (error instanceof AppError) { statusCode = error.statusCode; } if (options.enableErrorLogger) { console.error(error); } request.response = formatJSONResponse({ message: error.message }, statusCode); -
hoangsetup created this gist
Apr 25, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ 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, }; }