Skip to content

Commit

Permalink
API Gateway Support (#4)
Browse files Browse the repository at this point in the history
* Added commented out support for API Gateway to Serverless config
* Added a normalize response helper to support API Gateway
* Tweaked API GW support for POST instead of GET
Will now return the "Event type not supported" error if the httpMethod is a GET request, as body on GET is not standard practice.
  • Loading branch information
chrislentz authored and luisfarzati committed Jan 16, 2020
1 parent cd08176 commit 39626f3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
12 changes: 11 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ provider:
timeout: 30
tracing:
lambda: true
# See the API Gateway event below for additional details on api keys
# apiKeys:
# - key1

functions:
captureScreenshot:
Expand Down Expand Up @@ -51,12 +54,19 @@ functions:
IS_MOBILE: "false"
IS_LANDSCAPE: "false"
events:
# SNS Event
- sns:
arn: !Ref chromdaTopic
topicName: ${self:custom.snsTopic}
# API Gateway Events
# - http:
# path: capture
# method: post
# Uncomment the provider api key section above as well as the line
# below to enable private endpoint support
# private: true
# TODO: add SQS event
# TODO: add Scheduled event
# TODO: add API Gateway event

layers:
chromda:
Expand Down
3 changes: 2 additions & 1 deletion src/captureScreenshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { URL } = require("url");
const chromeLambda = require("chrome-aws-lambda");
const MrPuppetshot = require("mrpuppetshot");
const normalizeEvent = require("./helpers/normalizeEvent");
const normalizeResponse = require("./helpers/normalizeResponse");
const S3Bucket = require("./helpers/s3bucket");

// Defaults
Expand Down Expand Up @@ -131,5 +132,5 @@ exports.handler = async (event, callback) => {

const response = await S3Bucket.upload(buffer, imageType, options.s3key);

return response;
return normalizeResponse(event, response);
};
2 changes: 1 addition & 1 deletion src/helpers/normalizeEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const normalizedEvent = event => {
);
}
return JSON.parse(event.Records[0].body);
} else if ("httpMethod" in event) {
} else if ("httpMethod" in event && event.httpMethod === "POST") {
return JSON.parse(event.body);
} else if ("time" in event) {
return event.detail;
Expand Down
26 changes: 26 additions & 0 deletions src/helpers/normalizeResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @typedef {import('aws-lambda').APIGatewayEvent} APIGatewayEvent
* @typedef {import('aws-lambda').ScheduledEvent} ScheduledEvent
* @typedef {import('aws-lambda').SNSEvent} SNSEvent
* @typedef {import('aws-lambda').SQSEvent} SQSEvent
*/

/**
* @param {APIGatewayEvent|ScheduledEvent|SNSEvent|SQSEvent} event
* @param response
*/

const normalizedResponse = (event, response) => {
if ("httpMethod" in event) {
return {
"statusCode": 200,
"body": JSON.stringify(response),
"isBase64Encoded": false
};
}
else {
return response;
}
};

module.exports = normalizedResponse;

0 comments on commit 39626f3

Please sign in to comment.