@middy/http-content-negotiation 5.0.3 → 5.2.0
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/index.js +115 -61
- package/package.json +4 -4
package/index.js
CHANGED
|
@@ -1,66 +1,120 @@
|
|
|
1
|
-
import charset from 'negotiator/lib/charset.js'
|
|
2
|
-
import encoding from 'negotiator/lib/encoding.js'
|
|
3
|
-
import language from 'negotiator/lib/language.js'
|
|
4
|
-
import mediaType from 'negotiator/lib/mediaType.js'
|
|
5
|
-
import { createError } from '@middy/util'
|
|
1
|
+
import charset from 'negotiator/lib/charset.js'
|
|
2
|
+
import encoding from 'negotiator/lib/encoding.js'
|
|
3
|
+
import language from 'negotiator/lib/language.js'
|
|
4
|
+
import mediaType from 'negotiator/lib/mediaType.js'
|
|
5
|
+
import { createError } from '@middy/util'
|
|
6
|
+
|
|
6
7
|
const parseFn = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
8
|
+
Charset: charset,
|
|
9
|
+
Encoding: encoding,
|
|
10
|
+
Language: language,
|
|
11
|
+
MediaType: mediaType
|
|
12
|
+
}
|
|
13
|
+
|
|
12
14
|
const defaults = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
15
|
+
parseCharsets: true,
|
|
16
|
+
availableCharsets: undefined,
|
|
17
|
+
// defaultToFirstCharset: false, // Should not be used
|
|
18
|
+
parseEncodings: true,
|
|
19
|
+
availableEncodings: undefined,
|
|
20
|
+
// defaultToFirstEncoding: false, // Should not be used
|
|
21
|
+
parseLanguages: true,
|
|
22
|
+
availableLanguages: undefined,
|
|
23
|
+
// defaultToFirstLanguage: false, // Should not be used
|
|
24
|
+
parseMediaTypes: true,
|
|
25
|
+
availableMediaTypes: undefined,
|
|
26
|
+
// defaultToFirstMediaType: false, // Should not be used
|
|
27
|
+
failOnMismatch: true
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const httpContentNegotiationMiddleware = (opts = {}) => {
|
|
31
|
+
const options = { ...defaults, ...opts }
|
|
32
|
+
|
|
33
|
+
const httpContentNegotiationMiddlewareBefore = async (request) => {
|
|
34
|
+
const { event, context } = request
|
|
35
|
+
if (!event.headers) return
|
|
36
|
+
if (options.parseCharsets) {
|
|
37
|
+
parseHeader(
|
|
38
|
+
'Accept-Charset',
|
|
39
|
+
'Charset',
|
|
40
|
+
options.availableCharsets,
|
|
41
|
+
options.defaultToFirstCharset,
|
|
42
|
+
options.failOnMismatch,
|
|
43
|
+
event,
|
|
44
|
+
context
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
if (options.parseEncodings) {
|
|
48
|
+
parseHeader(
|
|
49
|
+
'Accept-Encoding',
|
|
50
|
+
'Encoding',
|
|
51
|
+
options.availableEncodings,
|
|
52
|
+
options.defaultToFirstEncoding,
|
|
53
|
+
options.failOnMismatch,
|
|
54
|
+
event,
|
|
55
|
+
context
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (options.parseLanguages) {
|
|
60
|
+
parseHeader(
|
|
61
|
+
'Accept-Language',
|
|
62
|
+
'Language',
|
|
63
|
+
options.availableLanguages,
|
|
64
|
+
options.defaultToFirstLanguage,
|
|
65
|
+
options.failOnMismatch,
|
|
66
|
+
event,
|
|
67
|
+
context
|
|
68
|
+
)
|
|
56
69
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
|
|
71
|
+
if (options.parseMediaTypes) {
|
|
72
|
+
parseHeader(
|
|
73
|
+
'Accept',
|
|
74
|
+
'MediaType',
|
|
75
|
+
options.availableMediaTypes,
|
|
76
|
+
options.defaultToFirstMediaType,
|
|
77
|
+
options.failOnMismatch,
|
|
78
|
+
event,
|
|
79
|
+
context
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
before: httpContentNegotiationMiddlewareBefore
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const parseHeader = (
|
|
90
|
+
headerName,
|
|
91
|
+
type,
|
|
92
|
+
availableValues,
|
|
93
|
+
defaultToFirstValue,
|
|
94
|
+
failOnMismatch,
|
|
95
|
+
event,
|
|
96
|
+
context
|
|
97
|
+
) => {
|
|
98
|
+
const resultsName = `preferred${type}s`
|
|
99
|
+
const resultName = `preferred${type}`
|
|
100
|
+
const headerValue =
|
|
101
|
+
event.headers[headerName] ?? event.headers[headerName.toLowerCase()]
|
|
102
|
+
|
|
103
|
+
context[resultsName] = parseFn[type](headerValue, availableValues)
|
|
104
|
+
context[resultName] = context[resultsName][0]
|
|
105
|
+
|
|
106
|
+
if (context[resultName] === undefined) {
|
|
107
|
+
if (defaultToFirstValue) {
|
|
108
|
+
context[resultName] = availableValues[0]
|
|
109
|
+
} else if (failOnMismatch) {
|
|
110
|
+
// NotAcceptable
|
|
111
|
+
throw createError(
|
|
112
|
+
406,
|
|
113
|
+
`Unsupported ${type}. Acceptable values: ${availableValues.join(', ')}`,
|
|
114
|
+
{ cause: { package: '@middy/http-content-negotiation' } }
|
|
115
|
+
)
|
|
63
116
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
117
|
+
}
|
|
118
|
+
}
|
|
66
119
|
|
|
120
|
+
export default httpContentNegotiationMiddleware
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-negotiation",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Http content negotiation middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
"url": "https://github.com/sponsors/willfarrell"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
|
-
"@middy/util": "5.0
|
|
63
|
+
"@middy/util": "5.2.0",
|
|
64
64
|
"negotiator": "0.6.3"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@middy/core": "5.0
|
|
67
|
+
"@middy/core": "5.2.0"
|
|
68
68
|
},
|
|
69
|
-
"gitHead": "
|
|
69
|
+
"gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
|
|
70
70
|
}
|