lambda-kernel

2.2.1 • Public • Published

Lambda Kernel

Kernel to handle and provide structure to requests and responses for AWS Lambda and Serverless.

view on npm

This module provides the necessary building blocks for you to build a structured Lambda function. Initially it is targeting Serverless v1, but easily used for vanilla Lambda.

Install

npm i -S lambda-kernel

Basic Usage

// handler.js (the lambda entrypoint)
 
const Kernel = require('lambda-kernel');
// controller is your own business logic
const controller = require('./lib/controller');
 
// instantiate the kernel, global configuration
// can be passed into the constructor here
const kernel = new Kernel();
 
module.exports = {
  exampleAction: kernel.dispatch(controller.exampleAction)
};
// ./lib/controller.js (your business logic)
 
const Response = require('lambda-kernel/lib/Response');
 
module.exports = {
  exampleAction: (req, env) => {
    return new Response(`this is an example in ${env.stage}`, 200);
  }
};

Middleware

Middleware provides a clean and re-usable way to instantiate services needed for your actions. You can create middleware by extending the middleware class and passing your middleware instances in an array into the Kernel constructor to apply it globally or as a parameter of the dispatch method to apply it to that action only.

Middleware#create is passed the same req and env parameters as controller methods. The return value of the Middleware#create method will then be provided to the controller method in the order that the middleware was provided (after the req, env parameters).

Action-specific middleware is always provided in order after the Kernel middleware.

Classes

ServerlessTransformerTransformer

Transformer to handle Serverless v1's default Lambda integration event.

Transformer

Abstract class that all transformers should extend and implement the interface as below

DictAccessor

Simple Dictionary (Map) accessor for data.

Kernel

The core class of Lambda-Kernel, use this at the root of your application to handle the request to response lifecycle.

Middleware

Abstract class that your Middleware must extend and implement the interface as below.

Request

Request object provided to your action by the dispatcher. You use this to access the event request data.

Response

You must return a Response from your action.

ServerlessTransformer ⇐ Transformer

Transformer to handle Serverless v1's default Lambda integration event.

Kind: global class
Extends: Transformer

serverlessTransformer.parseFromEvent(event) ⇒ RequestData

Kind: instance method of ServerlessTransformer

Param Type Description
event object lambda integration event

serverlessTransformer.parsePath(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parsePath

Param Type Description
event object lambda integration event

serverlessTransformer.parseMethod(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parseMethod

Param Type Description
event object lambda integration event

serverlessTransformer.parseHeaders(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parseHeaders

Param Type Description
event object lambda integration event

serverlessTransformer.parseQuery(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parseQuery

Param Type Description
event object lambda integration event

serverlessTransformer.getRawBody(event) ⇒ string

Return the raw body string from the event structure

Kind: instance abstract method of ServerlessTransformer
Overrides: getRawBody

Param Type
event object

Transformer

Abstract class that all transformers should extend and implement the interface as below

Kind: global abstract class

new Transformer(bodyParser)

Param Type
bodyParser function

transformer.parseFromEvent(event) ⇒ RequestData

Kind: instance method of Transformer

Param Type Description
event object lambda integration event

transformer.parsePath(event) ⇒ object

Kind: instance abstract method of Transformer

Param Type Description
event object lambda integration event

transformer.parseMethod(event) ⇒ object

Kind: instance abstract method of Transformer

Param Type Description
event object lambda integration event

transformer.parseHeaders(event) ⇒ object

Kind: instance abstract method of Transformer

Param Type Description
event object lambda integration event

transformer.parseQuery(event) ⇒ object

Kind: instance abstract method of Transformer

Param Type Description
event object lambda integration event

transformer.getRawBody(event) ⇒ string

Return the raw body string from the event structure

Kind: instance abstract method of Transformer

Param Type
event object

DictAccessor

Simple Dictionary (Map) accessor for data.

Kind: global class

dictAccessor.has(key) ⇒ boolean

Check whether the dictionary has the provided key.

Kind: instance method of DictAccessor

Param Type Description
key string dictionary key to check

dictAccessor.get(key, defaultVal) ⇒ *

Ger the value for the provided key, or the provided default value if the key does not exist. Otherwise returns null.

Kind: instance method of DictAccessor

Param Type Description
key string dictionary key to get
defaultVal default value if key doesn't exist

dictAccessor.all() ⇒ Object

Return a clone of this dictionary as a simple JavaScript Object.

Kind: instance method of DictAccessor

Kernel

The core class of Lambda-Kernel, use this at the root of your application to handle the request to response lifecycle.

Kind: global class

new Kernel([options])

Construct a new kernel with the provided options.

Param Type
[options] Object
[options.transformer] Transformer
[options.middlewares] Array.<Middleware>
[options.forceEnd] boolean

kernel.dispatch(action, [middleware]) ⇒

Public dispatch method to interface with the Lambda integration layer.

Kind: instance method of Kernel
Returns: function
Access: public

Param Type Description
action function function to invoke for the request
[middleware] Array.<Middleware> any additional middleware for this request

Middleware

Abstract class that your Middleware must extend and implement the interface as below.

Kind: global abstract class

new Middleware()

The constructor is the opportune time to pass any config information which can be later used in the create() method.

middleware.create(req, env) ⇒

Return the value which should be passed to the controller method.

Kind: instance abstract method of Middleware
Returns: any

Param Type Description
req Request Request transformed from Lambda integration event
env Object Lambda context information

middleware.destroy()

Perform any cleanup required before the termination of the request.

Kind: instance abstract method of Middleware

Request

Request object provided to your action by the dispatcher. You use this to access the event request data.

Kind: global class

request.path ⇒ string

The HTTP path requested

Kind: instance property of Request

request.method ⇒ string

The HTTP request method

Kind: instance property of Request

request.query ⇒ DictAccessor

The HTTP request path query params

Kind: instance property of Request

request.headers ⇒ DictAccessor

The HTTP request headers

Kind: instance property of Request

request.body ⇒ DictAccessor

The HTTP request body

Kind: instance property of Request

Response

You must return a Response from your action.

Kind: global class

new Response(body, [code], [headers])

Construct a new response

Param Type Default Description
body object object to be serialized for HTTP body
[code] number 200 integer HTTP code
[headers] object key/value map of any HTTP headers

response.simplified() ⇒ Object

Generate and return Lambda integration object for the response.

Kind: instance method of Response


© 2017 Matthew Webb

Package Sidebar

Install

npm i lambda-kernel

Weekly Downloads

1

Version

2.2.1

License

MIT

Last publish

Collaborators

  • mcwebb