H
Hono5mo ago
Phil

[zod, bun]#openapi async/await doesn't work

This ossue was previously on https://discord.com/channels/1011308539819597844/1012485912409690122/1242558525012709539, but I was advised to open it here. here is a noob question. Im quite new to TS, bun and Hono with Zod. I cant make sense of why TS is screaming at me in the first line with async
apps.openapi(routeGetApp, async (c) => {
~~~~~~~~~~~~~~
try {
const { identifier } = c.req.valid('param');
const result = await db.query.app.findFirst({
where: eq(app.name, identifier),
with: { feature: true },
});
if (!result) return c.notFound();
console.log(result);
return c.json({
id: result.id,
identifier: result.name,
features: {
featureX: result.feature.deleteMessage,
featureY: result.feature.inviteLinks,
},
});
} catch (error) { return c.json(error, 500); }
});
apps.openapi(routeGetApp, async (c) => {
~~~~~~~~~~~~~~
try {
const { identifier } = c.req.valid('param');
const result = await db.query.app.findFirst({
where: eq(app.name, identifier),
with: { feature: true },
});
if (!result) return c.notFound();
console.log(result);
return c.json({
id: result.id,
identifier: result.name,
features: {
featureX: result.feature.deleteMessage,
featureY: result.feature.inviteLinks,
},
});
} catch (error) { return c.json(error, 500); }
});
the error reads
Argument of type '(c: Context<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }>) => Promise<Response>' is not assignable to parameter of type 'Handler<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }, HandlerTypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>>'.
Type 'Promise<Response>' is not assignable to type 'HandlerTypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>'.
Type 'Promise<Response>' is not assignable to type 'Promise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>>'.
Type 'Response' is missing the following properties from type 'TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>': data, formatts(2345)
Argument of type '(c: Context<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }>) => Promise<Response>' is not assignable to parameter of type 'Handler<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }, HandlerTypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>>'.
Type 'Promise<Response>' is not assignable to type 'HandlerTypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>'.
Type 'Promise<Response>' is not assignable to type 'Promise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>>'.
Type 'Response' is missing the following properties from type 'TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>': data, formatts(2345)
I think im returning my result wrong? it works fine, when returning it normally without my DB stuff and async/await I havent tried running it with that error.. but I kind of want to resolve it first regardless.
8 Replies
bootsy
bootsy5mo ago
Hey! Typescript enjoys yelling. I think we’d need to see more code! My initial thought is the apps.openapi is not inferring types correctly. Understanding why requires seeing how everything was set up before being wired together if you can link to a repo that’d be ideal
Phil
Phil5mo ago
Argument of type '(c: Context<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }>) => Promise<Response>' is not assignable to parameter of type 'Handler<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }, MaybePromise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>>'.
Type 'Promise<Response>' is not assignable to type 'MaybePromise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>'.
Type 'Promise<Response>' is not assignable to type 'Promise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>'.
Type 'Response' is missing the following properties from type 'TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">': data, formatts(2345)
Argument of type '(c: Context<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }>) => Promise<Response>' is not assignable to parameter of type 'Handler<Env, "/:identifier", { in: { param: { identifier?: string; }; }; out: { param: { identifier?: string; }; }; }, MaybePromise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>>'.
Type 'Promise<Response>' is not assignable to type 'MaybePromise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>'.
Type 'Promise<Response>' is not assignable to type 'Promise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>'.
Type 'Response' is missing the following properties from type 'TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">': data, formatts(2345)
Nico
Nico5mo ago
Type 'Promise<Response>' is not assignable to type 'HandlerTypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>'.
Type 'Promise<Response>' is not assignable to type 'HandlerTypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }>'.
Promise<TypedResponse<{ id?: number; identifier?: string; features?: { featureX?: boolean; featureY?: number; }; }, 200, "json">>
Nico
Nico5mo ago
GitHub
[zod-openapi] Unable to use async handler · Issue #521 · honojs/mid...
hono: 4.3.6 before @hono/zod-openapi: 0.11.1 after @hono/zod-openapi: 0.12.0 After updating from version 0.11.1 to version 0.12.0, I get a type error when using async handler. const app = new OpenA...
Nico
Nico5mo ago
throw new HTTPException(404, { message: 'Custom error message' })
throw new HTTPException(404, { message: 'Custom error message' })
Nico
Nico5mo ago
Exception - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Nico
Nico5mo ago
GitHub
GitHub - NicoPlyley/hono-error-handler
Contribute to NicoPlyley/hono-error-handler development by creating an account on GitHub.
Nico
Nico5mo ago
For anyone else looking the issue is from if (!result) return c.notFound(); You need to throw an Error instead of returning a different response directly from the controller
Want results from more Discord servers?
Add your server