Skip to content

Instantly share code, notes, and snippets.

@bundle-js
Created March 22, 2023 06:13
Show Gist options
  • Select an option

  • Save bundle-js/19ff88b27c7c724f7dd9977f05972e39 to your computer and use it in GitHub Desktop.

Select an option

Save bundle-js/19ff88b27c7c724f7dd9977f05972e39 to your computer and use it in GitHub Desktop.
var u=({base:f="",routes:l=[]}={})=>({__proto__:new Proxy({},{get:(e,s,o)=>(a,...t)=>l.push([s.toUpperCase(),RegExp(`^${(f+"/"+a).replace(/\/+(\/|$)/g,"$1").replace(/(\/?\.?):(\w+)\+/g,"($1(?<$2>*))").replace(/(\/?\.?):(\w+)/g,"($1(?<$2>[^$1/]+?))").replace(/\./g,"\\.").replace(/(\/?)\*/g,"($1.*)?")}/*$`),t])&&o}),routes:l,async handle(e,...s){let o,a,t=new URL(e.url),$=e.query={};for(let[r,p]of t.searchParams)$[r]=$[r]===void 0?p:[$[r],p].flat();for(let[r,p,c]of l)if((r===e.method||r==="ALL")&&(a=t.pathname.match(p))){e.params=a.groups||{};for(let g of c)if((o=await g(e.proxy||e,...s))!==void 0)return o}}});export{u as Router};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment