@kitbag/router 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- (function(y,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],u):(y=typeof globalThis<"u"?globalThis:y||self,u(y["@kitbag/router"]={},y.Vue))})(this,function(y,u){"use strict";var mn=Object.defineProperty;var yn=(y,u,N)=>u in y?mn(y,u,{enumerable:!0,configurable:!0,writable:!0,value:N}):y[u]=N;var O=(y,u,N)=>(yn(y,typeof u!="symbol"?u+"":u,N),N);class N extends Error{constructor(t){super(`Invalid Param "${t}": Router does not support multiple params by the same name. All param names must be unique.`)}}class Z extends Error{constructor(){super("Router not installed")}}class Ae extends Error{constructor(t,n){super(`useRoute called with incorrect route. Given ${t}, expected ${n}`)}}const fe=Symbol();function xe(){const e=u.inject(fe);if(!e)throw new Z;return e}const le=Symbol();function ee(){const e=u.inject(le);if(!e)throw new Z;return e}function Be(e,t){return[e,t].filter(n=>!!n).join(".")}function ut(e){const t=ee();function n(){if(!e)return;const r=t.route.matches.map(c=>c.name);if(!ft(r).includes(e))throw new Ae(e,t.route.key)}return u.watch(t.route,n,{immediate:!0,deep:!0}),t.route}function ft(e){return e.reduce((t,n)=>{const r=t.pop(),o=n?[Be(r,n)]:[];return r?[...t,r,...o]:o},[])}const Le=Symbol();function he(){return u.inject(Le,0)}class te{constructor(){O(this,"onBeforeRouteEnter",new Set);O(this,"onBeforeRouteUpdate",new Set);O(this,"onBeforeRouteLeave",new Set);O(this,"onAfterRouteEnter",new Set);O(this,"onAfterRouteUpdate",new Set);O(this,"onAfterRouteLeave",new Set)}}class Ue extends Error{}class G extends Error{constructor(n){super("Error occurred during a router push operation.");O(this,"to");this.to=n}}class pe extends Error{constructor(n){super(`Routing action rejected: ${n}`);O(this,"type");this.type=n}}function J(e){return Array.isArray(e)?e:[e]}function lt(e,t){const n=new te;return e.matches.forEach((r,o)=>{r.onBeforeRouteEnter&&de(e,t,o)&&J(r.onBeforeRouteEnter).forEach(a=>n.onBeforeRouteEnter.add(a)),r.onBeforeRouteUpdate&&ye(e,t,o)&&J(r.onBeforeRouteUpdate).forEach(a=>n.onBeforeRouteUpdate.add(a))}),t.matches.forEach((r,o)=>{r.onBeforeRouteLeave&&me(e,t,o)&&J(r.onBeforeRouteLeave).forEach(a=>n.onBeforeRouteLeave.add(a))}),n}function ht(e,t){const n=new te;return e.matches.forEach((r,o)=>{r.onAfterRouteEnter&&de(e,t,o)&&J(r.onAfterRouteEnter).forEach(a=>n.onAfterRouteEnter.add(a)),r.onAfterRouteUpdate&&ye(e,t,o)&&J(r.onAfterRouteUpdate).forEach(a=>n.onAfterRouteUpdate.add(a))}),t.matches.forEach((r,o)=>{r.onAfterRouteLeave&&me(e,t,o)&&J(r.onAfterRouteLeave).forEach(a=>n.onAfterRouteLeave.add(a))}),n}function z(e){return typeof e!="string"?!1:/^(https?:\/\/|\/).*/g.test(e)}function pt(){const e=c=>{throw new pe(c)},t=(...c)=>{throw new G(c)},n=(c,s,f)=>{if(z(c)){const w=s??{};throw new G([c,{...w,replace:!0}])}const m=s,R=f??{};throw new G([c,m,{...R,replace:!0}])},r=()=>{throw new Ue};async function o({to:c,from:s,hooks:f}){const{global:m,component:R}=f,w=lt(c,s),L=[...m.onBeforeRouteEnter,...w.onBeforeRouteEnter,...m.onBeforeRouteUpdate,...w.onBeforeRouteUpdate,...R.onBeforeRouteUpdate,...m.onBeforeRouteLeave,...w.onBeforeRouteLeave,...R.onBeforeRouteLeave];try{const p=L.map(A=>A(c,{from:s,reject:e,push:t,replace:n,abort:r}));await Promise.all(p)}catch(p){if(p instanceof G)return{status:"PUSH",to:p.to};if(p instanceof pe)return{status:"REJECT",type:p.type};if(p instanceof Ue)return{status:"ABORT"};throw p}return{status:"SUCCESS"}}async function a({to:c,from:s,hooks:f}){const{global:m,component:R}=f,w=ht(c,s),L=[...R.onAfterRouteLeave,...w.onAfterRouteLeave,...m.onAfterRouteLeave,...R.onAfterRouteUpdate,...w.onAfterRouteUpdate,...m.onAfterRouteUpdate,...R.onAfterRouteEnter,...w.onAfterRouteEnter,...m.onAfterRouteEnter];try{const p=L.map(A=>A(c,{from:s,reject:e,push:t,replace:n}));await Promise.all(p)}catch(p){if(p instanceof G)return{status:"PUSH",to:p.to};if(p instanceof pe)return{status:"REJECT",type:p.type};throw p}return{status:"SUCCESS"}}return{runBeforeRouteHooks:o,runAfterRouteHooks:a}}const de=(e,t,n)=>{const r=e.matches,o=(t==null?void 0:t.matches)??[];return r.length<n||r[n]!==o[n]},me=(e,t,n)=>{const r=e.matches,o=(t==null?void 0:t.matches)??[];return r.length<n||r[n]!==o[n]},ye=(e,t,n)=>e.matches[n]===(t==null?void 0:t.matches[n]);function je(e){switch(e){case"onBeforeRouteEnter":case"onAfterRouteEnter":return de;case"onBeforeRouteUpdate":case"onAfterRouteUpdate":return ye;case"onBeforeRouteLeave":case"onAfterRouteLeave":return me;default:throw new Error(`Switch is not exhaustive for lifecycle: ${e}`)}}class dt{constructor(){O(this,"global",new te);O(this,"component",new te)}addBeforeRouteHook({lifecycle:t,timing:n,depth:r,hook:o}){const a=je(t),c=this[n][t],s=(f,m)=>{if(a(f,m.from,r))return o(f,m)};return c.add(s),()=>c.delete(s)}addAfterRouteHook({lifecycle:t,timing:n,depth:r,hook:o}){const a=je(t),c=this[n][t],s=(f,m)=>{if(a(f,m.from,r))return o(f,m)};return c.add(s),()=>c.delete(s)}}const He=Symbol();function mt(){const e=new dt;return{onBeforeRouteEnter:s=>e.addBeforeRouteHook({lifecycle:"onBeforeRouteEnter",hook:s,timing:"global",depth:0}),onBeforeRouteUpdate:s=>e.addBeforeRouteHook({lifecycle:"onBeforeRouteUpdate",hook:s,timing:"global",depth:0}),onBeforeRouteLeave:s=>e.addBeforeRouteHook({lifecycle:"onBeforeRouteLeave",hook:s,timing:"global",depth:0}),onAfterRouteEnter:s=>e.addAfterRouteHook({lifecycle:"onAfterRouteEnter",hook:s,timing:"global",depth:0}),onAfterRouteUpdate:s=>e.addAfterRouteHook({lifecycle:"onAfterRouteUpdate",hook:s,timing:"global",depth:0}),onAfterRouteLeave:s=>e.addAfterRouteHook({lifecycle:"onAfterRouteLeave",hook:s,timing:"global",depth:0}),hooks:e}}function $e(){const e=u.inject(He);if(!e)throw new Z;return e}function Ne(e){return t=>{const n=he(),o=$e().addBeforeRouteHook({lifecycle:e,hook:t,depth:n,timing:"component"});return u.onUnmounted(o),o}}function Re(e){return t=>{const n=he(),o=$e().addAfterRouteHook({lifecycle:e,hook:t,depth:n,timing:"component"});return u.onUnmounted(o),o}}const yt=Ne("onBeforeRouteUpdate"),Rt=Ne("onBeforeRouteLeave"),gt=Re("onAfterRouteEnter"),wt=Re("onAfterRouteUpdate"),vt=Re("onAfterRouteLeave"),Et=["href"],Oe=u.defineComponent({__name:"routerLink",props:{to:{type:[String,Function]},query:{},replace:{type:Boolean}},setup(e){const t=e,n=ee(),r=u.computed(()=>z(t.to)?t.to:t.to(n.resolve)),o=u.computed(()=>{const{to:w,...L}=t;return L}),a=u.computed(()=>{var w;return(w=n.find(r.value,o.value))==null?void 0:w.matched}),c=u.computed(()=>!!a.value&&n.route.matches.includes(u.readonly(a.value))),s=u.computed(()=>!!a.value&&n.route.matched===a.value),f=u.computed(()=>({"router-link--match":c.value,"router-link--exact-match":s.value})),m=u.computed(()=>{const{host:w}=new URL(r.value,window.location.origin);return w!==window.location.host});function R(w){w.preventDefault(),n.push(r.value,o.value)}return(w,L)=>(u.openBlock(),u.createElementBlock("a",{href:r.value,class:u.normalizeClass(["router-link",f.value]),onClick:R},[u.renderSlot(w.$slots,"default",u.normalizeProps(u.guardReactiveProps({resolved:r.value,match:c.value,exactMatch:s.value,isExternal:m.value})))],10,Et))}}),ge=u.defineComponent({__name:"routerView",setup(e){const t=ee(),n=xe(),r=he();u.provide(Le,r+1);const o=u.computed(()=>{var c;const a=(c=t.route.matches[r])==null?void 0:c.component;return a?typeof a=="function"?u.defineAsyncComponent(a):a:null});return(a,c)=>u.unref(n)?(u.openBlock(),u.createBlock(u.resolveDynamicComponent(u.unref(n).component),{key:0})):u.renderSlot(a.$slots,"default",{key:1,route:u.unref(t).route,component:o.value},()=>[(u.openBlock(),u.createBlock(u.resolveDynamicComponent(o.value)))])}});function bt(e,t){return u.defineComponent({name:"PropsWrapper",expose:[],async setup(){const n=await t();return()=>u.h(e,n)}})}function _(){return _=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},_.apply(this,arguments)}var U;(function(e){e.Pop="POP",e.Push="PUSH",e.Replace="REPLACE"})(U||(U={}));var F=process.env.NODE_ENV!=="production"?function(e){return Object.freeze(e)}:function(e){return e};function T(e,t){if(!e){typeof console<"u"&&console.warn(t);try{throw new Error(t)}catch{}}}var ne="beforeunload",Pt="hashchange",Ce="popstate";function Ve(e){e===void 0&&(e={});var t=e,n=t.window,r=n===void 0?document.defaultView:n,o=r.history;function a(){var h=r.location,i=h.pathname,l=h.search,E=h.hash,b=o.state||{};return[b.idx,F({pathname:i,search:l,hash:E,state:b.usr||null,key:b.key||"default"})]}var c=null;function s(){if(c)p.call(c),c=null;else{var h=U.Pop,i=a(),l=i[0],E=i[1];if(p.length)if(l!=null){var b=R-l;b&&(c={action:h,location:E,retry:function(){P(b*-1)}},P(b))}else process.env.NODE_ENV!=="production"&&T(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation.");else H(h)}}r.addEventListener(Ce,s);var f=U.Pop,m=a(),R=m[0],w=m[1],L=Q(),p=Q();R==null&&(R=0,o.replaceState(_({},o.state,{idx:R}),""));function A(h){return typeof h=="string"?h:M(h)}function C(h,i){return i===void 0&&(i=null),F(_({pathname:w.pathname,hash:"",search:""},typeof h=="string"?Y(h):h,{state:i,key:oe()}))}function V(h,i){return[{usr:h.state,key:h.key,idx:i},A(h)]}function $(h,i,l){return!p.length||(p.call({action:h,location:i,retry:l}),!1)}function H(h){f=h;var i=a();R=i[0],w=i[1],L.call({action:f,location:w})}function d(h,i){var l=U.Push,E=C(h,i);function b(){d(h,i)}if($(l,E,b)){var x=V(E,R+1),g=x[0],S=x[1];try{o.pushState(g,"",S)}catch{r.location.assign(S)}H(l)}}function v(h,i){var l=U.Replace,E=C(h,i);function b(){v(h,i)}if($(l,E,b)){var x=V(E,R),g=x[0],S=x[1];o.replaceState(g,"",S),H(l)}}function P(h){o.go(h)}var k={get action(){return f},get location(){return w},createHref:A,push:d,replace:v,go:P,back:function(){P(-1)},forward:function(){P(1)},listen:function(i){return L.push(i)},block:function(i){var l=p.push(i);return p.length===1&&r.addEventListener(ne,re),function(){l(),p.length||r.removeEventListener(ne,re)}}};return k}function kt(e){e===void 0&&(e={});var t=e,n=t.window,r=n===void 0?document.defaultView:n,o=r.history;function a(){var i=Y(r.location.hash.substr(1)),l=i.pathname,E=l===void 0?"/":l,b=i.search,x=b===void 0?"":b,g=i.hash,S=g===void 0?"":g,B=o.state||{};return[B.idx,F({pathname:E,search:x,hash:S,state:B.usr||null,key:B.key||"default"})]}var c=null;function s(){if(c)p.call(c),c=null;else{var i=U.Pop,l=a(),E=l[0],b=l[1];if(p.length)if(E!=null){var x=R-E;x&&(c={action:i,location:b,retry:function(){k(x*-1)}},k(x))}else process.env.NODE_ENV!=="production"&&T(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation.");else d(i)}}r.addEventListener(Ce,s),r.addEventListener(Pt,function(){var i=a(),l=i[1];M(l)!==M(w)&&s()});var f=U.Pop,m=a(),R=m[0],w=m[1],L=Q(),p=Q();R==null&&(R=0,o.replaceState(_({},o.state,{idx:R}),""));function A(){var i=document.querySelector("base"),l="";if(i&&i.getAttribute("href")){var E=r.location.href,b=E.indexOf("#");l=b===-1?E:E.slice(0,b)}return l}function C(i){return A()+"#"+(typeof i=="string"?i:M(i))}function V(i,l){return l===void 0&&(l=null),F(_({pathname:w.pathname,hash:"",search:""},typeof i=="string"?Y(i):i,{state:l,key:oe()}))}function $(i,l){return[{usr:i.state,key:i.key,idx:l},C(i)]}function H(i,l,E){return!p.length||(p.call({action:i,location:l,retry:E}),!1)}function d(i){f=i;var l=a();R=l[0],w=l[1],L.call({action:f,location:w})}function v(i,l){var E=U.Push,b=V(i,l);function x(){v(i,l)}if(process.env.NODE_ENV!=="production"&&T(b.pathname.charAt(0)==="/","Relative pathnames are not supported in hash history.push("+JSON.stringify(i)+")"),H(E,b,x)){var g=$(b,R+1),S=g[0],B=g[1];try{o.pushState(S,"",B)}catch{r.location.assign(B)}d(E)}}function P(i,l){var E=U.Replace,b=V(i,l);function x(){P(i,l)}if(process.env.NODE_ENV!=="production"&&T(b.pathname.charAt(0)==="/","Relative pathnames are not supported in hash history.replace("+JSON.stringify(i)+")"),H(E,b,x)){var g=$(b,R),S=g[0],B=g[1];o.replaceState(S,"",B),d(E)}}function k(i){o.go(i)}var h={get action(){return f},get location(){return w},createHref:C,push:v,replace:P,go:k,back:function(){k(-1)},forward:function(){k(1)},listen:function(l){return L.push(l)},block:function(l){var E=p.push(l);return p.length===1&&r.addEventListener(ne,re),function(){E(),p.length||r.removeEventListener(ne,re)}}};return h}function _e(e){e===void 0&&(e={});var t=e,n=t.initialEntries,r=n===void 0?["/"]:n,o=t.initialIndex,a=r.map(function(d){var v=F(_({pathname:"/",search:"",hash:"",state:null,key:oe()},typeof d=="string"?Y(d):d));return process.env.NODE_ENV!=="production"&&T(v.pathname.charAt(0)==="/","Relative pathnames are not supported in createMemoryHistory({ initialEntries }) (invalid entry: "+JSON.stringify(d)+")"),v}),c=qe(o??a.length-1,0,a.length-1),s=U.Pop,f=a[c],m=Q(),R=Q();function w(d){return typeof d=="string"?d:M(d)}function L(d,v){return v===void 0&&(v=null),F(_({pathname:f.pathname,search:"",hash:""},typeof d=="string"?Y(d):d,{state:v,key:oe()}))}function p(d,v,P){return!R.length||(R.call({action:d,location:v,retry:P}),!1)}function A(d,v){s=d,f=v,m.call({action:s,location:f})}function C(d,v){var P=U.Push,k=L(d,v);function h(){C(d,v)}process.env.NODE_ENV!=="production"&&T(f.pathname.charAt(0)==="/","Relative pathnames are not supported in memory history.push("+JSON.stringify(d)+")"),p(P,k,h)&&(c+=1,a.splice(c,a.length,k),A(P,k))}function V(d,v){var P=U.Replace,k=L(d,v);function h(){V(d,v)}process.env.NODE_ENV!=="production"&&T(f.pathname.charAt(0)==="/","Relative pathnames are not supported in memory history.replace("+JSON.stringify(d)+")"),p(P,k,h)&&(a[c]=k,A(P,k))}function $(d){var v=qe(c+d,0,a.length-1),P=U.Pop,k=a[v];function h(){$(d)}p(P,k,h)&&(c=v,A(P,k))}var H={get index(){return c},get action(){return s},get location(){return f},createHref:w,push:C,replace:V,go:$,back:function(){$(-1)},forward:function(){$(1)},listen:function(v){return m.push(v)},block:function(v){return R.push(v)}};return H}function qe(e,t,n){return Math.min(Math.max(e,t),n)}function re(e){e.preventDefault(),e.returnValue=""}function Q(){var e=[];return{get length(){return e.length},push:function(n){return e.push(n),function(){e=e.filter(function(r){return r!==n})}},call:function(n){e.forEach(function(r){return r&&r(n)})}}}function oe(){return Math.random().toString(36).substr(2,8)}function M(e){var t=e.pathname,n=t===void 0?"/":t,r=e.search,o=r===void 0?"":r,a=e.hash,c=a===void 0?"":a;return o&&o!=="?"&&(n+=o.charAt(0)==="?"?o:"?"+o),c&&c!=="#"&&(n+=c.charAt(0)==="#"?c:"#"+c),n}function Y(e){var t={};if(e){var n=e.indexOf("#");n>=0&&(t.hash=e.substr(n),e=e.substr(0,n));var r=e.indexOf("?");r>=0&&(t.search=e.substr(r),e=e.substr(0,r)),e&&(t.pathname=e)}return t}function St(e,t){function n(r,o,a){if(typeof r=="object"){const s={...e.params,...r};return t(e.key,s,o)}const c={...e.params,[r]:o};return t(e.key,c,a)}return new Proxy(e,{get:(r,o,a)=>o==="update"?n:o==="params"?new Proxy(e.params,{set(c,s,f){return n(s,f),!0}}):Reflect.get(r,o,a)})}function At(e,t){const n=u.reactive({...e}),r=c=>{Object.assign(n,{...c})},o=u.readonly(n),a=St(o,t);return{currentRoute:o,routerRoute:a,updateRoute:r}}class ae extends Error{}const K="[",W="]";function xt(e){return e!==String&&e!==Boolean&&e!==Number&&e!==Date}function De(e){return typeof e=="function"&&xt(e)}function we(e){return typeof e=="object"&&"get"in e&&typeof e.get=="function"&&"set"in e&&typeof e.set=="function"}function ce(e){return typeof e=="string"&&e.length>0}function Bt(e,t){return e[t]??String}const Te=Symbol();function Me(e){return Te in e}function Lt(e){return{[Te]:!0,get:t=>{if(ce(t))return ve(t,e)},set:t=>ce(t)?Ee(t,e):""}}const j={invalid:e=>{throw new ae(e)}},Ut={get:e=>e,set:(e,{invalid:t})=>{if(typeof e!="string")throw t();return e}},Ke={get:(e,{invalid:t})=>{if(e==="true")return!0;if(e==="false")return!1;throw t()},set:(e,{invalid:t})=>{if(typeof e!="boolean")throw t();return e.toString()}},Ie={get:(e,{invalid:t})=>{const n=Number(e);if(isNaN(n))throw t();return n},set:(e,{invalid:t})=>{if(typeof e!="number")throw t();return e.toString()}},Je={get:(e,{invalid:t})=>{console.log("inside date getter");const n=new Date(e);if(isNaN(n.getTime()))throw t();return n},set:(e,{invalid:t})=>{if(typeof e!="object"||!(e instanceof Date))throw t();return e.toISOString()}},Fe={get:(e,{invalid:t})=>{try{return JSON.parse(e)}catch{throw t()}},set:(e,{invalid:t})=>{try{return JSON.stringify(e)}catch{throw t()}}};function ve(e,t){if(e===void 0){if(Me(t))return t.get(e,j);throw new ae}if(t===String)return Ut.get(e,j);if(t===Boolean)return Ke.get(e,j);if(t===Number)return Ie.get(e,j);if(t===Date)return Je.get(e,j);if(t===JSON)return Fe.get(e,j);if(De(t))return t(e,j);if(we(t))return t.get(e,j);if(t instanceof RegExp){if(t.test(e))return e;throw new ae}return e}function Ee(e,t){if(t===Boolean)return Ke.set(e,j);if(t===Number)return Ie.set(e,j);if(t===Date)return Je.set(e,j);if(t===JSON)return Fe.set(e,j);if(we(t))return t.set(e,j);try{return e.toString()}catch{throw new ae}}function jt(e){const t=be(e.path.toString());return new RegExp(`^${t}$`,"i")}function Ht(e){const t=new URLSearchParams(e.query.toString());return Array.from(t.entries()).filter(([,n])=>!We(n)).map(([n,r])=>new RegExp(`${n}=${be(r)}(&|$)`,"i"))}function be(e){return[$t,Nt].reduce((t,n)=>n(t),e)}const Pe=`\\${K}\\?([\\w]+)\\${W}`,Qe=`\\${K}([\\w]+)\\${W}`;function $t(e){return e.replace(new RegExp(Pe,"g"),".*")}function We(e){return new RegExp(Pe,"g").test(e)}function Nt(e){return e.replace(new RegExp(Qe,"g"),".+")}function Ot(e){const[t]=se(e,new RegExp(Pe,"g")),[n]=se(e,new RegExp(Qe,"g"));return t??n}function se(e,t){return Array.from(e.matchAll(t)).flatMap(([,...r])=>r.map(o=>ce(o)?o:""))}function Ge(e,t,n){const r=ze(t,n),[o]=se(e,r);return o}function Ct(e,t){if(!t)return e;const{name:n,param:r,value:o}=t,a=ze(e,n);return se(e,a).reduce((s,f)=>f===void 0?s:s.replace(f,()=>Ee(o,r)),e)}function ze(e,t){const n=[Vt,_t,be].reduce((r,o)=>o(r,t),e);return new RegExp(n,"g")}function Vt(e,t){const n=new RegExp(`(\\${K}\\?${t})\\${W}`,"g");return e.replace(n,"(.*)")}function _t(e,t){const n=new RegExp(`(\\${K}${t})\\${W}`,"g");return e.replace(n,"(.+)")}function Ye(e,...t){return t.reduce((n,r)=>{if(!r)return n;const o=new URLSearchParams(r).toString();return Object.keys(o).length===0?n:n.includes("?")?`${n}&${o}`:`${n}?${o}`},e)}function qt(e,t={}){const{params:n={},query:r}=t,o={...e.path.params,...e.query.params},a=e.path.toString(),c=e.query.toString(),s=Dt(a,o,n),f=Tt(c,o,n);return Ye(s,f,r)}function Dt(e,t,n){return e.length?Object.entries(t).reduce((r,[o,a])=>Ct(r,{name:o,param:a,value:n[o]}),e):e}function Tt(e,t,n){if(!e.length)return{};const r=new URLSearchParams(e);return Array.from(r.entries()).reduce((o,[a,c])=>{const s=Ot(c);if(!s)return{...o,[a]:c};const f=Ee(n[s],t[s]);return We(c)&&n[s]===void 0&&f===""?o:{...o,[a]:f}},{})}function Xe(e){return(t,n,r)=>{if(z(t))return Ye(t,(n??{}).query);const o=n??{},a=r??{},c=e.find(f=>f.key===t);if(!c)throw`Route not found: "${String(t)}"`;if(c.matched.disabled)throw`Route disabled: "${String(t)}"`;return qt(c,{params:o,query:a.query})}}function X(e){return!e.startsWith("http")?Kt(e):Mt(e)}function Mt(e){const{protocol:t,host:n,pathname:r,search:o,searchParams:a,hash:c}=new URL(e,e);return{protocol:t,host:n,pathname:r,search:o,searchParams:a,hash:c}}function Kt(e){const{pathname:t,search:n,searchParams:r,hash:o}=new URL(e,"https://localhost");return{pathname:t,search:n,searchParams:r,hash:o}}function Ze(e){const t=new URLSearchParams(e);return{get:n=>t.get(n),getAll:n=>t.getAll(n)}}const It=(e,t)=>{try{et(e,t)}catch{return!1}return!0},et=(e,t)=>{const{pathname:n,search:r}=X(t);return{...Jt(e.path,n),...Ft(e.query,r)}};function Jt(e,t){const n={},r=decodeURIComponent(t);for(const[o,a]of Object.entries(e.params)){const c=Ge(r,e.toString(),o),s=ve(c,a);n[o]=s}return n}function Ft(e,t){const n={},r=new URLSearchParams(t);for(const[o,a]of Object.entries(e.params)){const c=r.get(o)??void 0,s=ve(c,a);n[o]=s}return n}const Qt=(e,t)=>{const{pathname:n}=X(t);return jt(e).test(n)},Wt=(e,t)=>{const{search:n}=X(t);return Ht(e).every(o=>o.test(n))};function Gt(e){const{searchParams:t,pathname:n}=X(e),r=-1,o=1;return(a,c)=>{const s=nt(a,t),f=tt(a,n),m=nt(c,t),R=tt(c,n);return s+f>m+R?r:s+f<m+R?o:a.depth>c.depth?r:a.depth<c.depth?o:0}}function tt(e,t){const n=Object.entries(e.path.params).filter(([,o])=>Me(o)).map(([o])=>o),r=n.filter(o=>Ge(t,e.path.toString(),o)===void 0);return n.length-r.length}function nt(e,t){const n=new URLSearchParams(e.query.toString()),r=Array.from(n.keys()),o=r.filter(a=>!t.has(a));return r.length-o.length}function rt(e,t){const n=[zt,Qt,Wt,It],r=Gt(t),o=e.filter(m=>n.every(R=>R(m,t))).sort(r);if(o.length===0)return;const[a]=o,{search:c}=X(t),s=Ze(c),f=et(a,t);return u.readonly({matched:a.matched,matches:a.matches,key:a.key,query:s,params:f})}const zt=e=>"name"in e.matched&&!!e.matched.name;function Yt(e){return(t,n={})=>{const o=Xe(e)(t,n);return rt(e,o)}}function ot(){return typeof window<"u"&&typeof window.document<"u"}function Xt({mode:e,listener:t}){const n=Zt(e),r=(f,m)=>{if(m!=null&&m.replace)return n.replace(f);n.push(f)},o=()=>{const f=M(n.location);return n.replace(f)};let a;return{...n,update:r,refresh:o,startListening:()=>{a==null||a(),a=n.listen(t)},stopListening:()=>{a==null||a()}}}function Zt(e="auto"){switch(e){case"auto":return ot()?Ve():_e();case"browser":return Ve();case"memory":return _e();case"hash":return kt();default:const t=e;throw new Error(`Switch is not exhaustive for mode: ${t}`)}}function en(e){return u.defineComponent(()=>()=>u.h("h1",e),{name:e,props:[]})}const at=Symbol();function tn({rejections:e}){const t=c=>{const s={...e};return u.markRaw(s[c]??en(c))},n=c=>{const s=u.markRaw(t(c)),f={name:c,path:"",component:s,meta:{}};return u.readonly({matched:f,matches:[f],key:c,query:Ze(""),params:{},[at]:!0})},r=c=>c[at]===!0,o=c=>{if(!c){a.value=null;return}const s=t(c);a.value={type:c,component:s}},a=u.ref(null);return{setRejection:o,rejection:a,getRejectionRoute:n,isRejectionRoute:r}}class nn extends Error{constructor(){super("initialUrl must be set if window.location is unavailable")}}function rn(e){if(e)return e;if(ot())return window.location.toString();throw new nn}function on(e,t={}){const n=Xe(e),r=Xt({mode:t.historyMode,listener:()=>{const g=M(location);p(g)}}),{runBeforeRouteHooks:o,runAfterRouteHooks:a}=pt(),{hooks:c,onBeforeRouteEnter:s,onAfterRouteUpdate:f,onBeforeRouteLeave:m,onAfterRouteEnter:R,onBeforeRouteUpdate:w,onAfterRouteLeave:L}=mt();async function p(g,{replace:S}={}){r.stopListening();const B=rt(e,g)??v("NotFound"),q={...k},I=await o({to:B,from:q,hooks:c});switch(I.status){case"ABORT":return;case"PUSH":r.update(g,{replace:S}),await A(...I.to);return;case"REJECT":r.update(g,{replace:S}),H(I.type),i(B);break;case"SUCCESS":r.update(g,{replace:S}),H(null),i(B);break;default:throw new Error(`Switch is not exhaustive for before hook response status: ${JSON.stringify(I)}`)}const D=await a({to:B,from:q,hooks:c});switch(D.status){case"PUSH":await A(...D.to);break;case"REJECT":H(D.type);break;case"SUCCESS":break;default:const ue=D;throw new Error(`Switch is not exhaustive for after hook response status: ${JSON.stringify(ue)}`)}r.startListening()}const A=(g,S,B)=>{if(z(g)){const ue={...S},dn=n(g,ue);return p(dn,{replace:ue.replace})}const q={...B},D=n(g,S??{},q);return p(D,{replace:q.replace})},C=(g,S,B)=>{if(z(g)){const D={...S,replace:!0};return A(g,D)}const q=S??{},I={...B,replace:!0};return A(g,q,I)},V=g=>H(g),$=Yt(e),{setRejection:H,rejection:d,getRejectionRoute:v}=tn(t),P=v("NotFound"),{currentRoute:k,routerRoute:h,updateRoute:i}=At(P,A);r.startListening();const l=rn(t.initialUrl),E=p(l,{replace:!0});function b(g){g.component("RouterView",ge),g.component("RouterLink",Oe),g.provide(le,x),g.provide(fe,d),g.provide(He,c)}const x={route:h,resolve:n,push:A,replace:C,reject:V,find:$,refresh:r.refresh,forward:r.forward,back:r.back,go:r.go,install:b,initialized:E,onBeforeRouteEnter:s,onAfterRouteUpdate:f,onBeforeRouteLeave:m,onAfterRouteEnter:R,onBeforeRouteUpdate:w,onAfterRouteLeave:L};return x}function ct(e,t){const n=new RegExp(`\\${K}\\??([\\w]+)\\${W}`,"g");return Array.from(e.matchAll(n)).reduce((o,[a,c])=>{const s=a.startsWith(`${K}?`),f=Bt(t,c);if(c in o)throw new N(c);return o[c]=s?Lt(f):f,o},{})}function ke(e,t){return{path:e,params:ct(e,t),toString:()=>e}}function Se(e,t){const n=Object.keys(e),r=Object.keys(t),o=n.find(a=>r.includes(a));return o?{key:o,hasDuplicates:!0}:{key:void 0,hasDuplicates:!1}}function an(e,t){const{hasDuplicates:n,key:r}=Se(e.params,t.params);if(n)throw new N(r);return ke(`${e.path}${t.path}`,{...e.params,...t.params})}function ie(e,t){return{query:e,params:ct(e,t),toString:()=>e}}function cn(e,t){const{hasDuplicates:n,key:r}=Se(e.params,t.params);if(n)throw new N(r);const o=[e.query,t.query].filter(ce).join("&");return ie(o,{...e.params,...t.params})}function st(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function sn(e){return st(e)&&typeof e.path=="string"}function un(e){return sn(e)?e:ke(e,{})}function fn(e){return st(e)&&typeof e.query=="string"}function ln(e){return e===void 0?ie("",{}):fn(e)?e:ie(e,{})}function it(e){return"children"in e}function hn(e){const t=e.reduce((n,r)=>{const o=pn({...r,component:r.component??ge});return it(r)&&n.push(...r.children.map(a=>({...a,key:Be(o.key,a.key),path:an(o.path,a.path),query:cn(o.query,a.query),matches:[o.matched,...a.matches],depth:a.depth+1}))),n.push(o),n},[]);return t.forEach(({path:n,query:r})=>{const{hasDuplicates:o,key:a}=Se(n.params,r.params);if(o)throw new N(a)}),t}function pn(e){const t=un(e.path),n=ln(e.query),r=u.markRaw({meta:{},...e});return{matched:r,matches:[r],key:e.name,path:t,query:n,depth:1,disabled:e.disabled??!1}}y.DuplicateParamsError=N,y.RouterLink=Oe,y.RouterNotInstalledError=Z,y.RouterView=ge,y.UseRouteInvalidError=Ae,y.component=bt,y.createRouter=on,y.createRoutes=hn,y.isParamGetSet=we,y.isParamGetter=De,y.isParentRoute=it,y.onAfterRouteEnter=gt,y.onAfterRouteLeave=wt,y.onAfterRouteUpdate=vt,y.onBeforeRouteLeave=yt,y.onBeforeRouteUpdate=Rt,y.paramEnd=W,y.paramStart=K,y.path=ke,y.query=ie,y.routerInjectionKey=le,y.routerRejectionKey=fe,y.useRejection=xe,y.useRoute=ut,y.useRouter=ee,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
1
+ (function(p,u){typeof exports=="object"&&typeof module<"u"?u(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],u):(p=typeof globalThis<"u"?globalThis:p||self,u(p["@kitbag/router"]={},p.Vue))})(this,function(p,u){"use strict";var Bn=Object.defineProperty;var jn=(p,u,$)=>u in p?Bn(p,u,{enumerable:!0,configurable:!0,writable:!0,value:$}):p[u]=$;var C=(p,u,$)=>(jn(p,typeof u!="symbol"?u+"":u,$),$);class $ extends Error{constructor(e){super(`Invalid Param "${e}": Router does not support multiple params by the same name. All param names must be unique.`)}}class et extends Error{constructor(){super("Router not installed")}}class xt extends Error{constructor(e,n){super(`useRoute called with incorrect route. Given ${e}, expected ${n}`)}}const lt=Symbol();function nt(){const t=u.inject(lt);if(!t)throw new et;return t}class J extends Error{}function K(t){return typeof t!="string"?!1:/^(https?:\/\/|\/).*/g.test(t)}function Bt(t,e={},n={}){const r=nt(),o=u.computed(()=>u.toValue(t)),a=u.computed(()=>u.toValue(e)),c=u.computed(()=>u.toValue(n)),s=u.computed(()=>{if(K(o.value))return o.value;try{return r.resolve(o.value,a.value,c.value)}catch(R){throw R instanceof J&&console.error(`Failed to resolve route "${o.value.toString()}" in RouterLink.`,R),R}}),f=u.computed(()=>r.find(s.value,c.value)),l=u.computed(()=>!!f.value&&r.route.matches.includes(u.readonly(f.value.matched))),g=u.computed(()=>!!f.value&&r.route.matched===f.value.matched);return{route:f,href:s,isMatch:l,isExactMatch:g,push:R=>r.push(s.value,{},{...c.value,...R}),replace:R=>r.replace(s.value,{},{...c.value,...R})}}const ht=Symbol();function jt(){const t=u.inject(ht);if(!t)throw new et;return t}function Ht(t,e){return[t,e].filter(n=>!!n).join(".")}const rt=Symbol("isRouterRouteSymbol");function ge(t){return typeof t=="object"&&t!==null&&rt in t&&t[rt]===!0}function we(t,e){function n(r,o,a){if(typeof r=="object"){const s={...t.params,...r};return e(t.key,s,o)}const c={...t.params,[r]:o};return e(t.key,c,a)}return new Proxy(t,{has:(r,o)=>["update","params",rt].includes(o)?!0:Reflect.has(r,o),get:(r,o,a)=>o===rt?!0:o==="update"?n:o==="params"?new Proxy(t.params,{set(c,s,f){return n(s,f),!0}}):Reflect.get(r,o,a)})}function Nt(t,e,{exact:n}={}){if(!ge(t))return!1;if(e===void 0)return!0;const r=ve(t);if(n){const o=r.at(-1);return e===o}return r.includes(e)}function ve(t){return t.matches.map(n=>n.name).reduce((n,r)=>{const o=n.pop(),a=r?[Ht(o,r)]:[];return o?[...n,o,...a]:a},[])}function $t(t,{exact:e}={}){const n=nt();function r(){if(!t)return;if(!Nt(n.route,t,{exact:e}))throw new xt(t,n.route.key)}return u.watch(n.route,r,{immediate:!0,deep:!0}),n.route}const Ct=Symbol();function pt(){return u.inject(Ct,0)}class ot{constructor(){C(this,"onBeforeRouteEnter",new Set);C(this,"onBeforeRouteUpdate",new Set);C(this,"onBeforeRouteLeave",new Set);C(this,"onAfterRouteEnter",new Set);C(this,"onAfterRouteUpdate",new Set);C(this,"onAfterRouteLeave",new Set)}}class Vt extends Error{}class X extends Error{constructor(n){super("Error occurred during a router push operation.");C(this,"to");this.to=n}}class dt extends Error{constructor(n){super(`Routing action rejected: ${n}`);C(this,"type");this.type=n}}function Q(t){return Array.isArray(t)?t:[t]}function Ee(t,e){const n=new ot;return t.matches.forEach((r,o)=>{r.onBeforeRouteEnter&&mt(t,e,o)&&Q(r.onBeforeRouteEnter).forEach(a=>n.onBeforeRouteEnter.add(a)),r.onBeforeRouteUpdate&&yt(t,e,o)&&Q(r.onBeforeRouteUpdate).forEach(a=>n.onBeforeRouteUpdate.add(a))}),e.matches.forEach((r,o)=>{r.onBeforeRouteLeave&&Rt(t,e,o)&&Q(r.onBeforeRouteLeave).forEach(a=>n.onBeforeRouteLeave.add(a))}),n}function Pe(t,e){const n=new ot;return t.matches.forEach((r,o)=>{r.onAfterRouteEnter&&mt(t,e,o)&&Q(r.onAfterRouteEnter).forEach(a=>n.onAfterRouteEnter.add(a)),r.onAfterRouteUpdate&&yt(t,e,o)&&Q(r.onAfterRouteUpdate).forEach(a=>n.onAfterRouteUpdate.add(a))}),e.matches.forEach((r,o)=>{r.onAfterRouteLeave&&Rt(t,e,o)&&Q(r.onAfterRouteLeave).forEach(a=>n.onAfterRouteLeave.add(a))}),n}function be(){const t=c=>{throw new dt(c)},e=(...c)=>{throw new X(c)},n=(c,s,f)=>{if(K(c)){const R=s??{};throw new X([c,{...R,replace:!0}])}const l=s,g=f??{};throw new X([c,l,{...g,replace:!0}])},r=()=>{throw new Vt};async function o({to:c,from:s,hooks:f}){const{global:l,component:g}=f,R=Ee(c,s),x=[...l.onBeforeRouteEnter,...R.onBeforeRouteEnter,...l.onBeforeRouteUpdate,...R.onBeforeRouteUpdate,...g.onBeforeRouteUpdate,...l.onBeforeRouteLeave,...R.onBeforeRouteLeave,...g.onBeforeRouteLeave];try{const m=x.map(A=>A(c,{from:s,reject:t,push:e,replace:n,abort:r}));await Promise.all(m)}catch(m){if(m instanceof X)return{status:"PUSH",to:m.to};if(m instanceof dt)return{status:"REJECT",type:m.type};if(m instanceof Vt)return{status:"ABORT"};throw m}return{status:"SUCCESS"}}async function a({to:c,from:s,hooks:f}){const{global:l,component:g}=f,R=Pe(c,s),x=[...g.onAfterRouteLeave,...R.onAfterRouteLeave,...l.onAfterRouteLeave,...g.onAfterRouteUpdate,...R.onAfterRouteUpdate,...l.onAfterRouteUpdate,...g.onAfterRouteEnter,...R.onAfterRouteEnter,...l.onAfterRouteEnter];try{const m=x.map(A=>A(c,{from:s,reject:t,push:e,replace:n}));await Promise.all(m)}catch(m){if(m instanceof X)return{status:"PUSH",to:m.to};if(m instanceof dt)return{status:"REJECT",type:m.type};throw m}return{status:"SUCCESS"}}return{runBeforeRouteHooks:o,runAfterRouteHooks:a}}const mt=(t,e,n)=>{const r=t.matches,o=(e==null?void 0:e.matches)??[];return r.length<n||r[n]!==o[n]},Rt=(t,e,n)=>{const r=t.matches,o=(e==null?void 0:e.matches)??[];return r.length<n||r[n]!==o[n]},yt=(t,e,n)=>t.matches[n]===(e==null?void 0:e.matches[n]);function Ot(t){switch(t){case"onBeforeRouteEnter":case"onAfterRouteEnter":return mt;case"onBeforeRouteUpdate":case"onAfterRouteUpdate":return yt;case"onBeforeRouteLeave":case"onAfterRouteLeave":return Rt;default:throw new Error(`Switch is not exhaustive for lifecycle: ${t}`)}}class Se{constructor(){C(this,"global",new ot);C(this,"component",new ot)}addBeforeRouteHook({lifecycle:e,timing:n,depth:r,hook:o}){const a=Ot(e),c=this[n][e],s=(f,l)=>{if(a(f,l.from,r))return o(f,l)};return c.add(s),()=>c.delete(s)}addAfterRouteHook({lifecycle:e,timing:n,depth:r,hook:o}){const a=Ot(e),c=this[n][e],s=(f,l)=>{if(a(f,l.from,r))return o(f,l)};return c.add(s),()=>c.delete(s)}}const _t=Symbol();function ke(){const t=new Se;return{onBeforeRouteEnter:s=>t.addBeforeRouteHook({lifecycle:"onBeforeRouteEnter",hook:s,timing:"global",depth:0}),onBeforeRouteUpdate:s=>t.addBeforeRouteHook({lifecycle:"onBeforeRouteUpdate",hook:s,timing:"global",depth:0}),onBeforeRouteLeave:s=>t.addBeforeRouteHook({lifecycle:"onBeforeRouteLeave",hook:s,timing:"global",depth:0}),onAfterRouteEnter:s=>t.addAfterRouteHook({lifecycle:"onAfterRouteEnter",hook:s,timing:"global",depth:0}),onAfterRouteUpdate:s=>t.addAfterRouteHook({lifecycle:"onAfterRouteUpdate",hook:s,timing:"global",depth:0}),onAfterRouteLeave:s=>t.addAfterRouteHook({lifecycle:"onAfterRouteLeave",hook:s,timing:"global",depth:0}),hooks:t}}function qt(){const t=u.inject(_t);if(!t)throw new et;return t}function Dt(t){return e=>{const n=pt(),o=qt().addBeforeRouteHook({lifecycle:t,hook:e,depth:n,timing:"component"});return u.onUnmounted(o),o}}function gt(t){return e=>{const n=pt(),o=qt().addAfterRouteHook({lifecycle:t,hook:e,depth:n,timing:"component"});return u.onUnmounted(o),o}}const Ae=Dt("onBeforeRouteUpdate"),Le=Dt("onBeforeRouteLeave"),Ue=gt("onAfterRouteEnter"),xe=gt("onAfterRouteUpdate"),Be=gt("onAfterRouteLeave"),je=["href"],Mt=u.defineComponent({__name:"routerLink",props:{to:{type:[String,Function]},query:{},replace:{type:Boolean}},setup(t){const e=t,n=nt(),r=u.computed(()=>K(e.to)?e.to:e.to(n.resolve)),o=u.computed(()=>{const{to:R,...x}=e;return x}),{href:a,isMatch:c,isExactMatch:s}=Bt(r),f=u.computed(()=>({"router-link--match":c.value,"router-link--exact-match":s.value})),l=u.computed(()=>{const{host:R}=new URL(r.value,window.location.origin);return R!==window.location.host});function g(R){R.preventDefault(),n.push(a.value,o.value)}return(R,x)=>(u.openBlock(),u.createElementBlock("a",{href:r.value,class:u.normalizeClass(["router-link",f.value]),onClick:g},[u.renderSlot(R.$slots,"default",u.normalizeProps(u.guardReactiveProps({resolved:r.value,isMatch:u.unref(c),isExactMatch:u.unref(s),isExternal:l.value})))],10,je))}});function wt(t){return"children"in t}function Tt(t){return wt(t)&&!("component"in t)&&!("components"in t)}function Wt(t){return typeof t=="object"&&t!==null&&"component"in t}function It(t){return typeof t=="object"&&t!==null&&"components"in t}const vt=u.defineComponent({__name:"routerView",props:{name:{}},setup(t){const{name:e="default"}=t,n=$t(),r=jt(),o=pt();u.provide(Ct,o+1);const a=u.computed(()=>{if(r.value)return r.value.component;const s=n.matches.at(o);if(!s)return null;const l=c(s)[e];return l?typeof l=="function"?u.defineAsyncComponent(l):l:null});function c(s){return It(s)?s.components:Wt(s)?{default:s.component}:{}}return(s,f)=>u.renderSlot(s.$slots,"default",u.normalizeProps(u.guardReactiveProps({route:u.unref(n),component:a.value,rejection:u.unref(r)})),()=>[(u.openBlock(),u.createBlock(u.resolveDynamicComponent(a.value)))])}});function He(t,e){return u.defineComponent({name:"PropsWrapper",expose:[],async setup(){const n=await e();return()=>u.h(t,n)}})}function _(){return _=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},_.apply(this,arguments)}var B;(function(t){t.Pop="POP",t.Push="PUSH",t.Replace="REPLACE"})(B||(B={}));var G=process.env.NODE_ENV!=="production"?function(t){return Object.freeze(t)}:function(t){return t};function M(t,e){if(!t){typeof console<"u"&&console.warn(e);try{throw new Error(e)}catch{}}}var at="beforeunload",Ne="hashchange",Ft="popstate";function Jt(t){t===void 0&&(t={});var e=t,n=e.window,r=n===void 0?document.defaultView:n,o=r.history;function a(){var d=r.location,i=d.pathname,h=d.search,E=d.hash,P=o.state||{};return[P.idx,G({pathname:i,search:h,hash:E,state:P.usr||null,key:P.key||"default"})]}var c=null;function s(){if(c)m.call(c),c=null;else{var d=B.Pop,i=a(),h=i[0],E=i[1];if(m.length)if(h!=null){var P=g-h;P&&(c={action:d,location:E,retry:function(){b(P*-1)}},b(P))}else process.env.NODE_ENV!=="production"&&M(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation.");else j(d)}}r.addEventListener(Ft,s);var f=B.Pop,l=a(),g=l[0],R=l[1],x=z(),m=z();g==null&&(g=0,o.replaceState(_({},o.state,{idx:g}),""));function A(d){return typeof d=="string"?d:T(d)}function V(d,i){return i===void 0&&(i=null),G(_({pathname:R.pathname,hash:"",search:""},typeof d=="string"?Z(d):d,{state:i,key:st()}))}function O(d,i){return[{usr:d.state,key:d.key,idx:i},A(d)]}function N(d,i,h){return!m.length||(m.call({action:d,location:i,retry:h}),!1)}function j(d){f=d;var i=a();g=i[0],R=i[1],x.call({action:f,location:R})}function y(d,i){var h=B.Push,E=V(d,i);function P(){y(d,i)}if(N(h,E,P)){var L=O(E,g+1),w=L[0],k=L[1];try{o.pushState(w,"",k)}catch{r.location.assign(k)}j(h)}}function v(d,i){var h=B.Replace,E=V(d,i);function P(){v(d,i)}if(N(h,E,P)){var L=O(E,g),w=L[0],k=L[1];o.replaceState(w,"",k),j(h)}}function b(d){o.go(d)}var S={get action(){return f},get location(){return R},createHref:A,push:y,replace:v,go:b,back:function(){b(-1)},forward:function(){b(1)},listen:function(i){return x.push(i)},block:function(i){var h=m.push(i);return m.length===1&&r.addEventListener(at,ct),function(){h(),m.length||r.removeEventListener(at,ct)}}};return S}function $e(t){t===void 0&&(t={});var e=t,n=e.window,r=n===void 0?document.defaultView:n,o=r.history;function a(){var i=Z(r.location.hash.substr(1)),h=i.pathname,E=h===void 0?"/":h,P=i.search,L=P===void 0?"":P,w=i.hash,k=w===void 0?"":w,U=o.state||{};return[U.idx,G({pathname:E,search:L,hash:k,state:U.usr||null,key:U.key||"default"})]}var c=null;function s(){if(c)m.call(c),c=null;else{var i=B.Pop,h=a(),E=h[0],P=h[1];if(m.length)if(E!=null){var L=g-E;L&&(c={action:i,location:P,retry:function(){S(L*-1)}},S(L))}else process.env.NODE_ENV!=="production"&&M(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation.");else y(i)}}r.addEventListener(Ft,s),r.addEventListener(Ne,function(){var i=a(),h=i[1];T(h)!==T(R)&&s()});var f=B.Pop,l=a(),g=l[0],R=l[1],x=z(),m=z();g==null&&(g=0,o.replaceState(_({},o.state,{idx:g}),""));function A(){var i=document.querySelector("base"),h="";if(i&&i.getAttribute("href")){var E=r.location.href,P=E.indexOf("#");h=P===-1?E:E.slice(0,P)}return h}function V(i){return A()+"#"+(typeof i=="string"?i:T(i))}function O(i,h){return h===void 0&&(h=null),G(_({pathname:R.pathname,hash:"",search:""},typeof i=="string"?Z(i):i,{state:h,key:st()}))}function N(i,h){return[{usr:i.state,key:i.key,idx:h},V(i)]}function j(i,h,E){return!m.length||(m.call({action:i,location:h,retry:E}),!1)}function y(i){f=i;var h=a();g=h[0],R=h[1],x.call({action:f,location:R})}function v(i,h){var E=B.Push,P=O(i,h);function L(){v(i,h)}if(process.env.NODE_ENV!=="production"&&M(P.pathname.charAt(0)==="/","Relative pathnames are not supported in hash history.push("+JSON.stringify(i)+")"),j(E,P,L)){var w=N(P,g+1),k=w[0],U=w[1];try{o.pushState(k,"",U)}catch{r.location.assign(U)}y(E)}}function b(i,h){var E=B.Replace,P=O(i,h);function L(){b(i,h)}if(process.env.NODE_ENV!=="production"&&M(P.pathname.charAt(0)==="/","Relative pathnames are not supported in hash history.replace("+JSON.stringify(i)+")"),j(E,P,L)){var w=N(P,g),k=w[0],U=w[1];o.replaceState(k,"",U),y(E)}}function S(i){o.go(i)}var d={get action(){return f},get location(){return R},createHref:V,push:v,replace:b,go:S,back:function(){S(-1)},forward:function(){S(1)},listen:function(h){return x.push(h)},block:function(h){var E=m.push(h);return m.length===1&&r.addEventListener(at,ct),function(){E(),m.length||r.removeEventListener(at,ct)}}};return d}function Kt(t){t===void 0&&(t={});var e=t,n=e.initialEntries,r=n===void 0?["/"]:n,o=e.initialIndex,a=r.map(function(y){var v=G(_({pathname:"/",search:"",hash:"",state:null,key:st()},typeof y=="string"?Z(y):y));return process.env.NODE_ENV!=="production"&&M(v.pathname.charAt(0)==="/","Relative pathnames are not supported in createMemoryHistory({ initialEntries }) (invalid entry: "+JSON.stringify(y)+")"),v}),c=Qt(o??a.length-1,0,a.length-1),s=B.Pop,f=a[c],l=z(),g=z();function R(y){return typeof y=="string"?y:T(y)}function x(y,v){return v===void 0&&(v=null),G(_({pathname:f.pathname,search:"",hash:""},typeof y=="string"?Z(y):y,{state:v,key:st()}))}function m(y,v,b){return!g.length||(g.call({action:y,location:v,retry:b}),!1)}function A(y,v){s=y,f=v,l.call({action:s,location:f})}function V(y,v){var b=B.Push,S=x(y,v);function d(){V(y,v)}process.env.NODE_ENV!=="production"&&M(f.pathname.charAt(0)==="/","Relative pathnames are not supported in memory history.push("+JSON.stringify(y)+")"),m(b,S,d)&&(c+=1,a.splice(c,a.length,S),A(b,S))}function O(y,v){var b=B.Replace,S=x(y,v);function d(){O(y,v)}process.env.NODE_ENV!=="production"&&M(f.pathname.charAt(0)==="/","Relative pathnames are not supported in memory history.replace("+JSON.stringify(y)+")"),m(b,S,d)&&(a[c]=S,A(b,S))}function N(y){var v=Qt(c+y,0,a.length-1),b=B.Pop,S=a[v];function d(){N(y)}m(b,S,d)&&(c=v,A(b,S))}var j={get index(){return c},get action(){return s},get location(){return f},createHref:R,push:V,replace:O,go:N,back:function(){N(-1)},forward:function(){N(1)},listen:function(v){return l.push(v)},block:function(v){return g.push(v)}};return j}function Qt(t,e,n){return Math.min(Math.max(t,e),n)}function ct(t){t.preventDefault(),t.returnValue=""}function z(){var t=[];return{get length(){return t.length},push:function(n){return t.push(n),function(){t=t.filter(function(r){return r!==n})}},call:function(n){t.forEach(function(r){return r&&r(n)})}}}function st(){return Math.random().toString(36).substr(2,8)}function T(t){var e=t.pathname,n=e===void 0?"/":e,r=t.search,o=r===void 0?"":r,a=t.hash,c=a===void 0?"":a;return o&&o!=="?"&&(n+=o.charAt(0)==="?"?o:"?"+o),c&&c!=="#"&&(n+=c.charAt(0)==="#"?c:"#"+c),n}function Z(t){var e={};if(t){var n=t.indexOf("#");n>=0&&(e.hash=t.substr(n),t=t.substr(0,n));var r=t.indexOf("?");r>=0&&(e.search=t.substr(r),t=t.substr(0,r)),t&&(e.pathname=t)}return e}function Ce(t,e){const n=u.reactive({...t}),r=c=>{Object.assign(n,{...c})},o=u.readonly(n),a=we(o,e);return{currentRoute:o,routerRoute:a,updateRoute:r}}class Ve extends Error{constructor(e){super(`Route disabled: "${e}"`)}}class Oe extends Error{constructor(e){super(`Route not found: "${e}"`)}}const W="[",Y="]";function _e(t){return t!==String&&t!==Boolean&&t!==Number&&t!==Date}function Gt(t){return typeof t=="function"&&_e(t)}function tt(t){return typeof t=="object"&&"get"in t&&typeof t.get=="function"&&"set"in t&&typeof t.set=="function"}function zt(t){return tt(t)?t:{get:e=>bt(e,t),set:e=>St(e,t)}}const Yt=Symbol();function Et(t){return Yt in t}function qe(t){return{[Yt]:!0,...zt(t)}}function Xt(t){return tt(t)&&t.defaultValue!==void 0}function De(t,e){return{defaultValue:e,...zt(t)}}function Pt(t){return typeof t=="string"&&t.length>0}function Me(t,e){return t[e]??String}const H={invalid:t=>{throw new J(t)}},Te={get:t=>t,set:(t,{invalid:e})=>{if(typeof t!="string")throw e();return t}},Zt={get:(t,{invalid:e})=>{if(t==="true")return!0;if(t==="false")return!1;throw e()},set:(t,{invalid:e})=>{if(typeof t!="boolean")throw e();return t.toString()}},te={get:(t,{invalid:e})=>{const n=Number(t);if(isNaN(n))throw e();return n},set:(t,{invalid:e})=>{if(typeof t!="number")throw e();return t.toString()}},ee={get:(t,{invalid:e})=>{const n=new Date(t);if(isNaN(n.getTime()))throw e();return n},set:(t,{invalid:e})=>{if(typeof t!="object"||!(t instanceof Date))throw e();return t.toISOString()}},ne={get:(t,{invalid:e})=>{try{return JSON.parse(t)}catch{throw e()}},set:(t,{invalid:e})=>{try{return JSON.stringify(t)}catch{throw e()}}};function bt(t,e){if(t===void 0||!Pt(t)){if(Xt(e))return e.defaultValue;if(Et(e))return;throw new J}if(e===String)return Te.get(t,H);if(e===Boolean)return Zt.get(t,H);if(e===Number)return te.get(t,H);if(e===Date)return ee.get(t,H);if(e===JSON)return ne.get(t,H);if(Gt(e))return e(t,H);if(tt(e))return e.get(t,H);if(e instanceof RegExp){if(e.test(t))return t;throw new J}return t}function St(t,e){if(t===void 0){if(Et(e))return"";throw new J}if(e===Boolean)return Zt.set(t,H);if(e===Number)return te.set(t,H);if(e===Date)return ee.set(t,H);if(e===JSON)return ne.set(t,H);if(tt(e))return e.set(t,H);try{return t.toString()}catch{throw new J}}function We(t){const e=kt(t.path.toString());return new RegExp(`^${e}$`,"i")}function Ie(t){const e=new URLSearchParams(t.query.toString());return Array.from(e.entries()).filter(([,n])=>!oe(n)).map(([n,r])=>new RegExp(`${n}=${kt(r)}(&|$)`,"i"))}function kt(t){return[Fe,Je].reduce((e,n)=>n(e),t)}const At=`\\${W}\\?([\\w-_]+)\\${Y}`,re=`\\${W}([\\w-_]+)\\${Y}`;function Fe(t){return t.replace(new RegExp(At,"g"),".*")}function oe(t){return new RegExp(At,"g").test(t)}function Je(t){return t.replace(new RegExp(re,"g"),".+")}function Ke(t){const[e]=it(t,new RegExp(At,"g")),[n]=it(t,new RegExp(re,"g"));return e??n}function it(t,e){return Array.from(t.matchAll(e)).flatMap(([,...r])=>r.map(o=>Pt(o)?o:""))}function ae(t,e,n){const r=ce(e,n),[o]=it(t,r);return o}function Qe(t,e){if(!e)return t;const{name:n,param:r,value:o}=e,a=ce(t,n);return it(t,a).reduce((s,f)=>f===void 0?s:s.replace(f,()=>St(o,r)),t)}function ce(t,e){const n=[Ge,ze,kt].reduce((r,o)=>o(r,e),t);return new RegExp(n,"g")}function Ge(t,e){const n=new RegExp(`(\\${W}\\?${e})\\${Y}`,"g");return t.replace(n,"(.*)")}function ze(t,e){const n=new RegExp(`(\\${W}${e})\\${Y}`,"g");return t.replace(n,"(.+)")}function se(t,...e){return e.reduce((n,r)=>{if(!r)return n;const o=new URLSearchParams(r).toString();return Object.keys(o).length===0?n:n.includes("?")?`${n}&${o}`:`${n}?${o}`},t)}function Ye(t,e={}){const{params:n={},query:r}=e,o={...t.path.params,...t.query.params},a=t.path.toString(),c=t.query.toString(),s=Xe(a,o,n),f=Ze(c,o,n);return se(s,f,r)}function Xe(t,e,n){return t.length?Object.entries(e).reduce((r,[o,a])=>Qe(r,{name:o,param:a,value:n[o]}),t):t}function Ze(t,e,n){if(!t.length)return{};const r=new URLSearchParams(t);return Array.from(r.entries()).reduce((o,[a,c])=>{const s=Ke(c);if(!s)return{...o,[a]:c};const l=St(n[s],e[s]),g=n[s]===void 0&&l==="";return oe(c)&&g?o:{...o,[a]:l}},{})}function ie(t){return(e,n,r)=>{if(K(e))return se(e,(n??{}).query);const o=n??{},a=r??{},c=t.find(f=>f.key===e);if(!c)throw new Oe(String(e));if(c.matched.disabled)throw new Ve(String(e));return Ye(c,{params:o,query:a.query})}}function I(t){return!t.startsWith("http")?en(t):tn(t)}function tn(t){const{protocol:e,host:n,pathname:r,search:o,searchParams:a,hash:c}=new URL(t,t);return{protocol:e,host:n,pathname:r,search:o,searchParams:a,hash:c}}function en(t){const{pathname:e,search:n,searchParams:r,hash:o}=new URL(t,"https://localhost");return{pathname:e,search:n,searchParams:r,hash:o}}function ue(t){const e=new URLSearchParams(t);return{get:n=>e.get(n),getAll:n=>e.getAll(n)}}const nn=(t,e)=>{try{fe(t,e)}catch{return!1}return!0},fe=(t,e)=>{const{pathname:n,search:r}=I(e);return{...rn(t.path,n),...on(t.query,r)}};function rn(t,e){const n={},r=decodeURIComponent(e);for(const[o,a]of Object.entries(t.params)){const c=ae(r,t.toString(),o),s=bt(c,a);n[o]=s}return n}function on(t,e){const n={},r=new URLSearchParams(e);for(const[o,a]of Object.entries(t.params)){const c=r.get(o)??void 0,s=bt(c,a);n[o]=s}return n}const an=t=>"name"in t.matched&&!!t.matched.name,cn=(t,e)=>{const{host:n}=I(e),{host:r}=I(t.path.toString());return n===r},sn=(t,e)=>{const{pathname:n}=I(e);return We(t).test(n)},un=(t,e)=>{const{search:n}=I(e);return Ie(t).every(o=>o.test(n))};function fn(t){const{searchParams:e,pathname:n}=I(t),r=-1,o=1;return(a,c)=>{const s=he(a,e),f=le(a,n),l=he(c,e),g=le(c,n);return s+f>l+g?r:s+f<l+g?o:a.depth>c.depth?r:a.depth<c.depth?o:0}}function le(t,e){const n=Object.entries(t.path.params).filter(([,o])=>Et(o)).map(([o])=>o),r=n.filter(o=>ae(e,t.path.toString(),o)===void 0);return n.length-r.length}function he(t,e){const n=new URLSearchParams(t.query.toString()),r=Array.from(n.keys()),o=r.filter(a=>!e.has(a));return r.length-o.length}const ln=[an,cn,sn,un,nn];function pe(t,e){const n=fn(e),r=t.filter(f=>ln.every(l=>l(f,e))).sort(n);if(r.length===0)return;const[o]=r,{search:a}=I(e),c=ue(a),s=fe(o,e);return u.readonly({matched:o.matched,matches:o.matches,key:o.key,query:c,params:s})}function hn(t){return(e,n={})=>{const o=ie(t)(e,n);return pe(t,o)}}function de(){return typeof window<"u"&&typeof window.document<"u"}function pn({mode:t,listener:e}){const n=dn(t),r=(f,l)=>{if(l!=null&&l.replace)return n.replace(f);n.push(f)},o=()=>{const f=T(n.location);return n.replace(f)};let a;return{...n,update:r,refresh:o,startListening:()=>{a==null||a(),a=n.listen(e)},stopListening:()=>{a==null||a()}}}function dn(t="auto"){switch(t){case"auto":return de()?Jt():Kt();case"browser":return Jt();case"memory":return Kt();case"hash":return $e();default:const e=t;throw new Error(`Switch is not exhaustive for mode: ${e}`)}}function mn(t){return u.defineComponent(()=>()=>u.h("h1",t),{name:t,props:[]})}const me=Symbol();function Rn({rejections:t}){const e=c=>{const s={...t};return u.markRaw(s[c]??mn(c))},n=c=>{const s=u.markRaw(e(c)),f={name:c,path:"",component:s,meta:{}};return u.readonly({matched:f,matches:[f],key:c,query:ue(""),params:{},[me]:!0})},r=c=>c[me]===!0,o=c=>{if(!c){a.value=null;return}const s=e(c);a.value={type:c,component:s}},a=u.ref(null);return{setRejection:o,rejection:a,getRejectionRoute:n,isRejectionRoute:r}}class yn extends Error{constructor(){super("initialUrl must be set if window.location is unavailable")}}function gn(t){if(t)return t;if(de())return window.location.toString();throw new yn}function wn(t,e={}){const n=ie(t),r=pn({mode:e.historyMode,listener:()=>{const w=T(location);m(w)}}),{runBeforeRouteHooks:o,runAfterRouteHooks:a}=be(),{hooks:c,onBeforeRouteEnter:s,onAfterRouteUpdate:f,onBeforeRouteLeave:l,onAfterRouteEnter:g,onBeforeRouteUpdate:R,onAfterRouteLeave:x}=ke();async function m(w,{replace:k}={}){r.stopListening();const U=pe(t,w)??v("NotFound"),q={...S},F=await o({to:U,from:q,hooks:c});switch(F.status){case"ABORT":return;case"PUSH":r.update(w,{replace:k}),await A(...F.to);return;case"REJECT":r.update(w,{replace:k}),j(F.type),i(U);break;case"SUCCESS":r.update(w,{replace:k}),j(null),i(U);break;default:throw new Error(`Switch is not exhaustive for before hook response status: ${JSON.stringify(F)}`)}const D=await a({to:U,from:q,hooks:c});switch(D.status){case"PUSH":await A(...D.to);break;case"REJECT":j(D.type);break;case"SUCCESS":break;default:const ft=D;throw new Error(`Switch is not exhaustive for after hook response status: ${JSON.stringify(ft)}`)}r.startListening()}const A=(w,k,U)=>{if(K(w)){const ft={...k},xn=n(w,ft);return m(xn,{replace:ft.replace})}const q={...U},D=n(w,k??{},q);return m(D,{replace:q.replace})},V=(w,k,U)=>{if(K(w)){const D={...k,replace:!0};return A(w,D)}const q=k??{},F={...U,replace:!0};return A(w,q,F)},O=w=>j(w),N=hn(t),{setRejection:j,rejection:y,getRejectionRoute:v}=Rn(e),b=v("NotFound"),{currentRoute:S,routerRoute:d,updateRoute:i}=Ce(b,A);r.startListening();const h=gn(e.initialUrl),E=m(h,{replace:!0});function P(w){w.component("RouterView",vt),w.component("RouterLink",Mt),w.provide(lt,L),w.provide(ht,y),w.provide(_t,c)}const L={route:d,resolve:n,push:A,replace:V,reject:O,find:N,refresh:r.refresh,forward:r.forward,back:r.back,go:r.go,install:P,initialized:E,onBeforeRouteEnter:s,onAfterRouteUpdate:f,onBeforeRouteLeave:l,onAfterRouteEnter:g,onBeforeRouteUpdate:R,onAfterRouteLeave:x};return L}function Re(t,e){const n=new RegExp(`\\${W}\\??([\\w-_]+)\\${Y}`,"g");return Array.from(t.matchAll(n)).reduce((o,[a,c])=>{const s=a.startsWith(`${W}?`),f=Me(e,c);if(c in o)throw new $(c);return o[c]=s?qe(f):f,o},{})}function Lt(t,e){return{path:t,params:Re(t,e),toString:()=>t}}function Ut(t,e){const n=Object.keys(t),r=Object.keys(e),o=n.find(a=>r.includes(a));return o?{key:o,hasDuplicates:!0}:{key:void 0,hasDuplicates:!1}}function vn(t,e){const{hasDuplicates:n,key:r}=Ut(t.params,e.params);if(n)throw new $(r);return Lt(`${t.path}${e.path}`,{...t.params,...e.params})}function ut(t,e){return{query:t,params:Re(t,e),toString:()=>t}}function En(t,e){const{hasDuplicates:n,key:r}=Ut(t.params,e.params);if(n)throw new $(r);const o=[t.query,e.query].filter(Pt).join("&");return ut(o,{...t.params,...e.params})}function ye(t){return typeof t=="object"&&t!==null&&!Array.isArray(t)}function Pn(t){return ye(t)&&typeof t.path=="string"}function bn(t){return Pn(t)?t:Lt(t,{})}function Sn(t){return ye(t)&&typeof t.query=="string"}function kn(t){return t===void 0?ut("",{}):Sn(t)?t:ut(t,{})}function An(t){const e=t.reduce((n,r)=>{const o=Ln(r);return wt(r)&&n.push(...r.children.map(a=>({...a,key:Ht(o.key,a.key),path:vn(o.path,a.path),query:En(o.query,a.query),matches:[o.matched,...a.matches],depth:a.depth+1}))),n.push(o),n},[]);return e.forEach(({path:n,query:r})=>{const{hasDuplicates:o,key:a}=Ut(n.params,r.params);if(o)throw new $(a)}),e}function Ln(t){const e=Un(t),n=bn(t.path),r=kn(t.query),o=u.markRaw({meta:{},...e});return{matched:o,matches:[o],key:t.name,path:n,query:r,depth:1,disabled:t.disabled??!1}}function Un(t){return Tt(t)?{...t,component:vt}:t}p.DuplicateParamsError=$,p.RouterLink=Mt,p.RouterNotInstalledError=et,p.RouterView=vt,p.UseRouteInvalidError=xt,p.component=He,p.createRouter=wn,p.createRoutes=An,p.isParamGetSet=tt,p.isParamGetter=Gt,p.isParamWithDefault=Xt,p.isParentRoute=wt,p.isParentRouteWithoutComponent=Tt,p.isRoute=Nt,p.isRouteWithComponent=Wt,p.isRouteWithComponents=It,p.onAfterRouteEnter=Ue,p.onAfterRouteLeave=xe,p.onAfterRouteUpdate=Be,p.onBeforeRouteLeave=Ae,p.onBeforeRouteUpdate=Le,p.paramEnd=Y,p.paramStart=W,p.path=Lt,p.query=ut,p.routerInjectionKey=lt,p.routerRejectionKey=ht,p.useLink=Bt,p.useRejection=jt,p.useRoute=$t,p.useRouter=nt,p.withDefault=De,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kitbag/router",
3
3
  "private": false,
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "bugs": {
6
6
  "url": "https://github.com/kitbagjs/router/issues"
7
7
  },