Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update path-to-regexp and don't override the params on the routes handler #368

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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