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

aws-lambda-handler-framework

v0.2.0

Published

A handler framework for AWS Lambda.

Downloads

6

Readme

AWS Lambda Handler Framework

A handler framework for AWS Lambda.

Getting Started

This package consists of the following packages. For detailed information about each package, see its documentation.

Prerequisites

The following versions of Node.js and TypeScript are required:

  • Node.js 20 or higher
  • TypeScript 4.7 or higher

This package is pure ESM, and you must configure your project to use the ESM package.

Installation

1. Install the packages using npm

npm install aws-lambda-handler-framework
npm install --save-dev @types/aws-lambda

2. Set compiler options in your tsconfig.json to enable experimental support for stage 2 decorators and metadata

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

Usage

The following example shows basic usage:

import { APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
import { Application, EntryPoint, Event, Handler } from 'aws-lambda-handler-framework';
import { Injectable, provide } from 'aws-lambda-handler-framework/dependency-injection';

@Injectable()
export class GreeterService {
    public greet(greeting: string): string {
        return `Hello, ${greeting}`;
    }
}

@Handler()
export class GreetHandler implements Handler {
    readonly #greeterService: GreeterService;

    public constructor(
        greeterService: GreeterService,
    ) {
        this.#greeterService = greeterService;
    }

    public execute(
        @Event()
        event: APIGatewayProxyEventV2,
    ): APIGatewayProxyResultV2 {
        return {
            statusCode: 200,
            body: JSON.stringify({
                message: this.#greeterService.greet(event.queryStringParameters?.greeting ?? 'world!'),
            }),
        };
    }
}

export const application: Application = new Application({
    handler: GreetHandler,
    providers: [
        provide({
            identifier: GreeterService,
            useClass: GreeterService,
        }),
    ],
});

export const handler: EntryPoint = application.createEntryPoint();

Middleware

You can use middleware to extend the functionality of your application.

Middleware is executed before and after the handler. It's like an onion structure.

@Middleware()
class ExampleMiddleware implements Middleware {
    public async use(
        @Next()
        next: Next,
    ): Promise<unknown> {
        // before ...

        const result: unknown = await next();

        // after ...

        return result;
    }
}

Error handling

You can handle errors that occur in handler and middleware with the try...catch statement.

@Middleware()
class ErrorHandlingMiddleware implements Middleware {
    public async use(
        @Next()
        next: Next,
    ): Promise<unknown> {
        try {
            return await next();
        }
        catch (error) {
            // ...
        }
    }
}

Early return

If you want to stop the execution flow and return the result immediately, return the result without calling the next function.

@Middleware()
class CacheMiddleware implements Middleware {
    #cache: unknown;

    public constructor() {
        this.#cache = null;
    }

    public async use(
        @Next()
        next: Next,
    ): Promise<unknown> {
        if (this.#cache !== null) {
            return this.#cache;
        }

        return this.#cache = await next();
    }
}

License

Distributed under the MIT License. See the LICENSE file for more details.