1.0.0-0 • Published 9 months ago

aws-lambda-assuage v1.0.0-0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

AWS Lambda Assuage

Build and Test

A super simple lightweight extensible router to take the AWS Lambda routing pain away from API Gateway.

Features

  • Simple to use
  • Extensible
  • Tiny
  • Includes TypeScript types

Getting Started

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

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 method
  • matchEverything - matchers everything
  • composeMatchers - higher order function for composing matchers together

Custom Matcher Example:

export const catMatch = (method: HttpMethod) => ({ requestContext }: APIGatewayProxyEventV2) =>
  requestContext.http.path === 'cat';

Enrichers

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'
      };
    }
  }
]);

Contributing

Submit an issue before contributing please.

1.0.0-0

9 months ago