Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nextjs-middleware-wrappers

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextjs-middleware-wrappers

For the common use case of wrapping a NextJS endpoint with methods that act as middleware.

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.5K
increased by319.3%
Maintainers
1
Weekly downloads
 
Created
Source

(NextJS) Middleware Wrappers

For the common use case of wrapping a NextJS endpoint with methods that act as middleware.

Installation

yarn install nextjs-middleware-wrappers

Usage

Wraps a function in layers of other functions, while preserving the input/output type. The output of wrappers will always have the type of its last parameter (the wrapped function)

This function turns this type of composition...

withDatabase(
  logger.withContext("somecontext")(async (req, res) => {
    res.status(200).end("...")
  })
)

Into...

wrappers(
  withDatabase,
  logger.withContext("somecontext"),
  async (req, res) => {
    res.status(200).end("...")
  }
)

Having this as a utility method helps preserve types, which otherwise can get messed up by the middlewares. It also can make the code cleaner where there are multiple wrappers.

EXAMPLES

In the context of request middleware you might write something like this...

const withRequestLoggingMiddleware = (next) => async (req, res) => {
  console.log(`GOT REQUEST ${req.method} ${req.path}`)
  return next(req, res)
}

Here's an example of a wrapper that takes some parameters...

const withLoggedArguments =
  (logPrefix: string) =>
  (next) =>
  async (...funcArgs) => {
    console.log(logPrefix, ...funcArgs)
    return next(...funcArgs)
  }

Installation

Automatic Deploy to NPM

To have your PR be automatically deployed to NPM, make sure to tag your commit messages with the Angular JS commit message format.

i.e.

Commit messageRelease type
fix(pencil): stop graphite breaking when too much pressure appliedFix Release
feat(pencil): add 'graphiteWidth' optionFeature Release
perf(pencil): remove graphiteWidth option
BREAKING CHANGE: The graphiteWidth option has been removed. The default graphite width of 10mm is always used for performance reasons.Breaking Release (Note that the BREAKING CHANGE: token must be in the footer of the commit)

FAQs

Package last updated on 13 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc