@middy/http-response-serializer 4.0.0 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -120
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -36,126 +36,6 @@
|
|
|
36
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
37
|
</div>
|
|
38
38
|
|
|
39
|
-
The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
## Install
|
|
43
|
-
|
|
44
|
-
To install this middleware you can use NPM:
|
|
45
|
-
|
|
46
|
-
```bash
|
|
47
|
-
npm install --save @middy/http-response-serializer
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
## Configuration
|
|
52
|
-
|
|
53
|
-
The 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
|
-
defaultContentType: 'application/json'
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
The `defaultContentType` (optional) option is used if the request and handler don't specify what type is wanted.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
## Serializer Functions
|
|
79
|
-
|
|
80
|
-
When a matching serializer is found, the `Content-Type` header is set and the serializer function is run.
|
|
81
|
-
|
|
82
|
-
The function is passed the entire `response` object, and should return either a string or an object.
|
|
83
|
-
|
|
84
|
-
If a string is returned, the `body` attribute of the response is updated.
|
|
85
|
-
|
|
86
|
-
If 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
|
-
|
|
91
|
-
The header is not the only way the middleware decides which serializer to execute.
|
|
92
|
-
|
|
93
|
-
The 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
|
-
|
|
100
|
-
All 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
|
|
106
|
-
import middy from '@middy/core'
|
|
107
|
-
import httpResponseSerializer from '@middy/http-response-serializer'
|
|
108
|
-
|
|
109
|
-
const handler = middy((event, context) => {
|
|
110
|
-
const body = 'Hello World'
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
statusCode: 200,
|
|
114
|
-
body
|
|
115
|
-
}
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
handler
|
|
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
|
-
|
|
137
|
-
const event = {
|
|
138
|
-
headers: {
|
|
139
|
-
'Accept': 'application/xml;q=0.9, text/x-dvi; q=0.8, text/x-c'
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
handler(event, {}, (_, response) => {
|
|
144
|
-
t.is(response.body,'<message>Hello World</message>')
|
|
145
|
-
})
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
## Middy documentation and examples
|
|
150
|
-
|
|
151
|
-
For 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
|
-
|
|
156
|
-
Everyone 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
39
|
## License
|
|
160
40
|
|
|
161
41
|
Licensed 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).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-response-serializer",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "The Http Serializer middleware lets you define serialization mechanisms based on the current content negotiation.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -66,10 +66,10 @@
|
|
|
66
66
|
"homepage": "https://middy.js.org",
|
|
67
67
|
"dependencies": {
|
|
68
68
|
"@hapi/accept": "6.0.0",
|
|
69
|
-
"@middy/util": "4.0.
|
|
69
|
+
"@middy/util": "4.0.2"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@middy/core": "4.0.
|
|
72
|
+
"@middy/core": "4.0.2"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "c77c9413ecb80999a71b67ff97edac1fed2ca754"
|
|
75
75
|
}
|