UNPKG

5.04 kBMarkdownView Raw
1# Middy http-response-serializer middleware
2
3<div align="center">
4 <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
5</div>
6
7<div align="center">
8 <p><strong>HTTP response serializer middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
9</div>
10
11<div align="center">
12<p>
13 <a href="http://badge.fury.io/js/%40middy%2Fhttp-response-serializer">
14 <img src="https://badge.fury.io/js/%40middy%2Fhttp-response-serializer.svg" alt="npm version" style="max-width:100%;">
15 </a>
16 <a href="https://snyk.io/test/github/middyjs/middy">
17 <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%;">
18 </a>
19 <a href="https://standardjs.com/">
20 <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
21 </a>
22 <a href="https://gitter.im/middyjs/Lobby">
23 <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
24 </a>
25</p>
26</div>
27
28The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.
29
30
31## Install
32
33To install this middleware you can use NPM:
34
35```bash
36npm install --save @middy/http-response-serializer
37```
38
39
40## Configuration
41
42The middleware is configured by defining some `serializers`.
43
44```
45{
46 serializers: [
47 {
48 regex: /^application\/xml$/,
49 serializer: ({ body }) => `<message>${body}</message>`,
50 },
51 {
52 regex: /^application\/json$/,
53 serializer: ({ body }) => JSON.stringify(body)
54 },
55 {
56 regex: /^text\/plain$/,
57 serializer: ({ body }) => body
58 }
59 ],
60 default: 'application/json'
61}
62```
63
64The `default` (optional) option is used if the request and handler don't specify what type is wanted.
65
66
67## Serializer Functions
68
69When a matching serializer is found, the `Content-Type` header is set and the serializer function is run.
70
71The function is passed the entire `response` object, and should return either a string or an object.
72
73If a string is returned, the `body` attribute of the response is updated.
74
75If an object 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.
76
77
78## Content Type Negotiation
79
80The header is not the only way the middleware decides which serializer to execute.
81
82The content type is determined in the following order:
83
84 * `event.requiredContentType` -- allows the handler to override everything else
85 * The `Accept` header via [accept](https://www.npmjs.com/package/accept)
86 * `event.preferredContentType` -- allows the handler to override the default, but lets the request ask first
87 * `default` middleware configuration
88
89All options allow for multiple types to be specified in your order of preference, and the first matching serializer will be executed.
90
91
92## Error Handling
93
94This middleware does work with `http-error-handler`. To serialize error responses, ensure the `httpErrorHandler` middleware is configured before `httpResponseSerializer`.
95
96
97## Dependencies
98
99Thie middleware does not rely on any other middlewares.
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 cb(null, {
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 default: '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-2021 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>