hono icon indicating copy to clipboard operation
hono copied to clipboard

Type for `output` in hono client route getting dropped

Open shahzeb1 opened this issue 1 year ago • 3 comments

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 avatar Jun 22 '24 00:06 shahzeb1

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" });
  });

shahzeb1 avatar Jun 23 '24 20:06 shahzeb1

@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?

yusukebe avatar Jun 23 '24 20:06 yusukebe

~~I have the same problem. Do you have any relevant GitHub Discussion URLs, etc.?~~

↓This URL helped me solve the problem. https://github.com/honojs/hono/issues/3485

mayone-du avatar Oct 21 '24 12:10 mayone-du

I think we can close this issue since it's not a bug. Thanks.

yusukebe avatar Dec 23 '24 05:12 yusukebe