Merge pull request #134 from itsactuallyluna9/filter-valid-methods

Add Support for app.all()
This commit is contained in:
Marc Laventure
2024-08-03 17:01:42 -07:00
committed by GitHub
3 changed files with 45 additions and 0 deletions

View File

@@ -109,12 +109,29 @@ export const swagger = async <Path extends string = '/swagger'>(
const routes = app.getGlobalRoutes() as InternalRoute[]
if (routes.length !== totalRoutes) {
const ALLOWED_METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'TRACE']
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
if (ALLOWED_METHODS.includes(route.method) === false && route.method !== 'ALL') return
if (route.method === 'ALL') {
ALLOWED_METHODS.forEach((method) => {
registerSchemaPath({
schema,
hook: route.hooks,
method,
path: route.path,
// @ts-ignore
models: app.definitions?.type,
contentType: route.hooks.type
})
})
return
}
registerSchemaPath({
schema,

View File

@@ -219,4 +219,31 @@ describe('Swagger', () => {
expect(response.paths['/public']).not.toBeUndefined();
expect(response.paths['/hidden']).toBeUndefined();
})
it('should expand .all routes', async () => {
const app = new Elysia().use(swagger())
.all("/all", "woah")
await app.modules
const res = await app.handle(req('/swagger/json'))
expect(res.status).toBe(200)
const response = await res.json()
expect(Object.keys(response.paths['/all'])).toBeArrayOfSize(8)
})
it('should hide routes that are invalid', async () => {
const app = new Elysia().use(swagger())
.get("/valid", "ok")
.route("LOCK", "/invalid", "nope")
await app.modules
const res = await app.handle(req('/swagger/json'))
expect(res.status).toBe(200)
const response = await res.json()
expect(response.paths['/valid']).not.toBeUndefined();
expect(response.paths['/invalid']).toBeUndefined();
})
})

View File

@@ -77,6 +77,7 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => {
)
}
)
.route('LOCK', '/lock', () => 'locked')
await app.modules