From: Eduardo San Martin Morote Date: Wed, 26 Jun 2024 14:12:40 +0000 (+0200) Subject: chore: error matches X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef57c3effcc1318a19b7cd2f4a2a7474f67a878d;p=thirdparty%2Fvuejs%2Frouter.git chore: error matches --- diff --git a/packages/router/src/new-route-resolver/matcher-pattern.ts b/packages/router/src/new-route-resolver/matcher-pattern.ts index a9f8f5e8..2e066bf8 100644 --- a/packages/router/src/new-route-resolver/matcher-pattern.ts +++ b/packages/router/src/new-route-resolver/matcher-pattern.ts @@ -9,7 +9,8 @@ import type { MatcherParamsFormatted } from './matcher-location' /** * Allows to match, extract, parse and build a path. Tailored to iterate through route records and check if a location * matches. When it cannot match, it returns `null` instead of throwing to not force a try/catch block around each - * iteration in for loops. + * iteration in for loops. Not meant to handle encoding/decoding. It expects different parts of the URL to be either + * encoded or decoded depending on the method. */ export interface MatcherPattern { /** @@ -36,8 +37,8 @@ export interface MatcherPattern { | null /** - * Extracts the defined params from an encoded path, query, and hash parsed from a URL. Does not apply formatting or - * decoding. If the URL does not match the pattern, returns `null`. + * Extracts the defined params from an encoded path, decoded query, and decoded hash parsed from a URL. Does not apply + * formatting or decoding. If the URL does not match the pattern, returns `null`. * * @example * ```ts @@ -54,6 +55,11 @@ export interface MatcherPattern { * pattern.parseLocation({ path: '/foo', query: {}, hash: '#hello' }) * // null // the query param is missing * ``` + * + * @param location - URL parts to extract from + * @param location.path - encoded path + * @param location.query - decoded query + * @param location.hash - decoded hash */ matchLocation(location: { path: string diff --git a/packages/router/src/new-route-resolver/matchers/errors.ts b/packages/router/src/new-route-resolver/matchers/errors.ts new file mode 100644 index 00000000..51c5574a --- /dev/null +++ b/packages/router/src/new-route-resolver/matchers/errors.ts @@ -0,0 +1,13 @@ +export class MatchMiss extends Error { + name = 'MatchMiss' +} + +export const miss = () => new MatchMiss() + +export class ParamInvalid extends Error { + name = 'ParamInvalid' + constructor(public param: string) { + super() + } +} +export const invalid = (param: string) => new ParamInvalid(param) diff --git a/packages/router/src/new-route-resolver/matchers/path-static.ts b/packages/router/src/new-route-resolver/matchers/path-static.ts index 0d6ebd3f..7d5e968f 100644 --- a/packages/router/src/new-route-resolver/matchers/path-static.ts +++ b/packages/router/src/new-route-resolver/matchers/path-static.ts @@ -1,12 +1,12 @@ import type { MatcherPatternPath } from '../matcher-pattern' +import { miss } from './errors' -export class PathMatcherStatic implements MatcherPatternPath { +export class MatcherPathStatic implements MatcherPatternPath { constructor(private path: string) {} match(path: string) { if (this.path === path) return {} - throw new Error() - // return this.path === path ? {} : null + throw miss() } buildPath() { diff --git a/packages/router/vue-router-auto.d.ts b/packages/router/vue-router-auto.d.ts index 56e8a097..797a7059 100644 --- a/packages/router/vue-router-auto.d.ts +++ b/packages/router/vue-router-auto.d.ts @@ -1,4 +1 @@ -/** - * Extended by unplugin-vue-router to create typed routes. - */ -export interface RouteNamedMap {} +// augmented by unplugin-vue-router