0.1.8 • Published 3 years ago

@sarcasmonaut/aws-lambda-utils v0.1.8

Weekly downloads
9
License
MIT
Repository
github
Last release
3 years ago

aws-lambda-utils

An opinionated, decorated-based approach to reduce code required to create AWS Lambda handler functions.

Available Decorators

LambdaProxy

A decorator that focuses to process AWS Lambda handler with ProxyIntegration. It will try to make sure to return a valid response that APIGateway can cope with, i.e.:

let res: {
  statusCode: number;
  headers: Record<string, string>;
  body?: string;
};

Lambda Proxy currently supports the following features:

  1. cors header injection
  2. body parsing
  3. response transformation
    1. statusCode
    2. body
  4. error handling
  5. user extraction

cors header injection

Will inject the following cors headers with the event.headers ultimatively returned by the decorated handler:

{
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": true
}

Usage:

  @LambdaProxy()
  class TestClass {
    public static injectCors(_event: any, _context: any) {
      return Promise.resolve();
    }
  }

  console.dir(TestClass.injectCors().headers)
  # >  {
  # >    'Access-Control-Allow-Origin': '*',
  # >    'Access-Control-Allow-Credentials': true
  # >  }

TBD:

  • make injection optional
  • allow custom header injection

body parsing

With the help of the body attribute in the LambdaProxyOpts, we can validate and transform the value provided in event.body:

In order to enable comfortable parsing & transformation of json-based input data, we are using class-transformer and class-validator.