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-restful-util

v1.0.2

Published

A lightweight utility for Lambda API development

Downloads

7

Readme

codecov

Lambda RESTful Utility

A simple package to offer some quick wins for API devs!

What is this?

A simple npm package to make API dev easy on AWS Lambda.

Documentation

We've included a docs folder with a Best Practices document. Please review this document as well as our CONTRIBUTING document before getting started with development contributions.

Setup

To contribute to development you must have NodeJS installed on your system. Additionally this project uses yarn instead of npm. Please ensure you have yarn installed globally. After you do, simply run yarn install from the project root to install all dependencies.

Using this product

To use this package in your work simply run npm install lambda-restful-util or yarn add lambda-restful-util then include it in your code as with any other dependency.

Using the validateAndParseRequestHeaders or validateAndParseRequestBody

Both the validateAndParseRequestHeaders and validateAndParseRequestBody operate very similarly. Simply pass the event from API Gateway and both return a truthy object you can use if they're valid. For example:

exports.handler = async (event: APIGatewayProxyEvent) => {
  const requestHeaders = utils.validateAndParseRequestHeaders(event)
  const requestBody = utils.validateAndParseRequestBody(event)

  if (requestHeaders.Authorization && requestBody) {
    const token = requestHeaders.Authorization.replace('Bearer ', '')
    ...
  }
  ...
}

Using the withStatusCode function

To use the withStatusCode you only need to specify the response code and the request origin (for CORS). An example of a simple 200 response is as follows:

import util from 'lambda-restful-util'
...
const ok = util.withStatusCode(200, 'http://localhost:8080')

exports.handler = async (event: APIGatewayProxyEvent) => {
  ...
  return ok('Hey Buddy!')
}

For convenience this package includes every HTTP response for reference. To use the HttpStatusCode enum you can modify the above example by modifying the var: const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080).

Adding a formatter

In addition to the HttpStatusCode you can pass a formatting function as an optional argument to withStatusCode. To add JSON.stringify simply modify the var again: const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080, JSON.stringify).

If you know your response is going to be JSON this will simplify converting your Object to JSON. For example:

...
const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080, JSON.stringify)
...
const res = {
  name: 'Homer Simpson'
  employer: 'Springfield Power Plant'
}
...
return ok(res)

The above will correctly return a JSON string as the 200 HTTP response to your API request. Consequently if you send return ok('test') that will also return a JSON 200 response. If you do not want to return JSON simply don't pass a formatting argument when declaring the ok response.

Testing

Run yarn test

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!