H
Hono3mo ago
Cyrlah

Looking for tips about best practices

hey everyone, question trying to use factories with my routes factory.handlers should contain one method ? or a lot of methods I mean like that
createMovie: factory.createHandlers(async (c) => {
const body = await c.req.json()
const result = service.createMovie(body)
return c.json(result, 200)
}),
getMovies:factory.createHandlers(async (c) => {
const results = service.getMovies()
return c.json(results, 201)
}),
getMovieById: factory.createHandlers(async (c) => {
const id = c.req.param('id')

if (id === undefined) {
return c.json({ error: 'Missing id' }, 400);
}

const result = service.getMovieById(id)

return c.json(result, 201)
})
createMovie: factory.createHandlers(async (c) => {
const body = await c.req.json()
const result = service.createMovie(body)
return c.json(result, 200)
}),
getMovies:factory.createHandlers(async (c) => {
const results = service.getMovies()
return c.json(results, 201)
}),
getMovieById: factory.createHandlers(async (c) => {
const id = c.req.param('id')

if (id === undefined) {
return c.json({ error: 'Missing id' }, 400);
}

const result = service.getMovieById(id)

return c.json(result, 201)
})
should I define a handler for each method? or a global handler like i'm doing for eg
createMovie: factory.createHandlers(async (c) => {
const body = await c.req.json()
const result = service.createMovie(body)
return c.json(result, 200)
}),
createMovie: factory.createHandlers(async (c) => {
const body = await c.req.json()
const result = service.createMovie(body)
return c.json(result, 200)
}),
and use my handlers in routes like that
const handlers = movieHandlers(movieService)

movieRoutes.post('/', ...handlers.createMovie)
movieRoutes.get('/', ...handlers.getMovies)
movieRoutes.get('/:id', ...handlers.getMovieById)
const handlers = movieHandlers(movieService)

movieRoutes.post('/', ...handlers.createMovie)
movieRoutes.get('/', ...handlers.getMovies)
movieRoutes.get('/:id', ...handlers.getMovieById)
4 Replies
ambergristle
ambergristle3mo ago
i would recommend writing your handlers (and middleware) inline until you have a reason to abstract them out the createhandlers factory is mainly for combining multiple (especially middleware) into a single function the examples above actually limit your ability to take advantage of hono type inference, and will slow down your typescript server in the long term, that level of abstraction will also make your code more difficult to maintain
Cyrlah
CyrlahOP3mo ago
Can't get what you mean by "inline", you mean directly in the router function ?
app.get('/movies', async (c) => {
const movies = service.getMovies()
return c.html(<Home movies={movies} />)
})
app.get('/movies', async (c) => {
const movies = service.getMovies()
return c.html(<Home movies={movies} />)
})
ambergristle
ambergristle3mo ago
exactly that's the recommended way to write hono apps
Arjix
Arjix3mo ago
even on a big scale you may not need to use factories I assume the best time to use factories, is after refactoring the codebase cause by then, you'd have an idea of what you are actually improving

Did you find this page helpful?