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

@hono/zod-validator not inferring app context types correctly within it's hook function #719

Open
DaveLo opened this issue Aug 27, 2024 · 2 comments

Comments

@DaveLo
Copy link

DaveLo commented Aug 27, 2024

If i have types passed to the Hono generic like:

type HonoBindings = {
  Bindings: { event:LambdaEvent; lambdaContext: LambdaContext };
  Variables: { logger: PowertoolsLogger }
}

const app = new Hono<HonoBindings>()

If I try to access the logger from within the hook

app.get(
  "/customer", 
  zValidator("query", querySchema, (result, ctx) => {
  // this is type never 
  const logger = ctx.get('logger');
  if (!result.success) {
    logger.debug('Validation error: ',  result.error)
    ctx.text("InvalidRequest", 400);
  }),
  async (ctx) => { // do things }
);

I think I can do this

zValidator<typeof querySchema, 'query', HonoBindings, "?">();

But the typings for the generic are not documented so I'm not sure if that's intended or even what that 4th one is only that it's required and is a string.

I feel like it should infer from it's context, but if inferring them isn't possible I would maybe suggest moving the env type up the list as all the rest of the types seem to infer without trouble.

@yusukebe
Copy link
Member

yusukebe commented Sep 2, 2024

Hi @DaveLo

This is related to honojs/hono#3341 honojs/hono#3202 honojs/hono#3198

@DaveLo
Copy link
Author

DaveLo commented Sep 3, 2024

I see. I guess in that case it might be beneficial to document how to type the generic since it seems unlikely to be fixed.

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

No branches or pull requests

2 participants