UNPKG

@route-weaver/core

Version:

A typesafe navigation package for creating and managing routes.

8 lines (7 loc) 2.94 kB
var P=Object.defineProperty;var M=Object.getOwnPropertySymbols;var I=Object.prototype.hasOwnProperty,b=Object.prototype.propertyIsEnumerable;var S=(u,a,c)=>a in u?P(u,a,{enumerable:!0,configurable:!0,writable:!0,value:c}):u[a]=c,R=(u,a)=>{for(var c in a||(a={}))I.call(a,c)&&S(u,c,a[c]);if(M)for(var c of M(a))b.call(a,c)&&S(u,c,a[c]);return u};import{compile as $,match as U}from"path-to-regexp";function E(u){let a=new Map,c=(...d)=>`/${d.join("/")}`.replace(/\/+/g,"/").replace(/\/$/,"")||"/";return{buildNav:(d,p={})=>{let{t:w=t=>t}=p,f={},m={},h=new Set;a.clear();let j=()=>{for(let t of h)t()},v=(t,e,r="")=>{if(m[t])return m[t];let n=u[t];if(!n)throw new Error(`Route with id "${t}" not found in declarations.`);let{path:o,meta:s}=typeof n=="string"?{path:n,meta:void 0}:n,i=o.startsWith("/")?o:c(r,o),l={id:t,path:o,fullPath:i,label:(()=>{try{return w(e||"")}catch(N){return e||""}})(),meta:s};if(!e)return null;a.set(t,l);let T=d[t];return T&&(l.children=Object.entries(T).map(([N,y])=>v(N,y,i)).filter(Boolean)),m[t]=l,l};for(let t in u)if(Object.prototype.hasOwnProperty.call(u,t)){let e=u[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 d)if(Object.prototype.hasOwnProperty.call(d,t)){let e=d[t];f[t]=Object.entries(e).map(([r,n])=>v(r,n)).filter(Boolean)}let g=t=>{let e=t.split("?")[0].split("#")[0]||"/";for(let r of a.values()){let o=U(r.fullPath,{decode:decodeURIComponent,end:!0})(e);if(o)return{route:r,params:o.params}}};return{navigation:f,getActiveRoute:g,getBreadcrumbs:t=>{let e=t.split("?")[0].split("#")[0]||"/";if(e==="/"){let s=g("/");return s?[s.route]:[]}let r=e.split("/").filter(Boolean),n=[],o="";for(let s of r){o=`${o}/${s}`;let i=g(o);i&&n.push(i.route)}return n},filterNavigation:t=>{let e={},r=n=>n.reduce((o,s)=>{if(t(s)){let l=R({},s);l.children&&(l.children=r(l.children)),o.push(l)}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 s=`${e.endsWith("/")?e.slice(0,-1):e}${n.fullPath}`;r.push(` <url> <loc>${s}</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 s;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=$(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 i=o(r||{});return(s=n.path)!=null&&s.endsWith("/")&&!i.endsWith("/")?`${i}/`:i}catch(i){throw i instanceof TypeError?new Error(`Invalid params for route "${e}": ${i.message}`):i}}}},join:c}}export{E as createRouteWeaver};