Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for isolated routes to solve multiple instances problem #171

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 38 additions & 40 deletions example/index2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,42 @@ import { swagger } from '../src/index'
import { plugin } from './plugin'

const app = new Elysia({
// aot: false
// aot: false
})
.use(
swagger({
documentation: {
info: {
title: 'Elysia',
version: '0.6.10'
},
tags: [
{
name: 'Test',
description: 'Hello'
}
],
security: [
{JwtAuth: []}
],
components: {
schemas: {
User: {
description: 'string'
}
},
securitySchemes: {
JwtAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Enter JWT Bearer token **_only_**'
}
}
}
},
swaggerOptions: {
persistAuthorization: true
},
})
)
.use(plugin)
.listen(3000)
.use(
swagger({
documentation: {
info: {
title: 'Elysia',
version: '0.6.10'
},
tags: [
{
name: 'Test',
description: 'Hello'
}
],
security: [{ JwtAuth: [] }],
components: {
schemas: {
User: {
description: 'string'
}
},
securitySchemes: {
JwtAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'Enter JWT Bearer token **_only_**'
}
}
}
},
swaggerOptions: {
persistAuthorization: true
}
})
)
.use(plugin)
.listen(3000)
35 changes: 35 additions & 0 deletions example/multiple-instancies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Elysia } from 'elysia'
import { swagger } from '../src/index'

const firstApp = new Elysia({ prefix: '/first' })

firstApp
.get('/first-route', () => {
return 'first route!'
})
.use(
swagger({
path: '/first-doc',
routes: firstApp.routes
})
)

const secondApp = new Elysia({ prefix: '/second' })

secondApp
.get('/second-route', () => {
return 'second route!'
})
.use(
swagger({
path: '/second-doc',
routes: secondApp.routes
})
)

const app = new Elysia({
// aot: false
})
.use(firstApp)
.use(secondApp)
.listen(3000)
45 changes: 33 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export const swagger = async <Path extends string = '/swagger'>(
theme = `https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css`,
autoDarkMode = true,
excludeMethods = ['OPTIONS'],
excludeTags = []
excludeTags = [],
routes
}: ElysiaSwaggerConfig<Path> = {
provider: 'scalar',
scalarVersion: 'latest',
Expand Down Expand Up @@ -96,27 +97,47 @@ export const swagger = async <Path extends string = '/swagger'>(
theme,
stringifiedSwaggerOptions,
autoDarkMode
)
: ScalarRender(info, scalarVersion, scalarConfiguration, scalarCDN),
)
: ScalarRender(
info,
scalarVersion,
scalarConfiguration,
scalarCDN
),
{
headers: {
'content-type': 'text/html; charset=utf8'
}
}
)
}).get(path === '/' ? '/json' : `${path}/json`, function openAPISchema() {
// @ts-expect-error Private property
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) => {
const allRoutes = routes
? [...routes, ...app.routes]
: // @ts-expect-error Private property
(app.getGlobalRoutes() as InternalRoute[])

if (allRoutes.length !== totalRoutes) {
const ALLOWED_METHODS = [
'GET',
'PUT',
'POST',
'DELETE',
'OPTIONS',
'HEAD',
'PATCH',
'TRACE'
]
totalRoutes = allRoutes.length

allRoutes.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 (
ALLOWED_METHODS.includes(route.method) === false &&
route.method !== 'ALL'
)
return

if (route.method === 'ALL') {
ALLOWED_METHODS.forEach((method) => {
Expand Down
Loading