Theo's Typesafe CultTTC
Theo's Typesafe Cult16mo ago
19 replies
DGCP3

How to add 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


this is my factory method. the instance returned doesn't have ".openapi' on it so I can pass my schema returned by createRoute().
Solution
turns out you have to break it up and it works
const base = () => {
const router= new OpenAPIHono()
router.use(databaseMiddleware)
router.use(bucketMiddleware)
router.use(cacheMiddleware);
return router
};
Was this page helpful?