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

aws-lambda-res

v1.0.2

Published

is a tiny helper to create a response object for AWS Lambda with Proxy Integration

Downloads

15

Readme

aws-lambda-res

is a tiny helper to create a response object for AWS Lambda with Proxy integration

Usage | Annotated source | License

NPM version No deps JavaScript Style Guide KLP

Usage

Suppose you have an API Gateway resource with a method configured with Lambda Proxy integration.

proxy flag

Suppose you have a dummy endpoint which returns JSON { "ok": true }, then the following code will be a working implementation.

const response = require('aws-lambda-res')

function handler (event, context, callback) {
  callback(null, response(200)({ ok: true }))
}

exports.handler = handler

You can pass headers as second argument, body can be null: for example to logout and redirect to homepage you can use something like

function handler (event, context, callback) {
  const Expires = 'Sat, 01 Jan 2000 00:00:00 GMT' // Some day in the past.

  callback(null, response(302)(null, {
    'Location': 'https://example.org',
    'set-cookie': `user_authenticated=; Domain=example.com; Expires=${Expires}`
  }))
}

exports.handler = handler

Let me write few tips I want to remember. When a method on API Gateway is configured with Lambda Proxy integration no additional mapping is needed. Everything you need will be available in event argument.

To get a JSON payload, just parse it from body.

function handler (event, context, callback) {
  const { id, name } = JSON.parse(event.body)

  // Follows your code...
}

To extract form parameters, or from any POST or PUT method, use querystring package.

const querystring = require('querystring')

function handler (event, context, callback) {
  const { email, password } = querystring.parse(event.body)

  // Follows your code...
}

To access query string parameters in a GET do

function handler (event, context, callback) {
  const { param1, param2 } = event.queryStringParameters

  // Follows your code...
}

To get cookies, parse event.cookie.

function handler (event, context, callback) {
  const cookies = event.cookie.split(';')

  let session

  cookies.forEach(cookie => {
    if (cookie.indexOf('session=') === 0) {
      session = cookie.split('=')[1]
    }
  })

  // Follows your code...
}

To get headers, look into event.headers. For example, you can get a JWT header with the following snippet.

function handler (event, context, callback) {
  const auth = event.headers.Authorization

  const token = (auth && auth.startsWith('BEARER ')) ? auth.substring(7) : null

  // Follows your code...
}

For route placeholders, for example suppose you want to handle an enpoint like GET /user/{id}, go to API Gateway and create first a resource with path /user. Then create a child resource with path /{id} and add a GET method. The code of associated Lambda function will be something like the following.

function handler (event, context, callback) {
  const { id } = event.pathParameters

  // Follows your code...
}

Annotated source

// This code is generated by command: npm run markdown2code

Lambda Proxy integration needs a response with the following properties:

  • isBase64Encoded
  • statusCode
  • headers
  • body
/**
 * Create a response helper.
 *
 * @params {Number} statusCode
 * @returns {Function} awsLambdaResponse helper
 */

function response (statusCode) {

The exported response function, requires statusCode argument and returns an helper function which accepts parameters:

  1. body: can be any data, even null.
  2. headers, defaults to { 'Content-type': 'application/json' }.
  /**
   * AWS Lambda response helper.
   *
   * @params {Object|null} body
   * @params {Object} [headers]
   * @returns {Object} responseObj required by AWS Lambda Proxy integration
   */

  function awsLambdaResponse (
    body,
    headers = { 'Content-Type': 'application/json' }
  ) {
    const responseObj = {
      isBase64Encoded: false,
      headers,
      statusCode,
      body: body === null ? null : JSON.stringify(body)
    }

    return responseObj
  }

Return helper and export function

  return awsLambdaResponse
}

module.exports = response

License

MIT