H
Hono6mo ago
mattp

@hono/zod-openapi: How to write OpenAPI doc for middleware responses?

I have an auth middleware which will check if the api token is provided in the request header, so it will respond with 401 or pass. How to make sure every protected route will be applied with the 401 response?
app.openapi(
createRoute({
middleware: [auth],
method: 'get',
path: '/my-endpoint',
security: [{ Authorization: [] }],
request: {
// ...
},
responses: {
200: {
// ...
},
// 401
},
}),
async (ctx) => {
// do something
}
)
app.openapi(
createRoute({
middleware: [auth],
method: 'get',
path: '/my-endpoint',
security: [{ Authorization: [] }],
request: {
// ...
},
responses: {
200: {
// ...
},
// 401
},
}),
async (ctx) => {
// do something
}
)
1 Reply
mattp
mattpOP6mo ago
oh i figured it out. must explicitly given the response status to prevent type error.
app.openapi(
createRoute({
middleware: [auth],
method: 'get',
path: '/my-endpoint',
security: [{ Authorization: [] }],
request: {
// ...
},
responses: {
200: {
// ...
},
401: {
// ...
}
},
}),
async (ctx) => {
// do something
return ctx.json({ ok: true }, 200) // <- explict
}
)
app.openapi(
createRoute({
middleware: [auth],
method: 'get',
path: '/my-endpoint',
security: [{ Authorization: [] }],
request: {
// ...
},
responses: {
200: {
// ...
},
401: {
// ...
}
},
}),
async (ctx) => {
// do something
return ctx.json({ ok: true }, 200) // <- explict
}
)
Want results from more Discord servers?
Add your server