Skip to content

Instantly share code, notes, and snippets.

@buggy
Last active April 22, 2020 13:41
Show Gist options
  • Save buggy/b1fbcc99caa0ee0c0e2edf8fd64180f9 to your computer and use it in GitHub Desktop.
Save buggy/b1fbcc99caa0ee0c0e2edf8fd64180f9 to your computer and use it in GitHub Desktop.
Lambda handler split by HTTP method
import { APIGatewayEvent, APIGatewayProxyHandler, Callback, Context } from "aws-lambda";
export function RestHandler(
onCreate?: APIGatewayProxyHandler,
onUpdate?: APIGatewayProxyHandler,
onDelete?: APIGatewayProxyHandler
) {
return async (event: APIGatewayEvent, context: Context, callback: Callback) => {
switch (event.httpMethod) {
case "DELETE":
return onDelete(event, context, callback);
case "PATCH":
return onUpdate(event, context, callback);
case "POST":
return onCreate(event, context, callback);
}
};
}
import { RestHandler } from "common";
function deleteHandler(event: APIGatewayEvent, context: Context, callback: Callback) {
// Do something
}
function createHandler(event: APIGatewayEvent, context: Context, callback: Callback) {
// Do something
}
function updateHandler(event: APIGatewayEvent, context: Context, callback: Callback) {
// Do something
}
// This is the only entry point exported for the Lambda. It will call the correct create, update, delete method base on the event.httpMethod
export const handler = RestHandler(createHandler, updateHandler, deleteHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment