HonoH
Hono2y ago
Korny

Any way to modify the request path in middleware?

(this is cross-posted on https://github.com/orgs/honojs/discussions/2642
  • I'm not quite sure which forum we are meant to use? trying both, apologies for the noise)
am trying to replicate a legacy API which had optional versions in paths, so GET /v1/foo actually called GET /foo under the covers.

I was hoping to add some middleware that just intercepted paths starting with /v1 and removed the prefix (or re-directed somehow to the correct route) - but it seems that modifying the ctx.req.path does nothing.

Is this possible? I've seen examples that clone ctx.req.raw and make a brand new request - that feels a bit ugly, and I'm not sure that Hono will use the new request's path for routing anyway.

Any suggestions? (for added complexity we are using the zod-openapi middleware so I can't just give our routes both /foo and /v1/foo paths easily. And we'd like to decrement the /v1 rubbish anyway, so I'd prefer to not have it in our openapi schema)
Was this page helpful?