Last active
April 22, 2020 13:41
-
-
Save buggy/b1fbcc99caa0ee0c0e2edf8fd64180f9 to your computer and use it in GitHub Desktop.
Lambda handler split by HTTP method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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