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

fix: use requestPath for OpenAPI spec in Swagger Options #162

Open
wants to merge 6 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
27 changes: 19 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { filterPaths, registerSchemaPath } from './utils'
import type { OpenAPIV3 } from 'openapi-types'
import type { ReferenceConfiguration } from '@scalar/types'
import type { ElysiaSwaggerConfig } from './types'
import { join } from 'pathe';

/**
* Plugin for [elysia](https://github.com/elysiajs/elysia) that auto-generate Swagger page.
Expand Down Expand Up @@ -60,13 +61,18 @@ export const swagger = async <Path extends string = '/swagger'>(
...documentation.info
}

const relativePath = path.startsWith('/') ? path.slice(1) : path

const app = new Elysia({ name: '@elysiajs/swagger' })
const appPrefix = app.config.prefix ?? "/"
const prefixedPath = join(appPrefix, path)

app.get(path, function documentation(request) {
// External Prefix, if the app is behind a reverse proxy
// For example in Traefik, the prefix is set in the header `X-Forwarded-Prefix`
const extPrefix = request.headers["x-forwarded-prefix"] ?? "/"
const relativePath = join(extPrefix, prefixedPath, "json")

app.get(path, function documentation() {
const combinedSwaggerOptions = {
url: `/${relativePath}/json`,
url: relativePath,
dom_id: '#swagger-ui',
...swaggerOptions
}
Expand All @@ -83,7 +89,7 @@ export const swagger = async <Path extends string = '/swagger'>(
const scalarConfiguration: ReferenceConfiguration = {
spec: {
...scalarConfig.spec,
url: `/${relativePath}/json`
url: relativePath
},
...scalarConfig
}
Expand All @@ -104,7 +110,12 @@ export const swagger = async <Path extends string = '/swagger'>(
}
}
)
}).get(path === '/' ? '/json' : `${path}/json`, function openAPISchema() {
}).get(path === '/' ? '/json' : `${path}/json`, function openAPISchema(request) {
// External Prefix, if the app is behind a reverse proxy
// For example in Traefik, the prefix is set in the header `X-Forwarded-Prefix`
const extPrefix = request.headers["x-forwarded-prefix"] ?? "/"
const relativePath = join(extPrefix, prefixedPath)

// @ts-expect-error Private property
const routes = app.getGlobalRoutes() as InternalRoute[]

Expand All @@ -124,7 +135,7 @@ export const swagger = async <Path extends string = '/swagger'>(
schema,
hook: route.hooks,
method,
path: route.path,
path: join(extPrefix, appPrefix, route.path),
// @ts-ignore
models: app.definitions?.type,
contentType: route.hooks.type
Expand All @@ -137,7 +148,7 @@ export const swagger = async <Path extends string = '/swagger'>(
schema,
hook: route.hooks,
method: route.method,
path: route.path,
path: join(extPrefix, appPrefix, route.path),
// @ts-ignore
models: app.definitions?.type,
contentType: route.hooks.type
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export const filterPaths = (
const newPaths: Record<string, any> = {}

// exclude docs path and OpenAPI json path
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
const excludePaths = [`${docsPath}`, `${docsPath}/json`].map((p) =>
normalize(p)
)

Expand Down