Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@btst/yar",
"version": "1.1.1",
"version": "1.2.0",
"packageManager": "pnpm@10.14.0",
"description": "Pluggable router for modern react frameworks",
"type": "module",
Expand Down
53 changes: 53 additions & 0 deletions src/__tests__/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,56 @@ describe("Edge cases", () => {
}
});
});

describe("routeKey", () => {
it("should expose the route key on a matched route", () => {
const routes = {
home: createRoute("/", () => ({ PageComponent: MockComponent })),
about: createRoute("/about", () => ({ PageComponent: AnotherComponent })),
};

const router = createRouter(routes);

expect(router.getRoute("/")?.routeKey).toBe("home");
expect(router.getRoute("/about")?.routeKey).toBe("about");
});

it("should return null for unmatched paths (no routeKey)", () => {
const routes = {
home: createRoute("/", () => ({ PageComponent: MockComponent })),
};

const router = createRouter(routes);
expect(router.getRoute("/missing")).toBeNull();
});

it("should set the correct routeKey for parameterised routes", () => {
const routes = {
post: createRoute("/posts/:slug", () => ({
PageComponent: MockComponent,
})),
user: createRoute("/users/:id", () => ({
PageComponent: AnotherComponent,
})),
};

const router = createRouter(routes);

expect(router.getRoute("/posts/hello-world")?.routeKey).toBe("post");
expect(router.getRoute("/users/42")?.routeKey).toBe("user");
});

it("should set the correct routeKey when exact and parameterised routes overlap", () => {
const routes = {
me: createRoute("/users/me", () => ({ PageComponent: MockComponent })),
user: createRoute("/users/:id", () => ({
PageComponent: AnotherComponent,
})),
};

const router = createRouter(routes);

expect(router.getRoute("/users/me")?.routeKey).toBe("me");
expect(router.getRoute("/users/123")?.routeKey).toBe("user");
});
});
13 changes: 11 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ type ExtractRouteReturn<R> = R extends (...args: any[]) => infer Return
? Return
: never;

// The return type for getRoute, combining each handler return with params
// We distribute the intersection over the union to preserve all properties
// The return type for getRoute, combining each handler return with params and routeKey.
// Distributing the mapped type over the union preserves the discriminated union so
// TypeScript can narrow params and other properties based on routeKey.
type GetRouteReturn<Routes extends Record<string, Route>> = {
[K in keyof Routes]: ExtractRouteReturn<Routes[K]> & {
params: Record<string, string>;
routeKey: K;
};
}[keyof Routes];

Expand Down Expand Up @@ -192,6 +194,12 @@ export const createRouter = <
extra,
} = responseObj;

// Resolve the route key by finding the entry whose handler is the same
// reference as the one the internal router matched.
const routeKey = Object.entries(routes).find(
([, v]) => v === handler,
)?.[0] as keyof E | undefined;

return {
PageComponent,
LoadingComponent,
Expand All @@ -200,6 +208,7 @@ export const createRouter = <
loader,
meta,
extra,
routeKey,
} as GetRouteReturn<E>;
},
};
Expand Down