55// Vue auto route: https://github.com/soybeanjs/elegant-router
66
77
8- import type { LazyRouteFunction , RouteObject , IndexRouteObject } from "react-router-dom" ;
9- import type { FunctionComponent } from " react" ;
8+ import type { IndexRouteObject , LazyRouteFunction , RouteObject } from "react-router-dom" ;
9+ import type { FunctionComponent } from ' react' ;
1010import type { ElegantConstRoute } from '@ohh-889/react-auto-route' ;
11- import type { RouteMap , RouteKey , RoutePath } from '@elegant-router/types' ;
12- import { redirect as redirectTo } from 'react-router-dom'
11+ import type { RouteKey , RouteMap , RoutePath } from '@elegant-router/types' ;
12+ import { redirect as redirectTo } from 'react-router-dom' ;
1313import ErrorBoundary from "../../../ErrorBoundary.tsx"
1414
15-
16- type CustomRouteObject = Omit < RouteObject , 'Component' | 'index' > & {
17- Component ?: React . ComponentType < any > | null ;
15+ type CustomRouteObject = Omit < RouteObject , 'Component' | 'index' > & {
16+ Component ?: React . ComponentType < any > | null ;
1817} ;
1918
20-
2119/**
2220 * transform elegant const routes to react routes
21+ *
2322 * @param routes elegant const routes
2423 * @param layouts layout components
2524 * @param views view components
@@ -34,15 +33,16 @@ export function transformElegantRoutesToReactRoutes(
3433
3534/**
3635 * transform elegant route to react route
36+ *
3737 * @param route elegant const route
3838 * @param layouts layout components
3939 * @param views view components
4040 */
4141export function transformElegantRouteToReactRoute (
4242 route : ElegantConstRoute ,
4343 layouts : Record < string , LazyRouteFunction < CustomRouteObject > > ,
44- views : Record < string , LazyRouteFunction < CustomRouteObject > >
45- ) :RouteObject {
44+ views : Record < string , LazyRouteFunction < CustomRouteObject > >
45+ ) : RouteObject {
4646 const LAYOUT_PREFIX = 'layout.' ;
4747 const VIEW_PREFIX = 'view.' ;
4848 const ROUTE_DEGREE_SPLITTER = '_' ;
@@ -55,7 +55,7 @@ export function transformElegantRouteToReactRoute(
5555 function getLayoutName ( component : string ) {
5656 const layout = component . replace ( LAYOUT_PREFIX , '' ) ;
5757
58- if ( ! layouts [ layout ] ) {
58+ if ( ! layouts [ layout ] ) {
5959 throw new Error ( `Layout component "${ layout } " not found` ) ;
6060 }
6161
@@ -69,7 +69,7 @@ export function transformElegantRouteToReactRoute(
6969 function getViewName ( component : string ) {
7070 const view = component . replace ( VIEW_PREFIX , '' ) ;
7171
72- if ( ! views [ view ] ) {
72+ if ( ! views [ view ] ) {
7373 throw new Error ( `View component "${ view } " not found` ) ;
7474 }
7575
@@ -88,31 +88,36 @@ export function transformElegantRouteToReactRoute(
8888 const [ layout , view ] = component . split ( FIRST_LEVEL_ROUTE_COMPONENT_SPLIT ) ;
8989
9090 return {
91- layout : layout ? getLayoutName ( layout ) : undefined ,
91+ layout : layout ? getLayoutName ( layout ) : undefined ,
9292 view : getViewName ( view )
9393 } ;
9494 }
9595
96+ const { name, props, path, meta, component, children, redirect, layout, ...rest } = route ;
9697
97- const { name, props, path, meta, component, children, redirect, layout, loader, ...rest } = route ;
98-
99- const reactRoute = { id :name , path, handle : {
100- ...meta
101- } , children :[ ] , ErrorBoundary } as RouteObject
98+ const reactRoute = {
99+ id : name ,
100+ path,
101+ handle : {
102+ ...meta
103+ } ,
104+ children : [ ] ,
105+ ErrorBoundary
106+ } as RouteObject ;
102107
103108 try {
104109 if ( component ) {
105110 if ( isSingleLevelRoute ( route ) ) {
106111 const { layout, view } = getSingleLevelRouteComponent ( component ) ;
107112
108- if ( layout ) {
109- const singleLevelRoute :RouteObject = {
113+ if ( layout ) {
114+ const singleLevelRoute : RouteObject = {
110115 path,
111116 lazy : layouts [ layout ] ,
112117 ErrorBoundary,
113118 children : [
114119 {
115- id :name ,
120+ id : name ,
116121 index : true ,
117122 lazy : views [ view ] ,
118123 handle : {
@@ -123,77 +128,67 @@ export function transformElegantRouteToReactRoute(
123128 ]
124129 } ;
125130
126- return singleLevelRoute ;
131+ return singleLevelRoute ;
127132 }
128133
129- return {
134+ return {
130135 path,
131136 lazy : views [ view ] ,
132137 id : name ,
133138 ...rest
134- } as RouteObject ;
139+ } as RouteObject ;
135140 }
136141
137142 if ( isLayout ( component ) ) {
138143 if ( layout ) {
139- reactRoute . lazy = views [ name ]
144+ reactRoute . lazy = views [ name ] ;
140145 } else {
141146 const layoutName = getLayoutName ( component ) ;
142147 reactRoute . lazy = layouts [ layoutName ] ;
143148 }
144-
145149 }
146150
147-
148151 if ( isView ( component ) ) {
149152 const viewName = getViewName ( component ) ;
150153 if ( props ) {
151154 reactRoute . lazy = async ( ) => {
152- const data = ( await views [ viewName ] ( ) ) . Component as FunctionComponent
155+ const data = ( await views [ viewName ] ( ) ) . Component as FunctionComponent ;
153156 return {
154- Component : ( ) => data ( props ) ,
155- ErrorBoundary : null
156- }
157- }
157+ Component : ( ) => data ( props ) ,
158+ ErrorBoundary : null
159+ } ;
160+ } ;
158161 } else {
159- reactRoute . lazy = views [ viewName ]
162+ reactRoute . lazy = views [ viewName ] ;
160163 }
161164 }
162-
163- }
165+ } else if ( ! layout && ! redirect ) {
166+ return Object . assign ( reactRoute , rest ) ;
167+ }
164168 } catch ( error : any ) {
165- console . error ( `Error transforming route "${ route . name } ": ${ error . toString ( ) } ` ) ;
169+ console . error ( `Error transforming route "${ route . name } ": ${ error . toString ( ) } ` ) ;
166170 return { } ;
167171 }
168172
169-
170-
171-
172-
173- if ( children ?. length ) {
173+ if ( children ?. length ) {
174174 reactRoute . children = children . flatMap ( child => transformElegantRouteToReactRoute ( child , layouts , views ) ) ;
175- const defaultRedirectPath = redirect || getRedirectPath ( path as string , children [ 0 ] . path as string ) ;
176-
177- reactRoute . children . unshift ( {
178- index : true ,
179- loader : ( ) => redirectTo ( defaultRedirectPath ) ,
180- ...rest
181- } ) ;
182- } else if ( redirect ) {
183- reactRoute . loader = ( ) => redirectTo ( redirect )
184- }
185-
186- if ( loader ) {
187- reactRoute . loader = ( ) => loader
175+ const defaultRedirectPath = redirect || getRedirectPath ( path as string , children [ 0 ] . path as string ) ;
176+
177+ reactRoute . children . unshift ( {
178+ index : true ,
179+ loader : ( ) => redirectTo ( defaultRedirectPath ) ,
180+ ...rest
181+ } ) ;
182+ } else if ( redirect ) {
183+ reactRoute . loader = ( ) => redirectTo ( redirect ) ;
188184 }
189185
190186 if ( layout ) {
191-
192187 return {
193188 lazy : layouts [ layout ] ,
194189 children : [ reactRoute ] ,
195190 ErrorBoundary
196- } as RouteObject ;
191+ } as RouteObject ;
197192 }
198193
199194 return reactRoute ;
@@ -217,6 +212,7 @@ const routeMap: RouteMap = {
217212 "document_unocss" : "unocss" ,
218213 "document_procomponents" : "procomponents" ,
219214 "document_antd" : "antd" ,
215+ "logout" : "/logout" ,
220216 "403" : "/403" ,
221217 "404" : "/404" ,
222218 "500" : "/500" ,
0 commit comments