Skip to content

Commit

Permalink
Merge pull request #368 from jadedevin13/main
Browse files Browse the repository at this point in the history
Update path-to-regexp and don't override the params on the routes handler
  • Loading branch information
nksaraf authored Oct 22, 2024
2 parents 0e41b50 + 4fd3e52 commit 461a2da
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-guests-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vinxi/router": patch
---

Update path-to-regexp and don't override the params on the routes handler
21 changes: 7 additions & 14 deletions packages/vinxi-router/api-handler.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
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);
},
})),
];

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,
};
});

Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vinxi-router/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}?`;
Expand Down
2 changes: 1 addition & 1 deletion packages/vinxi-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 461a2da

Please sign in to comment.