Skip to content

Commit 39737b4

Browse files
fix: write matched pattern into ctx.route (#3113)
Fixes #3112
1 parent 83da608 commit 39737b4

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

docs/canary/concepts/context.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ app.get("/", (ctx) => {
4949
});
5050
```
5151

52+
## `.route`
53+
54+
Contains the matched route pattern as a `string`. Will be `null` if no pattern
55+
matched.
56+
57+
```ts
58+
app.get("/foo/:id", (ctx) => {
59+
console.log(ctx.route); // Logs: "/foo/:id
60+
// ...
61+
});
62+
```
63+
5264
## `.params`
5365

5466
Contains the params of the matched route pattern.

src/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ export class App<State> {
494494
req,
495495
url,
496496
conn,
497+
matched.pattern,
497498
params,
498499
this.config,
499500
next,

src/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export class Context<State> {
6464
readonly url: URL;
6565
/** The original incoming {@linkcode Request} object. */
6666
readonly req: Request;
67+
/** The matched route pattern. */
68+
readonly route: string | null;
6769
/** The url parameters of the matched route pattern. */
6870
readonly params: Record<string, string>;
6971
/** State object that is shared with all middlewares. */
@@ -122,6 +124,7 @@ export class Context<State> {
122124
req: Request,
123125
url: URL,
124126
info: Deno.ServeHandlerInfo,
127+
route: string | null,
125128
params: Record<string, string>,
126129
config: ResolvedFreshConfig,
127130
next: () => Promise<Response>,
@@ -132,6 +135,7 @@ export class Context<State> {
132135
this.req = req;
133136
this.info = info;
134137
this.params = params;
138+
this.route = route;
135139
this.config = config;
136140
this.isPartial = url.searchParams.has(PARTIAL_SEARCH_PARAM);
137141
this.next = next;

src/context_test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,25 @@ Deno.test("ctx.isPartial - should indicate whether request is partial or not", a
8484

8585
expect(isPartials).toEqual([false, true]);
8686
});
87+
88+
Deno.test("ctx.route - should contain matched route", async () => {
89+
let route: string | null = null;
90+
const app = new App()
91+
.use((ctx) => {
92+
route = ctx.route;
93+
return ctx.next();
94+
})
95+
.get("/foo/bar", () => new Response("ok"))
96+
.get("/foo/:id", () => new Response("ok"));
97+
98+
const server = new FakeServer(app.handler());
99+
100+
await server.get("/invalid");
101+
expect(route).toEqual(null);
102+
103+
await server.get("/foo/bar");
104+
expect(route).toEqual("/foo/bar");
105+
106+
await server.get("/foo/123");
107+
expect(route).toEqual("/foo/:id");
108+
});

src/test_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export function serveMiddleware<T>(
7676
config?: ResolvedFreshConfig;
7777
buildCache?: BuildCache;
7878
next?: () => Promise<Response>;
79+
route?: string | null;
7980
} = {},
8081
): FakeServer {
8182
return new FakeServer(async (req) => {
@@ -89,6 +90,7 @@ export function serveMiddleware<T>(
8990
req,
9091
new URL(req.url),
9192
DEFAULT_CONN_INFO,
93+
options.route ?? null,
9294
{},
9395
config,
9496
() => Promise.resolve(next()),

0 commit comments

Comments
 (0)