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 diff --git a/packages/vinxi-router/api-handler.js b/packages/vinxi-router/api-handler.js index dfac7c1c..33d5aac6 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,14 +27,10 @@ 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 + setContext(event, 'params', { ...match.params }); return await route.handler(event); } } diff --git a/packages/vinxi-router/api.js b/packages/vinxi-router/api.js index 67282b09..acb35736 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)}?`; diff --git a/packages/vinxi-router/package.json b/packages/vinxi-router/package.json index 7662399d..b5c11dfc 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": {