fix: write matched pattern into ctx.route (#3113)
Fixes https://github.com/denoland/fresh/issues/3112
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -494,6 +494,7 @@ export class App<State> {
|
||||
req,
|
||||
url,
|
||||
conn,
|
||||
matched.pattern,
|
||||
params,
|
||||
this.config,
|
||||
next,
|
||||
|
||||
@@ -64,6 +64,8 @@ export class Context<State> {
|
||||
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<string, string>;
|
||||
/** State object that is shared with all middlewares. */
|
||||
@@ -122,6 +124,7 @@ export class Context<State> {
|
||||
req: Request,
|
||||
url: URL,
|
||||
info: Deno.ServeHandlerInfo,
|
||||
route: string | null,
|
||||
params: Record<string, string>,
|
||||
config: ResolvedFreshConfig,
|
||||
next: () => Promise<Response>,
|
||||
@@ -132,6 +135,7 @@ export class Context<State> {
|
||||
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;
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
@@ -76,6 +76,7 @@ export function serveMiddleware<T>(
|
||||
config?: ResolvedFreshConfig;
|
||||
buildCache?: BuildCache;
|
||||
next?: () => Promise<Response>;
|
||||
route?: string | null;
|
||||
} = {},
|
||||
): FakeServer {
|
||||
return new FakeServer(async (req) => {
|
||||
@@ -89,6 +90,7 @@ export function serveMiddleware<T>(
|
||||
req,
|
||||
new URL(req.url),
|
||||
DEFAULT_CONN_INFO,
|
||||
options.route ?? null,
|
||||
{},
|
||||
config,
|
||||
() => Promise.resolve(next()),
|
||||
|
||||
Reference in New Issue
Block a user