@route-weaver/core
Version:
A typesafe navigation package for creating and managing routes.
8 lines (7 loc) • 3.19 kB
JavaScript
var j=Object.defineProperty;var I=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var b=(i,a,s)=>a in i?j(i,a,{enumerable:!0,configurable:!0,writable:!0,value:s}):i[a]=s,$=(i,a)=>{for(var s in a||(a={}))k.call(a,s)&&b(i,s,a[s]);if(I)for(var s of I(a))x.call(a,s)&&b(i,s,a[s]);return i};import{compile as W,match as B}from"path-to-regexp";function F(i,a={}){let s={},T={},l=(f,m="",h={})=>{for(let p in f){let{path:N,label:v,children:R,meta:P}=f[p],M=[m,N].join("/").replace(/\/+/g,"/");if(s[p]={path:M,meta:P},h[p]=v,R){let w={};T[p]=w,l(R,M,w)}}},g={};return T.main=g,l(i,"",g),O(s).buildNav(T,a)}function O(i){let a=new Map,s=(...l)=>`/${l.join("/")}`.replace(/\/+/g,"/").replace(/\/$/,"")||"/";return{buildNav:(l,g={})=>{let{t:S=t=>t}=g,f={},m={},h=new Set;a.clear();let p=()=>{for(let t of h)t()},N=(t,e,r="")=>{if(m[t])return m[t];let n=i[t];if(!n)throw new Error(`Route with id "${t}" not found in declarations.`);let{path:o,meta:c}=typeof n=="string"?{path:n,meta:void 0}:n,u=o.startsWith("/")?o:s(r,o),d={id:t,path:o,fullPath:u,label:(()=>{try{return S(e||"")}catch(D){return e||""}})(),meta:c};if(!e)return null;a.set(t,d);let y=l[t];return y&&(d.children=Object.entries(y).map(([D,U])=>N(D,U,u)).filter(Boolean)),m[t]=d,d};for(let t in i)if(Object.prototype.hasOwnProperty.call(i,t)){let e=i[t],{path:r,meta:n}=typeof e=="string"?{path:e,meta:void 0}:e;typeof r=="string"&&!a.has(t)&&a.set(t,{id:t,path:r,fullPath:r.startsWith("/")?r:`/${r}`,label:"",meta:n})}for(let t in l)if(Object.prototype.hasOwnProperty.call(l,t)){let e=l[t];f[t]=Object.entries(e).map(([r,n])=>N(r,n)).filter(Boolean)}let v=t=>{let e=t.split("?")[0].split("#")[0]||"/";for(let r of a.values()){let o=B(r.fullPath,{decode:decodeURIComponent,end:!0})(e);if(o)return{route:r,params:o.params}}};return{navigation:f,getActiveRoute:v,getBreadcrumbs:t=>{let e=t.split("?")[0].split("#")[0]||"/";if(e==="/"){let c=v("/");return c?[c.route]:[]}let r=e.split("/").filter(Boolean),n=[],o="";for(let c of r){o=`${o}/${c}`;let u=v(o);u&&n.push(u.route)}return n},filterNavigation:t=>{let e={},r=n=>n.reduce((o,c)=>{if(t(c)){let d=$({},c);d.children&&(d.children=r(d.children)),o.push(d)}return o},[]);for(let n in f)e[n]=r(f[n]);return e},generateSitemap:t=>{let{domain:e}=t,r=[];for(let n of a.values())if(!n.fullPath.includes(":")){let c=`${e.endsWith("/")?e.slice(0,-1):e}${n.fullPath}`;r.push(` <url>
<loc>${c}</loc>
</url>`)}return`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${r.join(`
`)}
</urlset>`},on:(t,e)=>(t==="change"&&h.add(e),()=>{h.delete(e)}),buildPath:(...t)=>{var c;let[e,r]=t,n=a.get(e);if(!n)throw new Error(`Route with id "${e}" not found. Did you forget to call "buildNav()"?`);let o=W(n.fullPath,{encode:encodeURIComponent,validate:!1});try{if(r&&Object.keys(r).length>0&&!n.fullPath.includes(":"))throw new TypeError("Static routes cannot have params");let u=o(r||{});return(c=n.path)!=null&&c.endsWith("/")&&!u.endsWith("/")?`${u}/`:u}catch(u){throw u instanceof TypeError?new Error(`Invalid params for route "${e}": ${u.message}`):u}}}},join:s}}export{F as a,O as b};