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 🙏

© 2024 – Pkg Stats / Ryan Hefner

trail-middleware

v0.1.0

Published

A wrapper for managing and organizing Next.js middleware.

Downloads

2

Readme

Trail Middleware

Trail Middleware is a lightweight and efficient middleware wrapper for Next.js. It provides a simple interface to manage and organize your middleware.

⚠️ Important: This package is currently in an experimental stage. Please be aware that its API, functionality, and behavior are subject to change in upcoming releases. Use it cautiously in production environments, and keep track of updates to stay informed of breaking changes or enhancements.

Features

  • Easily integrates with your existing Next.js middleware
  • Lightweight and minimalistic
  • Customizable middleware stack

Installation

You can install Trail Middleware using npm:

npm i trail-middleware

Getting Started

Basic Example

import { withTrailMiddleware } from 'trail-middleware';

// Example middleware for logging request data
const const loggerMiddleware = (request: NextRequest) => {
  logger(`User Agent: ${req.headers.get('user-agent')}`);
  logger(`Path: ${req.nextUrl.pathname}`);
}

// Main Next.js middleware with TrailMiddleware wrapper
export const middleware = withTrailMiddleware((trail) => {
  // Use logger middleware on all routes
  trail('/*allroutes', loggerMiddleware);
});

Multiple Routes and Middleware

Trail Middleware also supports multiple routes:

// Example middleware for authenticating users
const authMiddleware = (request: NextRequest) {
  const cookie = req.cookies.get('jwt');

  if (cookie) {
    return NextResponse.next();
  }

  return NextResponse.redirect(new URL('/login', req.nextUrl.href));
}

export const middleware = withTrailMiddleware((trail) => {
  // When any of these routes are visited, logger middleware
  // will be called
  trail(['/route-one', '/route-two'], authMiddleware);
});

and multiple middleware:

export const middleware = withTrailMiddleware((trail) => {
  // When this route is visited, auth middleware and logger middleware
  // will both be called sequentially
  trail("/route-one", [authMiddleware, loggerMiddleware]);
});

Organization

Instead of having everything in one monolithic file, easily organize your middleware into separate files:

import { withTrailMiddleware } from "trail-middleware";

import { loggerMiddleware } from "./middleware/logger";
import { authMiddleware } from "./middleware/auth";

export const middleware = withTrailMiddleware((trail) => {
  // Use logger middleware on all routes
  trail("/*routes", loggerMiddleware);

  // Use auth middleware on protected routes
  trail("/protected/*routes", authMiddleware);

  // Etc...
});

API Reference

Types

  • MiddlewareFunction: (req: NextRequest) => Promise<NextResponse<unknown>> | NextResponse<unknown>
  • SetupFunction: (trail: TrailFunction, req: NextRequest) => void
  • TrailFunction: (routes: string | string[], middleware: MiddlewareFunction | MiddlewareFunction[]) => void

Functions

withTrailMiddleware(setup: SetupFunction) => Promise<MiddlewareFunction>

This function enhances the standard Next.js middleware by allowing for a better organized and customizable middleware stack.

Parameters
  • setup: SetupFunction

    This is the function that will contain your middleware organization code. It is passed two parameters: trail (a function used for creating route middleware) and request (a standard Next.js NextRequest).

Returns
  • MiddlewareFunction: A middleware function that can be used to handle requests in Next.js with additional route-specific processing via the trail function.

trail(routes: string | string[], middleware: MiddlewareFunction | MiddlewareFunction[]) => void

This function is passed to setup in withTrailMiddleware. It's used to assign middleware to your routes.

Parameters
  • routes: string | string[]

    A route or an array of routes where the supplied middleware will be called. Each route is matched according to the rules of the match function from Path-to-RegExp.

    Matching Examples

    trail("/route", myMiddleware); // Matches '/route'. Doesn't match '/route-one'.
    trail("/*allroutes", myMiddleware); // Matches '/route', '/login', '/user/signup', and all other routes.
    trail("/movies/*movie", myMiddleware); // Matches '/movies/10394234', `/movies/action/8345983`. Doesn't match '/movies'.
  • middleware: MiddlewareFunction | MiddlewareFunction[]

    A middleware function or an array of middleware functions to be executed for the specified routes.

Returns
  • void

License

This project is licensed under the MIT License. See the LICENSE file for details.