Skip to content

Commit c30d959

Browse files
authored
Implement a simple API Gateway event (#530)
Only 'body'.
1 parent 42ad7c6 commit c30d959

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

bin/node-lambda

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ program
146146
.option('-M, --enableRunMultipleEvents [ENABLE_RUN_MULTIPLE_EVENTS]', 'Enable run multiple events',
147147
ENABLE_RUN_MULTIPLE_EVENTS)
148148
.option('-y, --proxy [PROXY]', 'Proxy server', PROXY)
149+
.option('--apiGateway', 'Convert to API Gateway events', false)
149150
.action((prg) => lambda.run(prg))
150151

151152
program

lib/main.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ event_sources.json and ${program.eventFile} files as needed.`)
8080
return this._runMultipleHandlers(event)
8181
}
8282
context.local = true
83-
this._runHandler(handler, event, program, context)
83+
const eventObject = (() => {
84+
if (program.apiGateway) {
85+
return this._convertToApiGatewayEvents(event)
86+
}
87+
return event
88+
})()
89+
this._runHandler(handler, eventObject, program, context)
8490
}
8591

8692
_runHandler (handler, event, program, context) {
@@ -176,6 +182,16 @@ so you can easily test run multiple events.
176182
})
177183
}
178184

185+
_convertToApiGatewayEvents (event) {
186+
console.log(`!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
187+
Emulate only the body of the API Gateway event.
188+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
189+
`)
190+
return {
191+
body: JSON.stringify(event)
192+
}
193+
}
194+
179195
_isUseS3 (program) {
180196
if (typeof program.deployUseS3 === 'boolean') {
181197
return program.deployUseS3

test/node-lambda.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,38 @@ describe('bin/node-lambda', () => {
244244
})
245245
})
246246
})
247+
248+
describe('node-lambda run (API Gateway events))', () => {
249+
const eventObj = {
250+
asyncTest: false,
251+
callbackWaitsForEmptyEventLoop: true,
252+
callbackCode: 'callback(null);'
253+
}
254+
255+
it('`node-lambda run` exitCode is `0`', function (done) {
256+
_generateEventFile(eventObj)
257+
const run = spawn('node', [
258+
nodeLambdaPath, 'run',
259+
'--handler', 'index.handler',
260+
'--eventFile', 'event.json',
261+
'--apiGateway'
262+
])
263+
let stdoutString = ''
264+
run.stdout.on('data', (data) => {
265+
stdoutString += data.toString().replace(/\r|\n|\s/g, '')
266+
})
267+
268+
run.on('exit', (code) => {
269+
const expected = '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!EmulateonlythebodyoftheAPIGatewayevent.' +
270+
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Runningindex.handler==================================event' +
271+
'{body:\'{"asyncTest":false,"callbackWaitsForEmptyEventLoop":true,"callbackCode":"callback(null);"}\'}' +
272+
'==================================Stoppingindex.handlerSuccess:'
273+
assert.equal(stdoutString, expected)
274+
assert.equal(code, 0)
275+
done()
276+
})
277+
})
278+
})
247279
})
248280

249281
describe('node-lambda duplicate check of short option', () => {

0 commit comments

Comments
 (0)