Skip to content

Get typing for c.req.valid when seperate zValidator in another place #1050

Answered by yusukebe
harrytran998 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @harrytran998!

You can write the code like the following:

export const auth0Handler: Handler<{ Bindings: EnvBindings }, '/oauth/token', { out :{ query: {code: string, originUrl: string}}}> = async c => {
  const { code, originUrl } = c.req.valid('query')
  return c.text('res')
}

However, this is quite verbose. I recommend writing handlers with routing instead of keeping them separate.

export const authRoute = new Hono<{ Bindings: EnvBindings}>().basePath('/oauth')
authRoute.post('/token', zValidator('query', AuthValidation.oauthToken), (c)=> {
  const { code, originUrl } = c.req.valid('query') 
  // ...
})

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@harrytran998
Comment options

Answer selected by harrytran998
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants