Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ findRoute(router, "GET", "/");
> [!IMPORTANT]
> Method should **always be UPPERCASE**.

> [!TIP]
> 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.

## Compiler

<!-- automd:jsdocs src="./src/compiler.ts" -->
Expand Down
2 changes: 2 additions & 0 deletions src/operations/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function addRoute<T>(
path = `/${path}`;
}

path = path.replace(/\\([:*])/g, (_m, ch) => `${encodeURIComponent(ch)}`);

const segments = splitPath(path);

let node = ctx.root;
Expand Down
5 changes: 4 additions & 1 deletion test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ describe("Router insert", () => {
"/api/v1",
"/api/v2",
"/api/v3",
"/static\\:path\\/\\*\\*",
]);

addRoute(router, "", "/api/v3", {
Expand All @@ -510,7 +511,9 @@ describe("Router insert", () => {
├── /api
│ ├── /v1 ┈> [GET] /api/v1
│ ├── /v2 ┈> [GET] /api/v2
│ ├── /v3 ┈> [GET] /api/v3, [*] /api/v3(overridden)"
│ ├── /v3 ┈> [GET] /api/v3, [*] /api/v3(overridden)
├── /static%3Apath\\
│ ├── /** ┈> [GET] /static\\:path\\/\\*\\*"
`);
});
});
Expand Down