@middy/http-cors 2.5.3 → 3.0.0-alpha.2
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/LICENSE +1 -1
- package/README.md +3 -3
- package/index.d.ts +2 -1
- package/index.js +32 -32
- package/package.json +8 -7
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017-
|
|
3
|
+
Copyright (c) 2017-2022 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -43,14 +43,14 @@ npm install --save @middy/http-cors
|
|
|
43
43
|
|
|
44
44
|
- `credentials` (bool) (optional): if true, sets `Access-Control-Allow-Credentials` (default `false`)
|
|
45
45
|
- `headers` (string) (optional): value to put in `Access-Control-Allow-Headers` (default: `false`)
|
|
46
|
-
- `methods` (string) (optional): value to put in `Access-Control-Allow-
|
|
46
|
+
- `methods` (string) (optional): value to put in `Access-Control-Allow-Methods` (default: `false`)
|
|
47
47
|
- `getOrigin` (function(incomingOrigin:string, options)) (optional): take full control of the generating the returned origin. Defaults to using the origin or origins option.
|
|
48
48
|
- `origin` (string) (optional): origin to put in the header (default: "`*`")
|
|
49
49
|
- `origins` (array) (optional): An array of allowed origins. The incoming origin is matched against the list and is returned if present.
|
|
50
50
|
- `exposeHeaders` (string) (optional): value to put in `Access-Control-Expose-Headers` (default: `false`)
|
|
51
51
|
- `maxAge` (string) (optional): value to put in Access-Control-Max-Age header (default: `null`)
|
|
52
52
|
- `requestHeaders` (string) (optional): value to put in `Access-Control-Request-Headers` (default: `false`)
|
|
53
|
-
- `requestMethods` (string) (optional): value to put in `Access-Control-Request-
|
|
53
|
+
- `requestMethods` (string) (optional): value to put in `Access-Control-Request-Methods` (default: `false`)
|
|
54
54
|
- `cacheControl` (string) (optional): value to put in Cache-Control header on pre-flight (OPTIONS) requests (default: `null`)
|
|
55
55
|
|
|
56
56
|
```javascript
|
|
@@ -105,7 +105,7 @@ Everyone is very welcome to contribute to this repository. Feel free to [raise i
|
|
|
105
105
|
|
|
106
106
|
## License
|
|
107
107
|
|
|
108
|
-
Licensed under [MIT License](LICENSE). Copyright (c) 2017-
|
|
108
|
+
Licensed under [MIT License](LICENSE). Copyright (c) 2017-2022 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
|
|
109
109
|
|
|
110
110
|
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
|
|
111
111
|
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { normalizeHttpResponse } from '@middy/util';
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
|
|
5
|
-
} = require('@middy/util');
|
|
6
|
-
|
|
7
|
-
const getOrigin = (incomingOrigin, options) => {
|
|
8
|
-
if ((options === null || options === void 0 ? void 0 : options.origins.length) > 0) {
|
|
3
|
+
const getOrigin = (incomingOrigin, options = {}) => {
|
|
4
|
+
if (options.origins.length > 0) {
|
|
9
5
|
if (incomingOrigin && options.origins.includes(incomingOrigin)) {
|
|
10
6
|
return incomingOrigin;
|
|
11
7
|
} else {
|
|
@@ -40,12 +36,10 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
40
36
|
};
|
|
41
37
|
|
|
42
38
|
const httpCorsMiddlewareAfter = async request => {
|
|
43
|
-
var
|
|
39
|
+
var _getVersionHttpMethod;
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
request.response = normalizeHttpResponse(request.response);
|
|
48
|
-
const existingHeaders = Object.keys(request.response.headers); // Check if already setup the header Access-Control-Allow-Credentials
|
|
41
|
+
normalizeHttpResponse(request);
|
|
42
|
+
const existingHeaders = Object.keys(request.response.headers);
|
|
49
43
|
|
|
50
44
|
if (existingHeaders.includes('Access-Control-Allow-Credentials')) {
|
|
51
45
|
options.credentials = request.response.headers['Access-Control-Allow-Credentials'] === 'true';
|
|
@@ -53,27 +47,21 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
53
47
|
|
|
54
48
|
if (options.credentials) {
|
|
55
49
|
request.response.headers['Access-Control-Allow-Credentials'] = String(options.credentials);
|
|
56
|
-
}
|
|
57
|
-
|
|
50
|
+
}
|
|
58
51
|
|
|
59
52
|
if (options.headers && !existingHeaders.includes('Access-Control-Allow-Headers')) {
|
|
60
53
|
request.response.headers['Access-Control-Allow-Headers'] = options.headers;
|
|
61
|
-
}
|
|
62
|
-
|
|
54
|
+
}
|
|
63
55
|
|
|
64
56
|
if (options.methods && !existingHeaders.includes('Access-Control-Allow-Methods')) {
|
|
65
57
|
request.response.headers['Access-Control-Allow-Methods'] = options.methods;
|
|
66
|
-
}
|
|
67
|
-
|
|
58
|
+
}
|
|
68
59
|
|
|
69
60
|
if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const eventHeaders = (_request$event$header = (_request$event3 = request.event) === null || _request$event3 === void 0 ? void 0 : _request$event3.headers) !== null && _request$event$header !== void 0 ? _request$event$header : {};
|
|
73
|
-
const incomingOrigin = (_eventHeaders$origin = eventHeaders.origin) !== null && _eventHeaders$origin !== void 0 ? _eventHeaders$origin : eventHeaders.Origin;
|
|
61
|
+
const eventHeaders = request.event.headers ?? {};
|
|
62
|
+
const incomingOrigin = eventHeaders.origin ?? eventHeaders.Origin;
|
|
74
63
|
request.response.headers['Access-Control-Allow-Origin'] = options.getOrigin(incomingOrigin, options);
|
|
75
|
-
}
|
|
76
|
-
|
|
64
|
+
}
|
|
77
65
|
|
|
78
66
|
if (options.exposeHeaders && !existingHeaders.includes('Access-Control-Expose-Headers')) {
|
|
79
67
|
request.response.headers['Access-Control-Expose-Headers'] = options.exposeHeaders;
|
|
@@ -81,30 +69,42 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
81
69
|
|
|
82
70
|
if (options.maxAge && !existingHeaders.includes('Access-Control-Max-Age')) {
|
|
83
71
|
request.response.headers['Access-Control-Max-Age'] = String(options.maxAge);
|
|
84
|
-
}
|
|
85
|
-
|
|
72
|
+
}
|
|
86
73
|
|
|
87
74
|
if (options.requestHeaders && !existingHeaders.includes('Access-Control-Request-Headers')) {
|
|
88
75
|
request.response.headers['Access-Control-Request-Headers'] = options.requestHeaders;
|
|
89
|
-
}
|
|
90
|
-
|
|
76
|
+
}
|
|
91
77
|
|
|
92
|
-
if (options
|
|
78
|
+
if (options.requestMethods && !existingHeaders.includes('Access-Control-Request-Methods')) {
|
|
93
79
|
request.response.headers['Access-Control-Request-Methods'] = options.requestMethods;
|
|
94
80
|
}
|
|
95
81
|
|
|
96
|
-
|
|
82
|
+
const httpMethod = (_getVersionHttpMethod = getVersionHttpMethod[request.event.version ?? '1.0']) === null || _getVersionHttpMethod === void 0 ? void 0 : _getVersionHttpMethod.call(getVersionHttpMethod, request.event);
|
|
83
|
+
|
|
84
|
+
if (!httpMethod) {
|
|
85
|
+
throw new Error('Unknown API Gateway Payload format');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (httpMethod === 'OPTIONS') {
|
|
97
89
|
if (options.cacheControl && !existingHeaders.includes('Cache-Control')) {
|
|
98
90
|
request.response.headers['Cache-Control'] = String(options.cacheControl);
|
|
99
91
|
}
|
|
100
92
|
}
|
|
101
93
|
};
|
|
102
94
|
|
|
103
|
-
const httpCorsMiddlewareOnError =
|
|
95
|
+
const httpCorsMiddlewareOnError = async request => {
|
|
96
|
+
if (request.response === undefined) return;
|
|
97
|
+
return httpCorsMiddlewareAfter(request);
|
|
98
|
+
};
|
|
99
|
+
|
|
104
100
|
return {
|
|
105
101
|
after: httpCorsMiddlewareAfter,
|
|
106
102
|
onError: httpCorsMiddlewareOnError
|
|
107
103
|
};
|
|
108
104
|
};
|
|
109
105
|
|
|
110
|
-
|
|
106
|
+
const getVersionHttpMethod = {
|
|
107
|
+
'1.0': event => event.httpMethod,
|
|
108
|
+
'2.0': event => event.requestContext.http.method
|
|
109
|
+
};
|
|
110
|
+
export default httpCorsMiddleware;
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-cors",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-alpha.2",
|
|
4
4
|
"description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
|
|
5
|
-
"type": "
|
|
5
|
+
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=14"
|
|
8
8
|
},
|
|
9
9
|
"engineStrict": true,
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
13
|
+
"exports": "./index.js",
|
|
14
14
|
"types": "index.d.ts",
|
|
15
15
|
"files": [
|
|
16
|
+
"index.js",
|
|
16
17
|
"index.d.ts"
|
|
17
18
|
],
|
|
18
19
|
"scripts": {
|
|
@@ -44,11 +45,11 @@
|
|
|
44
45
|
"url": "https://github.com/middyjs/middy/issues"
|
|
45
46
|
},
|
|
46
47
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
47
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "de30419273ecbff08f367f47c7e320ec981cf145",
|
|
48
49
|
"dependencies": {
|
|
49
|
-
"@middy/util": "^
|
|
50
|
+
"@middy/util": "^3.0.0-alpha.2"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"@middy/core": "^
|
|
53
|
+
"@middy/core": "^3.0.0-alpha.2"
|
|
53
54
|
}
|
|
54
55
|
}
|