@middy/http-content-negotiation 4.0.0 → 4.0.1
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 -107
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -36,113 +36,6 @@
|
|
|
36
36
|
<p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/http-content-negotiation">https://middy.js.org/docs/middlewares/http-content-negotiation</a></p>
|
|
37
37
|
</div>
|
|
38
38
|
|
|
39
|
-
This middleware parses `Accept-*` headers and provides utilities for [HTTP content negotiation](https://tools.ietf.org/html/rfc7231#section-5.3) (charset, encoding, language and media type).
|
|
40
|
-
|
|
41
|
-
By default the middleware parses charsets (`Accept-Charset`), languages (`Accept-Language`), encodings (`Accept-Encoding`) and media types (`Accept`) during the
|
|
42
|
-
`before` phase and expands the `event` object by adding the following properties:
|
|
43
|
-
|
|
44
|
-
- `preferredCharsets` (`array`) - The list of charsets that can be safely used by the app (as the result of the negotiation)
|
|
45
|
-
- `preferredCharset` (`string`) - The preferred charset (as the result of the negotiation)
|
|
46
|
-
- `preferredEncodings` (`array`) - The list of encodings that can be safely used by the app (as the result of the negotiation)
|
|
47
|
-
- `preferredEncoding` (`string`) - The preferred encoding (as the result of the negotiation)
|
|
48
|
-
- `preferredLanguages` (`array`) - The list of languages that can be safely used by the app (as the result of the negotiation)
|
|
49
|
-
- `preferredLanguage` (`string`) - The preferred language (as the result of the negotiation)
|
|
50
|
-
- `preferredMediaTypes` (`array`) - The list of media types that can be safely used by the app (as the result of the negotiation)
|
|
51
|
-
- `preferredMediaType` (`string`) - The preferred media types (as the result of the negotiation)
|
|
52
|
-
|
|
53
|
-
This middleware expects the headers in canonical format, so it should be attached after the [`httpHeaderNormalizer`](#httpheadernormalizer) middleware.
|
|
54
|
-
It also can throw an HTTP exception, so it can be convenient to use it in combination with the [`httpErrorHandler`](#httperrorhandler).
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
## Install
|
|
58
|
-
|
|
59
|
-
To install this middleware you can use NPM:
|
|
60
|
-
|
|
61
|
-
```bash
|
|
62
|
-
npm install --save @middy/http-content-negotiation
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
## Options
|
|
67
|
-
|
|
68
|
-
- `parseCharsets` (boolean) (defaults to `true`) - Allows enabling/disabling the charsets parsing
|
|
69
|
-
- `availableCharsets` (string) (defaults to `undefined`) - Allows defining the list of charsets supported by the Lambda function
|
|
70
|
-
- `parseEncodings` (boolean) (defaults to `true`) - Allows enabling/disabling the encodings parsing
|
|
71
|
-
- `availableEncodings` (string) (defaults to `undefined`) - Allows defining the list of encodings supported by the Lambda function
|
|
72
|
-
- `parseLanguages` (boolean) (defaults to `true`) - Allows enabling/disabling the languages parsing
|
|
73
|
-
- `availableLanguages` (string) (defaults to `undefined`) - Allows defining the list of languages supported by the Lambda function
|
|
74
|
-
- `defaultToFirstLanguage` (boolean) (deafults to `false`) - Will set `preferredCharset` to the first value of `availableLanguages` if unset.
|
|
75
|
-
- `parseMediaTypes` (boolean) (defaults to `true`) - Allows enabling/disabling the media types parsing
|
|
76
|
-
- `availableMediaTypes` (string) (defaults to `undefined`) - Allows defining the list of media types supported by the Lambda function
|
|
77
|
-
- `failOnMismatch` (boolean) (defaults to `true`) - If set to true it will throw an HTTP `NotAcceptable` (406) exception when the negotiation fails for one of the headers (e.g. none of the languages requested are supported by the app)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
## Sample usage
|
|
81
|
-
|
|
82
|
-
```javascript
|
|
83
|
-
import middy from '@middy/core'
|
|
84
|
-
import httpContentNegotiation from '@middy/http-content-negotiation'
|
|
85
|
-
import httpHeaderNormalizer from '@middy/http-header-normalizer'
|
|
86
|
-
import httpErrorHandler from '@middy/http-error-handler'
|
|
87
|
-
|
|
88
|
-
const handler = middy((event, context) => {
|
|
89
|
-
let message, body
|
|
90
|
-
|
|
91
|
-
switch (event.preferredLanguage) {
|
|
92
|
-
case 'it-it':
|
|
93
|
-
message = 'Ciao Mondo'
|
|
94
|
-
break
|
|
95
|
-
case 'fr-fr':
|
|
96
|
-
message = 'Bonjour le monde'
|
|
97
|
-
break
|
|
98
|
-
default:
|
|
99
|
-
message = 'Hello world'
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
switch (event.preferredMediaType) {
|
|
103
|
-
case 'application/xml':
|
|
104
|
-
body = `<message>${message}</message>`
|
|
105
|
-
break
|
|
106
|
-
case 'application/yaml':
|
|
107
|
-
body = `---\nmessage: ${message}`
|
|
108
|
-
break
|
|
109
|
-
case 'application/json':
|
|
110
|
-
body = JSON.stringify({ message })
|
|
111
|
-
break
|
|
112
|
-
default:
|
|
113
|
-
body = message
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
statusCode: 200,
|
|
118
|
-
body
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
handler
|
|
123
|
-
.use(httpHeaderNormalizer())
|
|
124
|
-
.use(httpContentNegotiation({
|
|
125
|
-
parseCharsets: false,
|
|
126
|
-
parseEncodings: false,
|
|
127
|
-
availableLanguages: ['it-it', 'fr-fr', 'en'],
|
|
128
|
-
availableMediaTypes: ['application/xml', 'application/yaml', 'application/json', 'text/plain']
|
|
129
|
-
}))
|
|
130
|
-
.use(httpErrorHandler())
|
|
131
|
-
|
|
132
|
-
export default { handler }
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
## Middy documentation and examples
|
|
137
|
-
|
|
138
|
-
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).
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
## Contributing
|
|
142
|
-
|
|
143
|
-
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).
|
|
144
|
-
|
|
145
|
-
|
|
146
39
|
## License
|
|
147
40
|
|
|
148
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-content-negotiation",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Http content negotiation middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -62,11 +62,11 @@
|
|
|
62
62
|
},
|
|
63
63
|
"homepage": "https://middy.js.org",
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@middy/util": "4.0.
|
|
65
|
+
"@middy/util": "4.0.1",
|
|
66
66
|
"negotiator": "0.6.3"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@middy/core": "4.0.
|
|
69
|
+
"@middy/core": "4.0.1"
|
|
70
70
|
},
|
|
71
|
-
"gitHead": "
|
|
71
|
+
"gitHead": "c5ece2bfbb0d607dcdea5685bf194a6cc19acc8d"
|
|
72
72
|
}
|