/**
* 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 {
/**
| 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
* 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
--- /dev/null
+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)
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() {