@@ -15,6 +15,8 @@ import { encodeQueryValue as _encodeQueryValue } from '../encoding'
15
15
import { parseURL , stringifyURL } from '../location'
16
16
import type {
17
17
MatcherLocationAsNamed ,
18
+ MatcherLocationAsPathAbsolute ,
19
+ MatcherLocationAsPathRelative ,
18
20
MatcherLocationAsRelative ,
19
21
MatcherParamsFormatted ,
20
22
} from './matcher-location'
@@ -48,10 +50,16 @@ export interface RouteResolver<Matcher, MatcherNormalized> {
48
50
resolve ( location : MatcherLocationAsNamed ) : NEW_LocationResolved
49
51
50
52
/**
51
- * Resolves a location by its path. Any required query must be passed.
53
+ * Resolves a location by its absolute path (starts with `/`) . Any required query must be passed.
52
54
* @param location - The location to resolve.
53
55
*/
54
- // resolve(location: MatcherLocationAsPath): NEW_MatcherLocationResolved
56
+ resolve ( location : MatcherLocationAsPathAbsolute ) : NEW_LocationResolved
57
+
58
+ resolve (
59
+ location : MatcherLocationAsPathRelative ,
60
+ currentLocation : NEW_LocationResolved
61
+ ) : NEW_LocationResolved
62
+
55
63
// NOTE: in practice, this overload can cause bugs. It's better to use named locations
56
64
57
65
/**
@@ -66,11 +74,28 @@ export interface RouteResolver<Matcher, MatcherNormalized> {
66
74
addRoute ( matcher : Matcher , parent ?: MatcherNormalized ) : MatcherNormalized
67
75
removeRoute ( matcher : MatcherNormalized ) : void
68
76
clearRoutes ( ) : void
77
+
78
+ /**
79
+ * Get a list of all matchers.
80
+ * Previously named `getRoutes()`
81
+ */
82
+ getMatchers ( ) : MatcherNormalized [ ]
83
+
84
+ /**
85
+ * Get a matcher by its name.
86
+ * Previously named `getRecordMatcher()`
87
+ */
88
+ getMatcher ( name : MatcherName ) : MatcherNormalized | undefined
69
89
}
70
90
71
91
type MatcherResolveArgs =
72
92
| [ absoluteLocation : `/${string } `]
73
93
| [ relativeLocation : string , currentLocation : NEW_LocationResolved ]
94
+ | [ absoluteLocation : MatcherLocationAsPathAbsolute ]
95
+ | [
96
+ relativeLocation : MatcherLocationAsPathRelative ,
97
+ currentLocation : NEW_LocationResolved
98
+ ]
74
99
| [ location : MatcherLocationAsNamed ]
75
100
| [
76
101
relativeLocation : MatcherLocationAsRelative ,
@@ -224,6 +249,7 @@ export function createCompiledMatcher(): RouteResolver<
224
249
MatcherRecordRaw ,
225
250
MatcherPattern
226
251
> {
252
+ // TODO: we also need an array that has the correct order
227
253
const matchers = new Map < MatcherName , MatcherPattern > ( )
228
254
229
255
// TODO: allow custom encode/decode functions
@@ -241,6 +267,7 @@ export function createCompiledMatcher(): RouteResolver<
241
267
242
268
// string location, e.g. '/foo', '../bar', 'baz', '?page=1'
243
269
if ( typeof location === 'string' ) {
270
+ // parseURL handles relative paths
244
271
const url = parseURL ( parseQuery , location , currentLocation ?. path )
245
272
246
273
let matcher : MatcherPattern | undefined
@@ -266,7 +293,6 @@ export function createCompiledMatcher(): RouteResolver<
266
293
// }
267
294
268
295
parsedParams = { ...pathParams , ...queryParams , ...hashParams }
269
- // console.log('parsedParams', parsedParams)
270
296
271
297
if ( parsedParams ) break
272
298
} catch ( e ) {
@@ -296,6 +322,7 @@ export function createCompiledMatcher(): RouteResolver<
296
322
hash : url . hash ,
297
323
matched,
298
324
}
325
+ // TODO: handle object location { path, query, hash }
299
326
} else {
300
327
// relative location or by name
301
328
if ( __DEV__ && location . name == null && currentLocation == null ) {
@@ -368,11 +395,21 @@ export function createCompiledMatcher(): RouteResolver<
368
395
matchers . clear ( )
369
396
}
370
397
398
+ function getMatchers ( ) {
399
+ return Array . from ( matchers . values ( ) )
400
+ }
401
+
402
+ function getMatcher ( name : MatcherName ) {
403
+ return matchers . get ( name )
404
+ }
405
+
371
406
return {
372
407
resolve,
373
408
374
409
addRoute,
375
410
removeRoute,
376
411
clearRoutes,
412
+ getMatcher,
413
+ getMatchers,
377
414
}
378
415
}
0 commit comments