# lambda-lib

> Decorators and tools for AWS Lambda

Latest version **4.5.1** (published 2018-07-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install lambda-lib
pnpm add lambda-lib
yarn add lambda-lib
bun add lambda-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.5.1 |
| Published | 2018-07-26 |
| First published | 2017-05-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 526.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Steven Inskip |
| Maintainers | skippyza |
| Keywords | lambda, aws, promise, decorator |

## Links

- npm: https://www.npmjs.com/package/lambda-lib
- Repository: https://github.com/SkippyZA/lambda-lib
- Homepage: https://github.com/SkippyZA/lambda-lib#readme
- Issues: https://github.com/SkippyZA/lambda-lib/issues
- npm.io page: https://npm.io/package/lambda-lib

## Dependencies (2)

- [circular-json](https://npm.io/package/circular-json.md) 0.5.1
- [babel-polyfill](https://npm.io/package/babel-polyfill.md) 6.26.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 4.5.1 (latest) — 2018-07-26
- 4.5.0 — 2018-05-03
- 4.4.0 — 2018-04-23
- 4.3.0 — 2018-04-20
- 4.2.0 — 2018-02-07
- 4.1.1 — 2018-01-28
- 4.1.0 — 2018-01-15
- 4.0.0 — 2018-01-14
- 3.0.3 — 2018-01-09
- 3.0.2 — 2018-01-09
- 3.0.1 — 2018-01-07
- 3.0.0 — 2018-01-01
- 2.2.2 — 2017-12-15
- 2.2.1 — 2017-12-14
- 2.2.0 — 2017-12-14
- … 37 more at https://npm.io/package/lambda-lib/versions

## README

[![Build Status](https://travis-ci.org/SkippyZA/lambda-lib.svg?branch=master)](https://travis-ci.org/SkippyZA/lambda-lib)
[![License](https://img.shields.io/github/license/SkippyZA/lambda-lib.svg)](./LICENSE)
[![NPM](https://img.shields.io/npm/v/lambda-lib.svg)](http://npmjs.com/package/lambda-lib)
[![codecov](https://codecov.io/gh/SkippyZA/lambda-lib/branch/master/graph/badge.svg)](https://codecov.io/gh/SkippyZA/lambda-lib)

# AWS Lambda Lib

This library contains a set of decorators to apply to AWS Lambda function to help abstract the event source
and eliminate boilerplate code

## Example

```javascript
import { HandlerController, ApiGateway } from 'lambda-lib'

const errorMap = [
  {
    error: ReferenceError,
    status: 400
  },
  {
    error: Error,
    status: 404
  }
]

@HandlerController
class SampleLambdaHandler {
  @ApiGateway({ statusCode: 200, cors: true })
  helloHandler (event) {
    return Promise.resolve({ hello: world })
  }

  @ApiGateway({ statusCode: 200, errorMap: errorMap })
  failedHandler (event) {
    return Promise.reject(new ReferenceError('I am a reference error'))
  }
}

const handler = new SampleLambdaHandler()

export default handler.getHandlers()
```

### Sample Response

#### HelloHandler Response

```bash
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, PUT, PATCH, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Type: application/json
Date: Tue, 29 Aug 2017 17:07:04 GMT
accept-ranges: bytes
cache-control: no-cache
content-length: 39
vary: origin,accept-encoding

{
  "hello": "world"
}
```


#### FailedHandler Response

```bash
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Type: application/json
Date: Tue, 29 Aug 2017 17:08:00 GMT
cache-control: no-cache
content-length: 398
vary: accept-encoding

{
  "error": {
    "message": "I am a reference error",
    "name": "ReferenceError",
    "_stackTrace": [
      "ReferenceError: I am a reference error",
      "at SampleLambdaHandler.failedHandler (/.../src/resources/example/index.js:223:15)",
      "at /.../node_modules/lambda-lib/lib/api-gateway.js:93:19",
      "at process._tickDomainCallback (internal/process/next_tick.js:135:7)"
    ]
  }
}
```

## Specifying a custom error response

```javascript
import { Plugins } from 'lambda-lib'

// Registering a custom error response plugin. This is applied globally.
ApiGateway.registerPlugin(new Plugins.ErrorResponse(err => {
  return {
    test: 'This is the error response body for all errors',
    error: err.message
  }
}))
```

#### Sample response with custom error

```bash
HTTP/1.1 400 Bad Request
Connection: keep-alive
Content-Type: application/json
Date: Tue, 29 Aug 2017 17:08:00 GMT
cache-control: no-cache
content-length: 398
vary: accept-encoding

{
  "test": "This is the error response body for all errors",
  "error": "I am a reference error"
}
```

## Included Plugins

| Plugin           | Description                                                    |
| ---------------- | -------------------------------------------------------------- |
| cors             | When true, a set of default CORS headers are added to the response. Such as: `Access-Control-Allow-Origin`, `Access-Control-Allow-Credentials`, `Access-Control-Allow-Methods` |
| statusCode       | Default status code for the response. Any object resolved via a Promise in the handler, will get this status code. |
| errorMap         | Mapping of error types to response codes for rejected promises. |
| errorResponse    | Format the response of an error. |

## Plugin Hooks

| Hook             | Description                                                    |
| ---------------- | -------------------------------------------------------------- |
| INITIALIZE       | Initialize is executed right at the beginning of the request, before any default plugins have been executed |
| PRE_EXECUTE      | The pre execute hook is run right before the execution of handler code. |
| POST_EXECUTE     | This hook, post execute, is run after the execution of the handler code. |
| ON_ERROR         | When ever there is an error which results in a rejected promise, this hook is executed. |
| FINALLY          | Final hook executed after the response has been sent to the client already. (Unable to manipulate response contents here) |

## Writing your own plugin

See [built-in plugins](./src/plugins) for samples

---
_Source: https://npm.io/package/lambda-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
