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