Skip to content

Commit 65a0940

Browse files
committed
refactor: add methods needed by router
1 parent b8eba1a commit 65a0940

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

packages/router/src/new-route-resolver/matcher-location.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,41 @@ export interface MatcherLocationAsNamed {
1919
hash?: string
2020

2121
/**
22-
* A path is ignored if `name` is provided.
22+
* @deprecated This is ignored when `name` is provided
2323
*/
2424
path?: undefined
2525
}
2626

27-
export interface MatcherLocationAsPath {
27+
export interface MatcherLocationAsPathRelative {
2828
path: string
2929
query?: LocationQueryRaw
3030
hash?: string
3131

32+
/**
33+
* @deprecated This is ignored when `path` is provided
34+
*/
3235
name?: undefined
36+
/**
37+
* @deprecated This is ignored when `path` (instead of `name`) is provided
38+
*/
3339
params?: undefined
3440
}
41+
export interface MatcherLocationAsPathAbsolute
42+
extends MatcherLocationAsPathRelative {
43+
path: `/${string}`
44+
}
3545

3646
export interface MatcherLocationAsRelative {
3747
params?: MatcherParamsFormatted
3848
query?: LocationQueryRaw
3949
hash?: string
4050

51+
/**
52+
* @deprecated This location is relative to the next parameter. This `name` will be ignored.
53+
*/
4154
name?: undefined
55+
/**
56+
* @deprecated This location is relative to the next parameter. This `path` will be ignored.
57+
*/
4258
path?: undefined
4359
}

packages/router/src/new-route-resolver/matcher.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ describe('Matcher', () => {
209209
})
210210
})
211211

212+
describe('absolute locations as objects', () => {
213+
it('resolves an object location', () => {
214+
const matcher = createCompiledMatcher()
215+
matcher.addRoute(EMPTY_PATH_ROUTE)
216+
expect(matcher.resolve({ path: '/' })).toMatchObject({
217+
fullPath: '/',
218+
path: '/',
219+
params: {},
220+
query: {},
221+
hash: '',
222+
})
223+
})
224+
})
225+
212226
describe('named locations', () => {
213227
it('resolves named locations with no params', () => {
214228
const matcher = createCompiledMatcher()

packages/router/src/new-route-resolver/matcher.test-d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ describe('Matcher', () => {
3939
).toEqualTypeOf<NEW_LocationResolved>()
4040
})
4141
})
42+
43+
it('does not allow a name + path', () => {
44+
matcher.resolve({
45+
// ...({} as NEW_LocationResolved),
46+
name: 'foo',
47+
params: {},
48+
// @ts-expect-error: name + path
49+
path: '/e',
50+
})
51+
// @ts-expect-error: name + currentLocation
52+
matcher.resolve({ name: 'a', params: {} }, {} as NEW_LocationResolved)
53+
})
4254
})

packages/router/src/new-route-resolver/matcher.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { encodeQueryValue as _encodeQueryValue } from '../encoding'
1515
import { parseURL, stringifyURL } from '../location'
1616
import 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

7191
type 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

Comments
 (0)