forked from CodeGenieApp/serverless-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
23 lines (19 loc) · 856 Bytes
/
middleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
module.exports.eventContext = options => (req, res, next) => {
options = options || {} // defaults: {reqPropKey: 'apiGateway', deleteHeaders: true}
const reqPropKey = options.reqPropKey || 'apiGateway'
const deleteHeaders = options.deleteHeaders === undefined ? true : options.deleteHeaders
if (!req.headers['x-apigateway-event'] || !req.headers['x-apigateway-context']) {
console.error('Missing x-apigateway-event or x-apigateway-context header(s)')
next()
return
}
req[reqPropKey] = {
event: JSON.parse(decodeURIComponent(req.headers['x-apigateway-event'])),
context: JSON.parse(decodeURIComponent(req.headers['x-apigateway-context']))
}
if (deleteHeaders) {
delete req.headers['x-apigateway-event']
delete req.headers['x-apigateway-context']
}
next()
}