UNPKG

4.79 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 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.
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## Sample usage
93
94```javascript
95import middy from '@middy/core'
96import httpResponseSerializer from '@middy/http-response-serializer'
97
98const handler = middy((event, context) => {
99 const body = 'Hello World'
100
101 return {
102 statusCode: 200,
103 body
104 }
105})
106
107handler
108 .use(httpResponseSerializer({
109 serializers: [
110 {
111 regex: /^application\/xml$/,
112 serializer: ({ body }) => `<message>${body}</message>`,
113 },
114 {
115 regex: /^application\/json$/,
116 serializer: ({ body }) => JSON.stringify(body)
117 },
118 {
119 regex: /^text\/plain$/,
120 serializer: ({ body }) => body
121 }
122 ],
123 default: 'application/json'
124 }))
125
126const event = {
127 headers: {
128 'Accept': 'application/xml;q=0.9, text/x-dvi; q=0.8, text/x-c'
129 }
130}
131
132handler(event, {}, (_, response) => {
133 t.is(response.body,'<message>Hello World</message>')
134})
135```
136
137
138## Middy documentation and examples
139
140For 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).
141
142
143## Contributing
144
145Everyone 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).
146
147
148## License
149
150Licensed under [MIT License](LICENSE). Copyright (c) 2017-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
151
152<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
153 <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
154</a>