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

@fehmicorp/middleware

v1.0.2

Published

Redis driven proxy middleware for Next.js

Downloads

212

Readme

🚀 Fehmi Corporation Next Proxy Middleware

A powerful Redis-driven middleware proxy layer for Next.js that enables dynamic API routing, authentication validation, and request transformation — all directly inside Next.js middleware.

Instead of hardcoding API logic into routes, this middleware allows centralized API configuration stored in Redis or a database, making your Next.js app behave like a lightweight API Gateway.

Perfect for:

  • Microservice architectures
  • Multi-tenant platforms
  • Dynamic API routing
  • Authentication gateways
  • Edge middleware security

✨ Key Features

⚡ Dynamic Route Mapping

Routes are resolved dynamically using Redis keys, allowing you to change API behavior without redeploying your app.

🔐 Built-in Authentication

Supports authentication via:

  • JWT tokens
  • Secure cookies
  • Authorization headers

🧠 Smart Request Context Injection

Middleware injects metadata into request headers so downstream handlers can access authentication and routing context easily.

🔄 Supports All HTTP Methods

| Method | Purpose | |------|------| | GET | Authentication validation | | POST | Route mapping | | PUT | Route mapping | | PATCH | Route mapping | | DELETE | Route mapping | | WS | WebSocket routing |

🧩 Query-Based Routing

Dynamic routing based on query parameters.

Example:


/users?id=10&fnKey=getUser

🌐 WebSocket Ready

WebSocket connections use the same Redis routing logic as REST requests.

🍪 Cookie Management

Integrated helper utilities for setting and validating secure authentication cookies.


🏗 Architecture

The middleware acts as an Edge API Gateway inside your Next.js application.


Client Request
│
▼
Next.js Middleware
│
▼
Redis Route Lookup
│
▼
Authentication Validation
│
▼
Header Injection
│
▼
Next.js Route Handler

This design allows:

  • centralized API configuration
  • secure authentication enforcement
  • dynamic routing control

📦 Installation

npm install next-proxy-middleware

or

yarn add next-proxy-middleware

🚀 Quick Start

Create a middleware file in your Next.js project.

middleware.ts

import { Proxy } from "next-proxy-middleware";

export async function middleware(req: any) {
  return Proxy.handle(req, process.env.REDIS_URL!);
}

export const config = {
  matcher: "/api/:path*"
};

This will automatically process every request under /api.


🔑 Request Flow

POST / PUT / PATCH / DELETE

Write operations require a routing key header.

Example Request

POST /auth/login
x-key: accounts

The middleware generates a Redis lookup key.

Example Redis key:

accounts

Redis response example:

{
  "success": true,
  "data": {
    "module": "accounts",
    "route": "/auth/login"
  }
}

Middleware then injects metadata headers:

x-auth-key
x-auth-data
x-fn-key

🔐 GET Authentication Flow

GET requests validate user authentication.

Supported authentication sources:

Authorization Header

Authorization: Bearer <token>

Cookie Authentication

accounts_token=<jwt>

The middleware performs a Redis lookup:

jwtToken:auth:<token>

Then verifies the JWT using the stored secret.

Headers injected:

x-jwt-secret
Authorization

🔍 Query Parameter Routing

Middleware extracts query parameters to determine routing.

Ignored parameters:

fnKey
refresh

Example request:

/users?id=10&fnKey=getUser

Generated Redis key:

id:10:<x-key>:getUser

🧪 Testing

The repository includes a test harness to simulate middleware execution.

Run tests using:

npx tsx test/rest.ts

This will simulate:

  • POST
  • PUT
  • PATCH
  • DELETE
  • GET
  • WebSocket requests

Example output:

TEST: POST route mapping
Redis lookup: accounts
Status: 200
Headers injected successfully

🍪 Cookie Helper

Use the built-in cookie helper to set authentication cookies.

import { CookieConfig } from "next-proxy-middleware";

CookieConfig.setCookie(req, res, token, cookieConfig);

📁 Example Route Configuration

Routes can be stored in Redis or a database.

Example configuration:

{
  "module": "accounts",
  "route": "/auth/login",
  "methods": ["POST"],
  "security": {
    "jwt": {
      "enabled": true,
      "expire": "2h"
    },
    "cookie": {
      "enabled": true,
      "name": "accounts_token"
    }
  }
}

🛡 Security Features

  • JWT authentication
  • Cookie-based authentication
  • Dynamic secret validation
  • Header validation
  • Query-based routing control
  • Optional rate limiting
  • CORS configuration support

⚙️ Use Cases

This middleware is ideal for:

  • API gateway inside Next.js
  • Microservice routing
  • Authentication middleware
  • Multi-tenant SaaS platforms
  • Edge request processing
  • Dynamic API orchestration

📂 Project Structure

src
 ├── index.ts
 ├── get.ts
 ├── post.ts
 ├── put.ts
 ├── patch.ts
 ├── delete.ts
 ├── ws.ts
 └── dist
     ├── cookie.ts
     └── redis.ts

test
 └── rest.ts

🤝 Contributing

Contributions are welcome.

If you'd like to improve this middleware:

  1. Fork the repository
  2. Create a feature branch
  3. Submit a pull request

📜 License

MIT License


⭐ Support

If this project helps you, consider giving it a star ⭐ on GitHub.


---

✅ This version improves:

- **visual structure**
- **developer onboarding**
- **architecture clarity**
- **professional open-source appearance**

---

If you want, I can also help you add **three powerful sections that make GitHub repos look very professional**:

1️⃣ **Architecture diagram (SVG)**  
2️⃣ **Performance benchmarks vs traditional API routing**  
3️⃣ **Redis key design documentation**

These make your repo look like a **serious infrastructure project rather than a simple middleware library**.