npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dandi-contrib/aws-lambda

v1.0.0-alpha.77

Published

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

Downloads

38

Readme

@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:

  • LambdaEventTransformer - takes the raw AWS event and converts it into the desired format to be used by the LambdaHandler. Each type of Lambda event trigger will have its own implementation (e.g. HttpEventTransformer)
  • LambdaHandler - Implemented by the consumer (developer), contains the business logic for receiving the transformed event data and doing any processing.
  • LambdaResponder - takes the output of the LambdaHandler and converts it into the format expected by AWS Lambda. As with LambdaEventTransformer, each type of Lambda event has its own implementation (e.g. HttpResponder)

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:

  • Your handler implementation - an implementation of LambdaHandler<TEventData>
  • A "main" file using the Lambda helper, which references your LambdaHandler implementation, as well as a module containing the LambdaEventTransformer and LambdaResponder implementations required for the type of events that will be handled.

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,
);