Type safety in middleware?

Hi, it's there a way to get the types right here?
import { Hono } from "hono"
import { hc } from "hono/client"

export const honoApp = new Hono()
.use(async (c, next) => {
await next()
if (Math.random() > 0.5) {
return c.json({ authenticated: false }, 401)
}
})
.get("/", (c) => {
if (Math.random() > 0.5) {
return c.json({ message: "ok" }, 200)
}
return c.json({ error: "internal error" }, 500)
})

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _honoClient = hc<typeof honoApp>("/")
type HonoClient = typeof _honoClient

const createHonoClient = (...args: Parameters<typeof hc>): HonoClient => hc<typeof honoApp>(...args)

export const honoClient = createHonoClient("/")
honoClient.index.$get().then(async (res) => {
const data = await res.json()
console.log(data)
})
import { Hono } from "hono"
import { hc } from "hono/client"

export const honoApp = new Hono()
.use(async (c, next) => {
await next()
if (Math.random() > 0.5) {
return c.json({ authenticated: false }, 401)
}
})
.get("/", (c) => {
if (Math.random() > 0.5) {
return c.json({ message: "ok" }, 200)
}
return c.json({ error: "internal error" }, 500)
})

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _honoClient = hc<typeof honoApp>("/")
type HonoClient = typeof _honoClient

const createHonoClient = (...args: Parameters<typeof hc>): HonoClient => hc<typeof honoApp>(...args)

export const honoClient = createHonoClient("/")
honoClient.index.$get().then(async (res) => {
const data = await res.json()
console.log(data)
})
I expected data to be
const data: {
message: string;
} | {
error: string;
} | {
authenticated: boolean;
}
const data: {
message: string;
} | {
error: string;
} | {
authenticated: boolean;
}
But actually it is
const data: {
message: string;
} | {
error: string;
}
const data: {
message: string;
} | {
error: string;
}
6 Replies
ambergristle
ambergristle4d ago
https://hono.dev/docs/guides/rpc#status-code you can narrow the type client side using response.ok or response.status for just the types, you can use hono's InferResponseType utility type, which accepts an optional status code param that narrows the returned union
Gary, el Pingüino Artefacto
you can't narrow a type that isn't present...
Gary, el Pingüino Artefacto
GitHub
MiddlewareHandler response type inference · Issue #3746 · honojs/ho...
What is the feature you are proposing? When we use middleware, it can return a Response object or a Typed Response, for example, @hono/validator returns a MiddlewareHandler that can return a respon...
ambergristle
ambergristle4d ago
ah yeah, my bad. definitely didn't read your snippets closely enough i remember seeing the response type from the middleware in the json and thinking "huh, ig that works after all"
Gary, el Pingüino Artefacto
it's okay, I think it is a well known issue so I have to find another way thanks for the help 😄
ambergristle
ambergristle4d ago
yeah, i was just chatting w someone else who had the same issue np!

Did you find this page helpful?