-
-
Notifications
You must be signed in to change notification settings - Fork 546
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
JSON response can't contain Dates #1800
Comments
Hi @DeanMauro As you mentioned, indeed the
import { Hono } from 'hono'
const app = new Hono().get((c) => c.json({ date: new Date() }))
export default app
import { hc } from 'hono/client'
import type app from 'server'
const client = hc<typeof app>('http://localhost:3000')
const res = await client.index.$get()
const data = await res.json() // { date: Date } <===
console.log(typeof data.date) // string <=== So, I think it not so easy to resolve this issue. And I think it should not allow @ThatOneBro @usualoma Do you have any thoughts? |
I ran into this myself recently on upgrading. A few thoughts
Perhaps one line of code in the release notes to show how to maintain behavior would go a long way. |
@AdiRishi Thanks for sharing your thoughts.
I see, indeed, in that case, the current |
Sounds good. Just so I understand, the fix will be to relax the types of |
Thank you both for following up. Perhaps the input and output types should be separated. The output in RPC mode, for example can incorporate a utility type that makes it clear all non-JSONValues have become strings, but without having to put restrictions on the input. E.g.
|
Hi @DeanMauro Quick response.
That makes sense. |
I think letting things get auto-converted to strings fixes types but leads to inconsistent behavior across platforms. Not every runtime will do the same thing for any given runtime object (although Date is likely standard at this point), which makes it a little sketchy for me. I think we can handle What could be done is to allow users to specify a |
Thank you for sharing your thoughts! In this case, since I've created PR #1806 to fix this issue. Could you review it? The main idea is to separate the input and output types of |
@yusukebe Thank you for handling this! I appreciate the super fast turnaround :) |
This issue has been fixed. You can try the latest version |
@yusukebe Has it been fixed yet? Im facing this issuse and and looking for solution. |
Hi @linhtrinh18
This behavior may be correct, though I don't fully understand your problem. |
I'm getting this problem too so I'll add. My data tables contain dates like So when accessing the hono client everything is typed as a string as mentioned elsewhere in this issue. // server side
const app = new Hono<{ Bindings: EnvBindings }>()
.get('/:id', async (c) => {
const id = Number.parseInt(c.req.param('id'))
const [data] = await c.env.db.select().from(properties).where(eq(properties.id, id))
return c.json({ data }) // i.e. createdAt: Date | null;
})
// client side
queryFn: async () => {
if (!id) return
const res = await client.properties[`:id`].$get({ param: { id: id.toString() } })
if (!res.ok) {
throw new Error('Failed to fetch property data')
}
return res.json() // i.e. createdAt: string | null;
}, So because I'm sharing the types inferred by Drizzle in my front end code, I am getting type errors whenever I deal with data returned from the hono RPC client. What's the recommendation here? I am sure you wouldn't recommend that we cast the type of the data back to the correct type (i.e. the type produced by Drizzle) everywhere we return data from hono RPC? Is there a simple middleware that can be written to do this automatically? I imagine this would be a common problem, so would be appreciated of your guidance. I'm surprised it's not mentioned elsewhere in the docs. |
looking for some ideas as well... i pass it through zod parse on client to fix the dates.. but it feels hacky |
@yusukebe also to note, I had tried to implement the code you linked here #1559 (comment) but ran into type errors as it seems some of the hono types changed after that article was written. It was be appreciated to have an officially recommended way of going about this. |
What version of Hono are you using?
3.11.4
What runtime/platform is your app running on?
Cloudflare Pages Function
What steps can reproduce the bug?
Return an object containing a Date with
context.json
. For example:What is the expected behavior?
Because Hono uses JSON.stringify() under the hood, I would expect the code above to be valid.
What do you see instead?
A type error is shown because
Date
is not considered aJSONObject
orJSONPrimitive
.Additional information
The code executes fine, with the Date being stringified in the response as expected. For this reason especially, I consider the typing to be inaccurate. It disallows a valid type.
The text was updated successfully, but these errors were encountered: