Skip to content

Commit

Permalink
fix(findAll): last named segment is required (#128)
Browse files Browse the repository at this point in the history
* fix: find all

* chore: remove the only in test
  • Loading branch information
Barbapapazes authored Aug 22, 2024
1 parent 122de5f commit 13c522e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/operations/find-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ function _findAll<T>(
if (index === segments.length && node.param.methods) {
const match = node.param.methods[method] || node.param.methods[""];
if (match) {
matches.push(...match);
const pMap = match[0].paramsMap;
if (pMap?.[pMap?.length - 1]?.[2]) {
matches.push(...match);
}
}
}
}
Expand Down
42 changes: 41 additions & 1 deletion test/find-all.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const _findAllRoutes = (
path: string,
) => findAllRoutes(ctx, method, path).map((m) => m.data.path);

describe("fiind-all: basic", () => {
describe("find-all: basic", () => {
const router = createRouter([
"/foo",
"/foo/**",
Expand Down Expand Up @@ -220,3 +220,43 @@ describe("matcher: order", () => {
`);
});
});

describe("matcher: named", () => {
const router = createRouter(["/foo", "/foo/:bar", "/foo/:bar/:qaz"]);

it("snapshot", () => {
expect(formatTree(router.root)).toMatchInlineSnapshot(`
"<root>
β”œβ”€β”€ /foo β”ˆ> [GET] /foo
β”‚ β”œβ”€β”€ /* β”ˆ> [GET] /foo/:bar
β”‚ β”‚ β”œβ”€β”€ /* β”ˆ> [GET] /foo/:bar/:qaz"
`);
});

it("matches /foo", () => {
const matches = _findAllRoutes(router, "GET", "/foo");
expect(matches).to.toMatchInlineSnapshot(`
[
"/foo",
]
`);
});

it("matches /foo/123", () => {
const matches = _findAllRoutes(router, "GET", "/foo/123");
expect(matches).to.toMatchInlineSnapshot(`
[
"/foo/:bar",
]
`);
});

it("matches /foo/123/456", () => {
const matches = _findAllRoutes(router, "GET", "/foo/123/456");
expect(matches).to.toMatchInlineSnapshot(`
[
"/foo/:bar/:qaz",
]
`);
});
});

0 comments on commit 13c522e

Please sign in to comment.