Skip to content

lambda-lambda-lambda/middleware

Repository files navigation

L³ middleware

Collection of plugins for use in your application.

Supported plugins

Name Description
AccessControlHeaders Middleware to send Access-Control-* headers.
AppConfigPlugin Middleware to store the app configuration.
BasicAuthHandler Middleware to prompt Basic Authentication.
CloudFrontCacheHeader Middleware to send CloudFront Cache-Control header.
CookieParserPlugin Middleware to parse/store incoming HTTP cookies.
ContentNegotiationJson Middleware to enforce JSON content negotiation.
ContentTypeJsonHeader Middleware to send JSON Content-Type header.
GoogleRecaptchaHandler Middleware to validate Google ReCAPTCHA invisible responses.
PreflightOptionsHandler Middleware to handle preflight requests.
SessionPluginExample Middleware example of validating a session that uses cookies.
SwaggerUIViewer Middleware to generate Swagger UI viewer.

Installation

Install using the L³ CLI

Usage: lambda-lambda-lambda install [options] <PackageName>

Arguments:
  PackageName           Package name (Example: BasicAuthHandler)

Options:
-h, --help              display help for command

Manual installation

Copy the package to your application /middleware and include accordingly.

Usage

Globally scoped

// .. appName/src/app.js

'use strict';

const Router = require('@lambda-lambda-lambda/router');

// Load middleware.
const plugin1 = require(`${APP_ROOT}/middleware/PluginName1`);
const plugin2 = require(`${APP_ROOT}/middleware/PluginName2`);
const plugin3 = require(`${APP_ROOT}/middleware/PluginName3`);

/**
 * @see AWS::Serverless::Function
 */
exports.handler = (event, context, callback) => {
  const {request, response} = event.Records[0].cf;

  const router = new Router(request, response);

  // Executed in order (all routes) /api/*
  router.use(plugin1);
  router.use(plugin2);
  router.use(plugin3);

    // .. Router Methods
};

Locally scoped

// .. appName/src/routes/foo.js

'use strict';

// Load middleware.
const plugin1 = require(`${APP_ROOT}/middleware/PluginName1`);
const plugin2 = require(`${APP_ROOT}/middleware/PluginName2`);
const plugin3 = require(`${APP_ROOT}/middleware/PluginName3`);

/**
 * @export {Object}
 */
module.exports = {

  // Executed in order (route only) /api/foo
  middleware: [plugin1, plugin2, plugin3],

    // .. Router Methods
};

Developers

CLI options

From the plugin directory:

$ cd plugins/<pluginName>

Run ESLint on project sources:

$ npm run lint

Run Mocha integration tests:

$ npm run test

Contributions

If you fix a bug, or have a code you want to contribute, please send a pull-request with your changes. (Note: Before committing your code please ensure that you are following the Node.js style guide)

Versioning

This package is maintained under the Semantic Versioning guidelines.

License and Warranty

This package is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.

lambda-lambda-lambda/middleware is provided under the terms of the MIT license

Author

Marc S. Brooks