npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lambda-lib

v4.5.1

Published

Decorators and tools for AWS Lambda

Downloads

24

Readme

Build Status License NPM codecov

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

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

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

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

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

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 for samples