Skip to content

Commit 2650649

Browse files
authored
Merge pull request #57 from stevenyap/api-gateway-lambda-proxy
Catering to Lambda proxy enabled API request/response
2 parents 4c6b344 + 66662ce commit 2650649

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

lib/serve.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = {
5050
endpoint = `/${this.options.stage}${endpoint}`;
5151
}
5252
const path = endpoint.replace(/\{(.+?)\}/g, ':$1');
53-
let handler = this._handlerBase(funcConf);
53+
let handler = this._handlerBase(funcConf, httpEvent);
5454
let optionsHandler = this._optionsHandler;
5555
if (httpEvent.cors) {
5656
handler = this._handlerAddCors(handler);
@@ -101,22 +101,28 @@ module.exports = {
101101
};
102102
},
103103

104-
_handlerBase(funcConf) {
104+
_handlerBase(funcConf, httpEvent) {
105+
const isLambdaProxyIntegration = httpEvent && httpEvent.integration !== 'lambda'
106+
105107
return (req, res) => {
106108
const func = funcConf.handlerFunc;
107109
const event = {
108110
method: req.method,
109111
headers: req.headers,
110112
body: req.body,
111113
path: req.params,
112-
query: req.query,
114+
[isLambdaProxyIntegration ? 'queryStringParameters' : 'query']: req.query
113115
// principalId,
114116
// stageVariables,
115117
};
116118
const context = this.getContext(funcConf.id);
117119
func(event, context, (err, resp) => {
118120
if (err) {
119-
res.status(500).send(err);
121+
return res.status(500).send(err);
122+
}
123+
124+
if (isLambdaProxyIntegration) {
125+
res.status(resp.statusCode).send(resp.body);
120126
} else {
121127
res.status(200).send(resp);
122128
}

tests/serve.test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ describe('serve', () => {
6464
id: 'testFuncId',
6565
handlerFunc: testHandlerFunc,
6666
};
67+
const testHttpEvent = {
68+
integration: 'lambda'
69+
}
6770
const testReq = {
6871
method: 'testmethod',
6972
headers: 'testheaders',
@@ -76,7 +79,7 @@ describe('serve', () => {
7679
};
7780
testRes.status = sinon.stub().returns(testRes);
7881
module.getContext = sinon.stub().returns('testContext');
79-
const handler = module._handlerBase(testFuncConf);
82+
const handler = module._handlerBase(testFuncConf, testHttpEvent);
8083
handler(testReq, testRes);
8184
expect(testRes.status).to.have.been.calledWith(200);
8285
expect(testRes.send).to.have.been.calledWith(testHandlerResp);
@@ -111,6 +114,44 @@ describe('serve', () => {
111114
expect(testRes.status).to.have.been.calledWith(500);
112115
expect(testRes.send).to.have.been.calledWith(testHandlerErr);
113116
});
117+
118+
it('handles lambda-proxy integration for request and response', () => {
119+
const testHandlerResp = { statusCode: 200, body: 'testHandlerResp' };
120+
const testHandlerFunc = sinon.spy((ev, ct, cb) => {
121+
cb(null, testHandlerResp);
122+
});
123+
const testFuncConf = {
124+
id: 'testFuncId',
125+
handlerFunc: testHandlerFunc,
126+
};
127+
const testHttpEvent = {}
128+
const testReq = {
129+
method: 'testmethod',
130+
headers: 'testheaders',
131+
body: 'testbody',
132+
params: 'testparams',
133+
query: 'testquery',
134+
};
135+
const testRes = {
136+
send: sinon.spy(),
137+
};
138+
testRes.status = sinon.stub().returns(testRes);
139+
module.getContext = sinon.stub().returns('testContext');
140+
const handler = module._handlerBase(testFuncConf, testHttpEvent);
141+
handler(testReq, testRes);
142+
expect(testRes.status).to.have.been.calledWith(testHandlerResp.statusCode);
143+
expect(testRes.send).to.have.been.calledWith(testHandlerResp.body);
144+
expect(testHandlerFunc).to.have.been.calledWith(
145+
{
146+
body: 'testbody',
147+
headers: 'testheaders',
148+
method: 'testmethod',
149+
path: 'testparams',
150+
queryStringParameters: 'testquery',
151+
},
152+
'testContext'
153+
);
154+
});
114155
});
115156

116157
describe('_optionsHandler', () => {

0 commit comments

Comments
 (0)