-
Notifications
You must be signed in to change notification settings - Fork 4
/
function.js
48 lines (39 loc) · 1.41 KB
/
function.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
46
47
48
const LambdaReq = require('lambda-req').default
const LambdaReqError = require('lambda-req').LambdaReqError
// event - holds all the Lambda headers and request info
// context - has information about the Lambda runtime - http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
// callback - optional function to be called after the code execution, i.e. callback(error, stringResponse)
function handler (event, context, callback) {
console.log('Lambda params:', JSON.stringify(event))
// initialize Lambda with params
const lambda = new LambdaReq(event, context, callback)
// get | post | put | delete | options
lambda.get('/v1/test', (req, ev)=> {
// access params and headers for APIGateway invocations
const { params, headers } = req
if (params.email !== '[email protected]') {
// Throw on soft errors
throw new LambdaReqError({
message: {
error: 'User not found',
data: {
email: 'Invalid'
}
},
status: 404
})
}
// return on success
return { user: { id: 1 } }
})
// direct task invoke, returns a Promise
lambda.proxy('migrate', (req, ev)=> {
// access params for tasks
const { params } = req
// return a promise as a result, it will be awaited by LambdaReq
return Promise.resolve({ success: true })
})
// execute handlers
lambda.invoke()
}
module.exports = { handler }