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

@@ -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({

View File

@@ -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',

View File

@@ -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();
})
}) })