@middy/http-content-negotiation 6.1.6 → 6.2.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/index.d.ts +23 -23
- package/index.js +104 -104
- package/package.json +69 -72
package/index.d.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import middy from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
2
|
|
|
3
3
|
interface Options {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
parseCharsets?: boolean;
|
|
5
|
+
availableCharsets?: string[];
|
|
6
|
+
parseEncodings?: boolean;
|
|
7
|
+
availableEncodings?: string[];
|
|
8
|
+
parseLanguages?: boolean;
|
|
9
|
+
availableLanguages?: string[];
|
|
10
|
+
parseMediaTypes?: boolean;
|
|
11
|
+
availableMediaTypes?: string[];
|
|
12
|
+
failOnMismatch?: boolean;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
16
|
-
export
|
|
16
|
+
export type Event = {};
|
|
17
17
|
|
|
18
18
|
export interface Context {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
preferredCharsets: string[];
|
|
20
|
+
preferredCharset: string;
|
|
21
|
+
preferredEncodings: string[];
|
|
22
|
+
preferredEncoding: string;
|
|
23
|
+
preferredLanguages: string[];
|
|
24
|
+
preferredLanguage: string;
|
|
25
|
+
preferredMediaTypes: string[];
|
|
26
|
+
preferredMediaType: string;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
declare function httpContentNegotiation
|
|
30
|
-
|
|
31
|
-
): middy.MiddlewareObj<Event
|
|
29
|
+
declare function httpContentNegotiation(
|
|
30
|
+
options?: Options,
|
|
31
|
+
): middy.MiddlewareObj<Event>;
|
|
32
32
|
|
|
33
|
-
export default httpContentNegotiation
|
|
33
|
+
export default httpContentNegotiation;
|
package/index.js
CHANGED
|
@@ -1,120 +1,120 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
1
|
+
import { createError } from "@middy/util";
|
|
2
|
+
import charset from "negotiator/lib/charset.js";
|
|
3
|
+
import encoding from "negotiator/lib/encoding.js";
|
|
4
|
+
import language from "negotiator/lib/language.js";
|
|
5
|
+
import mediaType from "negotiator/lib/mediaType.js";
|
|
6
6
|
|
|
7
7
|
const parseFn = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
8
|
+
Charset: charset,
|
|
9
|
+
Encoding: encoding,
|
|
10
|
+
Language: language,
|
|
11
|
+
MediaType: mediaType,
|
|
12
|
+
};
|
|
13
13
|
|
|
14
14
|
const defaults = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
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
29
|
|
|
30
30
|
const httpContentNegotiationMiddleware = (opts = {}) => {
|
|
31
|
-
|
|
31
|
+
const options = { ...defaults, ...opts };
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
+
);
|
|
69
|
+
}
|
|
70
70
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
83
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
84
|
+
return {
|
|
85
|
+
before: httpContentNegotiationMiddlewareBefore,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
88
|
|
|
89
89
|
const parseHeader = (
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
90
|
+
headerName,
|
|
91
|
+
type,
|
|
92
|
+
availableValues,
|
|
93
|
+
defaultToFirstValue,
|
|
94
|
+
failOnMismatch,
|
|
95
|
+
event,
|
|
96
|
+
context,
|
|
97
97
|
) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
const resultsName = `preferred${type}s`;
|
|
99
|
+
const resultName = `preferred${type}`;
|
|
100
|
+
const headerValue =
|
|
101
|
+
event.headers[headerName] ?? event.headers[headerName.toLowerCase()];
|
|
102
102
|
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
context[resultsName] = parseFn[type](headerValue, availableValues);
|
|
104
|
+
context[resultName] = context[resultsName][0];
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
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
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
119
|
|
|
120
|
-
export default httpContentNegotiationMiddleware
|
|
120
|
+
export default httpContentNegotiationMiddleware;
|
package/package.json
CHANGED
|
@@ -1,74 +1,71 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"@middy/core": "6.1.6"
|
|
72
|
-
},
|
|
73
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/http-content-negotiation",
|
|
3
|
+
"version": "6.2.1",
|
|
4
|
+
"description": "Http content negotiation middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"Middy",
|
|
42
|
+
"HTTP",
|
|
43
|
+
"API",
|
|
44
|
+
"Content Negotiation"
|
|
45
|
+
],
|
|
46
|
+
"author": {
|
|
47
|
+
"name": "Middy contributors",
|
|
48
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
53
|
+
"directory": "packages/http-content-negotiation"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://middy.js.org",
|
|
59
|
+
"funding": {
|
|
60
|
+
"type": "github",
|
|
61
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@middy/util": "6.2.1",
|
|
65
|
+
"negotiator": "1.0.0"
|
|
66
|
+
},
|
|
67
|
+
"devDependencies": {
|
|
68
|
+
"@middy/core": "6.2.1"
|
|
69
|
+
},
|
|
70
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
74
71
|
}
|