@middy/http-cors 7.1.2 → 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 +7 -2
- package/index.js +13 -14
- package/package.json +5 -5
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-cors">https://middy.js.org/docs/middlewares/http-cors</a></p>
|
|
31
31
|
</div>
|
|
32
32
|
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install --save @middy/http-cors
|
|
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-cors).
|
|
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
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
import type middy from "@middy/core";
|
|
4
4
|
|
|
5
5
|
export interface Options {
|
|
6
|
-
getOrigin?: (
|
|
6
|
+
getOrigin?: (
|
|
7
|
+
incomingOrigin: string,
|
|
8
|
+
options: Options,
|
|
9
|
+
) => string | null | undefined;
|
|
7
10
|
credentials?: boolean | string;
|
|
8
11
|
disableBeforePreflightResponse?: boolean;
|
|
9
12
|
headers?: string;
|
|
@@ -18,6 +21,8 @@ export interface Options {
|
|
|
18
21
|
vary?: string;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
|
-
declare function httpCors(
|
|
24
|
+
declare function httpCors(
|
|
25
|
+
options?: Options,
|
|
26
|
+
): middy.MiddlewareObj<unknown, unknown, Error>;
|
|
22
27
|
|
|
23
28
|
export default httpCors;
|
package/index.js
CHANGED
|
@@ -126,8 +126,7 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
const modifyHeaders = (headers, options, request) => {
|
|
129
|
-
|
|
130
|
-
if (existingHeaders.includes("Access-Control-Allow-Credentials")) {
|
|
129
|
+
if (Object.hasOwn(headers, "Access-Control-Allow-Credentials")) {
|
|
131
130
|
options.credentials =
|
|
132
131
|
headers["Access-Control-Allow-Credentials"] === "true";
|
|
133
132
|
}
|
|
@@ -136,19 +135,19 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
136
135
|
}
|
|
137
136
|
if (
|
|
138
137
|
options.headers &&
|
|
139
|
-
!
|
|
138
|
+
!Object.hasOwn(headers, "Access-Control-Allow-Headers")
|
|
140
139
|
) {
|
|
141
140
|
headers["Access-Control-Allow-Headers"] = options.headers;
|
|
142
141
|
}
|
|
143
142
|
if (
|
|
144
143
|
options.methods &&
|
|
145
|
-
!
|
|
144
|
+
!Object.hasOwn(headers, "Access-Control-Allow-Methods")
|
|
146
145
|
) {
|
|
147
146
|
headers["Access-Control-Allow-Methods"] = options.methods;
|
|
148
147
|
}
|
|
149
148
|
|
|
150
149
|
let newOrigin;
|
|
151
|
-
if (!
|
|
150
|
+
if (!Object.hasOwn(headers, "Access-Control-Allow-Origin")) {
|
|
152
151
|
const eventHeaders = request.event.headers ?? {};
|
|
153
152
|
const incomingOrigin = eventHeaders.Origin ?? eventHeaders.origin;
|
|
154
153
|
newOrigin = options.getOrigin(incomingOrigin, options);
|
|
@@ -171,11 +170,11 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
171
170
|
|
|
172
171
|
if (
|
|
173
172
|
options.exposeHeaders &&
|
|
174
|
-
!
|
|
173
|
+
!Object.hasOwn(headers, "Access-Control-Expose-Headers")
|
|
175
174
|
) {
|
|
176
175
|
headers["Access-Control-Expose-Headers"] = options.exposeHeaders;
|
|
177
176
|
}
|
|
178
|
-
if (options.maxAge && !
|
|
177
|
+
if (options.maxAge && !Object.hasOwn(headers, "Access-Control-Max-Age")) {
|
|
179
178
|
headers["Access-Control-Max-Age"] = String(options.maxAge);
|
|
180
179
|
}
|
|
181
180
|
const httpMethod = getVersionHttpMethod[request.event.version ?? "1.0"]?.(
|
|
@@ -184,13 +183,13 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
184
183
|
if (
|
|
185
184
|
httpMethod === "OPTIONS" &&
|
|
186
185
|
options.cacheControl &&
|
|
187
|
-
!
|
|
186
|
+
!Object.hasOwn(headers, "Cache-Control")
|
|
188
187
|
) {
|
|
189
188
|
headers["Cache-Control"] = options.cacheControl;
|
|
190
189
|
}
|
|
191
190
|
};
|
|
192
191
|
|
|
193
|
-
const httpCorsMiddlewareBefore =
|
|
192
|
+
const httpCorsMiddlewareBefore = (request) => {
|
|
194
193
|
if (options.disableBeforePreflightResponse) return;
|
|
195
194
|
|
|
196
195
|
const method = getVersionHttpMethod[request.event.version ?? "1.0"]?.(
|
|
@@ -240,15 +239,15 @@ const httpCorsMiddleware = (opts = {}) => {
|
|
|
240
239
|
}
|
|
241
240
|
};
|
|
242
241
|
|
|
243
|
-
const httpCorsMiddlewareAfter =
|
|
242
|
+
const httpCorsMiddlewareAfter = (request) => {
|
|
244
243
|
normalizeHttpResponse(request);
|
|
245
|
-
const headers =
|
|
244
|
+
const headers = { ...request.response.headers };
|
|
246
245
|
modifyHeaders(headers, options, request);
|
|
247
246
|
request.response.headers = headers;
|
|
248
247
|
};
|
|
249
|
-
const httpCorsMiddlewareOnError =
|
|
250
|
-
if (request.response === undefined) return;
|
|
251
|
-
|
|
248
|
+
const httpCorsMiddlewareOnError = (request) => {
|
|
249
|
+
if (typeof request.response === "undefined") return;
|
|
250
|
+
httpCorsMiddlewareAfter(request);
|
|
252
251
|
};
|
|
253
252
|
return {
|
|
254
253
|
before: httpCorsMiddlewareBefore,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/http-cors",
|
|
3
|
-
"version": "7.1.
|
|
3
|
+
"version": "7.1.3",
|
|
4
4
|
"description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -63,12 +63,12 @@
|
|
|
63
63
|
"type": "github",
|
|
64
64
|
"url": "https://github.com/sponsors/willfarrell"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431",
|
|
67
66
|
"dependencies": {
|
|
68
|
-
"@middy/util": "7.1.
|
|
67
|
+
"@middy/util": "7.1.3"
|
|
69
68
|
},
|
|
70
69
|
"devDependencies": {
|
|
71
|
-
"@middy/core": "7.1.
|
|
72
|
-
"@types/aws-lambda": "^8.0.0"
|
|
70
|
+
"@middy/core": "7.1.3",
|
|
71
|
+
"@types/aws-lambda": "^8.0.0",
|
|
72
|
+
"@types/node": "^22.0.0"
|
|
73
73
|
}
|
|
74
74
|
}
|