@middy/http-cors 2.5.6 → 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 (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +2 -2
  3. package/index.js +23 -14
  4. package/package.json +10 -8
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
@@ -59,7 +59,7 @@ import httpErrorHandler from '@middy/http-error-handler'
59
59
  import cors from '@middy/http-cors'
60
60
 
61
61
  const handler = middy((event, context) => {
62
- throw createError(422)
62
+ throw new createError.UnprocessableEntity()
63
63
  })
64
64
  handler.use(httpErrorHandler())
65
65
  .use(cors())
@@ -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.js CHANGED
@@ -1,7 +1,7 @@
1
- const { normalizeHttpResponse } = require('@middy/util')
1
+ import { normalizeHttpResponse } from '@middy/util'
2
2
 
3
- const getOrigin = (incomingOrigin, options) => {
4
- if (options?.origins.length > 0) {
3
+ const getOrigin = (incomingOrigin, options = {}) => {
4
+ if (options.origins.length > 0) {
5
5
  if (incomingOrigin && options.origins.includes(incomingOrigin)) {
6
6
  return incomingOrigin
7
7
  } else {
@@ -33,10 +33,7 @@ const httpCorsMiddleware = (opts = {}) => {
33
33
  const options = { ...defaults, ...opts }
34
34
 
35
35
  const httpCorsMiddlewareAfter = async (request) => {
36
- // API Gateway v1 & v2
37
- if (!request.event?.httpMethod && !request.event?.requestContext?.http?.method) return
38
-
39
- request.response = normalizeHttpResponse(request.response)
36
+ normalizeHttpResponse(request)
40
37
 
41
38
  const existingHeaders = Object.keys(request.response.headers)
42
39
 
@@ -69,7 +66,7 @@ const httpCorsMiddleware = (opts = {}) => {
69
66
 
70
67
  // Check if already setup the header Access-Control-Allow-Origin
71
68
  if (!existingHeaders.includes('Access-Control-Allow-Origin')) {
72
- const eventHeaders = request.event?.headers ?? {}
69
+ const eventHeaders = request.event.headers ?? {}
73
70
  const incomingOrigin = eventHeaders.origin ?? eventHeaders.Origin
74
71
  request.response.headers[
75
72
  'Access-Control-Allow-Origin'
@@ -102,23 +99,35 @@ const httpCorsMiddleware = (opts = {}) => {
102
99
 
103
100
  // Check if already setup Access-Control-Request-Methods
104
101
  if (
105
- options?.requestMethods &&
102
+ options.requestMethods &&
106
103
  !existingHeaders.includes('Access-Control-Request-Methods')
107
104
  ) {
108
- request.response.headers['Access-Control-Request-Methods'] =
109
- options.requestMethods
105
+ request.response.headers['Access-Control-Request-Methods'] = options.requestMethods
110
106
  }
111
107
 
112
- if (request.event.httpMethod === 'OPTIONS') {
108
+ const httpMethod = getVersionHttpMethod[request.event.version ?? '1.0']?.(request.event)
109
+ if (!httpMethod) {
110
+ throw new Error('Unknown API Gateway Payload format')
111
+ }
112
+ if (httpMethod === 'OPTIONS') {
113
113
  if (options.cacheControl && !existingHeaders.includes('Cache-Control')) {
114
114
  request.response.headers['Cache-Control'] = String(options.cacheControl)
115
115
  }
116
116
  }
117
117
  }
118
- const httpCorsMiddlewareOnError = httpCorsMiddlewareAfter
118
+ const httpCorsMiddlewareOnError = async (request) => {
119
+ if (request.response === undefined) return
120
+ return httpCorsMiddlewareAfter(request)
121
+ }
119
122
  return {
120
123
  after: httpCorsMiddlewareAfter,
121
124
  onError: httpCorsMiddlewareOnError
122
125
  }
123
126
  }
124
- module.exports = httpCorsMiddleware
127
+
128
+ const getVersionHttpMethod = {
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,23 +1,25 @@
1
1
  {
2
2
  "name": "@middy/http-cors",
3
- "version": "2.5.6",
3
+ "version": "3.0.0-alpha.3",
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
+ "index.js",
16
17
  "index.d.ts"
17
18
  ],
18
19
  "scripts": {
19
20
  "test": "npm run test:unit",
20
- "test:unit": "ava"
21
+ "test:unit": "ava",
22
+ "test:benchmark": "node __benchmarks__/index.js"
21
23
  },
22
24
  "license": "MIT",
23
25
  "keywords": [
@@ -44,11 +46,11 @@
44
46
  "url": "https://github.com/middyjs/middy/issues"
45
47
  },
46
48
  "homepage": "https://github.com/middyjs/middy#readme",
47
- "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b",
49
+ "gitHead": "1441158711580313765e6d156046ef0fade0d156",
48
50
  "dependencies": {
49
- "@middy/util": "^2.5.6"
51
+ "@middy/util": "^3.0.0-alpha.3"
50
52
  },
51
53
  "devDependencies": {
52
- "@middy/core": "^2.5.6"
54
+ "@middy/core": "^3.0.0-alpha.3"
53
55
  }
54
56
  }