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-middleware

v1.1.2

Published

AWS Lambda Middleware

Downloads

103

Readme

aws-lambda-middleware

NPM version NPM downloads

AWS Lambda Middleware

You can simply apply Middleware in Lambda. 
Clean code split is possible, and it includes a simple and extensible Parameter PropTypes validater.
It is implemented as lightly as possible to reduce the burden when running Lambda.

🚀 v1.0 added features
A Validate function that is easy to expand and use has been added, and deep data of arrays and objects can now be processed.

It is compatible even in environments other than lambda. (node express etc.)

 

Install

npm i aws-lambda-middleware

 

Quick setting

const { Middleware, Prop } = require('aws-lambda-middleware')


exports.handler = new Middleware().add({
  queryStringParameters: {
      username: Prop.string.required(),
      age: Prop.integer,
      friends: [
        {
          name: Prop.string.length({ max: 20 }),
          gender: Prop.string.or(['male', 'female'])
        }
      ]
  }
}).add(async (event, context, prevData) => {
  const query = event.queryStringParameters

  //your code

  return {
      statusCode: 200,
      body: JSON.stringify({
        message: 'success'
      })
  }
})

 

Options

You can set global options and cluster options.

You can set options such as trim.

📖 Options detail docs

Middleware

You can simply apply Middleware in Lambda. 

📖 Middleware detail docs

PropTypes

Checks and corrects the data types of request parameters.

PropTypes and Prop are the same object.

📖 PropTypes detail docs

Validate

It only verifies the validity of the request parameter value.

You can use it by adding custom rules.

📖 Validate detail docs

 

The rules added to PropTypes and Validate are written in one line and used.

Message

Error messages can be organized into templates.

📖 Message detail docs

 

with Express

Introducing a method that can be applied to the Express framework.

It can also be used in other frameworks.

📖 with Express

   

⚠️ Upgrading from v0.9 to v1.0

1. object and array expressions

object and array are designated as reserved prop name, so the rule cannot be overwritten.

⚠️ Reserved prop names

2. Option settings for each PropTypes

trim settings for each PropTypes use .option().

{
  param: Prop.string.option({ trim: false })
}

3. PropTypes and Prop

The abbreviated Prop can be used instead of the PropTypes.
PropTypes can still be used as well.

4. .isRequired has been replaced by .required().

.isRequired is also compatible, but not recommended.

5. Parameter type of PropTypes.*.default() function

When dynamically setting the default value of PropTypes, the parameter type has been changed to named parameters.

v0.9

Prop.*.default((event) => {})

v1.0

Prop.*.default(({ event }) => {})

📖 PropTypes > Support methods

6. Interpreting object and array expressions

The interpretation of Object and Array expressions has been changed from validate only when value exists to required validation.
When setting the body as shown below, the returned status depends, so check the item document in PropTypes > Support methods.

📖 PropTypes > Support methods

exports.handler = new Middleware().add({
  body: {
    myId: Prop.string
  }
})

v0.9
Even if the body of the request parameter is an empty Object or has no value, status = 200 is returned.

v1.0
If the body of the request parameter is an empty Object or has no value, status = 400 is returned.
In order to validate only when value exists for the body, you must also set PropTypes on the body.

exports.handler = new Middleware().add({
  body: Prop.object.item({
    myId: Prop.string
  })
})