Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Router grouping. #2567

Closed
Ray-D-Song opened this issue Apr 28, 2024 · 3 comments
Closed

Better Router grouping. #2567

Ray-D-Song opened this issue Apr 28, 2024 · 3 comments
Labels
enhancement New feature or request.

Comments

@Ray-D-Song
Copy link

What is the feature you are proposing?

const book = new Hono()

book.get('/', (c) => c.text('List Books')) // GET /book
book.get('/:id', (c) => {
  // GET /book/:id
  const id = c.req.param('id')
  return c.text('Get Book: ' + id)
})
book.post('/', (c) => c.text('Create Book')) // POST /book

const app = new Hono()
app.route('/book', book)

The current routing grouping for running multiple Hono initialization methods is not very intuitive. I think we can attach a group method to the instance and return child instance, like this:

const app = new Hono()
const bookStore = app.group('/book_store')

bookStore.get('/:id', (c) => {
  const id = c.req.param('id')
  return c.text('Get Book Store: ' + id)
})

const book = bookStore.group('/book')
...
@Ray-D-Song Ray-D-Song added the enhancement New feature or request. label Apr 28, 2024
@NamesMT
Copy link
Contributor

NamesMT commented Apr 28, 2024

You just described basePath 😁
https://hono.dev/api/routing#base-path

@Ray-D-Song
Copy link
Author

You just described basePath 😁 https://hono.dev/api/routing#base-path

Yeah, I know basePath can achieve the same thing, I just think this is a more semantically organized way.

@ryuapp
Copy link
Contributor

ryuapp commented Apr 29, 2024

It will work if I change group() to basePath() from the code you suggested.

const app = new Hono()
- const bookStore = app.group('/book_store')
+ const bookStore = app.basePath('/book_store')
bookStore.get('/:id', (c) => {
  const id = c.req.param('id')
  return c.text('Get Book Store: ' + id)
})

- const book = bookStore.group('/book')
+ const book = bookStore.basePath('/book')
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

3 participants