-
-
Notifications
You must be signed in to change notification settings - Fork 681
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
Allow throwing responses (like Remix) and automatically wrap them in HttpException #2897
Comments
hey @yusukebe, could you take a look at that? |
Hi @ajaishankar One of the reasons why it occurs is the const app = new Hono()
const flag = true
const routes = app.get('/profile', (c) => {
if (!flag) {
return c.redirect('/', 301)
}
return c.json({ name: 'abc' }, 200)
})
const client = hc<typeof routes>('/')
const res = await client.profile.$get()
if (res.status === 301) {
console.log(res.headers.get('Location'))
} else {
const data = await res.json()
// ^ { name: string }
} |
Will Hono implement this soon? |
@molives yes. @ajaishankar Can you check #2908? |
Commented on the PR, looks good... Redirect is a common enough use case and nice to have support for this in hono RPC. For something like Remix thrown response I'd suggested very easy to accomplish with a simple middleware. Again Hono makes everything super simple! 🙂 const thrownResponseToHttpException = createMiddleware(async (c, next) => {
try {
await next();
} catch (e) {
throw e instanceof Response ? new HTTPException(undefined, { res: e }) : e;
}
}); |
Hi @ajaishankar Is this issue solved? |
Yes, no need to add this to Hono since it can be accomplished with a middleware.. |
What is the feature you are proposing?
Having a conditional response like the following breaks type inference as a generic Response is inferred
Feel the documentation could be enhanced to avoid conditional responses and use HttpException instead
Since we are throwing an exception for the redirect, the return type is inferred correctly for the happy path.
Even better maybe hono could support throwing responses similar to Remix?
https://remix.run/docs/en/main/utils/redirect
https://remix.run/docs/en/main/guides/not-found#how-to-send-a-404
This can be achieved with a simple middleware, but feel this might be a good enhancement?
The text was updated successfully, but these errors were encountered: