Skip to content

Commit

Permalink
Add apiGateway object to match aws-serverless-express
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmoscrop committed Jan 20, 2021
1 parent 7bce9cf commit 6f37483
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/provider/aws/create-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function requestUrl(event) {
});
}

module.exports = (event, options) => {
module.exports = (event, context, options) => {
const method = requestMethod(event);
const remoteAddress = requestRemoteAddress(event);
const headers = requestHeaders(event);
Expand All @@ -80,5 +80,10 @@ module.exports = (event, options) => {
});

req.requestContext = event.requestContext;
req.apiGateway = {
event,
context,
};

return req;
};
2 changes: 1 addition & 1 deletion lib/provider/aws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = options => {
return getResponse => async (event_, context = {}) => {
const event = cleanUpEvent(event_, options);

const request = createRequest(event, options);
const request = createRequest(event, context, options);
const response = await getResponse(request, event, context);

return formatResponse(event, response, options);
Expand Down
20 changes: 20 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ describe('spec', () => {
delete requestV2.event;
delete requestV1.requestContext;
delete requestV2.requestContext;
delete requestV1.apiGateway;
delete requestV2.apiGateway;

expect(JSON.stringify(requestV1)).to.equal(JSON.stringify(requestV2));
});
Expand Down Expand Up @@ -333,6 +335,24 @@ describe('spec', () => {
expect(length).to.equal(13);
});

it('should add apiGateway with event/context to req', async () => {
const body = `{"foo":"অ"}`;

let captured;

const handler = serverless((req, res) => {
captured = req;
res.end('');
});

const event = { body };
const context = { test: true };

await handler(event, context);

expect(captured.apiGateway).to.deep.equal({ event, context });
});

it('should throw if event.body is a number', async () => {
const body = 42;

Expand Down

0 comments on commit 6f37483

Please sign in to comment.