H
Hono4w ago
DGCP3

Adding swagger to hono API

I want to add a swagger doc to my API and when I add middleware to any new OpenAPIHono instance the .openapi method disappears.
/**
* Creates a base instance of Hono with middleware.
* @returns {Hono} The base instance of Hono.
*/
const base = () => new OpenAPIHono().use(databaseMiddleware).use(bucketMiddleware).use(cacheMiddleware);

/**
* Represents a public route.
* @returns {Hono}
*/
export const publicHonoRoute = () => base(); // public route

/**
* Marks a route as protected.
* @returns {Hono}
*/
export const protectedHonoRoute = () => base().use(protect).use(typesenseMiddleware); // protected route
/**
* Creates a base instance of Hono with middleware.
* @returns {Hono} The base instance of Hono.
*/
const base = () => new OpenAPIHono().use(databaseMiddleware).use(bucketMiddleware).use(cacheMiddleware);

/**
* Represents a public route.
* @returns {Hono}
*/
export const publicHonoRoute = () => base(); // public route

/**
* Marks a route as protected.
* @returns {Hono}
*/
export const protectedHonoRoute = () => base().use(protect).use(typesenseMiddleware); // protected route
this is my factory method. the instance returned doesn't have .openapi, so I can pass my schema returned by createRoute().
2 Replies
icanflyit
icanflyit4w ago
I believe hono openapi has a note about how the handle middleware hono instances, but the just is that it doesn’t change the return types of any middleware so if it’s your own middleware, you should define it using new OpenAPIHono instead of new Hono if it’s not your middleware, one workaround is you can define your routes in a separate route const routes = new OpenAPIHono()… and then in your main app new OpenAPIHono().use(middleware).route("", routes)
DGCP3
DGCP33w ago
it turns out you have to break it up and it works perfectly
const base = () => {
const router= new OpenAPIHono()
router.use(databaseMiddleware)
router.use(bucketMiddleware)
router.use(cacheMiddleware);
return router
};
const base = () => {
const router= new OpenAPIHono()
router.use(databaseMiddleware)
router.use(bucketMiddleware)
router.use(cacheMiddleware);
return router
};
Want results from more Discord servers?
Add your server