Export the type of the handler of app.openapi() for better code organization
Which middleware is the feature for?
@hono/zod-openapi
What is the feature you are proposing?
I would like to define routes and handlers in separate files for better organization and register them in a central file where I declare the app object.
Let's say I have the file routes/shorturls.ts:
export const createShortUrlRoute = createRoute({})
// What's the type of createShortUrlHandler ?
export const createShortUrlHandler = async (c) => {
const data = c.req.valid('json');
}
and the main file index.ts where I declare the app and I register the routes:
import { OpenAPIHono } from "@hono/zod-openapi";
export const app = new OpenAPIHono();
// I would like to register the routes here
app.openapi(createShortUrlRoute, createShortUrlHandler)
The problem is the type of createShortUrlHandler, if I use a normal Handler type, I loose the request and response validation types, I tried also to reuse the types declared in the sourcecode of @hono/zod-openapi but basically I need to copy 100s of lines of code.
Proposed solution
Can you expose the type of the handler so I can do something like this:
import { OpenAPIHandler } from "@hono/zod-openapi";
export const createShortUrlRoute = createRoute({})
export const createShortUrlHandler: OpenAPIHandler<typeof createShortUrlRoute> = async (c) => {
const data = c.req.valid('json');
}
Actual solution
Today I have my OpenAPIHono app defined in a dedicated app.ts file and I have multiple route files that import app and register the routes and finally all route files are imported into index.ts as import './routes/shorturls.ts'
Hi @lucafaggianelli
Thank you for the proposal. Is this like a createHandlers() exported from hono/factory in the plain hono case?
https://hono.dev/docs/helpers/factory#factory-createhandlers
hi @yusukebe yes something like the createHanders would do, or even just a Typescript type OpenAPIHandler, the only difference with the plain createHanders is that in the case of @hono/zod-openapi I think it must include the route definition, something like:
createHanders<typeof routeDefinition>(async (c) => {})
PS: I'm happy to contribute to this feature with a PR
@lucafaggianelli
I like the following API!
createHanders<typeof routeDefinition>(async (c) => {})
Can you create a PR for it? Thanks!