H
Hono•7d ago
Mattèo

Got `unknown` type on res.json() on a OpenAPI RPC route

Hey ! I have an issue with RPC client and OpenAPI route inference. Reproduction : https://stackblitz.com/edit/vitejs-vite-lqsp6pho?file=index.ts
import { hc } from "hono/client"
import app from "./openapi"
import { serve } from "@hono/node-server"

serve(app)

const client = hc<typeof app>("http://localhost:3000")

const res = await client["calendar-debug"].$get({
/* ^? const res: ClientResponse<{
page: number;
limit: number;
total: number;
events: {
type: string;
title: string;
status: string;
clubId: string;
....

Type of res seems to be good
*/
query: {
page: "1",
limit: "10",
clubId: "1",
sportId: "1",
},
})
const data = await res.json()
// ^? unknown

console.log(data)
import { hc } from "hono/client"
import app from "./openapi"
import { serve } from "@hono/node-server"

serve(app)

const client = hc<typeof app>("http://localhost:3000")

const res = await client["calendar-debug"].$get({
/* ^? const res: ClientResponse<{
page: number;
limit: number;
total: number;
events: {
type: string;
title: string;
status: string;
clubId: string;
....

Type of res seems to be good
*/
query: {
page: "1",
limit: "10",
clubId: "1",
sportId: "1",
},
})
const data = await res.json()
// ^? unknown

console.log(data)
Does someone had this problem before, or can someone help me? Wishing you a good day 😉
Mattèo Gauthier
StackBlitz
RPC + @hono/zod-openapi type inference - StackBlitz
Next generation frontend tooling. It's fast!
2 Replies
ambergristle
ambergristle•7d ago
in the openapi schema you've specified that the response could be a 200 or a 400, so you need to handle each case:
if (res.status === 200) {
const typedData = await res.json()
}
if (res.status === 200) {
const typedData = await res.json()
}
Mattèo
MattèoOP•7d ago
Ah, I see. Thanks a lot; that makes sense !! Thanks for your time

Did you find this page helpful?