diff --git a/bun.lockb b/bun.lockb index 21dca31..244dba9 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/index.ts b/src/index.ts index 621270e..ce35d27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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. @@ -60,13 +61,18 @@ export const swagger = async ( ...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 } @@ -83,7 +89,7 @@ export const swagger = async ( const scalarConfiguration: ReferenceConfiguration = { spec: { ...scalarConfig.spec, - url: `/${relativePath}/json` + url: relativePath }, ...scalarConfig, // so we can showcase the elysia theme @@ -107,7 +113,12 @@ export const swagger = async ( } } ) - }).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[] @@ -127,7 +138,7 @@ export const swagger = async ( schema, hook: route.hooks, method, - path: route.path, + path: join(extPrefix, appPrefix, route.path), // @ts-ignore models: app.definitions?.type, contentType: route.hooks.type @@ -140,7 +151,7 @@ export const swagger = async ( 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 diff --git a/src/utils.ts b/src/utils.ts index 4b69103..fc8fc76 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -317,7 +317,7 @@ export const filterPaths = ( const newPaths: Record = {} // exclude docs path and OpenAPI json path - const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) => + const excludePaths = [`${docsPath}`, `${docsPath}/json`].map((p) => normalize(p) )