Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chaining middleware with .openapi creates type errors #930

Open
Rick-Phoenix opened this issue Jan 9, 2025 · 5 comments
Open

Chaining middleware with .openapi creates type errors #930

Rick-Phoenix opened this issue Jan 9, 2025 · 5 comments
Labels

Comments

@Rick-Phoenix
Copy link

Rick-Phoenix commented Jan 9, 2025

Which middleware has the bug?

@hono/zod-openapi

What version of the middleware?

0.18.3

What version of Hono are you using?

4.6.15

What runtime/platform is your app running on? (with version if possible)

Node 20.17.0

What steps can reproduce the bug?

The readme for zod-openapi indicates this method for using middleware with OpenApiHono:

import { prettyJSON } from 'hono/pretty-json'
import { cache } from 'hono/cache'

app.use(route.getRoutingPath(), prettyJSON(), cache({ cacheName: 'my-cache' }))
app.openapi(route, handler)

However, this does not work and creates type errors.

So in my case I am doing this:

export type AppBindings = {
  Variables: {
    logger: PinoLogger;
    session: Session | null;
    user: User | null;
  };
  Bindings: typeof env;
};

export type AppOpenAPI = OpenAPIHono<AppBindings>;

export function createRouter() {
  return new OpenAPIHono<AppBindings>({
    strict: false,
    defaultHook,
  });
}

export const authRouter = createRouter().use(prettyJSON())
  .openapi(login, loginHandler)
  .openapi(signup, signupHandler)
  .openapi(logout, logoutHandler);

And this is creating a series of type errors

image

As the .openapi method is not present after chaining with .use.

The only way to make it work is to keep separate instances of OpenApiHono and not chain .use and .openapi on the same instance.
However, this method hinders flexibility in structuring routes.

@yusukebe
Copy link
Member

yusukebe commented Jan 10, 2025

Hi @Rick-Phoenix

 However, this does not work and creates type errors.

That works correctly and does not throw a type error in my environment:

CleanShot 2025-01-10 at 09 40 21@2x

Or can I reproduce it?

@Rick-Phoenix
Copy link
Author

That's strange. The only difference between your code and mine is that as the handler to my routes I'm using a custom handler type which is just a RouteHandler type with the RouteConfig and AppBindings attached to it.

export type AppRouteHandler<R extends RouteConfig> = RouteHandler<
  R,
  AppBindings
>;

But I tried using an inline handler and I get the same result.
image

https://github.com/Rick-Phoenix/odinproj-odinbook this is the repo where I am having this issue.
All of the routing-related files are in apps/server/src/routes.

It should be pretty easy to understand as all the parts involved in the process are the ones I've described in my original post, but let me know if I can clarify the issue any further.

@yusukebe
Copy link
Member

@Rick-Phoenix Thank you for your explanation and for sharing the repo.

I've tried it. Does this work for you? It does not throw a type error:

Image

@Rick-Phoenix
Copy link
Author

It does not throw a type error if called that way but then the middleware doesn't get called because the routes get matched beforehand.

The only workaround I have found for this is to apply all of my global middleware before appending the openapi routes to the hono instance like in the picture below.

Image

@yusukebe
Copy link
Member

@Rick-Phoenix

It does not throw a type error if called that way but then the middleware doesn't get called because the routes get matched beforehand.

The only workaround I have found for this is to apply all of my global middleware before appending the openapi routes to the hono instance like in the picture below.

Ah, yes. Exactly, you are right. I think your way is better currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants