next-middleware-enhancer
Version:
Enhance Next.js middleware with ease
26 lines (25 loc) • 609 B
JavaScript
import { NextResponse as s } from "next/server";
function p(r) {
return {
middleware: async (e) => {
for (const { matcher: n, handler: t } of r)
if (f(e.nextUrl.pathname, n)) {
const o = Array.isArray(t) ? t : [t];
for (const c of o) {
const a = await c(e);
if (a) return a;
}
}
return s.next();
},
config: {
matcher: r.map(({ matcher: e }) => e)
}
};
}
function f(r, e) {
return new RegExp(`^${e.replace(/:\w+\*/g, ".*").replace(/:\w+/g, "[^/]+")}$`).test(r);
}
export {
p as createMiddleware
};