From 3c554810fbbb3a8152a7acbf516c2970363509a9 Mon Sep 17 00:00:00 2001 From: Jade Devin Cabatlao Date: Fri, 6 Sep 2024 22:29:04 +0200 Subject: [PATCH 1/4] Update path-to-regexp and don't override the params on the routes handler --- packages/vinxi-router/api-handler.js | 23 ++++++++--------------- packages/vinxi-router/package.json | 2 +- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/vinxi-router/api-handler.js b/packages/vinxi-router/api-handler.js index dfac7c1c..82c8ccfa 100644 --- a/packages/vinxi-router/api-handler.js +++ b/packages/vinxi-router/api-handler.js @@ -1,13 +1,12 @@ -import { pathToRegexp } from "path-to-regexp"; -import { eventHandler, toWebRequest } from "vinxi/http"; +import { match } from "path-to-regexp"; +import { eventHandler, toWebRequest, setContext } from "vinxi/http"; import fileRoutes from "vinxi/routes"; const routes = [ ...fileRoutes.map((route) => ({ ...route, - handler: async (event, params) => { + handler: async (event) => { const mod = await route.$handler.import(); - event.context['params'] = params return await mod.default(event); }, })), @@ -15,12 +14,10 @@ const routes = [ function createRouter(routes) { const builtRoutes = routes.map((route) => { - const keys = []; - const regexp = pathToRegexp(route.path, keys); + const matcher = match(route.path) return { ...route, - regexp, - keys, + matcher, }; }); @@ -30,15 +27,11 @@ function createRouter(routes) { const url = new URL(toWebRequest(event).url); const path = url.pathname.replace(import.meta.env.BASE_URL, ""); - const match = route.regexp.exec(path); + const match = route.matcher(path) if (match) { - const params = {}; - for (let i = 0; i < route.keys.length; i++) { - params[route.keys[i].name] = match[i + 1]; - } // add params to context object - event.context['params'] = params - return await route.handler(event); + setContext(event, 'params', { ...match.params }) + return await route.handler(event) } } diff --git a/packages/vinxi-router/package.json b/packages/vinxi-router/package.json index 0878251c..94e9950e 100644 --- a/packages/vinxi-router/package.json +++ b/packages/vinxi-router/package.json @@ -9,7 +9,7 @@ "author": "", "license": "MIT", "dependencies": { - "path-to-regexp": "^6.2.1", + "path-to-regexp": "^8.0.0", "vite-tsconfig-paths": "^4.2.0" }, "repository": { From d50d38620882c487b28326f3496f91bf0c52a2b1 Mon Sep 17 00:00:00 2001 From: Jade Devin Cabatlao Date: Sat, 7 Sep 2024 04:35:59 +0800 Subject: [PATCH 2/4] Create four-guests-deny.md --- .changeset/four-guests-deny.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/four-guests-deny.md diff --git a/.changeset/four-guests-deny.md b/.changeset/four-guests-deny.md new file mode 100644 index 00000000..080162ee --- /dev/null +++ b/.changeset/four-guests-deny.md @@ -0,0 +1,5 @@ +--- +"@vinxi/router": patch +--- + +Update path-to-regexp and don't override the params on the routes handler From 50bff62ac725e4f4dcb6e30396bc9ecd01b6c0bd Mon Sep 17 00:00:00 2001 From: Jade Devin Cabatlao Date: Sat, 7 Sep 2024 04:37:04 +0800 Subject: [PATCH 3/4] Update api-handler.js --- packages/vinxi-router/api-handler.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vinxi-router/api-handler.js b/packages/vinxi-router/api-handler.js index 82c8ccfa..33d5aac6 100644 --- a/packages/vinxi-router/api-handler.js +++ b/packages/vinxi-router/api-handler.js @@ -14,7 +14,7 @@ const routes = [ function createRouter(routes) { const builtRoutes = routes.map((route) => { - const matcher = match(route.path) + const matcher = match(route.path); return { ...route, matcher, @@ -30,8 +30,8 @@ function createRouter(routes) { const match = route.matcher(path) if (match) { // add params to context object - setContext(event, 'params', { ...match.params }) - return await route.handler(event) + setContext(event, 'params', { ...match.params }); + return await route.handler(event); } } From 4fd3e5290ecf196b06435dc783a8dc1b0f5aa6f5 Mon Sep 17 00:00:00 2001 From: Jade Devin Cabatlao Date: Fri, 6 Sep 2024 23:02:02 +0200 Subject: [PATCH 4/4] Named params for toPath is changed since the update of path-to-regexp --- packages/vinxi-router/api.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vinxi-router/api.js b/packages/vinxi-router/api.js index 0221bffc..d85de70c 100644 --- a/packages/vinxi-router/api.js +++ b/packages/vinxi-router/api.js @@ -17,7 +17,7 @@ class APIFileSystemRouter extends BaseFileSystemRouter { .replace(/index$/, "") .replace(/\[([^\/]+)\]/g, (_, m) => { if (m.length > 3 && m.startsWith("...")) { - return `:${m.slice(3)}*`; + return `*${m.slice(3)}`; } if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) { return `:${m.slice(1, -1)}?`;