forked from middyjs/middy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (32 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module.exports = opts => {
const defaults = {
payloadFormatVersion: 1
}
const options = Object.assign({}, defaults, opts)
return {
before: (handler, next) => {
const { event } = handler
let isHttpEvent = false
switch (options.payloadFormatVersion) {
case 1:
isHttpEvent = Object.prototype.hasOwnProperty.call(event, 'httpMethod')
break
case 2:
isHttpEvent = Object.prototype.hasOwnProperty.call(event, 'requestContext') &&
Object.prototype.hasOwnProperty.call(event.requestContext, 'http') &&
Object.prototype.hasOwnProperty.call(event.requestContext.http, 'method')
break
default:
throw new Error('Unknow API Gateway Payload format. Please use value 1 or 2.')
}
if (isHttpEvent) {
event.queryStringParameters = event.queryStringParameters || {}
event.pathParameters = event.pathParameters || {}
if (options.payloadFormatVersion === 1) {
event.multiValueQueryStringParameters = event.multiValueQueryStringParameters || {}
}
}
return next()
}
}
}