-
Notifications
You must be signed in to change notification settings - Fork 278
Description
Which middleware is the feature for?
@hono/zod-openapi
What is the feature you are proposing?
I originally posted this on StackOverflow and plan to provide an answer if I can find a solution here. https://stackoverflow.com/questions/79715942/how-to-include-hono-jwt-auth-middleware-responses-in-hono-zod-openapi
I have an API built using Hono and the @hono/zod-openapi. I've integrated hono/jwt auth middleware.
My auth middleware is a thin wrapper around hono/jwt in order to supply secrets.
import { jwt } from 'hono/jwt'
import { env } from 'hono/adapter'
import { type ContextType } from '../context'
export default (c: ContextType, next: () => Promise<void>) => {
const { JWT_SECRET } = env(c)
const jwtMiddleware = jwt({ secret: JWT_SECRET })
return jwtMiddleware(c, next)
}For a protected route I'm applying the auth middleware as follows.
app.use('/protected/*', authMiddleware)
app.route('/protected', protectedRouter)However, I also need to create an OpenAPI definition for the 401 response that each route in the protectedRouter could return. This gives a type error since zod-openapi is unable to infer the error response from the route because it is not being explicitly returned from the route itself.
router.openapi(
createRoute({
method: 'get',
path: '/',
responses: {
200: {
description: 'List all items',
content: {
'application/json': {
schema: responseSchema(Item),
},
},
},
401: {
description: 'Unauthorized',
content: {
'application/json': {
schema: ErrorSchema,
},
},
},
},
}),
async (c) => { // <-- type error here
const db = c.get('db')
const res = await db.select().from(items)
return c.json({ data: res })
}
)How can I include hono/jwt auth middleware responses into my hono/zod-openapi schema?