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

lamda-api-gateway-event-parser

v1.0.1

Published

When using API Gateway with AWS Lamda, the HTTP data comes to the Lamda function in the form of an "event" object. This package helps you to parse that "event" object and extract the data from it.

Downloads

23

Readme

Video Version Of The Docs (Click on thumbnail below)

What Does This Package Do? The Goal.

When you connect your AWS Lambda function to an AWS API Gateway HTTP API, you will get an "event" objection as a parameter.

This package helps you easily an uniformly extract the parameters from the event object.

The package, handles the following types of HTTP events / requests:

  1. Simple GET Request with Query String Params
  2. POST, PUT, PATCH, DELETE request with application/x-www-form-urlencoded form data.
  3. POST, PUT, PATCH, DELETE request with multipart/form-data form data.
  4. JSON Body of HTTP Request
  5. XML Body of HTTP Request

What you get as an output..

In all the above cases, you get as an output an object with 3 to 5 keys with the below shape:

{
	userAgent: 'The user agent of the caller (in-case you need that)',
	originalEvent: {}, // the whole original event object, just in-case.
	prams: {}, // A nice neat prams object irrespective of the type of input HTTP event.
	error: 'In case there is an error parsing the XML or JSON, you get an error here.',
	[xmlString / jsonString]: 'The original XML / JSON string in-case you need that and are not happy with the parsing.' 
}

Quick Start

How to install?

The usual:

nmp i lamda-api-gateway-event-parser
yarn add lamda-api-gateway-event-parser

How to use?

Usually, parsing the event will be the very first thing you do in your Lamda Function. So, just add it like so..

const eventParser = require('lamda-api-gateway-event-parser'); // Bring it in.

exports.handler = async (event) => {
	let niceNeatParsedEvent = eventParser.parse(event); // Parsing the event.
    // All the other awesome things you need to do
};

About File Uploads & multipart/form-data Events

If the event we get is of the type: multipart/form-data, the package will extract all the form fields as usual and make a nice neat "params" object as described above.

In the case of the file, the contents of the file will be saved into the "tmp" folder (which is provided by AWS Lamda). When the "params" object is looked at, it will look like the following:

params: {
	simple_param_1: "Simple text value",
	file_upload_param_name: {
		type: 'file',
		filename: 'the name of the file',
		contentType: 'content type eg: image/jpeg',
		path: 'file path in lamda environment. eg: "/tmp/cat.jpeg"'
	}
}

Major credits to: https://github.com/myshenin/aws-lambda-multipart-parser for this part. But the repo is a little outdated and no longer maintained.

Not working as expected? Check Here!

There are 2 assumptions this package makes (if your Lambda function is not running as per these assumptions, it may not work):

  1. On the API Gateway side, we are using HTTP API (not REST API). Why? Because, its FASTER & CHEAPER. More info here.
  2. API Gateway version 2 (latest). This version has a different "event" object structure from version 1. Because of this, the package might not be able to identify the "event type" and deploy the correct parser. This is the default from AWS for new functions right now.