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

How to structure large apis using hono? #200

Closed
OultimoCoder opened this issue May 5, 2022 · 5 comments
Closed

How to structure large apis using hono? #200

OultimoCoder opened this issue May 5, 2022 · 5 comments

Comments

@OultimoCoder
Copy link

OultimoCoder commented May 5, 2022

Traditionally using express I would have this file structure:

── routes
    ├── index.js
    ├── users.route.js
── index.js  

And then in users.route.js I would define all my routes relating to users e.g.

const router = express.Router();

router
  .route('/')
  .post(auth('manageUsers'), validate(userValidation.createUser), userController.createUser)
  .get(auth('getUsers'), validate(userValidation.getUsers), userController.getUsers);

router
  .route('/:userId')
  .get(auth('getUsers'), validate(userValidation.getUser), userController.getUser)
  .patch(auth('manageUsers'), validate(userValidation.updateUser), userController.updateUser)
  .delete(auth('manageUsers'), validate(userValidation.deleteUser), userController.deleteUser);

module.exports = router;

In routes/index.js I would apply the base urls to all my defined routes and then in the top level index.js I would import and enable them:

// v1 api routes
app.use('/v1', routes);

What is the recommended way of doing this in Hono?

@yusukebe
Copy link
Member

yusukebe commented May 6, 2022

Hi @OultimoCoder

Currently, we can write routing using route like this:

export const app = new Hono()

const root = app.route('/v1')
root.get('/', (c) => c.text('getUsers'))
root.post('/', (c) => c.text('createUser'))

And, This syntax may be available in the next release.

root.post('/', basicAuth(), cors(), (c) => c.text('getUsers')) // <--- this syntax is coming soon!

I think this is not such a smart design for large APIs, so I want to add another way for routing, such as below:

import { Hono, Route } from 'hono'

const app = new Hono()
const route = new Route()

route.get('/entry', handler, handler).post(handler, handler)

app.use('/v1', route)
app.fire()

This is still just the idea.

Conclusion, please use app.route method. Thank you.

@OultimoCoder
Copy link
Author

What is the eta for the next release? Thanks!

@yusukebe
Copy link
Member

yusukebe commented May 7, 2022

@OultimoCoder

This feature is already implemented #203 .
We are planning to emit this feature in the next release v1.2.0. Maybe its' soon!

@yusukebe
Copy link
Member

@OultimoCoder

We released v1.2.0. See: https://github.com/honojs/hono/releases/tag/v1.2.0.

@yusukebe
Copy link
Member

Hi @OultimoCoder

Route is deprecated. Please use the Hono instance!

https://github.com/honojs/hono#grouping

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

No branches or pull requests

2 participants