UNPKG

6.23 kBMarkdownView Raw
1<div align="center">
2 <h1>Middy http-response-serializer middleware</h1>
3 <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4 <p><strong>HTTP response serializer middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
5<p>
6 <a href="https://www.npmjs.com/package/@middy/http-response-serializer?activeTab=versions">
7 <img src="https://badge.fury.io/js/%40middy%2Fhttp-response-serializer.svg" alt="npm version" style="max-width:100%;">
8 </a>
9 <a href="https://packagephobia.com/result?p=@middy/http-response-serializer">
10 <img src="https://packagephobia.com/badge?p=@middy/http-response-serializer" alt="npm install size" style="max-width:100%;">
11 </a>
12 <a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
13 <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
14 </a>
15 <br/>
16 <a href="https://standardjs.com/">
17 <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18 </a>
19 <a href="https://snyk.io/test/github/middyjs/middy">
20 <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21 </a>
22 <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23 <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
24 </a>
25 <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26 <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
27 </a>
28 <br/>
29 <a href="https://gitter.im/middyjs/Lobby">
30 <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31 </a>
32 <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33 <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34 </a>
35</p>
36<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-response-serializer">https://middy.js.org/docs/middlewares/http-response-serializer</a></p>
37</div>
38
39The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.
40
41
42## Install
43
44To install this middleware you can use NPM:
45
46```bash
47npm install --save @middy/http-response-serializer
48```
49
50
51## Configuration
52
53The middleware is configured by defining some `serializers`.
54
55```
56{
57 serializers: [
58 {
59 regex: /^application\/xml$/,
60 serializer: ({ body }) => `<message>${body}</message>`,
61 },
62 {
63 regex: /^application\/json$/,
64 serializer: ({ body }) => JSON.stringify(body)
65 },
66 {
67 regex: /^text\/plain$/,
68 serializer: ({ body }) => body
69 }
70 ],
71 default: 'application/json'
72}
73```
74
75The `defaultContentType` (optional) option is used if the request and handler don't specify what type is wanted.
76
77
78## Serializer Functions
79
80When a matching serializer is found, the `Content-Type` header is set and the serializer function is run.
81
82The function is passed the entire `response` object, and should return either a string or an object.
83
84If a string is returned, the `body` attribute of the response is updated.
85
86If 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.
87
88
89## Content Type Negotiation
90
91The header is not the only way the middleware decides which serializer to execute.
92
93The content type is determined in the following order:
94
95 * `event.requiredContentType` -- allows the handler to override everything else
96 * The `Accept` header via [accept](https://www.npmjs.com/package/accept)
97 * `event.preferredContentType` -- allows the handler to override the default, but lets the request ask first
98 * `defaultContentType` middleware option
99
100All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.
101
102
103## Sample usage
104
105```javascript
106import middy from '@middy/core'
107import httpResponseSerializer from '@middy/http-response-serializer'
108
109const handler = middy((event, context) => {
110 const body = 'Hello World'
111
112 return {
113 statusCode: 200,
114 body
115 }
116})
117
118handler
119 .use(httpResponseSerializer({
120 serializers: [
121 {
122 regex: /^application\/xml$/,
123 serializer: ({ body }) => `<message>${body}</message>`,
124 },
125 {
126 regex: /^application\/json$/,
127 serializer: ({ body }) => JSON.stringify(body)
128 },
129 {
130 regex: /^text\/plain$/,
131 serializer: ({ body }) => body
132 }
133 ],
134 defaultContentType: 'application/json'
135 }))
136
137const event = {
138 headers: {
139 'Accept': 'application/xml;q=0.9, text/x-dvi; q=0.8, text/x-c'
140 }
141}
142
143handler(event, {}, (_, response) => {
144 t.is(response.body,'<message>Hello World</message>')
145})
146```
147
148
149## Middy documentation and examples
150
151For more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).
152
153
154## Contributing
155
156Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
157
158
159## License
160
161Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
162
163<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
164 <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
165</a>