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

agw-lambda-proxy

v1.0.5

Published

AWS API Gateway Lambda Proxy Integration module

Downloads

4

Readme

What is this?

Helper module for creating Lambda functions for handling AWS API Gateway Lambda Proxy Integration calls. See AWS docs for details.

Provides common error handling and response formatting for proxy Lambda functions.

Installation

Simply install with npm:

npm install agw-lambda-proxy

Usage

The module exports contains a function createHandler that can be used for generating the Lambda handler function. The generated function handles the event, context, and callback parameters that the Lambda runtime passes so it can be directly used as the handler for your Lambda function.

The generator function accepts two parameters: delegate and options. The delegate is a mandatory parameter and must be a function. This function is defined by the caller and can be used for processing the Lambda request - when Lambda function is invoked the framework passes the event and context parameters to the delegate for request processing. The return value of the delegate function is used for generating the Lambda function response and it should be either a promise or a direct value if asynchronous processing is not needed. Rejections and thrown errors are automatically handled. See Error handling for details.

The options parameter can be used for overriding some of the defaults in the request and response processing, including response headers.

Example

Assuming you've put "index.handler" as your Lambda handler value you can use following index.js for simply returning a response with body "static string", status code 200 and default headers (see CORS & response headers for details).

const delegate = (event, context) => {
  return Promise.resolve('static string')
}
module.exports = {
  handler: require('agw-lambda-proxy').createHandler(delegate)
}

Response formatting

The delegate function can return the response in few different ways. For simple responses the function can return a string value. In this case the response's body will be the returned string and default status code 200 is used. This case is shown above.

If the return value is an object and the object contains one of the attributes statusCode, body, or headers the Lambda response is generated from these attributes and default values for missing attribute. If the body is not a string value it is automatically stringified.

const delegate = (event, context) => {
  return callSomeCreationFunction()
    .then((created) => {
      return {
        statusCode: 201,
        body: {
          id: created.id,
          message: "Object created"
        }
      }
    })
}
module.exports = {
  handler: require('agw-lambda-proxy').createHandler(delegate)
}

The handling for each attribute is specified in the table below:

attribute|When specified|Default value ---------|--------------|------------- statusCode|Parsed as an integer HTTP status code. If parsing fails, default is used.|200 body|If string, returned as is. Otherwise stringified with JSON.stringify|'' headers|Properties used as HTTP response headers.|See CORS & response headers

If the returned object does not have any of the attributes statusCode, body or headers the object is stringified as response body and default status code 200 and headers are used:

const delegate = (event, context) => {
  return {
    foo: 'bar'
  }
}
module.exports = {
  handler: require('agw-lambda-proxy').createHandler(delegate)
}

CORS & response headers

Default headers

By default, the Lambda function response will specify the header "Access-Control-Allow-Origin" with value "*". The default behaviour can be changed by providing an options object with headers attribute in the handler creation:

const defaultHeaders = {
  'Access-Control-Allow-Origin': 'https://github.com',
  'Some-Other-Default-Header': 'header value'
}
module.exports = {
  handler: require('agw-lambda-proxy').createHandler(delegate, {headers:defaultHeaders})
}

Note that when overwritten default headers don't contain a value for 'Access-Control-Allow-Origin' the response will not have the default CORS header.

Response headers

When using the object format in the delegate return value the headers can also be specified for individual responses:

const delegate = (event, context) => {
  return {
    body: 'headers from return value will overwrite default headers if same keys are found',
    headers: {
      overwrite: 'from response'
    }
  }
}
const defaultHeaders = {
  overwrite: 'from defaults'
}
module.exports = {
  handler: require('agw-lambda-proxy').createHandler(delegate, {headers:defaultHeaders})
}

In the example above the response headers will contain value "from response" for the header "overwrite" as delegate return value headers will take precedence over default header values.

Error handling

The modules handles thrown errors as well as rejections from the delegate function. These errors are automatically converted to Lamda Proxy Integration responses with appropriate status code. The default error code is 500 but it can be overwritten by specifying code attribute on the thrown error or on the rejection:

const delegateThatThrows = (event, context) => {
  const error = new Error('message')
  error.code = 404
  throw error // or return Promise.reject(error)
}

The error message will be set to the "message" property of the response. This can be customized by providing an errorFormatter function in the generator options. The response body contains also a direct link to CloudWatch Logs for this request. Returning the log link can be disabled by setting value false for key cloudWatchLogLinks in the generator options.

const delegate = (event, context) => {
  //
}
const options = {
  cloudWatchLogLinks: false,
  logErrors: true,
  errorFormatter: (error) => error.message.substring(0, 20)
}
module.exports = {
  handler: require('agw-lambda-proxy').createHandler(delegate, options)
}

License

MIT. Copyright (c) Olli Vättö.