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

Lambda Controller Cors

Adds Cross Origin Resource Sharing support Lambda Controller

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