-
-
Notifications
You must be signed in to change notification settings - Fork 635
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
RPC Client does not maintain the Date type #3771
Comments
I think this is the correct behavior. The date is being passed as a string because JSON itself does not have a Date object, so instead it passes in the ISO 8601 format, which each language can then turn into a Date object. As for de-serializing we can’t really know whether the string we pass is really a date unless in runtime. |
@askorupskyy is right. This is not a bug. |
True, I understand the reason why it is a string. However, the term RPC (Remote Procedure Call) should essentially, to the developer, be like calling a regular function, in this case, calling In my understanding of RPC, not only the request payload is sent but also some optional metadata, which can be used to decipher how to serialize/de-serialize objects, which isnt universal to all languages/architectures. |
@cybercoder-naj I agree, but tbh I don't think this should be a part of Hono's core functionality. Even tRPC does not have this out of the box. Instead what Hono should aim for is the support for Here's a rough example of what this could look like: import { Context } from 'hono';
const sendSerializedJson<P extends Record<string, unknown>>(c: Context, payload: P){
return c.json(superjson.stringify(payload) as P); // should give you RPC type completion, haven't tested
}
app.get('/users', async (c) => {
const users = await db.getUsers();
return sendSerializedJson(c, { users });
})
export const client = hc<AppType>('localhost:whatever', {
fetch: async (req: RequestInfo | URL, init?: RequestInit) => {
const req = await fetch(req, init);
const raw = await req.json();
return superjson.deserialize(raw);
}
}) Still, I'd advocate against building any serious software in such a manner for two reasons:
Also, this instantly makes shipping any documentation a lot harder – most of API docs rely on OpenAPI, which with this extra metadata will be hard to maintain. |
Understood, thanks for the detailed explanation. |
so @askorupskyy, from what you said, serializing date is a bad idea right? but i'm curious how you handle this, are you okay with date string on client? |
@mamlzy having Yes, it's annoying, but so much better than having to rewrite the entire project because a new service written in rust, for example, does not have a |
Got it! thanks for your insight! |
What version of Hono are you using?
4.5.5
What runtime/platform is your app running on? (with version if possible)
Bun 1.1.38
What steps can reproduce the bug?
What is the expected behavior?
The expected behaviour is:
What do you see instead?
The Date type from the return of the handler is converted to a string. In how I see this, the client does not de-serialize the string from the network request back to the Date class, as it should.
Additional information
The same behaviour is seen from the
testClient()
function.The text was updated successfully, but these errors were encountered: