From 54c38a90851ba383d69abca8a8f48fa79fab755d Mon Sep 17 00:00:00 2001 From: Kravets <57632712+kravetsone@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:44:10 +0300 Subject: [PATCH] feat: exclude routes with details.hide: true from OpenAPI/swagger file --- bun.lockb | Bin 373111 -> 373111 bytes src/index.ts | 4 +++- src/utils.ts | 2 +- test/index.test.ts | 19 +++++++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bun.lockb b/bun.lockb index b2fb5dfe24d93eb3b13f452535eb865d5fd8762f..833d9fec569df120aded5db42ee865e5469b6ceb 100755 GIT binary patch delta 164 zcmezVNbLI~u?-FEg5C@a4B`w74J%z47Wn&@ce4K%VH9lMDbv1Fh7pLF zw(pc-mYlD+oy;GWFgkAcjbo{io_?*8C6E_pq;65No>9Ac70Y(@D%JpV E0F#a{EdT%j delta 164 zcmezVNbLI~u?-FEf)NZ14B`w74J%z47Wr3~ce4K%VU%m$Dbv1Fh7pLF zw(pc-mYl;W2viSJCpZ1}4rWPdOCVPi$klo4tdx82LMsPjoS~kfk)A1o-*o<+%#w@| z(=~T8YjXev^$dV&x5w;c{;-76Z@X_CON}(Iovi{`Mz<(g&uIF|N|wNO^(vO_>Q$@( F<^WO2E~x+j diff --git a/src/index.ts b/src/index.ts index 2408a58..4b6cff6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -110,8 +110,10 @@ export const swagger = async ( if (routes.length !== totalRoutes) { 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({ diff --git a/src/utils.ts b/src/utils.ts index 814bf20..cb55b24 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -118,7 +118,7 @@ export const registerSchemaPath = ({ models: Record }) => { if (hook) hook = deepClone(hook) - + const contentType = hook?.type ?? [ 'application/json', 'multipart/form-data', diff --git a/test/index.test.ts b/test/index.test.ts index b84c549..7add09b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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(); + }) })