feat: exclude routes with details.hide: true from OpenAPI/swagger file

This commit is contained in:
Kravets
2024-08-02 17:44:10 +03:00
parent ca5bdfac02
commit 54c38a9085
4 changed files with 23 additions and 2 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -112,6 +112,8 @@ export const swagger = async <Path extends string = '/swagger'>(
totalRoutes = routes.length
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
registerSchemaPath({

View File

@@ -200,4 +200,23 @@ describe('Swagger', () => {
const response = await res.json()
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();
})
})