Skip to content

Commit 3021fe9

Browse files
committed
update
1 parent 260e3b9 commit 3021fe9

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ findRoute(router, "GET", "/");
9191
> [!IMPORTANT]
9292
> Method should **always be UPPERCASE**.
9393
94+
> [!TIP]
95+
> If you need to register a pattern containing literal `:` or `*`, you can escape them with `\\`. For example, `/static\\:path\\/\\*\\*` matches only the static `/static:path/**` route.
96+
9497
## Compiler
9598

9699
<!-- automd:jsdocs src="./src/compiler.ts" -->

β€Žsrc/operations/add.tsβ€Ž

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function addRoute<T>(
1616
path = `/${path}`;
1717
}
1818

19+
path = path.replace(/\\([:*])/g, (_m, ch) => `${encodeURIComponent(ch)}`);
20+
1921
const segments = splitPath(path);
2022

2123
let node = ctx.root;
@@ -26,10 +28,7 @@ export function addRoute<T>(
2628
const paramsRegexp: RegExp[] = [];
2729

2830
for (let i = 0; i < segments.length; i++) {
29-
const segment = segments[i].replace(
30-
/\\([:*])/g,
31-
(_m, ch) => `${encodeURIComponent(ch)}`,
32-
);
31+
const segment = segments[i];
3332

3433
// Wildcard
3534
if (segment.startsWith("**")) {

β€Žtest/router.test.tsβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ describe("Router insert", () => {
486486
"/api/v1",
487487
"/api/v2",
488488
"/api/v3",
489-
"/test\\:special",
489+
"/static\\:path\\/\\*\\*",
490490
]);
491491

492492
addRoute(router, "", "/api/v3", {
@@ -512,7 +512,8 @@ describe("Router insert", () => {
512512
β”‚ β”œβ”€β”€ /v1 β”ˆ> [GET] /api/v1
513513
β”‚ β”œβ”€β”€ /v2 β”ˆ> [GET] /api/v2
514514
β”‚ β”œβ”€β”€ /v3 β”ˆ> [GET] /api/v3, [*] /api/v3(overridden)
515-
β”œβ”€β”€ /test%3Aspecial β”ˆ> [GET] /test\\:special"
515+
β”œβ”€β”€ /static%3Apath\\
516+
β”‚ β”œβ”€β”€ /** β”ˆ> [GET] /static\\:path\\/\\*\\*"
516517
`);
517518
});
518519
});

0 commit comments

Comments
Β (0)