aws-lambda-router icon indicating copy to clipboard operation
aws-lambda-router copied to clipboard

Pass rawBody instead of parsed

Open cvanputt opened this issue 3 years ago • 0 comments

I customized this module locally and added a jsonParse flag to the routes so that I can have the option to parse or not parse a JSON event body. Since I don't know typescript I can't do a pull request on your code but this is a very quick to add and useful feature.

example route:

routes: [
      {
        path: '/somepath',
        method: 'POST',
        action: async (event) => {
         //some function
          };
          return response;
        },
       jsonParse: true
      }
]

code change (javascript proxyIntegration.js line 80):

    if (event.body && actionConfig.jsonParse) {
      try {
        proxyEvent.body = JSON.parse(event.body);
      } catch (parseError) {
        console.log(`Could not parse body as json: ${event.body}`, parseError);
        return {
          statusCode: 400,
          headers,
          body: JSON.stringify({ message: 'body is not a valid JSON', error: 'ParseError' }),
        };
      }
    } else proxyEvent.body = event.body;

cvanputt avatar Mar 05 '21 01:03 cvanputt