UNPKG

5.93 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">
13 <img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test 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</div>
37
38The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.
39
40
41## Install
42
43To install this middleware you can use NPM:
44
45```bash
46npm install --save @middy/http-response-serializer
47```
48
49
50## Configuration
51
52The middleware is configured by defining some `serializers`.
53
54```
55{
56 serializers: [
57 {
58 regex: /^application\/xml$/,
59 serializer: ({ body }) => `<message>${body}</message>`,
60 },
61 {
62 regex: /^application\/json$/,
63 serializer: ({ body }) => JSON.stringify(body)
64 },
65 {
66 regex: /^text\/plain$/,
67 serializer: ({ body }) => body
68 }
69 ],
70 default: 'application/json'
71}
72```
73
74The `defaultContentType` (optional) option is used if the request and handler don't specify what type is wanted.
75
76
77## Serializer Functions
78
79When a matching serializer is found, the `Content-Type` header is set and the serializer function is run.
80
81The function is passed the entire `response` object, and should return either a string or an object.
82
83If a string is returned, the `body` attribute of the response is updated.
84
85If 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.
86
87
88## Content Type Negotiation
89
90The header is not the only way the middleware decides which serializer to execute.
91
92The content type is determined in the following order:
93
94 * `event.requiredContentType` -- allows the handler to override everything else
95 * The `Accept` header via [accept](https://www.npmjs.com/package/accept)
96 * `event.preferredContentType` -- allows the handler to override the default, but lets the request ask first
97 * `defaultContentType` middleware option
98
99All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.
100
101
102## Sample usage
103
104```javascript
105import middy from '@middy/core'
106import httpResponseSerializer from '@middy/http-response-serializer'
107
108const handler = middy((event, context) => {
109 const body = 'Hello World'
110
111 return {
112 statusCode: 200,
113 body
114 }
115})
116
117handler
118 .use(httpResponseSerializer({
119 serializers: [
120 {
121 regex: /^application\/xml$/,
122 serializer: ({ body }) => `<message>${body}</message>`,
123 },
124 {
125 regex: /^application\/json$/,
126 serializer: ({ body }) => JSON.stringify(body)
127 },
128 {
129 regex: /^text\/plain$/,
130 serializer: ({ body }) => body
131 }
132 ],
133 defaultContentType: 'application/json'
134 }))
135
136const event = {
137 headers: {
138 'Accept': 'application/xml;q=0.9, text/x-dvi; q=0.8, text/x-c'
139 }
140}
141
142handler(event, {}, (_, response) => {
143 t.is(response.body,'<message>Hello World</message>')
144})
145```
146
147
148## Middy documentation and examples
149
150For 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).
151
152
153## Contributing
154
155Everyone 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).
156
157
158## License
159
160Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
161
162<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
163 <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
164</a>