@middy/http-cors 3.0.0-alpha.2 → 3.0.0-alpha.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.
Files changed (2) hide show
  1. package/index.js +73 -50
  2. package/package.json +6 -5
package/index.js CHANGED
@@ -1,20 +1,19 @@
1
- import { normalizeHttpResponse } from '@middy/util';
1
+ import { normalizeHttpResponse } from '@middy/util'
2
2
 
3
3
  const getOrigin = (incomingOrigin, options = {}) => {
4
4
  if (options.origins.length > 0) {
5
5
  if (incomingOrigin && options.origins.includes(incomingOrigin)) {
6
- return incomingOrigin;
6
+ return incomingOrigin
7
7
  } else {
8
- return options.origins[0];
8
+ return options.origins[0]
9
9
  }
10
10
  } else {
11
11
  if (incomingOrigin && options.credentials && options.origin === '*') {
12
- return incomingOrigin;
12
+ return incomingOrigin
13
13
  }
14
-
15
- return options.origin;
14
+ return options.origin
16
15
  }
17
- };
16
+ }
18
17
 
19
18
  const defaults = {
20
19
  getOrigin,
@@ -28,83 +27,107 @@ const defaults = {
28
27
  requestHeaders: undefined,
29
28
  requestMethods: undefined,
30
29
  cacheControl: undefined
31
- };
30
+ }
32
31
 
33
32
  const httpCorsMiddleware = (opts = {}) => {
34
- const options = { ...defaults,
35
- ...opts
36
- };
33
+ const options = { ...defaults, ...opts }
37
34
 
38
- const httpCorsMiddlewareAfter = async request => {
39
- var _getVersionHttpMethod;
35
+ const httpCorsMiddlewareAfter = async (request) => {
36
+ normalizeHttpResponse(request)
40
37
 
41
- normalizeHttpResponse(request);
42
- const existingHeaders = Object.keys(request.response.headers);
38
+ const existingHeaders = Object.keys(request.response.headers)
43
39
 
40
+ // Check if already setup the header Access-Control-Allow-Credentials
44
41
  if (existingHeaders.includes('Access-Control-Allow-Credentials')) {
45
- options.credentials = request.response.headers['Access-Control-Allow-Credentials'] === 'true';
42
+ options.credentials =
43
+ request.response.headers['Access-Control-Allow-Credentials'] === 'true'
46
44
  }
47
-
48
45
  if (options.credentials) {
49
- request.response.headers['Access-Control-Allow-Credentials'] = String(options.credentials);
46
+ request.response.headers['Access-Control-Allow-Credentials'] = String(
47
+ options.credentials
48
+ )
50
49
  }
51
50
 
52
- if (options.headers && !existingHeaders.includes('Access-Control-Allow-Headers')) {
53
- request.response.headers['Access-Control-Allow-Headers'] = options.headers;
51
+ // Check if already setup Access-Control-Allow-Headers
52
+ if (
53
+ options.headers &&
54
+ !existingHeaders.includes('Access-Control-Allow-Headers')
55
+ ) {
56
+ request.response.headers['Access-Control-Allow-Headers'] = options.headers
54
57
  }
55
58
 
56
- if (options.methods && !existingHeaders.includes('Access-Control-Allow-Methods')) {
57
- request.response.headers['Access-Control-Allow-Methods'] = options.methods;
59
+ // Check if already setup Access-Control-Allow-Methods
60
+ if (
61
+ options.methods &&
62
+ !existingHeaders.includes('Access-Control-Allow-Methods')
63
+ ) {
64
+ request.response.headers['Access-Control-Allow-Methods'] = options.methods
58
65
  }
59
66
 
67
+ // Check if already setup the header Access-Control-Allow-Origin
60
68
  if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
61
- const eventHeaders = request.event.headers ?? {};
62
- const incomingOrigin = eventHeaders.origin ?? eventHeaders.Origin;
63
- request.response.headers['Access-Control-Allow-Origin'] = options.getOrigin(incomingOrigin, options);
69
+ const eventHeaders = request.event.headers ?? {}
70
+ const incomingOrigin = eventHeaders.origin ?? eventHeaders.Origin
71
+ request.response.headers[
72
+ 'Access-Control-Allow-Origin'
73
+ ] = options.getOrigin(incomingOrigin, options)
64
74
  }
65
75
 
66
- if (options.exposeHeaders && !existingHeaders.includes('Access-Control-Expose-Headers')) {
67
- request.response.headers['Access-Control-Expose-Headers'] = options.exposeHeaders;
76
+ // Check if already setup Access-Control-Expose-Headers
77
+ if (
78
+ options.exposeHeaders &&
79
+ !existingHeaders.includes('Access-Control-Expose-Headers')
80
+ ) {
81
+ request.response.headers['Access-Control-Expose-Headers'] =
82
+ options.exposeHeaders
68
83
  }
69
84
 
70
85
  if (options.maxAge && !existingHeaders.includes('Access-Control-Max-Age')) {
71
- request.response.headers['Access-Control-Max-Age'] = String(options.maxAge);
86
+ request.response.headers['Access-Control-Max-Age'] = String(
87
+ options.maxAge
88
+ )
72
89
  }
73
90
 
74
- if (options.requestHeaders && !existingHeaders.includes('Access-Control-Request-Headers')) {
75
- request.response.headers['Access-Control-Request-Headers'] = options.requestHeaders;
91
+ // Check if already setup Access-Control-Request-Headers
92
+ if (
93
+ options.requestHeaders &&
94
+ !existingHeaders.includes('Access-Control-Request-Headers')
95
+ ) {
96
+ request.response.headers['Access-Control-Request-Headers'] =
97
+ options.requestHeaders
76
98
  }
77
99
 
78
- if (options.requestMethods && !existingHeaders.includes('Access-Control-Request-Methods')) {
79
- request.response.headers['Access-Control-Request-Methods'] = options.requestMethods;
100
+ // Check if already setup Access-Control-Request-Methods
101
+ if (
102
+ options.requestMethods &&
103
+ !existingHeaders.includes('Access-Control-Request-Methods')
104
+ ) {
105
+ request.response.headers['Access-Control-Request-Methods'] = options.requestMethods
80
106
  }
81
107
 
82
- const httpMethod = (_getVersionHttpMethod = getVersionHttpMethod[request.event.version ?? '1.0']) === null || _getVersionHttpMethod === void 0 ? void 0 : _getVersionHttpMethod.call(getVersionHttpMethod, request.event);
83
-
108
+ const httpMethod = getVersionHttpMethod[request.event.version ?? '1.0']?.(request.event)
84
109
  if (!httpMethod) {
85
- throw new Error('Unknown API Gateway Payload format');
110
+ throw new Error('Unknown API Gateway Payload format')
86
111
  }
87
-
88
112
  if (httpMethod === 'OPTIONS') {
89
113
  if (options.cacheControl && !existingHeaders.includes('Cache-Control')) {
90
- request.response.headers['Cache-Control'] = String(options.cacheControl);
114
+ request.response.headers['Cache-Control'] = String(options.cacheControl)
91
115
  }
92
116
  }
93
- };
94
-
95
- const httpCorsMiddlewareOnError = async request => {
96
- if (request.response === undefined) return;
97
- return httpCorsMiddlewareAfter(request);
98
- };
99
-
117
+ }
118
+ const httpCorsMiddlewareOnError = async (request) => {
119
+ if (request.response === undefined) return
120
+ return httpCorsMiddlewareAfter(request)
121
+ }
100
122
  return {
101
123
  after: httpCorsMiddlewareAfter,
102
124
  onError: httpCorsMiddlewareOnError
103
- };
104
- };
125
+ }
126
+ }
105
127
 
106
128
  const getVersionHttpMethod = {
107
- '1.0': event => event.httpMethod,
108
- '2.0': event => event.requestContext.http.method
109
- };
110
- export default httpCorsMiddleware;
129
+ '1.0': (event) => event.httpMethod,
130
+ '2.0': (event) => event.requestContext.http.method
131
+ }
132
+
133
+ export default httpCorsMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/http-cors",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -18,7 +18,8 @@
18
18
  ],
19
19
  "scripts": {
20
20
  "test": "npm run test:unit",
21
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
22
23
  },
23
24
  "license": "MIT",
24
25
  "keywords": [
@@ -45,11 +46,11 @@
45
46
  "url": "https://github.com/middyjs/middy/issues"
46
47
  },
47
48
  "homepage": "https://github.com/middyjs/middy#readme",
48
- "gitHead": "de30419273ecbff08f367f47c7e320ec981cf145",
49
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156",
49
50
  "dependencies": {
50
- "@middy/util": "^3.0.0-alpha.2"
51
+ "@middy/util": "^3.0.0-alpha.3"
51
52
  },
52
53
  "devDependencies": {
53
- "@middy/core": "^3.0.0-alpha.2"
54
+ "@middy/core": "^3.0.0-alpha.3"
54
55
  }
55
56
  }