@@ -15,6 +15,8 @@ import { encodeQueryValue as _encodeQueryValue } from '../encoding'
1515import { parseURL , stringifyURL } from '../location'
1616import type {
1717 MatcherLocationAsNamed ,
18+ MatcherLocationAsPathAbsolute ,
19+ MatcherLocationAsPathRelative ,
1820 MatcherLocationAsRelative ,
1921 MatcherParamsFormatted ,
2022} from './matcher-location'
@@ -48,10 +50,16 @@ export interface RouteResolver<Matcher, MatcherNormalized> {
4850 resolve ( location : MatcherLocationAsNamed ) : NEW_LocationResolved
4951
5052 /**
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.
5254 * @param location - The location to resolve.
5355 */
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+
5563 // NOTE: in practice, this overload can cause bugs. It's better to use named locations
5664
5765 /**
@@ -66,11 +74,28 @@ export interface RouteResolver<Matcher, MatcherNormalized> {
6674 addRoute ( matcher : Matcher , parent ?: MatcherNormalized ) : MatcherNormalized
6775 removeRoute ( matcher : MatcherNormalized ) : void
6876 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
6989}
7090
7191type MatcherResolveArgs =
7292 | [ absoluteLocation : `/${string } `]
7393 | [ relativeLocation : string , currentLocation : NEW_LocationResolved ]
94+ | [ absoluteLocation : MatcherLocationAsPathAbsolute ]
95+ | [
96+ relativeLocation : MatcherLocationAsPathRelative ,
97+ currentLocation : NEW_LocationResolved
98+ ]
7499 | [ location : MatcherLocationAsNamed ]
75100 | [
76101 relativeLocation : MatcherLocationAsRelative ,
@@ -224,6 +249,7 @@ export function createCompiledMatcher(): RouteResolver<
224249 MatcherRecordRaw ,
225250 MatcherPattern
226251> {
252+ // TODO: we also need an array that has the correct order
227253 const matchers = new Map < MatcherName , MatcherPattern > ( )
228254
229255 // TODO: allow custom encode/decode functions
@@ -241,6 +267,7 @@ export function createCompiledMatcher(): RouteResolver<
241267
242268 // string location, e.g. '/foo', '../bar', 'baz', '?page=1'
243269 if ( typeof location === 'string' ) {
270+ // parseURL handles relative paths
244271 const url = parseURL ( parseQuery , location , currentLocation ?. path )
245272
246273 let matcher : MatcherPattern | undefined
@@ -266,7 +293,6 @@ export function createCompiledMatcher(): RouteResolver<
266293 // }
267294
268295 parsedParams = { ...pathParams , ...queryParams , ...hashParams }
269- // console.log('parsedParams', parsedParams)
270296
271297 if ( parsedParams ) break
272298 } catch ( e ) {
@@ -296,6 +322,7 @@ export function createCompiledMatcher(): RouteResolver<
296322 hash : url . hash ,
297323 matched,
298324 }
325+ // TODO: handle object location { path, query, hash }
299326 } else {
300327 // relative location or by name
301328 if ( __DEV__ && location . name == null && currentLocation == null ) {
@@ -368,11 +395,21 @@ export function createCompiledMatcher(): RouteResolver<
368395 matchers . clear ( )
369396 }
370397
398+ function getMatchers ( ) {
399+ return Array . from ( matchers . values ( ) )
400+ }
401+
402+ function getMatcher ( name : MatcherName ) {
403+ return matchers . get ( name )
404+ }
405+
371406 return {
372407 resolve,
373408
374409 addRoute,
375410 removeRoute,
376411 clearRoutes,
412+ getMatcher,
413+ getMatchers,
377414 }
378415}
0 commit comments