Nascent Prototype Metaverse
Miss any of our Open RFC calls?Watch the recordings here! »

@middy/http-event-normalizer

1.4.0 • Public • Published

Middy http-event-normalizer middleware

Middy logo

HTTP error handler middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda

If you need to access the query string or path parameters in an API Gateway event you can do so by reading the attributes in event.queryStringParameters, event.multiValueQueryStringParameters and event.pathParameters, for example: event.pathParameters.userId. Unfortunately if there are no parameters for these parameter holders, the relevant key queryStringParameters, multiValueQueryStringParameters or pathParameters won't be available in the object, causing an expression like event.pathParameters.userId to fail with the error: TypeError: Cannot read property 'userId' of undefined.

A simple solution would be to add an if statement to verify if the pathParameters (or queryStringParameters/multiValueQueryStringParameters) exists before accessing one of its parameters, but this approach is very verbose and error prone.

This middleware normalizes the API Gateway event, making sure that an object for queryStringParameters, multiValueQueryStringParameters and pathParameters is always available (resulting in empty objects when no parameter is available), this way you don't have to worry about adding extra if statements before trying to read a property and calling event.pathParameters.userId will result in undefined when no path parameter is available, but not in an error.

Important note : API Gateway HTTP API format 2.0 doesn't have multiValueQueryStringParameters fields. Duplicate query strings are combined with commas and included in the queryStringParameters field.

Install

To install this middleware you can use NPM:

npm install --save @middy/http-event-normalizer

Options

  • payloadFormatVersion (number) (optional): Defaults to 1. Set it to 2 to use API Gateway HTTP API v2.0 event payload (https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html).

Sample usage

const middy = require('@middy/core')
const httpEventNormalizer = require('@middy/http-event-normalizer')
 
const handler = middy((event, context, cb) => {
  console.log(`Hello user ${event.pathParameters.userId}`) // might produce `Hello user #undefined`, but not an error
  cb(null, {})
})
 
handler.use(httpEventNormalizer())

Middy documentation and examples

For more documentation and examples, refers to the main Middy monorepo on GitHub or Middy official website.

Contributing

Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.

License

Licensed under MIT License. Copyright (c) 2017-2018 Luciano Mammino and the Middy team.

FOSSA Status

Install

npm i @middy/http-event-normalizer

DownloadsWeekly Downloads

7,692

Version

1.4.0

License

MIT

Unpacked Size

12.3 kB

Total Files

8

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar