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

@igloobuster/aws_lambda_middleware

v1.0.7

Published

Middleware and annotation for AWS Lambda

Downloads

32

Readme

@igloobuster/aws_lambda_middleware

Middleware and annotations in typescript for AWS lambda

NPM

installations

npm install --save @igloobuster/aws_lambda_middleware

Changelog

  • v1.0.5

    • response.json() set ContentType automatically
  • v1.0.4

    • Fix parsing argumentNames with default values
  • v1.0.2

    • Add Pre and Post middlewares for methods
  • v1.0.1

    • Bug Fix on Node12.19+, proto made http_agent crash, change it by a Map and className
  • v0.1.9

    • Bug Fix on noResponse
    • Bug Fix on missing location header on response.redirect
  • v0.1.8

    • Bug Fix on boolean params that were not cast properly
  • v0.1.7

    • Adding addClass to the router to have one class instance per request
    • Adding the annotation ClassController wich add a class to the router

how It works

Lambda middleware provide a way to chain middleware for AWS lambda before executing the request

It provides body parser, middlewares and tools to build the response for aws

Getting Started

Declare handlers

You need to declare function to handle the lambda call. I prefer using class but it's not mandatory

import {LambdaRequest, Router, Response} from "@igloobuster/aws_lambda_middleware/dist/middleware/Router";
import {BodyParserMiddleware} from "@igloobuster/aws_lambda_middleware/dist/middleware/BodyParserMiddleware";


class HelloWorld {

	constructor(router: Router) {
		router.add(exports, "hello", (req: LambdaRequest<any>, response: Response) => this.hello(req, response))
	}

	private async hello(req: LambdaRequest<any>, response: Response) {
		response.json({hello: "world"});
	}
}

const router = new Router([new BodyParserMiddleware()]);
new HelloWorld(router);

And that's all you need to do, the router will export your function with the hello name and handle it with the middleware mechanism

In the LambdaRequest Object thanks to the BodyParserMiddleware you have access to the body of the request. You also have access to all the path parameters and query strings.

For example :

private async hello(req: LambdaRequest<any>, response: Response) {
	response.json({hello: req.pathParameters, body: req.json, age: req.queryStringParameters.age});
}

Declare Class handlers (v0.1.7+)

If you need to have one instance per request you can use router.addClass instead of adding method. You need to add every method inside your class in order to get it work

For example :

import {Router, LambdaRequest, Response} from "@igloobuster/aws_lambda_middleware/dist/middleware/Router";
import {BodyParserMiddleware} from "@igloobuster/aws_lambda_middleware/dist/middleware/BodyParserMiddleware";

class ClassControllerExample {
	
    constructor (req : LambdaRequest<any>, response : Response) {
		// Here we can initialize our class with the request if needed
	}

	public async echo(req: LambdaRequest<any>, response: Response) {
		console.log(req.json);
		response.json({...req.json});
	}
}

const router = new Router([new BodyParserMiddleware()]);
router.addClass(exports, "echo", ClassControllerExample);

Build your own middlewares

Let's say for example that you need to response with cors headers.

import {Config} from "../helper/Config";
import {AbstractMiddleware, LambdaRequest, Response} from "@igloobuster/aws_lambda_middleware/dist/middleware/Router";
import {APIGatewayEventRequestContext, APIGatewayProxyEvent} from "aws-lambda";


export class CorsMiddleware extends AbstractMiddleware<any> {

	protected async after(event: LambdaRequest<any>, context: APIGatewayEventRequestContext, response: Response) {
		if (event.headers) {
			const origin = event.headers.origin || event.headers.Host || event.headers.host;
			if (origin === "https://example/com") {
				response.setHeader('Access-Control-Allow-Origin', origin);
				response.setHeader('Access-Control-Allow-Methods', 'GET,PUT,PATCH,POST,DELETE');
				response.setHeader('Access-Control-Allow-Headers', 'authorization, content-type, x-force-lang, cookie');
				response.setHeader('Access-Control-Expose-Headers', 'x-api-authorization, set-cookie, x-force-lang');
				response.setHeader('Access-Control-Allow-Credentials', 'true');
			}
		}
	}

	protected async before(event: APIGatewayProxyEvent, context: APIGatewayEventRequestContext) {

	}
}

Here we have a before and after function where we can do what we need before or after the execution of the handler In the after function here, we had some headers in the response to allow the CORS for the example.com domain

In our router we can now provide this new middleware:

const router = new Router([new BodyParserMiddleware(), new CorsMiddleware()]);
new HelloWorld(router);

Pre and Post middlewares

Sometime you need a method to have its own middlewares running before or after the class middlewares. You can declare them like this :

const router = new Router([new BodyParserMiddleware(), new CorsMiddleware()]);
new HelloWorld(router, [new PreMiddleware1(), new PreMiddleware2()], [new PostMiddleware1(), new PostMiddleware2()]);

Annotations

The library also provides annotation to make it more easy to use

import {BodyParserMiddleware} from "@igloobuster/aws_lambda_middleware/dist/middleware/BodyParserMiddleware";
import {Controller, custom, Method, param, query} from "@igloobuster/aws_lambda_middleware/dist/Annotations";
import {Router} from "@igloobuster/aws_lambda_middleware/dist/middleware/Router";

@Controller({exports, json: true, router: new Router([new BodyParserMiddleware()])})
export class HelloController {

	@Method()
	private async hello(@param() name: string, @query() age: number = 21) {
		return {hello: name, age};
	};
}

If you need to have on class instance per request use ClassController instead of controller

import {BodyParserMiddleware} from "@igloobuster/aws_lambda_middleware/dist/middleware/BodyParserMiddleware";
import {ClassController, Method, param, query} from "@igloobuster/aws_lambda_middleware/dist/Annotations";
import {Router, LambdaRequest, Response} from "@igloobuster/aws_lambda_middleware/dist/middleware/Router"; import {LambdaRequest} from "./Router";

@ClassController({exports, json: true, router: new Router([new BodyParserMiddleware()])})
export class HelloController {

    constructor (request : LambdaRequest<any>, response : Response) {
    	// do initialization of the class here 
    }

	@Method()
	private async hello(@param() name: string, @query() age: number = 21) {
		return {hello: name, age};
	};
}

The annotation will use the same class that we see before and will exports the hello method. You now have to declare your parameters and the annotation will pass it for you.

Here are the annotations availables:

@param: it will search in the path parameters @query: it will search in the query parameters @body: it will give you the json body @custom: it will give you a custom property contains in the request object @header: If you specify a name, it will search for this specific params in the headers object, it will give you all the raw headers instead @request: To have the full request object @response: To have the full response object