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

[zod-openapi] Wrong return type from use #843

Open
hpohlmeyer opened this issue Nov 25, 2024 · 2 comments
Open

[zod-openapi] Wrong return type from use #843

hpohlmeyer opened this issue Nov 25, 2024 · 2 comments

Comments

@hpohlmeyer
Copy link

When using the chaining approach needed for Hono RPC with a middleware, OpenApiHono specific methods do not exist on the chain anymore after using use, because use returns Hono instead of OpenApiHono.

const app = new OpenAPIHono()
//     ^? const app: any
  .use("/*", cors())
  .route("/", orgsRouter)
  .route("/", sitesRouter)
  .doc("/doc", { // Property 'doc' does not exist on type 'Hono<{}, { "/*": {}; } | MergeSchemaPath<{ "/orgs": { $get: { input: {}; output: { orgs: { name: string; id: string; createdAt: string; updatedAt: string; }[]; }; outputFormat: "json" | "text"; status: 200; }; }; } & { "/orgs/:orgId": { ...; }; }, "/"> | MergeSchemaPath<...>, "/">'
    openapi: "3.0.0",
    info: {},
  })
  .get("/ui", swaggerUI({ url: "/doc" }));

Ideally these kind of issues would be solved by returning this from the base hono class, but we would need to pass on the modified generics to this, which is not supported by typescript yet. That means every Hono method should be repesented in the OpenAPIHono class definition as well, but with OpenAPIHono as the return type. To fix this issue it looks like we need to add the use method with the proper return type to the OpenAPIHono type declaration.

@hpohlmeyer hpohlmeyer changed the title @hono/zod-openapi returns wrong type from use [zod-openapi] returns wrong type from use Nov 25, 2024
@hpohlmeyer hpohlmeyer changed the title [zod-openapi] returns wrong type from use [zod-openapi] Wrong return type from use Nov 25, 2024
@yusukebe
Copy link
Member

Hi @hpohlmeyer

That's right, OpenAPIHono does not provide that feature due to the limitations of Hono's type definitions. It will have to be redefined in OpenAPIHono.

@askorupskyy
Copy link
Contributor

askorupskyy commented Nov 26, 2024

@hpohlmeyer the way that i do it is i separate the router and server part:

export const mainRouter = new OpenAPIHono()
  .basePath('/api/v1')
  .route('/auth', authRouter)
  .doc('/doc', {})

export type AppType = typeof mainRouter;
const app = new OpenAPIHono()
  .use(
    '*',
    cors({ origin: [process.env.DASHBOARD_URL], credentials: true })
  )
  .route('/', mainRouter);

const port = getPortFromURL(process.env.API_URL, 3000);

serve({ fetch: app.fetch, port }).on('listening', () => {
  console.log(`>>> API running on ::${port}`);
});

Hope that helps!

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

No branches or pull requests

3 participants