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 🙏

© 2025 – Pkg Stats / Ryan Hefner

midnext

v1.7.0

Published

A lightweight library for managing Next middleware

Readme

Midnext

NPM Version NPM Downloads npm bundle size

Midnext is a library that enhances the middleware development experience in Next.js, offering support with a syntax similar to express / connect.

Installation

npm i midnext

Or via yarn

yarn add midnext

Use middleware

Middleware in midnext is executed in a stack, processing each middleware sequentially. Middleware functions can be synchronous or asynchronous, allowing flexibility in handling requests.

// middleware.ts
import { NextUse } from 'midnext'

export async function middleware(request: NextRequest, event: NextEvent) {
  return new NextUse({ request, event })
    .use((req, res) => {
      req.cookies.set('req-c-1', '1')
      req.headers.set('req-h-1', '1')
    })
    .use('/test', async (req, res, event) => {
      res.cookies.set('res-c-1', '2')
      res.headers.set('res-h-1', '2')
    })
    .run()
}

export const config = {}

Rewrites

To rewrite a request, return res.rewrite from the middleware. The next middleware will receive the updated request with the rewritten pathname.

// middleware.ts
import { NextUse } from 'midnext'
import join from 'url-join'

export async function middleware(request: Request, event: FetchEvent) {
  return new NextUse({ request, event })
    .use((req, res) => {
      const url = new URL(req.url)
      url.pathname = join('test', url.pathname)

      return res.rewrite(url)
    })
    .use((req, res) => {
      // Here, req.url's pathname starts with `test` due to the rewrite above
      console.log(req.parsedUrl.pathname)
    })
    .run()
}

export const config = {}

SendRewrite

You can also send rewrite as a response (this will stop further middleware execution).

// middleware.ts
import { NextUse } from 'midnext'
import join from 'url-join'

export async function middleware(request: Request, event: FetchEvent) {
  return new NextUse({ request, event })
    .use((request, response) => {
      const url = new URL(request.url)
      url.pathname = join('test', url.pathname)

      return response.sendRewrite(url, { request, headers: response.headers })
    })
    .use((request, response) => {
      // This middleware won't be executed since the rewrite was returned as a response.
    })
    .run()
}

export const config = {}

Redirects

Returning res.redirect will immediately stop further middleware execution and perform a redirect.

// middleware.ts
import { NextUse, EdgeRequest, EdgeResponse } from 'midnext'

export async function middleware(request: Request, event: FetchEvent) {
  return new NextUse({ request, event })
    .use((req: EdgeRequest, res: EdgeResponse) => {
      res.cookies.set('test', 'abc')
      
      return res.redirect('https://example.com', {
        headers: res.headers,
        status: 302
      })
    })
    .use((req, res) => {
      // This middleware won't be executed since the redirect was returned above
    })
    .run()
}

export const config = {}

Response

You can respond from Middleware directly by returning a Response. Like a redirect, this prevents further middleware execution.

// middleware.ts
import { NextUse, EdgeRequest, EdgeResponse } from 'midnext'

export async function middleware(request: Request, event: FetchEvent) {
  return new NextUse({ request, event })
    .use((req: EdgeRequest, res: EdgeResponse) => {
      return Response.json({ message: 'ok' })
    })
    .use((req, res) => {
      // This middleware won't be executed since the response was returned above
    })
    .run()
}

export const config = {}

Sharing Data

To share data between multiple middleware, u can add custom properties the request or response objects. This enables storing various types of data, including functions, for later use in the middleware chain.

// middleware.ts
import { NextUse } from 'midnext'

export async function middleware(request: NextRequest, event: FetchEvent) {
  return new NextUse({ request, event })
    .use((req, res) => {
      req.isBot = false
      req.printHello = () => console.log('hello')
    })
    .use(req => {
      console.log(req.isBot)
      req.printHello()
    })
    .run()
}

export const config = {}

Optionally, you can type the data field on request objects for TypeScript safety:

declare global {
  namespace Midnext {
    interface EdgeRequest {
      isBot: boolean
      printHello: () => void
    }
  }
}

export {}

API

The NextUse class provides a middleware management system similar to Express, allowing sequential execution of middleware functions.

  • The middleware receives EdgeRequest and EdgeResponse as arguments allowing you to modify both request and response. you can stop further middleware execution by returning a Response object from the middleware (new Response(), Response.json(), res.redirect(url))

  • Both EdgeRequest and EdgeResponse extend the standard Web Request and Response objects, meaning they support all their default properties and methods.

    Below, we list only the additional properties introduced by midnext.

NextUse Properties

| Constructor | Description | |----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------| | new NextUse({ request: Request, response?: Response, event?: FetchEvent }) | Initializes a new instance of NextUse, wrapping the incoming request in an EdgeRequest and setting an initial EdgeResponse |

| Method | Description | |--------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------| | use(input: string | RegExp | Middleware, middleware: Middleware) | Adds a middleware function to the execution stack. The middleware receives EdgeRequest and EdgeResponse as arguments. | | run() | Executes the middleware stack in sequence, handling redirects, rewrites, and response finalization. |

EdgeRequest Properties

| Property | Description | |-----------|------------------------------------------------------------------------------------------------------------| | cookies | Manages request cookies using @edge-runtime/cookies | | parsedUrl | Provides the parsed URL object of the request. |

EdgeResponse Properties

| Property | Description | |-------------|-------------------------------------------------------------------------------------------------------------| | cookies | Manages response cookies using @edge-runtime/cookies | | rewrite | Rewrites the request URL and continues middleware execution with the updated request. | | sendRewrite | Rewrites the request URL and prevents further middleware execution. | | redirect | Redirects to a new URL and halts further middleware execution. |