@middy/http-cors 2.5.4 → 3.0.0-alpha.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2021 Luciano Mammino, will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
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
@@ -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-2021 Luciano Mammino, will Farrell, and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
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
@@ -1,6 +1,7 @@
1
1
  import middy from '@middy/core'
2
2
 
3
- interface Options {
3
+ export interface Options {
4
+ getOrigin?: (incomingOrigin: string, options: Options) => string
4
5
  credentials?: boolean | string
5
6
  headers?: string
6
7
  methods?: string
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@middy/http-cors",
3
- "version": "2.5.4",
3
+ "version": "3.0.0-alpha.0",
4
4
  "description": "CORS (Cross-Origin Resource Sharing) middleware for the middy framework",
5
- "type": "commonjs",
5
+ "type": "module",
6
6
  "engines": {
7
- "node": ">=12"
7
+ "node": ">=14"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "index.js",
13
+ "exports": "./index.js",
14
14
  "types": "index.d.ts",
15
15
  "files": [
16
16
  "index.d.ts"
@@ -44,11 +44,11 @@
44
44
  "url": "https://github.com/middyjs/middy/issues"
45
45
  },
46
46
  "homepage": "https://github.com/middyjs/middy#readme",
47
- "gitHead": "a4134a579c757a9fdfed3006877ba2c0ec8a2cfa",
47
+ "gitHead": "c533f62841c8a39d061d7b94f30ba178f002c8db",
48
48
  "dependencies": {
49
- "@middy/util": "^2.5.4"
49
+ "@middy/util": "^3.0.0-alpha.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@middy/core": "^2.5.4"
52
+ "@middy/core": "^3.0.0-alpha.0"
53
53
  }
54
54
  }
package/index.js DELETED
@@ -1,110 +0,0 @@
1
- "use strict";
2
-
3
- const {
4
- normalizeHttpResponse
5
- } = require('@middy/util');
6
-
7
- const getOrigin = (incomingOrigin, options) => {
8
- if ((options === null || options === void 0 ? void 0 : options.origins.length) > 0) {
9
- if (incomingOrigin && options.origins.includes(incomingOrigin)) {
10
- return incomingOrigin;
11
- } else {
12
- return options.origins[0];
13
- }
14
- } else {
15
- if (incomingOrigin && options.credentials && options.origin === '*') {
16
- return incomingOrigin;
17
- }
18
-
19
- return options.origin;
20
- }
21
- };
22
-
23
- const defaults = {
24
- getOrigin,
25
- credentials: undefined,
26
- headers: undefined,
27
- methods: undefined,
28
- origin: '*',
29
- origins: [],
30
- exposeHeaders: undefined,
31
- maxAge: undefined,
32
- requestHeaders: undefined,
33
- requestMethods: undefined,
34
- cacheControl: undefined
35
- };
36
-
37
- const httpCorsMiddleware = (opts = {}) => {
38
- const options = { ...defaults,
39
- ...opts
40
- };
41
-
42
- const httpCorsMiddlewareAfter = async request => {
43
- var _request$event, _request$event2, _request$event2$reque, _request$event2$reque2;
44
-
45
- // API Gateway v1 & v2
46
- if (!((_request$event = request.event) !== null && _request$event !== void 0 && _request$event.httpMethod) && !((_request$event2 = request.event) !== null && _request$event2 !== void 0 && (_request$event2$reque = _request$event2.requestContext) !== null && _request$event2$reque !== void 0 && (_request$event2$reque2 = _request$event2$reque.http) !== null && _request$event2$reque2 !== void 0 && _request$event2$reque2.method)) return;
47
- request.response = normalizeHttpResponse(request.response);
48
- const existingHeaders = Object.keys(request.response.headers); // Check if already setup the header Access-Control-Allow-Credentials
49
-
50
- if (existingHeaders.includes('Access-Control-Allow-Credentials')) {
51
- options.credentials = request.response.headers['Access-Control-Allow-Credentials'] === 'true';
52
- }
53
-
54
- if (options.credentials) {
55
- request.response.headers['Access-Control-Allow-Credentials'] = String(options.credentials);
56
- } // Check if already setup Access-Control-Allow-Headers
57
-
58
-
59
- if (options.headers && !existingHeaders.includes('Access-Control-Allow-Headers')) {
60
- request.response.headers['Access-Control-Allow-Headers'] = options.headers;
61
- } // Check if already setup Access-Control-Allow-Methods
62
-
63
-
64
- if (options.methods && !existingHeaders.includes('Access-Control-Allow-Methods')) {
65
- request.response.headers['Access-Control-Allow-Methods'] = options.methods;
66
- } // Check if already setup the header Access-Control-Allow-Origin
67
-
68
-
69
- if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
70
- var _request$event$header, _request$event3, _eventHeaders$origin;
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;
74
- request.response.headers['Access-Control-Allow-Origin'] = options.getOrigin(incomingOrigin, options);
75
- } // Check if already setup Access-Control-Expose-Headers
76
-
77
-
78
- if (options.exposeHeaders && !existingHeaders.includes('Access-Control-Expose-Headers')) {
79
- request.response.headers['Access-Control-Expose-Headers'] = options.exposeHeaders;
80
- }
81
-
82
- if (options.maxAge && !existingHeaders.includes('Access-Control-Max-Age')) {
83
- request.response.headers['Access-Control-Max-Age'] = String(options.maxAge);
84
- } // Check if already setup Access-Control-Request-Headers
85
-
86
-
87
- if (options.requestHeaders && !existingHeaders.includes('Access-Control-Request-Headers')) {
88
- request.response.headers['Access-Control-Request-Headers'] = options.requestHeaders;
89
- } // Check if already setup Access-Control-Request-Methods
90
-
91
-
92
- if (options !== null && options !== void 0 && options.requestMethods && !existingHeaders.includes('Access-Control-Request-Methods')) {
93
- request.response.headers['Access-Control-Request-Methods'] = options.requestMethods;
94
- }
95
-
96
- if (request.event.httpMethod === 'OPTIONS') {
97
- if (options.cacheControl && !existingHeaders.includes('Cache-Control')) {
98
- request.response.headers['Cache-Control'] = String(options.cacheControl);
99
- }
100
- }
101
- };
102
-
103
- const httpCorsMiddlewareOnError = httpCorsMiddlewareAfter;
104
- return {
105
- after: httpCorsMiddlewareAfter,
106
- onError: httpCorsMiddlewareOnError
107
- };
108
- };
109
-
110
- module.exports = httpCorsMiddleware;