Skip to content

Commit e2fbf8d

Browse files
authored
fix: handle root wildcard routes (#173)
1 parent d299937 commit e2fbf8d

7 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/operations/_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function getMatchParams(
1313
const params = new NullProtoObj();
1414
for (const [index, name] of paramsMap) {
1515
const segment =
16-
index < 0 ? segments.slice(-1 * index).join("/") : segments[index];
16+
index < 0 ? segments.slice(-(index + 1)).join("/") : segments[index];
1717
if (typeof name === "string") {
1818
params[name] = segment;
1919
} else {

src/operations/add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function addRoute<T>(
3535
}
3636
node = node.wildcard;
3737
paramsMap.push([
38-
-i,
38+
-(i + 1),
3939
segment.split(":")[1] || "_",
4040
segment.length === 2 /* no id */,
4141
]);

test/.snapshot/compiled-aot.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const findRoute = /* @__PURE__ */ (() => {
1010
$8 = { path: "/test/:id" },
1111
$9 = { path: "/test/:idY/y" },
1212
$10 = { path: "/test/:idYZ/y/z" },
13-
$11 = { path: "/wildcard/**" };
13+
$11 = { path: "/wildcard/**" },
14+
$12 = { path: "/**" };
1415
return (m, p) => {
1516
if (p.charCodeAt(p.length - 1) === 47) p = p.slice(0, -1) || "/";
1617
if (p === "/test") {
@@ -59,5 +60,6 @@ const findRoute = /* @__PURE__ */ (() => {
5960
if (m === "GET")
6061
return { data: $11, params: { _: s.slice(2).join("/") } };
6162
}
63+
if (m === "GET") return { data: $12, params: { _: s.slice(1).join("/") } };
6264
};
6365
})();

test/.snapshot/compiled-jit.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@
4444
if (s[1] === "wildcard") {
4545
if (m === "GET") return { data: $11, params: { _: s.slice(2).join("/") } };
4646
}
47+
if (m === "GET") return { data: $12, params: { _: s.slice(1).join("/") } };
4748
};

test/find.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("route matching", () => {
1818
"/test/fooo",
1919
"/another/path",
2020
"/wildcard/**",
21+
"/**",
2122
]);
2223

2324
const compiledLookup = compileRouter(router);
@@ -39,7 +40,8 @@ describe("route matching", () => {
3940
├── /another
4041
│ ├── /path ┈> [GET] /another/path
4142
├── /wildcard
42-
│ ├── /** ┈> [GET] /wildcard/**"
43+
│ ├── /** ┈> [GET] /wildcard/**
44+
├── /** ┈> [GET] /**"
4345
`);
4446
});
4547

@@ -123,6 +125,15 @@ describe("route matching", () => {
123125
data: { path: "/wildcard/**" },
124126
params: { _: "" },
125127
});
128+
// Root wildcard
129+
expect(match("GET", "/anything")).toMatchObject({
130+
data: { path: "/**" },
131+
params: { _: "anything" },
132+
});
133+
expect(match("GET", "/any/deep/path")).toMatchObject({
134+
data: { path: "/**" },
135+
params: { _: "any/deep/path" },
136+
});
126137
});
127138
}
128139

@@ -131,6 +142,7 @@ describe("route matching", () => {
131142
removeRoute(router, "GET", "/test/*");
132143
removeRoute(router, "GET", "/test/foo/*");
133144
removeRoute(router, "GET", "/test/foo/**");
145+
removeRoute(router, "GET", "/**");
134146
expect(formatTree(router.root)).toMatchInlineSnapshot(`
135147
"<root>
136148
├── /test

test/regexp.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ describe("routeToRegExp", () => {
4444
regex: /^\/base\/?(?<path>.+)\/?$/,
4545
match: [["/base/anything/more", { path: "anything/more" }]],
4646
},
47+
"/**": {
48+
regex: /^\/?(?<_>.*)\/?$/,
49+
match: [
50+
["/", { _: "" }],
51+
["/anything", { _: "anything" }],
52+
["/any/deep/path", { _: "any/deep/path" }],
53+
],
54+
},
4755
} as const;
4856

4957
for (const [route, expected] of Object.entries(routes)) {

test/router.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ describe("Router lookup", function () {
260260
},
261261
},
262262
);
263+
264+
testRouter(
265+
["/**"],
266+
(router) =>
267+
expect(formatTree(router.root)).toMatchInlineSnapshot(`
268+
"<root>
269+
├── /** ┈> [GET] /**"
270+
`),
271+
{
272+
"/anything": {
273+
data: { path: "/**" },
274+
params: { _: "anything" },
275+
},
276+
"/any/deep/path": {
277+
data: { path: "/**" },
278+
params: { _: "any/deep/path" },
279+
},
280+
},
281+
);
263282
});
264283

265284
describe("fallback to dynamic", () => {

0 commit comments

Comments
 (0)