honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
18.65k stars 523 forks source link

In Hono RPC, specifying an array as a FormData value will result in type error #3116

Closed uttk closed 2 months ago

uttk commented 2 months ago

What version of Hono are you using?

4.4.12

What runtime/platform is your app running on?

Node.js

What steps can reproduce the bug?

FormData values ​​can contain arrays, but if you are using Honor RPC you will get a type error.

import { Hono } from "hono";
import { hc } from "hono/client";
import { validator } from "hono/validator";

const route = new Hono().post(
  "/",

  validator("form", (value: { body?: string[] }, c) => {
    const body = value["body"];

    if (!body || !Array.isArray(body)) {
      return c.text("Invalid!", 400);
    }

    return { body };
  }),

  (ctx) => {
    return ctx.text(`Body is [${ctx.req.valid("form").body}]`)
  }
);

const client = hc<typeof route>("");

client.index.$post({
  form: {
    body: ["1", "2", "3"], /*********** TYPE ERROR ! ***********/
  },
});

What is the expected behavior?

Hono PRC allows you to specify the array.

client.index.$post({
  form: {
    body: ["1", "2", "3"],  // No type errors
  },
});

What do you see instead?

No response

Additional information

Since "hono/client" allows you to specify an array, I guess the problem is only with type information.

https://github.com/honojs/hono/blob/51264492269c0f842697736825e47903ae868bca/src/client/client.ts#L68-L80

yusukebe commented 2 months ago

Hi @uttk

Thank you for the issue. This is a bug. I'll fix it later.

uttk commented 2 months ago

Thank you! Hono is verrrrrrrrrrrrrrrry fast, but I was surprised at the author respond is verrrrrrrrrrrrrrrry fast😹