A super simple lightweight extensible router to take the AWS Lambda routing pain away from API Gateway.
- Simple to use
- Extensible
- Tiny
- Includes TypeScript types
Just import the router and define your routes as objects. You can add as many routes as you need.
The following would match on all GET
requests:
import { router, HttpMethod, methodMatcher } from 'aws-lambda-assuage';
export const handler = router([
{
matcher: methodMatcher(HttpMethod.Get),
handler: (event) => {
console.log('API Gateway Event:', event)
return {
statusCode: 200,
body: 'Hello World!'
};
}
}
]);
Tip: Use the
priority
field if you have multiple routes which might match
Matchers are simple functions which determine if the route should match on the incoming event. There are a couple built in matchers, but it's super easy to build your own.
Built in matchers:
methodMatcher
- matches on a given methodmatchEverything
- matchers everythingcomposeMatchers
- higher order function for composing matchers together
export const catMatch = (method: HttpMethod) => ({ requestContext }: APIGatewayProxyEventV2) =>
requestContext.http.path === 'cat';
Need to run something before your handler fires? Enrichers run before your route and are added as the second parameter to the handler.
Simply add it to the route using the enricher
.
import { router, HttpMethod, methodMatcher } from 'aws-lambda-assuage';
export const handler = router([
{
matcher: methodMatcher(HttpMethod.Get),
enricher: e => 'LASER DRAGON', // Can be any type you want
handler: (event, extra) => {
console.log('API Gateway Event:', event)
console.log('Enriched Content:', extra)
return {
statusCode: 200,
body: `Hello, ${extra}!` // Produces 'Hello LASER DRAGON'
};
}
}
]);
Submit an issue before contributing please.