-
Notifications
You must be signed in to change notification settings - Fork 38
/
params.js
45 lines (40 loc) · 1.52 KB
/
params.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
38
39
40
41
42
43
44
45
let unUndefined = [ 'body', 'pathParameters', 'queryStringParameters' ]
let unNulled = [ 'body', 'pathParameters', 'queryStringParameters', 'multiValueQueryStringParameters' ]
let pathParams = /\{\w+\}/g
module.exports = function interpolateParams (req) {
// Make the raw body accessible, handy for use with external libraries
if (req.body) req.rawBody = req.body
// Handle HTTP API v2.0 payload scenarios, which omit params instead of passing them as null
if (req?.version === '2.0') {
let { requestContext: context } = req
if (context?.http?.method) {
req.httpMethod = context.http.method
}
unUndefined.forEach(i => {
if (req[i] === undefined) req[i] = {}
})
// Expect 'GET /foo' or '$default'
req.resource = req.routeKey.split(' ')[1] || req.routeKey
// Backfill `req.path`
req.path = req.rawPath
}
// Un-null APIG-proxy-Lambda params in 6+
unNulled.forEach(i => {
if (req[i] === null) req[i] = {}
})
// Convenient params commonplace in other web servers
if (!req.method) req.method = req.httpMethod
if (!req.params) req.params = req.pathParameters
if (!req.query) req.query = req.queryStringParameters
// Convenient path parameter interpolation; 6+ REST gets this for free in `req.path`
if (pathParams.test(req.path)) {
let matches = req.path.match(pathParams)
let vars = matches.map(a => a.replace(/\{|\}/g, ''))
let idx = 0
matches.forEach(m => {
req.path = req.path.replace(m, req.params[vars[idx]])
idx += 1
})
}
return req
}