Skip to content

Commit

Permalink
fix: http-response-serializer Accept headers parse
Browse files Browse the repository at this point in the history
Fixes #1141
  • Loading branch information
willfarrell committed Nov 23, 2023
1 parent d80216c commit 15de9fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 37 deletions.
35 changes: 6 additions & 29 deletions packages/http-response-serializer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/http-response-serializer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"url": "https://github.com/sponsors/willfarrell"
},
"dependencies": {
"@hapi/accept": "6.0.3",
"@middy/util": "5.0.2"
},
"devDependencies": {
Expand Down
12 changes: 5 additions & 7 deletions website/docs/middlewares/http-response-serializer.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ npm install --save @middy/http-response-serializer
## Options

- `defaultContentType` (optional): used if the request and handler don't specify what type is wanted.

## Configuration

The middleware is configured by defining some `serializers`.
- `serializers` (array): Array for regex and serializer function.

```javascript
{
Expand Down Expand Up @@ -56,9 +53,8 @@ The header is not the only way the middleware decides which serializer to execut

The content type is determined in the following order:

- `context.requiredContentType` -- allows the handler to override everything else
- The `Accept` header via [accept](https://www.npmjs.com/package/accept)
- `context.preferredContentType` -- allows the handler to override the default, but lets the request ask first
- `event.requiredContentType` -- allows the handler to override everything else (legacy, will be deprecated in v6)
- `context.preferredMediaTypes` -- allows the handler to override the default, but lets the request ask first
- `defaultContentType` middleware configuration

All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.
Expand All @@ -68,6 +64,7 @@ When planning to use `Accept`, an external input, it is recommended to validate

```javascript
import middy from '@middy/core'
import httpContentNegotiation from '@middy/http-content-negotiation'
import httpResponseSerializer from '@middy/http-response-serializer'

const lambdaHandler = (event, context) => {
Expand All @@ -80,6 +77,7 @@ const lambdaHandler = (event, context) => {
}

export const handler = middy()
.use(httpContentNegotiation()) // Creates `context.preferredMediaTypes`
.use(
httpResponseSerializer({
serializers: [
Expand Down

0 comments on commit 15de9fb

Please sign in to comment.