Skip to content

Commit bf58e93

Browse files
authored
Merge pull request #134 from itsactuallyluna9/filter-valid-methods
Add Support for app.all()
2 parents 5303809 + d6fda56 commit bf58e93

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,29 @@ export const swagger = async <Path extends string = '/swagger'>(
109109
const routes = app.getGlobalRoutes() as InternalRoute[]
110110

111111
if (routes.length !== totalRoutes) {
112+
const ALLOWED_METHODS = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'TRACE']
112113
totalRoutes = routes.length
113114

114115
routes.forEach((route: InternalRoute) => {
115116
if (route.hooks?.detail?.hide === true) return
116117
// TODO: route.hooks?.detail?.hide !== false add ability to hide: false to prevent excluding
117118
if (excludeMethods.includes(route.method)) return
119+
if (ALLOWED_METHODS.includes(route.method) === false && route.method !== 'ALL') return
120+
121+
if (route.method === 'ALL') {
122+
ALLOWED_METHODS.forEach((method) => {
123+
registerSchemaPath({
124+
schema,
125+
hook: route.hooks,
126+
method,
127+
path: route.path,
128+
// @ts-ignore
129+
models: app.definitions?.type,
130+
contentType: route.hooks.type
131+
})
132+
})
133+
return
134+
}
118135

119136
registerSchemaPath({
120137
schema,

test/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,31 @@ describe('Swagger', () => {
219219
expect(response.paths['/public']).not.toBeUndefined();
220220
expect(response.paths['/hidden']).toBeUndefined();
221221
})
222+
223+
it('should expand .all routes', async () => {
224+
const app = new Elysia().use(swagger())
225+
.all("/all", "woah")
226+
227+
await app.modules
228+
229+
const res = await app.handle(req('/swagger/json'))
230+
expect(res.status).toBe(200)
231+
const response = await res.json()
232+
expect(Object.keys(response.paths['/all'])).toBeArrayOfSize(8)
233+
})
234+
235+
it('should hide routes that are invalid', async () => {
236+
const app = new Elysia().use(swagger())
237+
.get("/valid", "ok")
238+
.route("LOCK", "/invalid", "nope")
239+
240+
await app.modules
241+
242+
const res = await app.handle(req('/swagger/json'))
243+
expect(res.status).toBe(200)
244+
const response = await res.json()
245+
expect(response.paths['/valid']).not.toBeUndefined();
246+
expect(response.paths['/invalid']).toBeUndefined();
247+
248+
})
222249
})

test/validate-schema.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ it('returns a valid Swagger/OpenAPI json config for many routes', async () => {
7777
)
7878
}
7979
)
80+
.route('LOCK', '/lock', () => 'locked')
8081

8182
await app.modules
8283

0 commit comments

Comments
 (0)