-
|
How can we use params in prefix ? such as we will have router for products and items, in that case, how can we inject {workspaceId} to the routers? I have tried using const { matched, response } = await handler.handle(request, {
prefix: '/api/{workspaceId}',
context: { db, workspaceId },
});but it will always return 404 and |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi @SSam0419! I'm Dosu and I’m helping the orpc team. orpc does not natively support dynamic parameters in the prefix (like The recommended workaround is to use an external router (such as Hono) to extract the dynamic parameter from the path, then pass it as both the prefix and in the context when calling import { Hono } from 'hono'
import { RPCHandler } from '@orpc/server/fetch'
const app = new Hono()
const handler = new RPCHandler(router)
app.use('/api/:workspaceId/*', async (c, next) => {
const workspaceId = c.req.param('workspaceId')
const { matched, response } = await handler.handle(c.req.raw, {
prefix: `/api/${workspaceId}`,
context: { db, workspaceId }
})
if (matched) return c.newResponse(response.body, response)
await next()
})This way, Hono handles the dynamic segment and injects it into both the prefix and context for orpc, allowing your product and item routers to work as expected [example and discussion]. If you want to define dynamic parameters in the route itself (not the prefix), make sure your input schema matches the path parameters exactly, e.g., Let me know if you need a more detailed example for your specific router setup! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @SSam0419! I'm Dosu and I’m helping the orpc team.
orpc does not natively support dynamic parameters in the prefix (like
/api/{workspaceId}) for routers, which is why your current approach results in 404 andmatchedis always false. The framework expects static prefixes, and dynamic path parameters must be defined directly in the route path and input schema, not in the prefix option. This is a known limitation and there is an open discussion and draft proposal about supporting this feature natively, but it is not available yet [discussion].The recommended workaround is to use an external router (such as Hono) to extract the dynamic parameter from the path, then pass it as both the pr…