1.1.6 • Published 5 months ago

next-middleware-enhancer v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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! ⭐

1.1.6

5 months ago

1.1.5

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.7

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago