Skip to content

Commit 4d2c23b

Browse files
committed
refactor: simplify router resolve
1 parent 42c9849 commit 4d2c23b

File tree

8 files changed

+359
-259
lines changed

8 files changed

+359
-259
lines changed

packages/router/src/experimental/router.ts

Lines changed: 143 additions & 166 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
import { decode, MatcherName, MatcherQueryParams } from './matcher'
1+
import { decode, MatcherQueryParams } from './matcher'
22
import { EmptyParams, MatcherParamsFormatted } from './matcher-location'
33
import { miss } from './matchers/errors'
44

5-
export interface MatcherPattern {
6-
/**
7-
* Name of the matcher. Unique across all matchers.
8-
*/
9-
name: MatcherName
10-
11-
path: MatcherPatternPath
12-
query?: MatcherPatternQuery
13-
hash?: MatcherPatternHash
14-
15-
parent?: MatcherPattern
16-
}
17-
185
export interface MatcherPatternParams_Base<
196
TIn = string,
207
TOut extends MatcherParamsFormatted = MatcherParamsFormatted

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { mockWarn } from '../../__tests__/vitest-mock-warn'
88
import {
99
createCompiledMatcher,
1010
MatcherLocationRaw,
11-
MatcherRecordRaw,
11+
NEW_MatcherRecordRaw,
1212
NEW_LocationResolved,
1313
} from './matcher'
1414
import { PathParams, tokensToParser } from '../matcher/pathParserRanker'
@@ -24,7 +24,7 @@ const components = { default: component }
2424
function compileRouteRecord(
2525
record: RouteRecordRaw,
2626
parentRecord?: RouteRecordRaw
27-
): MatcherRecordRaw {
27+
): NEW_MatcherRecordRaw {
2828
// we adapt the path to ensure they are absolute
2929
// TODO: aliases? they could be handled directly in the path matcher
3030
const path = record.path.startsWith('/')
@@ -100,7 +100,7 @@ describe('RouterMatcher.resolve', () => {
100100
| `/${string}` = START_LOCATION
101101
) {
102102
const records = (Array.isArray(record) ? record : [record]).map(
103-
(record): MatcherRecordRaw => compileRouteRecord(record)
103+
(record): NEW_MatcherRecordRaw => compileRouteRecord(record)
104104
)
105105
const matcher = createCompiledMatcher()
106106
for (const record of records) {

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

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import {
66
} from './matcher'
77
import {
88
MatcherPatternParams_Base,
9-
MatcherPattern,
109
MatcherPatternPath,
1110
MatcherPatternQuery,
1211
MatcherPatternPathStatic,
1312
MatcherPatternPathDynamic,
1413
} from './matcher-pattern'
14+
import { NEW_MatcherRecord } from './matcher'
1515
import { miss } from './matchers/errors'
1616
import { EmptyParams } from './matcher-location'
1717

@@ -72,12 +72,17 @@ const ANY_HASH_PATTERN_MATCHER: MatcherPatternParams_Base<
7272
const EMPTY_PATH_ROUTE = {
7373
name: 'no params',
7474
path: EMPTY_PATH_PATTERN_MATCHER,
75-
} satisfies MatcherPattern
75+
} satisfies NEW_MatcherRecord
76+
77+
const ANY_PATH_ROUTE = {
78+
name: 'any path',
79+
path: ANY_PATH_PATTERN_MATCHER,
80+
} satisfies NEW_MatcherRecord
7681

7782
const USER_ID_ROUTE = {
7883
name: 'user-id',
7984
path: USER_ID_PATH_PATTERN_MATCHER,
80-
} satisfies MatcherPattern
85+
} satisfies NEW_MatcherRecord
8186

8287
describe('RouterMatcher', () => {
8388
describe('new matchers', () => {
@@ -135,6 +140,20 @@ describe('RouterMatcher', () => {
135140
const matcher = createCompiledMatcher()
136141
matcher.addRoute(USER_ID_ROUTE)
137142
})
143+
144+
it('removes static path', () => {
145+
const matcher = createCompiledMatcher()
146+
matcher.addRoute(EMPTY_PATH_ROUTE)
147+
matcher.removeRoute(EMPTY_PATH_ROUTE)
148+
// Add assertions to verify the route was removed
149+
})
150+
151+
it('removes dynamic path', () => {
152+
const matcher = createCompiledMatcher()
153+
matcher.addRoute(USER_ID_ROUTE)
154+
matcher.removeRoute(USER_ID_ROUTE)
155+
// Add assertions to verify the route was removed
156+
})
138157
})
139158

140159
describe('resolve()', () => {
@@ -293,5 +312,37 @@ describe('RouterMatcher', () => {
293312
})
294313
})
295314
})
315+
316+
describe('encoding', () => {
317+
it('handles encoded string path', () => {
318+
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
319+
console.log(matcher.resolve('/%23%2F%3F'))
320+
expect(matcher.resolve('/%23%2F%3F')).toMatchObject({
321+
fullPath: '/%23%2F%3F',
322+
path: '/%23%2F%3F',
323+
query: {},
324+
params: {},
325+
hash: '',
326+
})
327+
})
328+
329+
it('decodes query from a string', () => {
330+
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
331+
expect(matcher.resolve('/foo?foo=%23%2F%3F')).toMatchObject({
332+
path: '/foo',
333+
fullPath: '/foo?foo=%23%2F%3F',
334+
query: { foo: '#/?' },
335+
})
336+
})
337+
338+
it('decodes hash from a string', () => {
339+
const matcher = createCompiledMatcher([ANY_PATH_ROUTE])
340+
expect(matcher.resolve('/foo#h-%23%2F%3F')).toMatchObject({
341+
path: '/foo',
342+
fullPath: '/foo#h-%23%2F%3F',
343+
hash: '#h-#/?',
344+
})
345+
})
346+
})
296347
})
297348
})

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { describe, expectTypeOf, it } from 'vitest'
2-
import { NEW_LocationResolved, RouteResolver } from './matcher'
2+
import {
3+
NEW_LocationResolved,
4+
NEW_MatcherRecordRaw,
5+
NEW_RouterMatcher,
6+
} from './matcher'
7+
import { EXPERIMENTAL_RouteRecordNormalized } from '../experimental/router'
38

49
describe('Matcher', () => {
5-
const matcher: RouteResolver<unknown, unknown> = {} as any
10+
type TMatcherRecordRaw = NEW_MatcherRecordRaw
11+
type TMatcherRecord = EXPERIMENTAL_RouteRecordNormalized
12+
13+
const matcher: NEW_RouterMatcher<TMatcherRecordRaw, TMatcherRecord> =
14+
{} as any
615

716
describe('matcher.resolve()', () => {
817
it('resolves absolute string locations', () => {
9-
expectTypeOf(
10-
matcher.resolve('/foo')
11-
).toEqualTypeOf<NEW_LocationResolved>()
18+
expectTypeOf(matcher.resolve('/foo')).toEqualTypeOf<
19+
NEW_LocationResolved<TMatcherRecord>
20+
>()
1221
})
1322

1423
it('fails on non absolute location without a currentLocation', () => {
@@ -18,14 +27,14 @@ describe('Matcher', () => {
1827

1928
it('resolves relative locations', () => {
2029
expectTypeOf(
21-
matcher.resolve('foo', {} as NEW_LocationResolved)
22-
).toEqualTypeOf<NEW_LocationResolved>()
30+
matcher.resolve('foo', {} as NEW_LocationResolved<TMatcherRecord>)
31+
).toEqualTypeOf<NEW_LocationResolved<TMatcherRecord>>()
2332
})
2433

2534
it('resolved named locations', () => {
26-
expectTypeOf(
27-
matcher.resolve({ name: 'foo', params: {} })
28-
).toEqualTypeOf<NEW_LocationResolved>()
35+
expectTypeOf(matcher.resolve({ name: 'foo', params: {} })).toEqualTypeOf<
36+
NEW_LocationResolved<TMatcherRecord>
37+
>()
2938
})
3039

3140
it('fails on object relative location without a currentLocation', () => {
@@ -35,8 +44,11 @@ describe('Matcher', () => {
3544

3645
it('resolves object relative locations with a currentLocation', () => {
3746
expectTypeOf(
38-
matcher.resolve({ params: { id: 1 } }, {} as NEW_LocationResolved)
39-
).toEqualTypeOf<NEW_LocationResolved>()
47+
matcher.resolve(
48+
{ params: { id: 1 } },
49+
{} as NEW_LocationResolved<TMatcherRecord>
50+
)
51+
).toEqualTypeOf<NEW_LocationResolved<TMatcherRecord>>()
4052
})
4153
})
4254

0 commit comments

Comments
 (0)