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

@seedrs/middyjs-middleware

v1.0.1

Published

Middleware for middyjs

Downloads

20

Readme

middyjs-middleware

Middleware for use with middyjs 🛵 middyjs

Setup

Install with npm or yarn (preferred)

npm i @seedrs/middyjs-middleware --save
OR
yarn add @seedrs/middyjs-middleware

Available middleware

ensureJson

This middleware is used to ensure the client has set the Content-Type header to application/json when sending a request body. A 406 error is returned when application/json is not present in the Content-Type header. To ensure that header names are normalized use in conjunction with middyjs/middlewares/httpHeaderNormalizer and with middyjs/middlewares/jsonBodyParser to parse the incoming body for you.

const middy = require('middy');
const {
  httpHeaderNormalizer,
  jsonBodyParser
} = require('middyjs/middlewares');
const { ensureJson } = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  return cb(null, {});
})
.use(httpHeaderNormalizer())
.use(ensureJson())
.use(jsonBodyParser())
.use(httpErrorHandler(logger);

httpErrorHandler

This middleware is used to catch any errors thrown in your application code and return a formatted message using errors created with jshttp/http-errors. Errors are logged by the logger passed as the argument to the middleware. Intended for use with node-bunyan and @seedrs/bunyan-seedrs-serverless-serializer.

const middy = require('middy');
const createError = require('http-errors');
const { httpErrorHandler } = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  const error = createError.NotFoundError();
  return cb(error);
})
.use(httpErrorHandler(logger));

dynamodbStreamErrorHandler

This middleware will log errors when consuming a dynamodb stream. It is intended for use with node-bunyan and @seedrs/bunyan-seedrs-serverless-serializer.

const middy = require('middy');
const { dynamodbStreamErrorHandler
} = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  throw new Error('Whoops');
})
.use(dynamodbStreamErrorHandler(logger));

dynamodbStreamFilter

This middleware filters out unwanted events and ends the lambda execution early if there are no matching events in the stream. In the example below the business logic will only execute when there are Records with the INSERT event type.

const middy = require('middy');
const { dynamodbStreamFilter } = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  //Some business logic runs when there are INSERT events
})
.use(dynamodbStreamFilter({ eventName: 'INSERT' }));

dynamodbStreamParser

This middleware parses and extracts fields specified in the event. You can specify either to extract from the OldImage or the NewImage (if your stream is configured to show both) or specify either OldImage or NewImage.

const middy = require('middy');
const { dynamodbStreamParser } = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  console.log(event.Records);
  /* Shown below is the parsed stream objects
  [{
    {
      OldImage: {
        key_one: 1
      },
      NewImage: {
        key_one: 2
      }
    }
  }]
  */
})
.use(dynamodbStreamParser({
  NewImage: [
    'key_one'
  ],
  OldImage: [
    'key_one'
  ]
});

s3ErrorHandler

This middleware is intended to be used with node-bunyan and @seedrs/bunyan-seedrs-serverless-serializer. It logs the error produced along with the S3 event that triggered the invocation.

const middy = require('middy');
const { httpErrorHandler } = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  //Some business logic
})
.use(s3ErrorHandler(logger));

Usage without bunyan and seedrs/bunyan-seedrs-serverless-serializer

It is possible to provide a custom object that uses your own logging system with these middlewares. However we suggest using bunyan with our serializers to help parse and log events and responses as JSON. Shown below is an example using the default console object.

const middy = require('middy');
const { httpErrorHandler } = require('@seedrs/middyjs-middleware');

const handler = middy((event, context, cb) => {
  //Some business logic
})
.use(httpErrorHandler(console));

Contribute

To contribute to the project, please read the guide.