chanfana icon indicating copy to clipboard operation
chanfana copied to clipboard

Docs Issue: middleware Typescript errors

Open omonk opened this issue 5 months ago • 5 comments

Docs listed here produce typescript errors - https://chanfana.pages.dev/advanced-topics-patterns#middleware-and-interceptors

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'typeof ProtectedEndpoint' is not assignable to parameter of type 'H<{ Bindings: Env; }, "/protected", BlankInput, Promise<any>>'.ts(2769)
types.d.ts(774, 5): The last overload is declared here.

Any ideas?

omonk avatar Jul 10 '25 19:07 omonk

I'm getting the same

cendyne avatar Jul 25 '25 14:07 cendyne

@cendyne im doing it like this now

const openapi = fromHono(new Hono());

openapi.use(accountMiddleware);
openapi.use(adminMiddleware);

openapi.route("/group", groupRoutes);

export default openapi;

omonk avatar Jul 26 '25 10:07 omonk

Thank you for responding! I do plan to use this method for some middleware, however some routes need to have route-specific middleware. For example, I regularly do something like this:

openapi.get("/object/:id", hasScope('object:*'), ObjectReadHandler)
openapi.put("/object/:id", hasScope('object:create'), ObjectCreateHandler)
openapi.delete("/object/:id", hasScope('object:delete'), ObjectDeleteHandler)

I've found this pattern to be very useful in hono at separating authorization from code.

cendyne avatar Jul 28 '25 21:07 cendyne

For now, I'm using as any to bypass the typescript error. I look forward to its resolution.

openapi.get("/object/:id", hasScope('object:*'), ObjectReadHandler as any)
openapi.put("/object/:id", hasScope('object:create'), ObjectCreateHandler as any)
openapi.delete("/object/:id", hasScope('object:delete'), ObjectDeleteHandler as any)

cendyne avatar Jul 28 '25 21:07 cendyne

May be we can try extending OpenAPIRoute for reusable logic but not very convenient and reusable with regular hono.

5war00p avatar Oct 10 '25 17:10 5war00p