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

Type for output in hono client route getting dropped #3020

Open
shahzeb1 opened this issue Jun 22, 2024 · 2 comments
Open

Type for output in hono client route getting dropped #3020

shahzeb1 opened this issue Jun 22, 2024 · 2 comments
Labels

Comments

@shahzeb1
Copy link

What version of Hono are you using?

^4.4.7

What runtime/platform is your app running on?

Bun / Vite

What steps can reproduce the bug?

The Setup:

  1. A /api folder which has a basic Hono server with some routes (uses bun)
  2. A /frontend folder which has a react app (uses vite)

The goal:

Use the hono/client in a react useQuery hook to fetch data.

What's wrong:

Somewhere along the way, the hc object is not being properly typed.

In the /api code, notice if I hover over the type of ExecutionsRoute we can see all attributes of output:

Screenshot 2024-06-21 at 5 53 37 PM

In the /frontend code, notice if I hover over the type of client, we can see the attributes of output getting dropped:

Screenshot 2024-06-21 at 5 55 01 PM

What's wild is that if I jump-to-definition of ExecutionsRoute from the /frontend folder, it correctly brings me to /api.

What is the expected behavior?

The typing of the client which uses hono/client should preserve the types of the input and outputs.

What do you see instead?

The output is getting cast to

 [x: string]: any;

When it should be

{
    id: string;
    prompt: string;
    jobId: string;
    done: boolean;
    result: {
        url?: string | undefined;
    } | null;
    createdAt: string;
    updatedAt: string;
}

Additional information

Could it be the difference in build tools (bun vs. vite)?
I've tried restarting the TS lang server on VSCode multiple times.

@shahzeb1 shahzeb1 added the bug label Jun 22, 2024
@shahzeb1
Copy link
Author

I have narrowed it down, something about createInsertSchema from the drizzle-zod package, combined with zValidator("json", test) doesn't work well together.

Will not work:

import { createInsertSchema } from "drizzle-zod";

export const insertExecutionSchema = createInsertSchema(executionTable);

export const executionsRoute = new Hono()
  .post("/", zValidator("json", insertExecutionSchema), async (c) => {
    const execution = c.req.valid("json");
    c.status(201);
    return c.json({ hello: "world" });
  });

Will work:

const test = z.object({
  body: z.string(),
});

export const executionsRoute = new Hono()
  .post("/", zValidator("json", test), async (c) => {
    const execution = c.req.valid("json");
    c.status(201);
    return c.json({ hello: "world" });
  });

@yusukebe
Copy link
Member

@shahzeb1

This is a TypeScript type matter, not dependent on a runtime. It does not seem to be a bug. Could you ask this on Discussion or provide a minima project to reproduce it?

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

No branches or pull requests

2 participants