Skip to content

Commit ef57c3e

Browse files
committed
chore: error matches
1 parent 6f2da87 commit ef57c3e

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import type { MatcherParamsFormatted } from './matcher-location'
99
/**
1010
* Allows to match, extract, parse and build a path. Tailored to iterate through route records and check if a location
1111
* matches. When it cannot match, it returns `null` instead of throwing to not force a try/catch block around each
12-
* iteration in for loops.
12+
* iteration in for loops. Not meant to handle encoding/decoding. It expects different parts of the URL to be either
13+
* encoded or decoded depending on the method.
1314
*/
1415
export interface MatcherPattern {
1516
/**
@@ -36,8 +37,8 @@ export interface MatcherPattern {
3637
| null
3738

3839
/**
39-
* Extracts the defined params from an encoded path, query, and hash parsed from a URL. Does not apply formatting or
40-
* decoding. If the URL does not match the pattern, returns `null`.
40+
* Extracts the defined params from an encoded path, decoded query, and decoded hash parsed from a URL. Does not apply
41+
* formatting or decoding. If the URL does not match the pattern, returns `null`.
4142
*
4243
* @example
4344
* ```ts
@@ -54,6 +55,11 @@ export interface MatcherPattern {
5455
* pattern.parseLocation({ path: '/foo', query: {}, hash: '#hello' })
5556
* // null // the query param is missing
5657
* ```
58+
*
59+
* @param location - URL parts to extract from
60+
* @param location.path - encoded path
61+
* @param location.query - decoded query
62+
* @param location.hash - decoded hash
5763
*/
5864
matchLocation(location: {
5965
path: string
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class MatchMiss extends Error {
2+
name = 'MatchMiss'
3+
}
4+
5+
export const miss = () => new MatchMiss()
6+
7+
export class ParamInvalid extends Error {
8+
name = 'ParamInvalid'
9+
constructor(public param: string) {
10+
super()
11+
}
12+
}
13+
export const invalid = (param: string) => new ParamInvalid(param)

packages/router/src/new-route-resolver/matchers/path-static.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { MatcherPatternPath } from '../matcher-pattern'
2+
import { miss } from './errors'
23

3-
export class PathMatcherStatic implements MatcherPatternPath {
4+
export class MatcherPathStatic implements MatcherPatternPath {
45
constructor(private path: string) {}
56

67
match(path: string) {
78
if (this.path === path) return {}
8-
throw new Error()
9-
// return this.path === path ? {} : null
9+
throw miss()
1010
}
1111

1212
buildPath() {
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
/**
2-
* Extended by unplugin-vue-router to create typed routes.
3-
*/
4-
export interface RouteNamedMap {}
1+
// augmented by unplugin-vue-router

0 commit comments

Comments
 (0)