JSPM

aws-simple-lambda-router

0.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q31674F
  • License MIT

Class to route apigateway requests inside lambda

Package Exports

  • aws-simple-lambda-router

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (aws-simple-lambda-router) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Lightweight promise based router for AWS Lambda invoked by AWS Apigateway lambda proxy integration

Inspiration

Usage

Define your common middleware, routes, and default route.

const Router = require('lambda-router');
const routes = require('./routes'); // your routes
const authorizer = require('./authorizer'); // your authorizer
let router = new Router({
  middleware: [
      middleware1,
      middleware2
  ],
  routes: [
      [ 'GET', '/widgets', routes.getWidgets ],
      [ 'GET', '/widgets/:widgetId', [ authorizer.widget.get, routes.getWidget ] ],
      [ 'PUT', '/widgets/:widgetId', [ authorizer.widget.put, routes.putWidget ] ]
  ],
  defaultRoute: routes.defaultRoute
});

Use the router

module.exports.handler = function (event, context, callback) {
  event._context = context // lambda-router only passes the event object. I do this if I need the context object.
  return router.route(event)
  .then(result => {
      // post route processing
      callback(null, result)
  })
  .catch(err => {
      // handle the error here
  })
}

Middleware

Middleware is a function that takes the lambda event as a parameter, performs some work, including modifying the event and returns the event (or another object). The result is passed to the next middleware function. Middleware can return a promise of the event object or the event object.

Middleware errors/rejections are not handled.

How it works

Each common middleware function is called in turn with the results from the preceeding middleware. The first middleware function is called with the event object passed into router.route. After the common middleware executes, the correct route is determined using the path-to-regexp.

The path parameters are added to the event.pathParameters object to make migrating from apigateway routing simpler.

After the route is determine the route's middleware is called in the same manner as the common middleware. Finally the route handler is called.

Using the above example router configuration and the request GET /widgets/1234 router in effect makes these calls

Promise.resolve(event)
.then(middleware1)
.then(middleware2)
.then(router.checkRoute) // this is where the route is determined and path parameters are added to the event
.then(authorizer.widget.get)
.then(routes.getWidget)