UNPKG

@route-weaver/core

Version:

A typesafe navigation package for creating and managing routes.

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