UNPKG

@route-weaver/core

Version:

A typesafe navigation package for creating and managing routes.

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