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
/**
* 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 ```ts const base = () => { const router= new OpenAPIHono() router.use(databaseMiddleware)...
Jump to solution
13 Replies
Neto
Neto4w ago
DGCP3
DGCP34w ago
export const authRoute = publicHonoRoute()
.basePath("/auth")
// POST /auth/login
.post(
"/login",
validate("json", LoginSchema),
async (c) => {
const { userName, Password } = c.req.valid("json");
}
)
export const authRoute = publicHonoRoute()
.basePath("/auth")
// POST /auth/login
.post(
"/login",
validate("json", LoginSchema),
async (c) => {
const { userName, Password } = c.req.valid("json");
}
)
this is how I consume the factory method. so how can I add the swagger to the returned instance if the instance doesn't have .openapi on it? by the way, you actually suggested creating the factory methods a while ago
Neto
Neto4w ago
me?
DGCP3
DGCP34w ago
yes, it was a good solution.
Neto
Neto4w ago
probably its been a long time oh, i think i remember now let me try i don't think that will work for swagger it needs a stable reference for creating stuff
DGCP3
DGCP34w ago
is there any other alternative that is not Postman that hono has integration
Neto
Neto4w ago
you can use the hono client thingy via monorepo
Neto
Neto4w ago
RPC - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
Neto
Neto4w ago
idk if you can the whole composition with builder for the client setup
DGCP3
DGCP34w ago
I'm using RPC, but I need to provide API documentation for the security team, and swagger was the default because the company I work for uses c# as primary language for the backend and they expect swagger
Neto
Neto4w ago
idk if composing is good for this case then
DGCP3
DGCP34w ago
ya, I will just import functions on every route that uses them
Solution
DGCP3
DGCP33w ago
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
};
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