Last active
December 9, 2020 20:36
-
-
Save TSMMark/74dd83bbf1fa797417d728b35084df61 to your computer and use it in GitHub Desktop.
next.js netlify aws lambda higher order function composition handler middleware
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
export const handler: Handler = compose( | |
httpRespond(), | |
httpMethod('POST'), | |
)(async ({ body }: APIGatewayEvent) => ({ | |
post: await createPost(JSON.parse(body)) | |
})) | |
/* | |
Recently wrote some Next.js/Netlify functions to run in AWS Lambdas. | |
Noticed a lot of code duplication in the handler functions | |
for basic things like | |
- Enforce specific HTTP method | |
- Convert response data to JSON | |
Refactored common logic into higher-order functions, which | |
act as handler middleware. Yay function composition! | |
Full source here: | |
https://github.com/cloworm/guess-that-gif/blob/master/src/lambda/lib/http.ts | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment