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`
|
## `.params`
|
||||||
|
|
||||||
Contains the params of the matched route pattern.
|
Contains the params of the matched route pattern.
|
||||||
|
|||||||
@@ -494,6 +494,7 @@ export class App<State> {
|
|||||||
req,
|
req,
|
||||||
url,
|
url,
|
||||||
conn,
|
conn,
|
||||||
|
matched.pattern,
|
||||||
params,
|
params,
|
||||||
this.config,
|
this.config,
|
||||||
next,
|
next,
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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");
|
||||||
|
});
|
||||||
|
|||||||
@@ -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()),
|
||||||
|
|||||||
Reference in New Issue
Block a user