Skip to content

Instantly share code, notes, and snippets.

@TSMMark
Last active December 9, 2020 20:36
Show Gist options
  • Save TSMMark/74dd83bbf1fa797417d728b35084df61 to your computer and use it in GitHub Desktop.
Save TSMMark/74dd83bbf1fa797417d728b35084df61 to your computer and use it in GitHub Desktop.
next.js netlify aws lambda higher order function composition handler middleware
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