Skip to content

Commit

Permalink
Merge pull request #3 from lteacher/start-doc-update
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
lteacher authored May 5, 2017
2 parents cb32832 + ff1ad91 commit 6119f92
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 22 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Hugo Armstrong

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
93 changes: 73 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,98 @@
[![Build Status](https://travis-ci.org/lteacher/lambda-chain.svg?branch=master)](https://travis-ci.org/lteacher/lambda-chain)
[![Coverage Status](https://coveralls.io/repos/github/lteacher/lambda-chain/badge.svg?branch=master)](https://coveralls.io/github/lteacher/lambda-chain?branch=master)
[![npm version](https://badge.fury.io/js/lambda-chain.svg)](https://badge.fury.io/js/lambda-chain)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/lteacher/lambda-chain/master/LICENSE.md)

## Details

This package makes it possible to chain aws lambda functions together by registering handlers and hooks (also just handlers) which can occur before or after a given handler, similar to middleware.

The package also promisifies all handlers so that you don't worry about the callback, instead returning or throwing errors appropriately. This is handy if you are maybe using babel with async / await and want to have it work straight away.

Here is a short full example in some `babel`-ified code

```javascript
import lambda from 'lambda-chain';

// Imaginary async stuffs
const handler = async (event, context) => 'handled...';

// Register the handler, this sets up the promise wrap
// and ability to add before or after hooks here or by name
lambda.register(handler);

// Unwrap the handlers as an export -> { handler: Function }
module.exports = lambda.exports();
```

_**Note: ** When using `register`, function names are detected. Different node versions handle function naming differently. You need to consider this when passing function handlers (not hooks). _



## Install

`npm install --save lambda-chain`

## Usage

This package uses function names to manage the handlers which are exported and wrapped. This is fine in Node v6+ but in v4 if you use anonymous functions or expressions then you must provide a function name.
### register(handler, [hooks])

_Registers a handler and optionally the given hooks._

### Nodejs v6
**handler**: Function|Object|Handler

**hooks**: Object - e.g. ``{ before: Function|Array<Function>, after: Function|Array<Function> }``

**Example**
```javascript
const chain = require('lambda-chain');
const lambda = require('lambda-chain');
const handlers = require('./cool/handlers');
const parser = require('./magical/parser');

// This function is named in v6
const todos = (event, context) => {
// Logic
}
lambda.register(handlers, {
before: [parser.fromJSON],
after: [parser.toJSON]
});
```

// Register the handler
chain.register(todos);

module.exports = chain.exports();
### before(handler, [hooks])

```
_Registers a hook or array of hooks to run before the given handler._

### Nodejs v4
**handler**: Function|Handler|String - The handler, or the name of a handler

**hooks**: Function|Array<Function> - The function or functions to execute before the given handler

**Example**
```javascript
const chain = require('lambda-chain');
const lambda = require('lambda-chain');
const handlers = require('./cool/handlers');
const parser = require('./magical/parser');

// This function is NOT named in v4
const noName = (event, context) => {}
lambda.register(handlers);
lambda.before(handler.doStuff, parser.transformThing);
```

// Register an anonymous handler
chain.registerByName('todos', (event, context) => {
// Stuffs
});
### after(handler, [hooks])

module.exports = chain.exports();
_Registers a hook or array of hooks to run after the given handler._

**handler**: Function|Handler|String - The handler, or the name of a handler

**hooks**: Function|Array<Function> - The function or functions to execute after the given handler

**Example**
```javascript
const lambda = require('lambda-chain');
const handlers = require('./cool/handlers');
const {cors} = require('./magical/httpstuffs');

lambda.register(handlers);
lambda.after(handler.doStuff, cors);
```

### Other

There are other ways to use the package but this documentation requires an update. Try taking a look in the tests. For example you can pass an instance of a `Handler` class to the register, such as the `HttpEventHandler` class which just calls the relevant http method.
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-chain",
"version": "0.1.0-alpha",
"version": "0.1.0",
"description": "An AWS lambda handler promies wrapper and chain (i.e middleware) executor",
"main": "index.js",
"scripts": {
Expand All @@ -26,5 +26,13 @@
"dependencies": {
"compare-versions": "^3.0.0",
"lodash": "^4.17.4"
}
},
"keywords": [
"aws",
"awslambda",
"lambda",
"middleware",
"chain",
"handler"
]
}

0 comments on commit 6119f92

Please sign in to comment.