Skip to content

Commit

Permalink
feat(router): expose event.context.matchedRoute (#500)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
iainsproat and pi0 authored Aug 8, 2023
1 parent e3c9f96 commit 4fcb9c1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export interface Router extends AddRouteShortcuts {
handler: EventHandler;
}

interface RouteNode {
export interface RouteNode {
handlers: Partial<Record<RouterMethod | "all", EventHandler>>;
path: string;
}

export interface CreateRouterOptions {
Expand All @@ -59,7 +60,7 @@ export function createRouter(opts: CreateRouterOptions = {}): Router {
) => {
let route = routes[path];
if (!route) {
routes[path] = route = { handlers: {} };
routes[path] = route = { path, handlers: {} };
_router.insert(path, route);
}
if (Array.isArray(method)) {
Expand Down Expand Up @@ -143,7 +144,8 @@ export function createRouter(opts: CreateRouterOptions = {}): Router {
}
}

// Add params
// Add matched route and params to the context
event.context.matchedRoute = matched;
const params = matched.params || {};
event.context.params = params;

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { QueryObject } from "ufo";
import type { H3Event } from "./event";
import type { Session } from "./utils/session";
import type { RouteNode } from "./router";

export type {
ValidateFunction,
Expand Down Expand Up @@ -35,6 +36,12 @@ export type Encoding =
export interface H3EventContext extends Record<string, any> {
/* Matched router parameters */
params?: Record<string, string>;
/**
* Matched router Node
*
* @experimental The object structure may change in non-major version.
*/
matchedRoute?: RouteNode;
/* Cached session data */
sessions?: Record<string, Session>;
}
Expand Down
43 changes: 43 additions & 0 deletions test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,46 @@ describe("getRouterParam", () => {
});
});
});

describe("event.context.matchedRoute", () => {
let app: App;
let request: SuperTest<Test>;

beforeEach(() => {
app = createApp({ debug: false });
request = supertest(toNodeListener(app));
});

describe("with router", () => {
it("can return the matched path", async () => {
const router = createRouter().get(
"/test/:template",
eventHandler((event) => {
expect(event.context.matchedRoute).toMatchObject({
path: "/test/:template",
});
return "200";
}),
);
app.use(router);
const result = await request.get("/test/path");

expect(result.text).toBe("200");
});
});

describe("without router", () => {
it("can return `undefined` for matched path", async () => {
app.use(
"/",
eventHandler((event) => {
expect(event.context.matchedRoute).toEqual(undefined);
return "200";
}),
);
const result = await request.get("/test/path");

expect(result.text).toBe("200");
});
});
});

0 comments on commit 4fcb9c1

Please sign in to comment.