dandi

🌻A modular DI, MVC, and Model binding/validation framework for NodeJS and TypeScript or ES6

View the Project on GitHub just-dandi/dandi

@dandi-contrib/aws-lambda

@dandi-contrib/aws-lambda provides helpers for using @dandi with the AWS Lambda service.

Concepts

Providing functionality for an AWS Lambda function is broken into several chunks:

The transformer and responder implementations are grouped into modules to make them easy to set up.

Overview

There are 2 pieces of logic required to set up a Lambda function:

API Gateway/HTTP Events

LambdaEventTransformer and LambdaResponder implementations for API Gateway proxied events are provided in the AwsLambdaHttpModule:

// my-handler.ts
import { HttpHandlerRequest, LambdaHandler } from '@dandi-contrib/aws-lambda';
import { Context } from 'aws-lambda';

export class MyHandler implements LambdaHandler<HttpHandlerRequest> {
    public handleEvent(eventData: HttpHandlerRequest, scope?: Context): Promise<any> {
        ...
    }

}

// main.ts
import { AwsLambdaHttpModule, Lambda } from '@dandi-contrib/aws-lambda';
import { MyHandler } from './my-handler';

export handler = Lambda.handler(MyHandler, AwsLambdaHttpModule);

Interceptors

Implementations of HttpResponseInterceptor can be used to modify the response sent by the Lambda function. This can be used, for example, to add extra headers or modify the body or statusCode. Interceptors can be enabled by adding their classes to the Lambda.handler() call:

// my-interceptor.ts
import { Injectable } from '@dandi/core';
import { HttpResponseInterceptor } from '@dandi-contrib/aws-lambda';
import { APIGatewayProxyResult } from 'aws-lambda';

@Injectable(HttpResponseInterceptor)
export class MyInterceptor implements HttpResponseInterceptor {
    public exec(response: APIGatewayProxyResult): void {
    }
}

// main.ts
import { AwsLambdaHttpModule, Lambda } from '@dandi-contrib/aws-lambda';
import { MyInterceptor } from './my-interceptor';
import { MyHandler } from './my-handler';

export handler = Lambda.handler(MyHandler, AwsLambdaHttpModule, MyInterceptor);

Customizing Default Status Codes

By default, the HttpResponder will send a 200 status code for successful requests, and 500 when an error is encountered.

The error code used can be easily changed by throwing errors that have a statusCode property. If the statusCode property is present on an error, that value will be used.

These defaults can be changed by specifying options for the AwsLambdaHttpModule:

// main.ts
import { AwsLambdaHttpModule, Lambda } from '@dandi-contrib/aws-lambda';
import { MyHandler } from './my-handler';

export handler = Lambda.handler(MyHandler, AwsLambdaHttpModule.configure({
    successStatusCode: 201,
    errorStatusCode: 418,
);

Model Validation

AwsLambdaHttpModule can be configured to use model validation features from @dandi/model and @dandi/model-builder:

// my-model.ts
import { Property, Required } from '@dandi/model';

export class MyModel {
    @Property(String)
    @Required()
    public name: string;
}

// main.ts
import { AwsLambdaHttpModule, Lambda } from '@dandi-contrib/aws-lambda';
import { Validation } from '@dandi/model-builder';
import { MyHandler } from './my-handler';
import { MyModel } from './my-model';

export handler = Lambda.handler(MyHandler, Validation, AwsLambdaHttpModule.configure({
    validateBody: MyModel,
);