Rescale logo

NEMO

Untangle your requests and simplify middlewares mess

bun add @rescale/nemo

First, let's let the code get the word out...

before
middleware.ts
import { NextRequest } from 'next/server';

export const middleware = async (req: NextRequest) => {
  let user = undefined;
  let team = undefined;
  const token = req.headers.get('token');

  if(req.nextUrl.pathname.startsWith('/auth')) {
    user = await getUserByToken(token);

    if(!user) {
      return NextResponse.redirect('/login');
    }

    return NextResponse.next();
  }

  if(req.nextUrl.pathname.startsWith('/team')) {
    user = await getUserByToken(token);

    if(!user) {
      return NextResponse.redirect('/login');
    }

    const slug = req.nextUrl.query.slug;
    team = await getTeamBySlug(slug);

    if(!team) {
      return NextResponse.redirect('/');
    }

    return NextResponse.next();
  }

  return NextResponse.next();
}

export const config = {
  matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'],
};
after
middleware.ts
import { createMiddleware, type MiddlewareFunctionProps } from '@rescale/nemo';
import { auth } from '@/app/(auth)/auth/_middleware';
import { team } from '@/app/(team)/team/_middleware';

const globalMiddlewares = {
  before: auth,
};

const middlewares = {
  '/team/:slug': team,
};

export const middleware = createMiddleware(middlewares);

export const config = {
  matcher: ['/((?!_next/|_static|_vercel|[\\w-]+\\.\\w+).*)'],
};

Do you feel the difference?

Simplify your middlewares now!

Supports

Dynamic segments

Run your middleware for any route or dynamic segment

Functions Chaining

Chain multiple middlewares for single group

Shared context

Functions can share context between each other

Open-source

Something is missing? Just add it or post an issue on GitHub

Developer Experience

Developers are most important to me, this package will be as easy and useful as possible

Want to help with this project?

Review existing issues, make an PR's with what's missing!

Handcrafted by