H
Hono5mo ago
veeque

Maintaining slugs when grouping

If I group my API into two apps, and then mount one of the groups into a path that contains a slug, how can I use that slug in the app? For example, here, how would I use thisSlugHere in booksApp?
import { Hono } from 'hono'
import { hc } from 'hono/client'

const authorsApp = new Hono()
.get('/', (c) => c.json({ result: 'list authors' }))
.post('/', (c) => c.json({ result: 'create an author' }, 201))
.get('/:id', (c) => c.json({ result: get ${c.req.param('id')} }))

const booksApp = new Hono()
.get('/', (c) => c.json({ result: 'list books' }))
.post('/', (c) => c.json({ result: 'create a book' }, 201))
.get('/:id', (c) => c.json({ result: get ${c.req.param('id')} }))

const app = new Hono()
.route('/authors', authorsApp)
.route('/books/{thisSlugHere}', booksApp)

type AppType = typeof app
import { Hono } from 'hono'
import { hc } from 'hono/client'

const authorsApp = new Hono()
.get('/', (c) => c.json({ result: 'list authors' }))
.post('/', (c) => c.json({ result: 'create an author' }, 201))
.get('/:id', (c) => c.json({ result: get ${c.req.param('id')} }))

const booksApp = new Hono()
.get('/', (c) => c.json({ result: 'list books' }))
.post('/', (c) => c.json({ result: 'create a book' }, 201))
.get('/:id', (c) => c.json({ result: get ${c.req.param('id')} }))

const app = new Hono()
.route('/authors', authorsApp)
.route('/books/{thisSlugHere}', booksApp)

type AppType = typeof app
4 Replies
ambergristle
ambergristle5mo ago
parameters are defined in hono with a colon, as you did w ID: /:thisSlugHere
veeque
veequeOP5mo ago
oh shoot, yes, i meant :thisSlugHere
ambergristle
ambergristle5mo ago
afaik, they can't be applied to an entire route like that, though you might be able to do something w basePath
veeque
veequeOP5mo ago
ah ok, i see ty!

Did you find this page helpful?