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 🙏

© 2026 – Pkg Stats / Ryan Hefner

next-middleware-enhancer

v1.1.6

Published

Enhance Next.js middleware with ease

Readme

Next.js Middleware Enhancer

A lightweight middleware enhancer for Next.js, allowing route-based middleware execution with support for sequential middleware execution.

🚀 Features

  • Route Matching: Supports Next.js matcher patterns to apply middleware based on specific routes.
  • Multiple Middleware Execution: Allows executing multiple middleware functions sequentially.
  • Automatic Response Handling: Returns NextResponse.next() if no matching middleware is found.

📦 Installation

Install the package using npm or pnpm:

npm install next-middleware-enhancer

or

pnpm add next-middleware-enhancer

🛠 Usage

Define your middleware in middleware.ts and use createMiddleware to apply route-based handlers.

import { NextRequest, NextResponse } from "next/server";
import { createMiddleware } from "next-middleware-enhancer";

const authMiddleware = (req: NextRequest) => {
  if (!req.headers.get("Authorization")) {
    return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
  }
};

const { middleware, config } = createMiddleware([{ matcher: "/admin", handler: authMiddleware }]);

export { middleware, config };

🛠 Usage (Multiple Handlers) Apply multiple middleware functions sequentially for a route:

import { NextRequest, NextResponse } from "next/server";
import { createMiddleware } from "next-middleware-enhancer";

const logMiddleware = (req: NextRequest) => console.log(`Request: ${req.nextUrl.pathname}`);

const authMiddleware = (req: NextRequest) => {
  if (!req.headers.get("Authorization")) return NextResponse.json({ message: "Unauthorized" }, { status: 401 });
};

const adminMiddleware = (req: NextRequest) => {
  if (req.headers.get("Authorization") !== "admin-secret")
    return NextResponse.json({ message: "Forbidden" }, { status: 403 });
};

const { middleware, config } = createMiddleware([
  { matcher: "/admin", handler: [logMiddleware, authMiddleware, adminMiddleware] },
]);

export { middleware, config };

Execution Flow: 1️⃣ Logs request path → 2️⃣ Checks auth → 3️⃣ Verifies admin access 🚀


📖 API Reference

createMiddleware(middlewareList: MiddlewareList)

Creates a Next.js middleware function that applies the specified middleware handlers based on route patterns.

Parameters

type MiddlewareFunction = (req: NextRequest) => NextResponse | void | Promise<NextResponse | void>;
type MiddlewareConfig = { matcher: string; handler: MiddlewareFunction | MiddlewareFunction[] };
type MiddlewareList = MiddlewareConfig[];

Returns

{
  middleware: (req: NextRequest) => NextResponse | void;
  config: { matcher: string[] };
}

🎯 How It Works

  1. Requests are matched against matcher patterns.
  2. Matched middleware functions execute sequentially.
  3. A NextResponse returned from a middleware stops execution.
  4. If no middleware handles the request, NextResponse.next() is returned.

📝 License

This project is licensed under the MIT License.

👥 Contributors

Feel free to contribute to this project by opening issues or pull requests!


🌟 Enjoy using Next.js Middleware Enhancer? Give it a star on GitHub!