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

@dotmh/lambda-controller

v2.1.0

Published

A controller to work on AWS Lambda behind API Gateway

Downloads

62

Readme

Lambda Controller Logo

Serverless Lambda Controller

DotMH Future Gadget Lab Buy us a tree NPM npm bundle size Contributor Covenant

Build Status

A class to help make lambda function behind AWS API Gateway.

It exposes some common methods to allow you to use Lambda behind API Gateway more as you would making a normal HTTP app.

Installation

To install

npm i @dotmh/lamda-controller

Usage

Create a new controller

import {LambdaController} from '@dotmh/lambda-controller';

class MyController extends LambdaController {}

You then need to declare a method or methods to handle your requests

import {LambdaController} from '@dotmh/lambda-controller';

class MyController extends LambdaController {
  handler() {
    // Your logic goes here
  }
}

This will contain your functions logic for that request

Lastly create a function to call the handler on your controller, and export the function as your serverless function

import {LambdaController} from '@dotmh/lambda-controller';

class MyController extends LambdaController {
  handler() {
    // Your logic goes here
  }
}

module.exports.handler = (event, ctx, callback) => {
  new MyController(event, ctx, callback).handler();
};

For API , see Documentation

Extending

To keep the library as small as possible it doesn't include some functionality that you may need. This includes request body handling and also functionality like cookies etc. However, the system is designed to be extended. The extention system is based on mixin's these are just normal JS objects that are mixed in to the Lambda controller class.

A mixin that adds a function (method) and getter would look like this

const mixin = {
  hello: () => 'Hello',
  get bye() {
    return 'Goodbye';
  },
};

You can then add the mixin to Lamda controller using the add method.

// ...
new MyController(event, ctx, callback).add(mixin).handler();
// ...

Inside your Controller class (the class that extends Lambda Controller) you can use the mixin methods , getters and setters as if they were originally defined on the main Lambda controller class.

import {LambdaController} from '@dotmh/lambda-controller';

class MyController extends Controller {
  handler() {
    return this.bye;
  }
}

Initialising

You may want to do somethings on intialization of the extending mixin. Normally you would use the constructor for this but because of the way the addon system works, the constructor A) Can not be overridden or extended, and B) would have already have been invoked. For this purpose you can use an "init" function.

To use an init function declare a function called init on your mixin.

const mixin = {
  init() {
    // ... do something
  },
  get foo() {
    return 'bar';
  },
};

Your init function wont appear on the Controller after it has been added, but will be called when the mixin is added to the controller class. It is called in the context of the controller so this will refer to the controller object.

Plugins

Lambda Controller Plugin

DotMH has created a number of plugins to add extra functionality to Lambda Controller

Lamdda Controler Request Body

Adds Request body handling to Lambda Controller

Lambda Controller Cors

Adds Cross Origin Resource Sharing support Lambda Controller

Documentation

For the API documentation see https://dotmh.github.io/lambda-controller

Or to read locally

npm run readdocs

Licence

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Credits

Logo design by @dotmh