1- import type {
2- NavigationHookAfter ,
3- RouteLocationRaw ,
4- RouteRecordRaw ,
5- } from "vue-router" ;
1+ import type { NavigationHookAfter , RouteLocationRaw , RouteRecordRaw } from "vue-router" ;
62
73import { computed } from "vue" ;
84import { createRouter , createWebHistory } from "vue-router" ;
95
106import { createRouterLink , createRouterView } from "./components" ;
11- import {
12- CREATE_PAGE_NAME ,
13- EDIT_PAGE_NAME ,
14- OVERVIEW_PAGE_NAME ,
15- SHOW_PAGE_NAME ,
16- } from "./routes" ;
17- import type {
18- BeforeRouteMiddleware ,
19- RouterService ,
20- RouterServiceOptions ,
21- } from "./types" ;
7+ import { CREATE_PAGE_NAME , EDIT_PAGE_NAME , OVERVIEW_PAGE_NAME , SHOW_PAGE_NAME } from "./routes" ;
8+ import type { BeforeRouteMiddleware , RouterService , RouterServiceOptions } from "./types" ;
229
2310export const createRouterService = < Routes extends RouteRecordRaw [ ] > (
2411 routes : Routes ,
@@ -36,9 +23,7 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
3623 const getRoutePath = ( name : string ) : string =>
3724 router . getRoutes ( ) . find ( ( route ) => route . name === name ) ?. path ?? "" ;
3825
39- const resolveParentId = (
40- overrideParentId ?: number ,
41- ) : string | number | undefined => {
26+ const resolveParentId = ( overrideParentId ?: number ) : string | number | undefined => {
4227 if ( overrideParentId ) return overrideParentId ;
4328
4429 // CRUD routes use single :parentId — repeatable params (:parentId+) are not supported
@@ -57,21 +42,15 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
5742 if ( parentId ) params . parentId = parentId ;
5843 if ( id ) {
5944 params . id = id ;
60- if ( ! params . parentId || ! targetPath . includes ( ":id" ) )
61- params . parentId = id ;
45+ if ( ! params . parentId || ! targetPath . includes ( ":id" ) ) params . parentId = id ;
6246 }
6347
6448 return Object . fromEntries (
6549 Object . entries ( params ) . filter ( ( [ key ] ) => targetPath . includes ( `:${ key } ` ) ) ,
6650 ) ;
6751 } ;
6852
69- const goToRoute : RouterService < Routes > [ "goToRoute" ] = async (
70- name ,
71- id ,
72- query ,
73- parentId ,
74- ) => {
53+ const goToRoute : RouterService < Routes > [ "goToRoute" ] = async ( name , id , query , parentId ) => {
7554 const route : RouteLocationRaw = { name } ;
7655 const params = resolveRouteParams ( name as string , id , parentId ) ;
7756
@@ -81,17 +60,17 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
8160 await router . push ( route ) ;
8261 } ;
8362
84- const normalizedRouteToSpecificRoute : RouterService < Routes > [ "normalizedRouteToSpecificRoute" ] =
85- ( route ) => {
86- const specificRoute = flattenedRoutes . find (
87- ( { path, name } ) => name === route . name || path === route . path ,
88- ) ;
63+ const normalizedRouteToSpecificRoute : RouterService < Routes > [ "normalizedRouteToSpecificRoute" ] = (
64+ route ,
65+ ) => {
66+ const specificRoute = flattenedRoutes . find (
67+ ( { path, name } ) => name === route . name || path === route . path ,
68+ ) ;
8969
90- if ( ! specificRoute )
91- throw new Error ( `${ route . path } is an unknown route` ) ;
70+ if ( ! specificRoute ) throw new Error ( `${ route . path } is an unknown route` ) ;
9271
93- return specificRoute ;
94- } ;
72+ return specificRoute ;
73+ } ;
9574
9675 const getUrlForRouteName : RouterService < Routes > [ "getUrlForRouteName" ] = (
9776 name ,
@@ -108,17 +87,13 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
10887 const beforeRouteMiddleware : BeforeRouteMiddleware < Routes > [ ] = [ ] ;
10988 router . beforeEach ( async ( to , from ) => {
11089 const toNormalized = normalizedRouteToSpecificRoute ( to ) ;
111- const fromNormalized = from . name
112- ? normalizedRouteToSpecificRoute ( from )
113- : toNormalized ;
90+ const fromNormalized = from . name ? normalizedRouteToSpecificRoute ( from ) : toNormalized ;
11491
11592 for ( const middleware of beforeRouteMiddleware )
11693 if ( await middleware ( toNormalized , fromNormalized ) ) return false ;
11794 } ) ;
11895
119- const afterRouteMiddleware : NavigationHookAfter [ ] = [
120- ...( options ?. afterRouteCallbacks ?? [ ] ) ,
121- ] ;
96+ const afterRouteMiddleware : NavigationHookAfter [ ] = [ ...( options ?. afterRouteCallbacks ?? [ ] ) ] ;
12297 router . afterEach ( ( to , from , failure ) => {
12398 for ( const middleware of afterRouteMiddleware ) middleware ( to , from , failure ) ;
12499 } ) ;
@@ -133,9 +108,7 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
133108 } ;
134109
135110 const fullPath =
136- ( options ?. base
137- ? location . pathname . replace ( options . base , "" )
138- : location . pathname ) +
111+ ( options ?. base ? location . pathname . replace ( options . base , "" ) : location . pathname ) +
139112 location . search +
140113 location . hash ;
141114
@@ -147,8 +120,7 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
147120 goToCreatePage : ( name ) => goToRoute ( `${ name } ${ CREATE_PAGE_NAME } ` ) ,
148121 goToOverviewPage : ( name ) => goToRoute ( `${ name } ${ OVERVIEW_PAGE_NAME } ` ) ,
149122 goToEditPage : ( name , id ) => goToRoute ( `${ name } ${ EDIT_PAGE_NAME } ` , id ) ,
150- goToShowPage : ( name , id , query ) =>
151- goToRoute ( `${ name } ${ SHOW_PAGE_NAME } ` , id , query ) ,
123+ goToShowPage : ( name , id , query ) => goToRoute ( `${ name } ${ SHOW_PAGE_NAME } ` , id , query ) ,
152124
153125 getUrlForRouteName,
154126 goBack : ( ) => router . back ( ) ,
@@ -194,11 +166,9 @@ export const createRouterService = <Routes extends RouteRecordRaw[]>(
194166 changeRouteQuery : ( query ) => void router . push ( { query } ) ,
195167
196168 onPage,
197- onCreatePage : ( baseRouteName ) =>
198- onPage ( baseRouteName + CREATE_PAGE_NAME ) ,
169+ onCreatePage : ( baseRouteName ) => onPage ( baseRouteName + CREATE_PAGE_NAME ) ,
199170 onEditPage : ( baseRouteName ) => onPage ( baseRouteName + EDIT_PAGE_NAME ) ,
200- onOverviewPage : ( baseRouteName ) =>
201- onPage ( baseRouteName + OVERVIEW_PAGE_NAME ) ,
171+ onOverviewPage : ( baseRouteName ) => onPage ( baseRouteName + OVERVIEW_PAGE_NAME ) ,
202172 onShowPage : ( baseRouteName ) => onPage ( baseRouteName + SHOW_PAGE_NAME ) ,
203173 routeExists : ( to ) => {
204174 try {
0 commit comments