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 inference for c.req.json() #3679

Closed
tmokmss opened this issue Nov 17, 2024 · 6 comments
Closed

type inference for c.req.json() #3679

tmokmss opened this issue Nov 17, 2024 · 6 comments
Labels

Comments

@tmokmss
Copy link

tmokmss commented Nov 17, 2024

What is the feature you are proposing?

When I define a route handler with the following code, the variable foo is infered as any, where I want it to be string without additional type annotation.

{
  const route = createRoute({
    method: 'post',
    path: '/',
    request: {
      body: {
        content: {
          'application/json': {
            schema: z.object({
              foo: z.string(),
            }),
          },
        },
      },
    },
    responses: {
      200: {
        description: 'success',
      },
    },
  });

  app.openapi(route, async (c) => {
    const { foo } = await c.req.json();
    return c.json({}, 200);
  });
}

It appears the HonoRequest type already has enough information to infer the json type:

image

However foo is inferred as any:

image

c.req.json<{foo:string}>() works, but it would be great if we can avoid adding (seemingly) redundant type annotation, like how c.req.valid('query') works.

Let me know if I'm misusing something, thanks!

@tmokmss tmokmss added the enhancement New feature or request. label Nov 17, 2024
@tmokmss
Copy link
Author

tmokmss commented Nov 17, 2024

I found c.req.valid('json') seems to work as expected, but not sure why c.req.json is async whereas c.req.valid is sync function? Is there any behavioural difference?

@EdamAme-x
Copy link
Contributor

@yusukebe
Copy link
Member

Hi @tmokmss

Please use c.req.valid('json') in the app.openapi for the Zod OpenAPI. And it's expected that the object returned from c.req.json() is not typed. If you want to add type, pass the generics, as you said:

c.req.json<{ foo: string }>()

@yusukebe yusukebe added not bug and removed enhancement New feature or request. labels Nov 18, 2024
@tmokmss
Copy link
Author

tmokmss commented Nov 18, 2024

@EdamAme-x @yusukebe Thanks for the guidance!
Still not sure how the async function works in sync call but I'll look into the code later 🙏

@tmokmss tmokmss closed this as completed Nov 18, 2024
@EdamAme-x
Copy link
Contributor

EdamAme-x commented Nov 18, 2024

Still not sure how the async function works in sync call but I'll look into the code later 🙏

The c.req.json is called and validated in the async middleware before c.req.valid is called.
After that, the validated data is kept in the Context so that it can be called sync.

@tmokmss
Copy link
Author

tmokmss commented Nov 18, 2024

@EdamAme-x That makes sense! I just started learning hono and you helped me a lot :) thank you so much!

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

3 participants