mirror of
https://github.com/zoriya/elysia-swagger.git
synced 2025-12-06 00:36:10 +00:00
feat: exclude routes with details.hide: true from OpenAPI/swagger file
This commit is contained in:
@@ -110,8 +110,10 @@ export const swagger = async <Path extends string = '/swagger'>(
|
|||||||
|
|
||||||
if (routes.length !== totalRoutes) {
|
if (routes.length !== totalRoutes) {
|
||||||
totalRoutes = routes.length
|
totalRoutes = routes.length
|
||||||
|
|
||||||
routes.forEach((route: InternalRoute) => {
|
routes.forEach((route: InternalRoute) => {
|
||||||
|
if (route.hooks?.detail?.hide === true) return
|
||||||
|
// TODO: route.hooks?.detail?.hide !== false add ability to hide: false to prevent excluding
|
||||||
if (excludeMethods.includes(route.method)) return
|
if (excludeMethods.includes(route.method)) return
|
||||||
|
|
||||||
registerSchemaPath({
|
registerSchemaPath({
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ export const registerSchemaPath = ({
|
|||||||
models: Record<string, TSchema>
|
models: Record<string, TSchema>
|
||||||
}) => {
|
}) => {
|
||||||
if (hook) hook = deepClone(hook)
|
if (hook) hook = deepClone(hook)
|
||||||
|
|
||||||
const contentType = hook?.type ?? [
|
const contentType = hook?.type ?? [
|
||||||
'application/json',
|
'application/json',
|
||||||
'multipart/form-data',
|
'multipart/form-data',
|
||||||
|
|||||||
@@ -200,4 +200,23 @@ describe('Swagger', () => {
|
|||||||
const response = await res.json()
|
const response = await res.json()
|
||||||
expect(response.paths).toContainKey('/id/{id}')
|
expect(response.paths).toContainKey('/id/{id}')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should hide routes with hide = true from paths', async () => {
|
||||||
|
const app = new Elysia().use(swagger())
|
||||||
|
.get("/public", "omg")
|
||||||
|
.guard({
|
||||||
|
detail: {
|
||||||
|
hide: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.get("/hidden", "ok")
|
||||||
|
|
||||||
|
await app.modules
|
||||||
|
|
||||||
|
const res = await app.handle(req('/swagger/json'))
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
const response = await res.json()
|
||||||
|
expect(response.paths['/public']).not.toBeUndefined();
|
||||||
|
expect(response.paths['/hidden']).toBeUndefined();
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user