[bug] Eden Treaty 2: dynamic params unreachable when parent path segment is an HTTP method name (e.g. /api/post/:id)
Environment elysia 1.4.30, @elysia/eden 1.4.10 Bun 1.3.14 (server), Chromium (client) Describe the bug Eden Treaty 2 cannot select dynamic path params when the node's path ends with a segment named like an HTTP method (get / post / put / delete / patch / options / head / connect / subscribe).
For a route GET /api/post/:id:
Expected: await eden.api.post({ id: 1 }).get() → GET /api/post/1 Actual: the runtime pops the trailing post segment as the HTTP method of the parent node and fires POST /api, and the call returns a Promise, so the chained .get() throws .get is not a function. With a typed client this is worse: eden.api.post({ id: 1 }).get() type-checks and infers the correct response type, so types and runtime disagree on the exact same call.
Minimal reproduction server.ts
import { Elysia, t } from 'elysia'
const app = new Elysia({ prefix: '/api' })
.use(
new Elysia({ prefix: '/post' })
.get('/:id', ({ params: { id } }) => ({ msg: 'ok', data: { id } }), {
params: t.Object({ id: t.Integer() }),
}),
)
.use(
new Elysia({ prefix: '/user' })
.get('/:id', ({ params: { id } }) => ({ msg: 'ok', data: { id } }), {
params: t.Object({ id: t.Integer() }),
}),
)
app.listen(3210)client.ts
import { treaty } from '@elysia/eden'
const eden = treaty('http://localhost:3210')
// ✅ control: parent tail is not a method name
await eden.api.user({ id: 1 }).get() // → GET /api/user/1
// ❌ bug: parent tail is `post`
await eden.api.post({ id: 1 }).get() // fires POST /api, then throws ".get is not a function"
// workaround: address the segment explicitly
await (eden.api.post as any)['1'].get() // → GET /api/post/1Observed output (verified locally):
1) user({ id }).get() => 200 {"msg":"ok","data":{"id":1}}
2) post({ id }).get() threw: eden.api.post({ id: 1 }).get is not a function
captured requests:
GET http://localhost:3210/api/user/1 → 200
POST http://localhost:3210/api → 404
GET http://localhost:3210/api/post/1 → 200 (workaround)
Root cause The treaty runtime decides between "dynamic param selection" and "fire request" with this heuristic (minified excerpt from the runtime chunk):
var X = ["get","post","put","delete","patch","options","head","connect","subscribe"];
apply(o, i, [s, u]) {
if (!s || u || (typeof s == "object" && Object.keys(s).length !== 1) || X.includes(n.at(-1))) {
let x = [...n], m = x.pop(), b = "/" + x.join("/"); // pops last segment as the HTTP method
/* ... fetch ... */
}
/* else: dynamic segment selection (appends the single value as a path segment) */
}
Calling eden.api.post({ id: 1 }): s = { id: 1 } is a single-key object (which qualifies for dynamic selection), but n.at(-1) === 'post' is in X, so it short-circuits into the fetch branch and treats post as the HTTP method on the parent /api node. The type-level tree models /api/post as a namespace with an :id child, hence the mismatch. Same applies to any dynamic route whose immediate parent segment is one of the X names (/get/:id, /admin/post/:id, …).
Workarounds Explicit segment access: (eden.api.post as any)['1'].get() — works, but loses type-safety on the path. edenFetch with explicit string paths is unaffected. Suggested direction (maintainers' call) Since the runtime proxy has no route metadata, the heuristic is inherently ambiguous — e.g. changing it to "prefer dynamic when the single key is not a request-option key" would then break single-key bodies on method calls like eden.api.post.post({ title: 'x' }). So an explicit, non-ambiguous escape seems like the robust fix, e.g.:
await eden.api.post({ $params: { id: 1 } }).get() // reserved wrapper
// or
await eden.api.post.params({ id: 1 }).get() // reserved method-like helper
Happy to provide a failing test/repro repo if useful.
Source: elysiajs/elysia