fix: write matched pattern into ctx.route (#3113)

Fixes https://github.com/denoland/fresh/issues/3112
This commit is contained in:
Marvin Hagemeister
2025-07-18 22:38:56 +02:00
committed by GitHub
parent 83da608504
commit 39737b4530
5 changed files with 41 additions and 0 deletions
+12
View File
@@ -49,6 +49,18 @@ app.get("/", (ctx) => {
}); });
``` ```
## `.route`
Contains the matched route pattern as a `string`. Will be `null` if no pattern
matched.
```ts
app.get("/foo/:id", (ctx) => {
console.log(ctx.route); // Logs: "/foo/:id
// ...
});
```
## `.params` ## `.params`
Contains the params of the matched route pattern. Contains the params of the matched route pattern.
+1
View File
@@ -494,6 +494,7 @@ export class App<State> {
req, req,
url, url,
conn, conn,
matched.pattern,
params, params,
this.config, this.config,
next, next,
+4
View File
@@ -64,6 +64,8 @@ export class Context<State> {
readonly url: URL; readonly url: URL;
/** The original incoming {@linkcode Request} object. */ /** The original incoming {@linkcode Request} object. */
readonly req: Request; readonly req: Request;
/** The matched route pattern. */
readonly route: string | null;
/** The url parameters of the matched route pattern. */ /** The url parameters of the matched route pattern. */
readonly params: Record<string, string>; readonly params: Record<string, string>;
/** State object that is shared with all middlewares. */ /** State object that is shared with all middlewares. */
@@ -122,6 +124,7 @@ export class Context<State> {
req: Request, req: Request,
url: URL, url: URL,
info: Deno.ServeHandlerInfo, info: Deno.ServeHandlerInfo,
route: string | null,
params: Record<string, string>, params: Record<string, string>,
config: ResolvedFreshConfig, config: ResolvedFreshConfig,
next: () => Promise<Response>, next: () => Promise<Response>,
@@ -132,6 +135,7 @@ export class Context<State> {
this.req = req; this.req = req;
this.info = info; this.info = info;
this.params = params; this.params = params;
this.route = route;
this.config = config; this.config = config;
this.isPartial = url.searchParams.has(PARTIAL_SEARCH_PARAM); this.isPartial = url.searchParams.has(PARTIAL_SEARCH_PARAM);
this.next = next; this.next = next;
+22
View File
@@ -84,3 +84,25 @@ Deno.test("ctx.isPartial - should indicate whether request is partial or not", a
expect(isPartials).toEqual([false, true]); expect(isPartials).toEqual([false, true]);
}); });
Deno.test("ctx.route - should contain matched route", async () => {
let route: string | null = null;
const app = new App()
.use((ctx) => {
route = ctx.route;
return ctx.next();
})
.get("/foo/bar", () => new Response("ok"))
.get("/foo/:id", () => new Response("ok"));
const server = new FakeServer(app.handler());
await server.get("/invalid");
expect(route).toEqual(null);
await server.get("/foo/bar");
expect(route).toEqual("/foo/bar");
await server.get("/foo/123");
expect(route).toEqual("/foo/:id");
});
+2
View File
@@ -76,6 +76,7 @@ export function serveMiddleware<T>(
config?: ResolvedFreshConfig; config?: ResolvedFreshConfig;
buildCache?: BuildCache; buildCache?: BuildCache;
next?: () => Promise<Response>; next?: () => Promise<Response>;
route?: string | null;
} = {}, } = {},
): FakeServer { ): FakeServer {
return new FakeServer(async (req) => { return new FakeServer(async (req) => {
@@ -89,6 +90,7 @@ export function serveMiddleware<T>(
req, req,
new URL(req.url), new URL(req.url),
DEFAULT_CONN_INFO, DEFAULT_CONN_INFO,
options.route ?? null,
{}, {},
config, config,
() => Promise.resolve(next()), () => Promise.resolve(next()),