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

Phenomenon that type definition does not work when dynamic route of @hono/zod-openapi is assigned to hono/client. #3106

Closed
tsuki-lab opened this issue Jul 6, 2024 · 3 comments · Fixed by honojs/middleware#623
Labels

Comments

@tsuki-lab
Copy link

tsuki-lab commented Jul 6, 2024

What version of Hono are you using?

4.4.12

What runtime/platform is your app running on?

local

What steps can reproduce the bug?

Please use the tsuki-lab/verification-hono-openapi-rpc repository me created as a reproduction environment.

const app = new OpenAPIHono();

const route = createRoute({
  method: "get",
  path: "/users/{id}", // Use path parameter.
  request: {
    params: z.object({
      id: z.coerce.number().openapi({ description: "userId", example: 1 }),
    }),
  },
  responses: {
    200: {
      content: {
        "application/json": {
          schema: userSchema,
        },
      },
      description: "Get a user",
    },
  },
});

const routes = app.openapi(route, (c) => {
  return c.json([{ id: "1", name: "John" }]);
});

const client = hc<typeof routes>("");
// The type does not match the intended shape
const client: {
  [x: string]: ClientRequest<{
      [Method: `$${Lowercase<string>}`]: Endpoint;
  }>;
}

What is the expected behavior?

const client is correctly type-stored.

const client: {
  user: {
    ":id": {
    // ...
    }
  }
}

What do you see instead?

const client: {
  [x: string]: ClientRequest<{
      [Method: `$${Lowercase<string>}`]: Endpoint;
  }>;
}

Additional information

No response

@tsuki-lab
Copy link
Author

I found that this condition reproduces itself when using z.numer() and z.coerce.number().
Using z.string() is a workaround at this time.

  // ...
  request: {
    params: z.object({
      id: z.string().openapi({ description: "userId", example: 1 }),
    }),
  },
  // ...

However, when using $get(), the id passed to param must be converted to a string type each time it is used.

const r = await client.api.users[":id"].$get({ param: { id: "1" } });
console.log(await r.json());

@yusukebe
Copy link
Member

yusukebe commented Jul 7, 2024

Hi @tsuki-lab !

This is a bug in @hono/zod-openapi. I've fixed it and released a new version 0.14.8. Try it!

@tsuki-lab
Copy link
Author

@yusukebe

Thank you for fixing the bug in @hono/zod-openapi and releasing the new version 0.14.8! I'll try it out right away.

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

Successfully merging a pull request may close this issue.

2 participants