@middy/http-content-negotiation 7.1.1 → 7.1.3
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 +17 -0
- package/index.d.ts +8 -5
- package/index.js +8 -7
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -30,6 +30,23 @@
|
|
|
30
30
|
<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>
|
|
31
31
|
</div>
|
|
32
32
|
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install --save @middy/http-content-negotiation
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## Documentation and examples
|
|
41
|
+
|
|
42
|
+
For documentation and examples, refer to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org/docs/middlewares/http-content-negotiation).
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
## Contributing
|
|
46
|
+
|
|
47
|
+
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).
|
|
48
|
+
|
|
49
|
+
|
|
33
50
|
## License
|
|
34
51
|
|
|
35
52
|
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2026 [will Farrell](https://github.com/willfarrell), [Luciano Mammino](https://github.com/lmammino), and [Middy contributors](https://github.com/middyjs/middy/graphs/contributors).
|
package/index.d.ts
CHANGED
|
@@ -1,22 +1,25 @@
|
|
|
1
1
|
// Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
import type middy from "@middy/core";
|
|
4
|
+
import type { Context as LambdaContext } from "aws-lambda";
|
|
4
5
|
|
|
5
6
|
export interface Options {
|
|
6
7
|
parseCharsets?: boolean;
|
|
7
8
|
availableCharsets?: string[];
|
|
9
|
+
defaultToFirstCharset?: boolean;
|
|
8
10
|
parseEncodings?: boolean;
|
|
9
11
|
availableEncodings?: string[];
|
|
12
|
+
defaultToFirstEncoding?: boolean;
|
|
10
13
|
parseLanguages?: boolean;
|
|
11
14
|
availableLanguages?: string[];
|
|
15
|
+
defaultToFirstLanguage?: boolean;
|
|
12
16
|
parseMediaTypes?: boolean;
|
|
13
17
|
availableMediaTypes?: string[];
|
|
18
|
+
defaultToFirstMediaType?: boolean;
|
|
14
19
|
failOnMismatch?: boolean;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
|
-
export type
|
|
18
|
-
|
|
19
|
-
export interface Context {
|
|
22
|
+
export type Context = LambdaContext & {
|
|
20
23
|
preferredCharsets: string[];
|
|
21
24
|
preferredCharset: string;
|
|
22
25
|
preferredEncodings: string[];
|
|
@@ -25,10 +28,10 @@ export interface Context {
|
|
|
25
28
|
preferredLanguage: string;
|
|
26
29
|
preferredMediaTypes: string[];
|
|
27
30
|
preferredMediaType: string;
|
|
28
|
-
}
|
|
31
|
+
};
|
|
29
32
|
|
|
30
33
|
declare function httpContentNegotiation(
|
|
31
34
|
options?: Options,
|
|
32
|
-
): middy.MiddlewareObj<
|
|
35
|
+
): middy.MiddlewareObj<unknown, unknown, Error, Context>;
|
|
33
36
|
|
|
34
37
|
export default httpContentNegotiation;
|
package/index.js
CHANGED
|
@@ -16,23 +16,19 @@ const parseFn = {
|
|
|
16
16
|
const defaults = {
|
|
17
17
|
parseCharsets: true,
|
|
18
18
|
availableCharsets: undefined,
|
|
19
|
-
// defaultToFirstCharset: false, // Should not be used
|
|
20
19
|
parseEncodings: true,
|
|
21
20
|
availableEncodings: undefined,
|
|
22
|
-
// defaultToFirstEncoding: false, // Should not be used
|
|
23
21
|
parseLanguages: true,
|
|
24
22
|
availableLanguages: undefined,
|
|
25
|
-
// defaultToFirstLanguage: false, // Should not be used
|
|
26
23
|
parseMediaTypes: true,
|
|
27
24
|
availableMediaTypes: undefined,
|
|
28
|
-
// defaultToFirstMediaType: false, // Should not be used
|
|
29
25
|
failOnMismatch: true,
|
|
30
26
|
};
|
|
31
27
|
|
|
32
28
|
const httpContentNegotiationMiddleware = (opts = {}) => {
|
|
33
29
|
const options = { ...defaults, ...opts };
|
|
34
30
|
|
|
35
|
-
const httpContentNegotiationMiddlewareBefore =
|
|
31
|
+
const httpContentNegotiationMiddlewareBefore = (request) => {
|
|
36
32
|
const { event, context } = request;
|
|
37
33
|
if (!event.headers) return;
|
|
38
34
|
if (options.parseCharsets) {
|
|
@@ -105,7 +101,7 @@ const parseHeader = (
|
|
|
105
101
|
context[resultsName] = parseFn[type](headerValue, availableValues);
|
|
106
102
|
context[resultName] = context[resultsName][0];
|
|
107
103
|
|
|
108
|
-
if (context[resultName] === undefined) {
|
|
104
|
+
if (typeof context[resultName] === "undefined") {
|
|
109
105
|
if (defaultToFirstValue) {
|
|
110
106
|
context[resultName] = availableValues[0];
|
|
111
107
|
} else if (failOnMismatch) {
|
|
@@ -113,7 +109,12 @@ const parseHeader = (
|
|
|
113
109
|
throw createError(
|
|
114
110
|
406,
|
|
115
111
|
`Unsupported ${type}. Acceptable values: ${availableValues.join(", ")}`,
|
|
116
|
-
{
|
|
112
|
+
{
|
|
113
|
+
cause: {
|
|
114
|
+
package: "@middy/http-content-negotiation",
|
|
115
|
+
data: { [headerName]: headerValue },
|
|
116
|
+
},
|
|
117
|
+
},
|
|
117
118
|
);
|
|
118
119
|
}
|
|
119
120
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-content-negotiation",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.3",
|
|
4
4
|
"description": "HTTP content negotiation middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -65,12 +65,12 @@
|
|
|
65
65
|
"url": "https://github.com/sponsors/willfarrell"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@middy/util": "7.1.
|
|
68
|
+
"@middy/util": "7.1.3",
|
|
69
69
|
"negotiator": "1.0.0"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@middy/core": "7.1.
|
|
73
|
-
"@types/aws-lambda": "^8.0.0"
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
"@middy/core": "7.1.3",
|
|
73
|
+
"@types/aws-lambda": "^8.0.0",
|
|
74
|
+
"@types/node": "^22.0.0"
|
|
75
|
+
}
|
|
76
76
|
}
|