1
- import { type LocationQuery , normalizeQuery , stringifyQuery } from '../query'
1
+ import {
2
+ type LocationQuery ,
3
+ normalizeQuery ,
4
+ parseQuery ,
5
+ stringifyQuery ,
6
+ } from '../query'
2
7
import type {
3
8
MatcherPatternHash ,
4
9
MatcherPatternPath ,
5
10
MatcherPatternQuery ,
6
11
} from './matcher-pattern'
7
12
import { warn } from '../warning'
8
13
import { encodeQueryValue as _encodeQueryValue , encodeParam } from '../encoding'
9
- import { NEW_stringifyURL , resolveRelativePath } from '../location'
14
+ import {
15
+ LocationNormalized ,
16
+ NEW_stringifyURL ,
17
+ parseURL ,
18
+ resolveRelativePath ,
19
+ } from '../location'
10
20
import type {
11
21
MatcherLocationAsNamed ,
12
22
MatcherLocationAsPathAbsolute ,
@@ -32,19 +42,19 @@ export interface NEW_RouterResolver<TMatcherRecordRaw, TMatcherRecord> {
32
42
/**
33
43
* Resolves an absolute location (like `/path/to/somewhere`).
34
44
*/
35
- // resolve(
36
- // absoluteLocation: `/${string}`,
37
- // currentLocation?: undefined | NEW_LocationResolved<TMatcherRecord>
38
- // ): NEW_LocationResolved<TMatcherRecord>
45
+ resolve (
46
+ absoluteLocation : `/${string } `,
47
+ currentLocation ?: undefined
48
+ ) : NEW_LocationResolved < TMatcherRecord >
39
49
40
50
/**
41
51
* Resolves a string location relative to another location. A relative location can be `./same-folder`,
42
52
* `../parent-folder`, `same-folder`, or even `?page=2`.
43
53
*/
44
- // resolve(
45
- // relativeLocation: string,
46
- // currentLocation: NEW_LocationResolved<TMatcherRecord>
47
- // ): NEW_LocationResolved<TMatcherRecord>
54
+ resolve (
55
+ relativeLocation : string ,
56
+ currentLocation : NEW_LocationResolved < TMatcherRecord >
57
+ ) : NEW_LocationResolved < TMatcherRecord >
48
58
49
59
/**
50
60
* Resolves a location by its name. Any required params or query must be passed in the `options` argument.
@@ -53,6 +63,7 @@ export interface NEW_RouterResolver<TMatcherRecordRaw, TMatcherRecord> {
53
63
location : MatcherLocationAsNamed ,
54
64
// TODO: is this useful?
55
65
currentLocation ?: undefined
66
+ // currentLocation?: undefined | NEW_LocationResolved<TMatcherRecord>
56
67
) : NEW_LocationResolved < TMatcherRecord >
57
68
58
69
/**
@@ -63,7 +74,7 @@ export interface NEW_RouterResolver<TMatcherRecordRaw, TMatcherRecord> {
63
74
location : MatcherLocationAsPathAbsolute ,
64
75
// TODO: is this useful?
65
76
currentLocation ?: undefined
66
- // currentLocation?: NEW_LocationResolved<TMatcherRecord>
77
+ // currentLocation?: NEW_LocationResolved<TMatcherRecord> | undefined
67
78
) : NEW_LocationResolved < TMatcherRecord >
68
79
69
80
resolve (
@@ -121,7 +132,7 @@ export interface NEW_RouterResolver<TMatcherRecordRaw, TMatcherRecord> {
121
132
*/
122
133
export type MatcherLocationRaw =
123
134
// | `/${string}`
124
- // | string
135
+ | string
125
136
| MatcherLocationAsNamed
126
137
| MatcherLocationAsPathAbsolute
127
138
| MatcherLocationAsPathRelative
@@ -355,23 +366,27 @@ export function createCompiledMatcher<
355
366
356
367
// NOTE: because of the overloads, we need to manually type the arguments
357
368
type MatcherResolveArgs =
358
- // | [
359
- // absoluteLocation: `/${string}`,
360
- // currentLocation?: undefined | NEW_LocationResolved<TMatcherRecord>
361
- // ]
362
- // | [
363
- // relativeLocation: string,
364
- // currentLocation: NEW_LocationResolved<TMatcherRecord>
365
- // ]
369
+ | [ absoluteLocation : `/${string } `, currentLocation ?: undefined ]
370
+ | [
371
+ relativeLocation : string ,
372
+ currentLocation : NEW_LocationResolved < TMatcherRecord >
373
+ ]
366
374
| [
367
375
absoluteLocation : MatcherLocationAsPathAbsolute ,
376
+ // Same as above
377
+ // currentLocation?: NEW_LocationResolved<TMatcherRecord> | undefined
368
378
currentLocation ?: undefined
369
379
]
370
380
| [
371
381
relativeLocation : MatcherLocationAsPathRelative ,
372
382
currentLocation : NEW_LocationResolved < TMatcherRecord >
373
383
]
374
- | [ location : MatcherLocationAsNamed , currentLocation ?: undefined ]
384
+ | [
385
+ location : MatcherLocationAsNamed ,
386
+ // Same as above
387
+ // currentLocation?: NEW_LocationResolved<TMatcherRecord> | undefined
388
+ currentLocation ?: undefined
389
+ ]
375
390
| [
376
391
relativeLocation : MatcherLocationAsRelative ,
377
392
currentLocation : NEW_LocationResolved < TMatcherRecord >
@@ -382,7 +397,7 @@ export function createCompiledMatcher<
382
397
) : NEW_LocationResolved < TMatcherRecord > {
383
398
const [ to , currentLocation ] = args
384
399
385
- if ( to . name || to . path == null ) {
400
+ if ( typeof to === 'object' && ( to . name || to . path == null ) ) {
386
401
// relative location or by name
387
402
if ( __DEV__ && to . name == null && currentLocation == null ) {
388
403
console . warn (
@@ -442,13 +457,17 @@ export function createCompiledMatcher<
442
457
// string location, e.g. '/foo', '../bar', 'baz', '?page=1'
443
458
} else {
444
459
// parseURL handles relative paths
445
- // parseURL(to.path, currentLocation?.path)
446
- const query = normalizeQuery ( to . query )
447
- const url = {
448
- fullPath : NEW_stringifyURL ( stringifyQuery , to . path , query , to . hash ) ,
449
- path : resolveRelativePath ( to . path , currentLocation ?. path || '/' ) ,
450
- query,
451
- hash : to . hash || '' ,
460
+ let url : LocationNormalized
461
+ if ( typeof to === 'string' ) {
462
+ url = parseURL ( parseQuery , to , currentLocation ?. path )
463
+ } else {
464
+ const query = normalizeQuery ( to . query )
465
+ url = {
466
+ fullPath : NEW_stringifyURL ( stringifyQuery , to . path , query , to . hash ) ,
467
+ path : resolveRelativePath ( to . path , currentLocation ?. path || '/' ) ,
468
+ query,
469
+ hash : to . hash || '' ,
470
+ }
452
471
}
453
472
454
473
let matcher : TMatcherRecord | undefined
0 commit comments