@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.
Files changed (3) hide show
  1. package/index.d.ts +23 -23
  2. package/index.js +104 -104
  3. package/package.json +69 -72
package/index.d.ts CHANGED
@@ -1,33 +1,33 @@
1
- import middy from '@middy/core'
1
+ import type middy from "@middy/core";
2
2
 
3
3
  interface Options {
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
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 interface Event {}
16
+ export type Event = {};
17
17
 
18
18
  export interface Context {
19
- preferredCharsets: string[]
20
- preferredCharset: string
21
- preferredEncodings: string[]
22
- preferredEncoding: string
23
- preferredLanguages: string[]
24
- preferredLanguage: string
25
- preferredMediaTypes: string[]
26
- preferredMediaType: string
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
- options?: Options
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 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 { 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
- Charset: charset,
9
- Encoding: encoding,
10
- Language: language,
11
- MediaType: mediaType
12
- }
8
+ Charset: charset,
9
+ Encoding: encoding,
10
+ Language: language,
11
+ MediaType: mediaType,
12
+ };
13
13
 
14
14
  const defaults = {
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
- }
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
- const options = { ...defaults, ...opts }
31
+ const options = { ...defaults, ...opts };
32
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
- }
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
- 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
- }
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
- 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
- }
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
- return {
85
- before: httpContentNegotiationMiddlewareBefore
86
- }
87
- }
84
+ return {
85
+ before: httpContentNegotiationMiddlewareBefore,
86
+ };
87
+ };
88
88
 
89
89
  const parseHeader = (
90
- headerName,
91
- type,
92
- availableValues,
93
- defaultToFirstValue,
94
- failOnMismatch,
95
- event,
96
- context
90
+ headerName,
91
+ type,
92
+ availableValues,
93
+ defaultToFirstValue,
94
+ failOnMismatch,
95
+ event,
96
+ context,
97
97
  ) => {
98
- const resultsName = `preferred${type}s`
99
- const resultName = `preferred${type}`
100
- const headerValue =
101
- event.headers[headerName] ?? event.headers[headerName.toLowerCase()]
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
- context[resultsName] = parseFn[type](headerValue, availableValues)
104
- context[resultName] = context[resultsName][0]
103
+ context[resultsName] = parseFn[type](headerValue, availableValues);
104
+ context[resultName] = context[resultsName][0];
105
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
- )
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
- "name": "@middy/http-content-negotiation",
3
- "version": "6.1.6",
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": [
27
- "index.js",
28
- "index.d.ts"
29
- ],
30
- "scripts": {
31
- "test": "npm run test:unit && npm run test:fuzz",
32
- "test:unit": "node --test __tests__/index.js",
33
- "test:fuzz": "node --test __tests__/fuzz.js",
34
- "test:benchmark": "node __benchmarks__/index.js"
35
- },
36
- "license": "MIT",
37
- "keywords": [
38
- "Lambda",
39
- "Middleware",
40
- "Serverless",
41
- "Framework",
42
- "AWS",
43
- "AWS Lambda",
44
- "Middy",
45
- "HTTP",
46
- "API",
47
- "Content Negotiation"
48
- ],
49
- "author": {
50
- "name": "Middy contributors",
51
- "url": "https://github.com/middyjs/middy/graphs/contributors"
52
- },
53
- "repository": {
54
- "type": "git",
55
- "url": "git+https://github.com/middyjs/middy.git",
56
- "directory": "packages/http-content-negotiation"
57
- },
58
- "bugs": {
59
- "url": "https://github.com/middyjs/middy/issues"
60
- },
61
- "homepage": "https://middy.js.org",
62
- "funding": {
63
- "type": "github",
64
- "url": "https://github.com/sponsors/willfarrell"
65
- },
66
- "dependencies": {
67
- "@middy/util": "6.1.6",
68
- "negotiator": "1.0.0"
69
- },
70
- "devDependencies": {
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
  }