Api PlatformConnectMiddlewareSerializer Middleware

Serializer Middleware

Last updated:

Configuration

The middleware is configured by defining some serializers.

serializersMiddleware(
  [
    {
      regex: /^application\/xml$/,
      serializer: (data) => `<message>${data}</message>`,
    },
    {
      regex: /^application\/json$/,
      serializer: (data) => JSON.stringify(data)
    },
    {
      regex: /^text\/plain$/,
      serializer: (data) => data
    }
  ],
  'application/json'
)

The defaultContentType (optional) option is used if the request and handler don't specify what type is wanted.

Serializer Functions

When a matching serializer is found, the Content-Type header is set and the serializer function is run.

The function is passed the entire data object, and should return a string.

If a string is returned, the body attribute of the response is updated.

If an object with a body attribute is returned, the entire response object is replaced. This is useful if you want to manipulate headers or add additional attributes in the Lambda response.

Content Type Negotiation

The header is not the only way the middleware decides which serializer to execute.

The content type is determined in the following order:

  • The Accept header via accept
  • defaultContentType middleware option

All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues