2.2.1 • Published 7 years ago

lambda-kernel v2.2.1

Weekly downloads
13
License
MIT
Repository
github
Last release
7 years ago

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

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

ParamTypeDescription
eventobjectlambda integration event

serverlessTransformer.parsePath(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parsePath

ParamTypeDescription
eventobjectlambda integration event

serverlessTransformer.parseMethod(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parseMethod

ParamTypeDescription
eventobjectlambda integration event

serverlessTransformer.parseHeaders(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parseHeaders

ParamTypeDescription
eventobjectlambda integration event

serverlessTransformer.parseQuery(event) ⇒ object

Kind: instance abstract method of ServerlessTransformer
Overrides: parseQuery

ParamTypeDescription
eventobjectlambda integration event

serverlessTransformer.getRawBody(event) ⇒ string

Return the raw body string from the event structure

Kind: instance abstract method of ServerlessTransformer
Overrides: getRawBody

ParamType
eventobject

Transformer

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

Kind: global abstract class

new Transformer(bodyParser)

ParamType
bodyParserfunction

transformer.parseFromEvent(event) ⇒ RequestData

Kind: instance method of Transformer

ParamTypeDescription
eventobjectlambda integration event

transformer.parsePath(event) ⇒ object

Kind: instance abstract method of Transformer

ParamTypeDescription
eventobjectlambda integration event

transformer.parseMethod(event) ⇒ object

Kind: instance abstract method of Transformer

ParamTypeDescription
eventobjectlambda integration event

transformer.parseHeaders(event) ⇒ object

Kind: instance abstract method of Transformer

ParamTypeDescription
eventobjectlambda integration event

transformer.parseQuery(event) ⇒ object

Kind: instance abstract method of Transformer

ParamTypeDescription
eventobjectlambda integration event

transformer.getRawBody(event) ⇒ string

Return the raw body string from the event structure

Kind: instance abstract method of Transformer

ParamType
eventobject

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

ParamTypeDescription
keystringdictionary 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

ParamTypeDescription
keystringdictionary key to get
defaultValdefault 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.

ParamType
optionsObject
options.transformerTransformer
options.middlewaresArray.<Middleware>
options.forceEndboolean

kernel.dispatch(action, middleware) ⇒

Public dispatch method to interface with the Lambda integration layer.

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

ParamTypeDescription
actionfunctionfunction to invoke for the request
middlewareArray.<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

ParamTypeDescription
reqRequestRequest transformed from Lambda integration event
envObjectLambda 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

ParamTypeDefaultDescription
bodyobjectobject to be serialized for HTTP body
codenumber200integer HTTP code
headersobjectkey/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

2.2.1

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.4.0

8 years ago

0.2.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago