@@ -10,7 +10,7 @@ export interface FreeObject {
10
10
export interface RouteDefinition {
11
11
path ?: string ;
12
12
route : Route | undefined ;
13
- routeName : string ;
13
+ name : string ;
14
14
options ?: FreeObject ;
15
15
indexRoute ?: Route ;
16
16
}
@@ -23,7 +23,7 @@ export interface routerJSRouteDefinition {
23
23
path ?: string ;
24
24
route : Route | undefined ;
25
25
options ?: FreeObject ;
26
- routeName : string ;
26
+ name : string ;
27
27
nestedRoutes ?: [ routerJSRouteDefinition ?] ;
28
28
}
29
29
@@ -35,13 +35,14 @@ export default class Router {
35
35
static LOG_MODELS = true ;
36
36
static SERVICES : FreeObject = { } ;
37
37
static ROUTE_REGISTRY : RouteRegistry = { } ;
38
- static ROUTER_SERVICE : RouterService | null = null ;
38
+ static ROUTER_SERVICE : RouterService ;
39
39
40
40
// static IS_TESTING() {
41
41
// return !!globalThis.QUnit;
42
42
// }
43
43
44
44
static visit ( ) {
45
+ // @ts -ignore
45
46
return this . ROUTER_SERVICE . visit ( ...arguments ) ;
46
47
}
47
48
@@ -67,7 +68,9 @@ export default class Router {
67
68
} ) ;
68
69
}
69
70
70
- static definitionsToRegistry ( arrayOfRouteDefinitions : Array < RouteDefinition > = [ ] ) : RouteRegistry {
71
+ static definitionsToRegistry (
72
+ arrayOfRouteDefinitions : Array < RouteDefinition > = [ ]
73
+ ) : RouteRegistry {
71
74
arrayOfRouteDefinitions . forEach ( ( routeDefinition : RouteDefinition ) => {
72
75
if ( ! routeDefinition . path ) {
73
76
throw new Error ( 'One of the RouteDefinition on Router.start(RouteDefinition[]) misses "path" key' ) ;
@@ -88,14 +91,14 @@ export default class Router {
88
91
const targetIndex = index >= routePathSegments . length ? routePathSegments . length - 1 : index ;
89
92
90
93
checkInRouteRegistryOrCreateRoute ( this . ROUTE_REGISTRY , {
91
- routeName : targetSegmentName ,
94
+ name : targetSegmentName ,
92
95
options : { path : `/${ routePathSegments [ targetIndex ] } ` } ,
93
96
route : index === routeNameSegments . length - 1 ? routeDefinition . route : undefined ,
94
97
} as routerJSRouteDefinition ) ;
95
98
96
99
if ( currentSegment && ! this . ROUTE_REGISTRY [ `${ currentSegment } .index` ] ) {
97
100
this . ROUTE_REGISTRY [ `${ currentSegment } .index` ] = {
98
- routeName : `${ currentSegment } .index` ,
101
+ name : `${ currentSegment } .index` ,
99
102
options : { path : '/' } ,
100
103
route : undefined , // TODO: add index route from parent by default
101
104
} ;
@@ -106,17 +109,19 @@ export default class Router {
106
109
107
110
if ( routeDefinition . indexRoute && routeName !== 'index' ) {
108
111
this . ROUTE_REGISTRY [ `${ routeName } .index` ] = {
109
- routeName : `${ routeName } .index` ,
112
+ name : `${ routeName } .index` ,
110
113
options : { path : '/' } ,
111
114
route : routeDefinition . indexRoute ,
112
115
} ;
113
116
}
114
117
} ) ;
115
118
119
+ // @ts -ignore
116
120
if ( ! arrayOfRouteDefinitions . find ( ( routeDefinition ) => routeDefinition . path . startsWith ( '/*' ) ) ) {
117
121
this . ROUTE_REGISTRY [ 'not-found' ] = {
118
- routeName : 'not-found' ,
122
+ name : 'not-found' ,
119
123
options : { path : '/*slug' } ,
124
+ // @ts -ignore
120
125
route : class NotFoundRoute extends Route { } ,
121
126
} ;
122
127
}
@@ -166,10 +171,11 @@ export default class Router {
166
171
167
172
export function createRouteNameFromRouteClass ( routeClass : Route | void ) : string | void {
168
173
if ( routeClass ) {
174
+ // @ts -ignore
169
175
return routeClass . name
170
176
. replace ( / R o u t e $ / g, '' )
171
177
. split ( '' )
172
- . reduce ( ( result , character , index ) => {
178
+ . reduce ( ( result : string [ ] , character : string , index : number ) => {
173
179
if ( index === 0 ) {
174
180
return character . toLowerCase ( ) ;
175
181
} else if ( character . toUpperCase ( ) === character ) {
@@ -188,8 +194,8 @@ export function createRouteNameFromPath(routePath: string): string {
188
194
}
189
195
190
196
function checkInRouteRegistryOrCreateRoute ( registry : RouteRegistry , targetRoute : routerJSRouteDefinition ) {
191
- const routeName = targetRoute . routeName ;
192
- const foundRoute = registry [ targetRoute . routeName ] ;
197
+ const routeName = targetRoute . name ;
198
+ const foundRoute = registry [ targetRoute . name ] ;
193
199
194
200
if ( ! foundRoute ) {
195
201
registry [ routeName ] = targetRoute ;
@@ -198,9 +204,9 @@ function checkInRouteRegistryOrCreateRoute(registry: RouteRegistry, targetRoute:
198
204
}
199
205
200
206
if ( targetRoute . route ) {
201
- if ( foundRoute . route && foundRoute . routeName !== targetRoute . routeName ) {
207
+ if ( foundRoute . route && foundRoute . name !== targetRoute . name ) {
202
208
console . log (
203
- `[WARNING]: ${ routeName } .route already has ${ foundRoute . routeName } . You tried to overwrite ${ routeName } .route with ${ targetRoute . routeName } `
209
+ `[WARNING]: ${ routeName } .route already has ${ foundRoute . name } . You tried to overwrite ${ routeName } .route with ${ targetRoute . name } `
204
210
) ;
205
211
}
206
212
@@ -215,8 +221,8 @@ function findNestedRoute(routerJSRouteArray: Array<routerJSRouteDefinition>, rou
215
221
const target = result . nestedRoutes || routerJSRouteArray ;
216
222
const targetRouteName = index === 0 ? routeNameSegment : routeNameSegments . slice ( 0 , index + 1 ) . join ( '.' ) ;
217
223
218
- return target . find ( ( routeObject : routerJSRouteDefinition ) => routeObject . routeName === targetRouteName ) ;
224
+ return target . find ( ( routeObject : routerJSRouteDefinition ) => routeObject . name === targetRouteName ) ;
219
225
} , { } as FreeObject ) ;
220
226
}
221
227
222
- window . Router = Router ;
228
+ // window.Router = Router;
0 commit comments