@sarcasmonaut/aws-lambda-utils
TypeScript icon, indicating that this package has built-in type declarations

0.1.8 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @sarcasmonaut/aws-lambda-utils

Weekly Downloads

0

Version

0.1.8

License

MIT

Unpacked Size

30.2 kB

Total Files

25

Last publish

Collaborators

  • sarcasmonaut