From 8213e003b0e7b6246ecf4c947416021bc5991f95 Mon Sep 17 00:00:00 2001 From: barbapapazes Date: Thu, 22 Aug 2024 14:06:23 +0200 Subject: [PATCH] fix: find all --- src/operations/find-all.ts | 5 ++++- test/find-all.test.ts | 42 +++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/operations/find-all.ts b/src/operations/find-all.ts index 5d80819..3866d04 100644 --- a/src/operations/find-all.ts +++ b/src/operations/find-all.ts @@ -52,7 +52,10 @@ function _findAll( 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); + } } } } diff --git a/test/find-all.test.ts b/test/find-all.test.ts index e64e35d..88578fe 100644 --- a/test/find-all.test.ts +++ b/test/find-all.test.ts @@ -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/**", @@ -220,3 +220,43 @@ describe("matcher: order", () => { `); }); }); + +describe.only("matcher: named", () => { + const router = createRouter(["/foo", "/foo/:bar", "/foo/:bar/:qaz"]); + + it("snapshot", () => { + expect(formatTree(router.root)).toMatchInlineSnapshot(` + " + ├── /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", + ] + `); + }); +});