From 39737b45308b511dc453074bab2bed3c3b745c7a Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Fri, 18 Jul 2025 22:38:56 +0200 Subject: [PATCH] fix: write matched pattern into `ctx.route` (#3113) Fixes https://github.com/denoland/fresh/issues/3112 --- docs/canary/concepts/context.md | 12 ++++++++++++ src/app.ts | 1 + src/context.ts | 4 ++++ src/context_test.tsx | 22 ++++++++++++++++++++++ src/test_utils.ts | 2 ++ 5 files changed, 41 insertions(+) diff --git a/docs/canary/concepts/context.md b/docs/canary/concepts/context.md index d3eaff81..bbcf87b5 100644 --- a/docs/canary/concepts/context.md +++ b/docs/canary/concepts/context.md @@ -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` Contains the params of the matched route pattern. diff --git a/src/app.ts b/src/app.ts index 583b2815..36733ef0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -494,6 +494,7 @@ export class App { req, url, conn, + matched.pattern, params, this.config, next, diff --git a/src/context.ts b/src/context.ts index 39c44b07..eac92cd9 100644 --- a/src/context.ts +++ b/src/context.ts @@ -64,6 +64,8 @@ export class Context { readonly url: URL; /** The original incoming {@linkcode Request} object. */ readonly req: Request; + /** The matched route pattern. */ + readonly route: string | null; /** The url parameters of the matched route pattern. */ readonly params: Record; /** State object that is shared with all middlewares. */ @@ -122,6 +124,7 @@ export class Context { req: Request, url: URL, info: Deno.ServeHandlerInfo, + route: string | null, params: Record, config: ResolvedFreshConfig, next: () => Promise, @@ -132,6 +135,7 @@ export class Context { this.req = req; this.info = info; this.params = params; + this.route = route; this.config = config; this.isPartial = url.searchParams.has(PARTIAL_SEARCH_PARAM); this.next = next; diff --git a/src/context_test.tsx b/src/context_test.tsx index d3b6bd89..6bc6ff05 100644 --- a/src/context_test.tsx +++ b/src/context_test.tsx @@ -84,3 +84,25 @@ Deno.test("ctx.isPartial - should indicate whether request is partial or not", a 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"); +}); diff --git a/src/test_utils.ts b/src/test_utils.ts index 7d7a77a8..35908758 100644 --- a/src/test_utils.ts +++ b/src/test_utils.ts @@ -76,6 +76,7 @@ export function serveMiddleware( config?: ResolvedFreshConfig; buildCache?: BuildCache; next?: () => Promise; + route?: string | null; } = {}, ): FakeServer { return new FakeServer(async (req) => { @@ -89,6 +90,7 @@ export function serveMiddleware( req, new URL(req.url), DEFAULT_CONN_INFO, + options.route ?? null, {}, config, () => Promise.resolve(next()),