H
Hono•2w ago
dpeter

Hono not inferring output correctly with RPC

Im having an issue related to the output of hono RPC. I have this route to check if an organization exists by slug.
const app = new Hono<{
Variables: {
session: Session | null
}
}>()
.use(authMiddleware)
.get(
'/:slug/available',
zValidator(
'param',
z.object({
slug: z.string(),
}),
),
async (c) => {
const session = c.get('session')
const user = session?.user

if (!user) return c.body(null, 401)

const { slug } = c.req.valid('param')

const useCase = makeOrgExistsBySlugUseCase()

const { exists } = await useCase.execute({ slug })

return c.json({ available: !exists }, 200)
},
)

export default app
const app = new Hono<{
Variables: {
session: Session | null
}
}>()
.use(authMiddleware)
.get(
'/:slug/available',
zValidator(
'param',
z.object({
slug: z.string(),
}),
),
async (c) => {
const session = c.get('session')
const user = session?.user

if (!user) return c.body(null, 401)

const { slug } = c.req.valid('param')

const useCase = makeOrgExistsBySlugUseCase()

const { exists } = await useCase.execute({ slug })

return c.json({ available: !exists }, 200)
},
)

export default app
And in my AppType the Input is being inferred properly but the output is just an empty object:
MergeSchemaPath<{
"/:slug/available": {
$get: {
input: {
param: {
...;
};
};
output: {};
outputFormat: string;
status: StatusCode;
};
};
}, "/organizations">, "/">
MergeSchemaPath<{
"/:slug/available": {
$get: {
input: {
param: {
...;
};
};
output: {};
outputFormat: string;
status: StatusCode;
};
};
}, "/organizations">, "/">
What am i doing wrong?
7 Replies
ambergristle
ambergristle•2w ago
hi @dpeter! it looks like the issue stems from your use of c.body it looks like that method's typing isn't generic, and it doesn't return a TypedResponse
dpeter
dpeterOP•2w ago
It worked, thanks!
ambergristle
ambergristle•2w ago
switching to c.json?
dpeter
dpeterOP•2w ago
yes
ambergristle
ambergristle•2w ago
word. no problem! just for reference, discord supports syntax highlighting in code blocks just add the language (e.g., typescript) immediately after the opening backticks (as you would normally w markdown code blocks)
function highlighted() {
console.log(syntax);
}
function highlighted() {
console.log(syntax);
}
i'm a bit surprised that the response type gets totally obliterated like that. perhaps the intent is that c.body is used only in specific contexts (e.g., returning blobs), but i've seen nothing to that effect in the docs i've actually used c.body the same way you did. i just wasn't using the client, so i didn't run into this issue ah, my bad. they sort of do https://hono.dev/docs/guides/rpc#rpc
dpeter
dpeterOP•2w ago
Thanks for the info. I missed that when i was reading the docs 😅
ambergristle
ambergristle•2w ago
happy to help! imo the wording is a bit unclear, and it's not mentioned in the Context spec at all

Did you find this page helpful?