UNPKG

next-wayfinder

Version:

Apply multiple next.js middlewares with ease

2 lines (1 loc) 3.39 kB
"use strict";var P=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var U=(e,r)=>{for(var t in r)P(e,t,{get:r[t],enumerable:!0})},C=(e,r,t,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let a of A(r))!O.call(e,a)&&a!==t&&P(e,a,{get:()=>r[a],enumerable:!(i=W(r,a))||i.enumerable});return e};var F=e=>C(P({},"__esModule",{value:!0}),e);var I={};U(I,{getHost:()=>j,handlePaths:()=>E});module.exports=F(I);var h=require("next/server"),H=require("ts-pattern");var o={isRewrite:e=>"rewriteTo"in e,isRedirect:e=>"redirectTo"in e,isPath:e=>"path"in e,isHostname:e=>"hostname"in e};var p=require("path-to-regexp");var f=e=>{let r=e.headers.get("host")??"",t=e.nextUrl.pathname;return{hostname:r,pathname:t}},b=(e,r)=>(0,p.match)(e)(r).params??{};function D(e,r){return o.isHostname(e)?!1:e.method===r}var N=(e,r)=>typeof e=="string"?e===r:e instanceof RegExp?e.test(r):e instanceof Function?e(r):!1;function y(e,{path:r,hostname:t,method:i}){return e.find(a=>{let c=!1;if(o.isHostname(a))return Array.isArray(a.hostname)?a.hostname.some(x=>N(x,t)):N(a.hostname,t);c=(0,p.pathToRegexp)(a.path).test(r);let d=!0,T=!0;return a.method&&(T=D(a,i)),a.guard&&(d=a.guard(b(a.path,r))),c&&T&&d})}var R=e=>({enumerable:!0,writable:!1,value:e,configurable:!0}),k=e=>({enumerable:!0,writable:!1,value:e,configurable:!0}),q=(e,r)=>(Object.defineProperty(e,"ctx",k(r)),e),w=(e,r,t)=>(Object.defineProperty(e,"params",R(b(r,t))),e),M=(e,r)=>Object.entries(r).reduce((t,[i,a])=>a?t.replace(`:${i}`,Array.isArray(a)?a.join("/"):a):t,e);var j=e=>f(e).hostname;function E(e,{response:r,...t}={}){return async function(i,a){var g;t.debug&&t.beforeAll&&console.debug("[BeforeAll] >> Executing...");let c;r instanceof Function?c=r(i,a):r?c=r:c=h.NextResponse.next();let d=await((g=t.beforeAll)==null?void 0:g.call(t,i,c))||c;if(d.redirected)return t.debug&&console.debug("[BeforeAll] >> Redirected!"),d;let{pathname:T,hostname:x}=((t==null?void 0:t.parser)??f)(i),s=y(e,{path:T,hostname:x,method:i.method.toUpperCase()});if(t!=null&&t.debug&&(console.debug(`Middleware ${s?"":"Not "}Found!`),s&&console.debug(s)),!s)return d;if(t!=null&&t.context){let n;t.context instanceof Function?n=await t.context(i):n=t.context,t.debug&&console.debug("[Context] >> Injecting: ",n),q(i,n),t.debug&&console.debug("[Context] >> Injected: ",i.ctx)}if(o.isRewrite(s)||o.isRedirect(s)){let n=w(i,s.path,T),l=n.nextUrl.clone();return l.pathname=(0,H.match)(s).when(o.isRedirect,u=>u.redirectTo instanceof Function?u.redirectTo(n):M(u.redirectTo,n.params)).when(o.isRewrite,u=>u.rewriteTo instanceof Function?u.rewriteTo(n):M(u.rewriteTo,n.params)).otherwise(()=>""),o.isRewrite(s)?h.NextResponse.rewrite(l):(s.includeOrigin&&l.searchParams.set(s.includeOrigin===!0?"origin":s.includeOrigin,n.nextUrl.pathname),h.NextResponse.redirect(l))}if(Array.isArray(s.handler))return E(s.handler,t)(i,a);if(o.isHostname(s))return Object.defineProperty(i,"params",R({})),s.handler(i,d,a);let m=w(i,s.path,T);if(s.pre){let n=await s.pre(m);if(n!==!0){if(!n)return d;if(typeof n.redirectTo=="string"){let l=m.nextUrl.clone();return l.pathname=n.redirectTo,h.NextResponse.redirect(l,{status:n.statusCode})}return h.NextResponse.redirect(new URL(n.redirectTo,m.nextUrl),{status:n.statusCode})}}return s.handler(m,d,a)}}0&&(module.exports={getHost,handlePaths});