-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
c.req.routePath
returns a wrong value when used inside a custom middleware
#3876
Comments
This is not a bug. Expected behavior. What you are seeing is the response from |
Thanks for your quick reply @yusukebe Here's my confusion. If I return the path details in // Anonymize a user.
.put("/:id/anonymize", async (c) => {
return c.json({
path: c.req.path,
method: c.req.method,
routePath: c.req.routePath,
params: c.req.param(),
url: c.req.url
}, 200)
}) {
"path": "/api/users/52/anonymize",
"method": "PUT",
"routePath": "/api/users/:id/anonymize",
"params": {
"id": "52"
},
"url": "http://localhost:3000/api/users/52/anonymize"
} But if I try to get the path details inside the middleware: export const myMiddleware: MiddlewareHandler = async (c, next) => {
return c.json({
path: c.req.path,
method: c.req.method,
routePath: c.req.routePath,
params: c.req.param(),
url: c.req.url,
}, 200)
} {
"path": "/api/users/52/anonymize",
"method": "PUT",
"routePath": "/api/*",
"params": {},
"url": "http://localhost:3000/api/users/52/anonymize"
} I want to create a logic inside the middleware based on the I appreciate if you could point me to the right direction. |
I think you can use this |
Thanks @EdamAme-x, I thought about that too. but is this the best way? return c.json({
path: c.req.path,
method: c.req.method,
routePath: c.req.routePath,
params: c.req.param(),
url: c.req.url,
matchedRoutesFiltered: c.req.matchedRoutes.filter((r) => r.method === c.req.method),
matchedRoutes: c.req.matchedRoutes,
}, 200) could return: {
"path": "/api/users/52/anonymize",
"method": "PUT",
"routePath": "/api/*",
"params": {},
"url": "http://localhost:3000/api/users/52/anonymize",
"matchedRoutesFiltered": [
{
"path": "/api/users/:id/anonymize", <-- The desired value
"method": "PUT" <-- for matching method.
}
],
"matchedRoutes": [
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/*",
"method": "ALL"
},
{
"path": "/api/*",
"method": "ALL"
},
{
"path": "/api/users/:id/anonymize",
"method": "PUT"
}
]
} |
Do you need import { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'
export const myMiddleware = createMiddleware(async (c, next) => {
await next()
const requestPath = c.req.routePath
const requestMethod = c.req.method
console.log(requestPath, requestMethod) // /api/users/:id/anonymize PUT
})
const user = new Hono().put('/:id/anonymize', async (c) => {
return c.json({}, 201)
})
const app = new Hono().use('/api/*', myMiddleware).route('/api/users', user)
await app.request('/api/users/foo/anonymize', {
method: 'PUT'
}) |
Thanks @yusukebe for the code example, and suggestions. I had to do some more testing... I realized calling Calling I was calling |
@gormus Okay! Can you close this issue? |
Not a bug indeed : ) Thanks for the support! |
What version of Hono are you using?
4.6.16
What runtime/platform is your app running on? (with version if possible)
node
What steps can reproduce the bug?
When a custom middleware enabled for all routes matching a wildcard pattern, returns the same value for the
c.req.routePath
method used inside the middleware.What is the expected behavior?
I expect the
c.req.routePath
method that run inside the custom middleware to return/api/users/:id/anonymize
What do you see instead?
... instead it returns the value defined in the
use()
method:/api/*
If no pattern is defined for the
use(myMiddleware)
method, it returns*
forc.req.routePath
method.Additional information
Similar reports:
The text was updated successfully, but these errors were encountered: