middleware icon indicating copy to clipboard operation
middleware copied to clipboard

Status code used in the third argument of zValidator is not inferred on the client side

Open mayone-du opened this issue 1 year ago • 4 comments

I have the following code. When trying to handle branching on the client side based on res.status, it always ends up being 200. Is there any documentation or solution regarding this issue?

const sampleRoute = new Hono().get(
  '/sample',
  zValidator(
    'json',
    z.object({
      keyword: z.string().min(1),
    }),
    (result, c) => {
      if (!result.success) {
        return c.json({ error: 'error!' }, 400)
      }
    }
  ),
  async (c) => {
    return c.json({ data: 'data' }, 200)
  }
)

in client スクリーンショット 2024-06-16 13 04 46

mayone-du avatar Jun 16 '24 04:06 mayone-du

I am also noticing the client type doesn't have the 400 response code without the third argument when using valibot.

lukeshay avatar Jun 18 '24 18:06 lukeshay

Hi @mayone-du

Thank you for the issue. This should be fixed. But we need time since it is a little difficult.

@NamesMT Can you also take a look if you have time?

yusukebe avatar Jun 21 '24 21:06 yusukebe

Hi @yusukebe,

Currently MiddlewareHandler's Inputs is not taken to account for the final TypedResponse output in HandlerInterface flow, as well as the Input interface for middlewares does not have a way to pass status code along with it yet. So, we need:

  • A way for middlewares to pass along the status code in type,
  • Taken account of all Input['out'] for the final response, this could be done mainly at ToSchema level and/or the HandlerInterface I think.

I don't have much time to work on it yet, I'll start a draft PR when I do.

NamesMT avatar Jun 22 '24 05:06 NamesMT

honojs/hono#2719

KaelWD avatar Jul 28 '24 04:07 KaelWD