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

nextjs-interceptor

v1.0.10

Published

A powerful and flexible interceptor middleware for Next.js applications, providing seamless request/response manipulation capabilities

Downloads

35

Readme

Next.js Interceptor

一个强大而灵活的 Next.js 中间件拦截器,用于处理请求和响应的操作。

特性

  • 🚀 基于 Next.js Middleware 构建
  • 🛡️ 支持请求和响应的拦截和修改
  • 🔄 灵活的中间件链式调用
  • 📦 TypeScript 支持
  • 🎯 零配置,即插即用
  • 🔧 高度可定制的拦截规则

安装

使用 pnpm 安装依赖:

pnpm install

使用方法

  1. 在你的 Next.js 项目中创建 middleware.ts 文件:
import { NextResponse } from "next/server";
import { interceptorRegistry } from "nextjs-interceptor";
export { interceptorMiddleware as middleware } from "nextjs-interceptor";

// 认证拦截器
interceptorRegistry.use(
  {
    id: "auth",
    pattern: "/demo/*",
    priority: 1,
  },
  async ({ request }) => { 
    const token = request.headers.get("authorization");
    if (!token) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
    // 返回 null 自动继续执行下一个拦截器
    return null;
  }
);

// 配置匹配路径:可以把大部分地址都拦截下来,后续可以转交给 InterceptorRegistry 来处理
export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico (favicon file)
     */
    "/((?!_next/static|_next/image|favicon.ico).*)",
  ],
};
  1. 支持排除模式(exclude)

你可以使用 exclude 选项来排除特定路径,避免被拦截:

import { NextResponse } from "next/server";
import { interceptorRegistry } from "nextjs-interceptor";

// 这个拦截器会匹配所有 /api/* 路径,但排除 /api/public/*
interceptorRegistry.use(
  {
    id: "api-auth",
    pattern: "/api/*",
    exclude: "/api/public/*", // 排除公共 API 路由
    priority: 1,
  },
  async (request) => {
    const token = request.headers.get("authorization");
    if (!token) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }
    return; // 继续执行下一个拦截器
  }
);

// 你也可以使用数组和正则表达式来定义排除模式
interceptorRegistry.use(
  {
    id: "admin-protection",
    pattern: "/admin/*",
    exclude: [
      "/admin/login",
      "/admin/public/*",
      /\/admin\/assets\/.*/  // 正则表达式模式
    ],
    priority: 2,
  },
  async (request) => {
    // 检查管理员权限
    const isAdmin = checkAdminAuth(request);
    if (!isAdmin) {
      return NextResponse.redirect(new URL('/admin/login', request.url));
    }
    return;
  }
);

export { interceptorMiddleware as middleware } from "nextjs-interceptor";

开发

# 启动开发服务器
pnpm dev

# 构建项目
pnpm build

# 运行测试
pnpm test

许可证

ISC

作者

liuhuapiaoyuan