-
Suppose I have some routes with a shared prefix: new Elysia()
.post('devices/:id/connect', (params: {id}) => getDevice(id).connect(),
{params: t.Object({id: t.Numeric()})})
.post('devices/:id/disconnect', (params: {id}) => getDevice(id).disconnect(),
{params: t.Object({id: t.Numeric()})})
.post('devices/:id/start', (params: {id}) => getDevice(id).start(),
{params: t.Object({id: t.Numeric()})})
.post('devices/:id/stop', (params: {id}) => getDevice(id).stop(),
{params: t.Object({id: t.Numeric()})}) I'd like to group these routes and use something like new Elysia()
.group('devices/:id', app => app
.derive(({params: {id}}) => {
return {device: getDevice(parseInt(id))} // ⚠️ `id` here is not yet validated or cast to a `number`
})
.post('devices/:id/connect', ({device}) => device.connect())
.post('devices/:id/disconnect', ({device}) => device.disconnect())
.post('devices/:id/start', ({device}) => device.start())
.post('devices/:id/stop', ({device}) => device.stop()))) It works, but in Is there an alternative to |
Beta Was this translation helpful? Give feedback.
Answered by
crishoj
Jul 5, 2024
Replies: 1 comment
-
Found the answer — just use resolve! new Elysia()
.group('devices/:id', {params: t.Object({id: t.Numeric()})}, app => app
.resolve(({params: {id}}) => ({device: getDevice(id)}))
.post('devices/:id/connect', ({device}) => device.connect())
.post('devices/:id/disconnect', ({device}) => device.disconnect())
.post('devices/:id/start', ({device}) => device.start())
.post('devices/:id/stop', ({device}) => device.stop())) Elysia is elegant ✨ |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
crishoj
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found the answer — just use resolve!
Elysia is elegant ✨