From d07c33376e4b5d4d3d51c72a76cbe4ca646f04d7 Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Fri, 18 Oct 2024 00:57:35 +0700 Subject: [PATCH 1/7] fix: use requestPath for /json Fixes #161. This will also work when you don't specify any prefix to Elysia but uses Traefik Middlewares to strip /api prefix too. --- src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 621270e..584d11a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { Elysia, type InternalRoute } from 'elysia' +import { resolve } from 'node:path'; import { SwaggerUIRender } from './swagger' import { ScalarRender } from './scalar' @@ -64,9 +65,9 @@ export const swagger = async ( const app = new Elysia({ name: '@elysiajs/swagger' }) - app.get(path, function documentation() { + app.get(path, function documentation({ path: reqPath }) { const combinedSwaggerOptions = { - url: `/${relativePath}/json`, + url: resolve(reqPath, '/json'), dom_id: '#swagger-ui', ...swaggerOptions } @@ -83,7 +84,7 @@ export const swagger = async ( const scalarConfiguration: ReferenceConfiguration = { spec: { ...scalarConfig.spec, - url: `/${relativePath}/json` + url: resolve(reqPath, '/json') }, ...scalarConfig, // so we can showcase the elysia theme From 849400c71679fcd68a7edd3d5ce95d7d0fc70591 Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Sat, 19 Oct 2024 19:09:34 +0700 Subject: [PATCH 2/7] fix: consider Elysia prefix and external prefixes --- src/index.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 584d11a..ae0e972 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,5 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { Elysia, type InternalRoute } from 'elysia' -import { resolve } from 'node:path'; import { SwaggerUIRender } from './swagger' import { ScalarRender } from './scalar' @@ -10,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. @@ -61,13 +61,17 @@ export const swagger = async ( ...documentation.info } - const relativePath = path.startsWith('/') ? path.slice(1) : path - const app = new Elysia({ name: '@elysiajs/swagger' }) + const prefixedPath = join(app.config.prefix ?? "/", 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) - app.get(path, function documentation({ path: reqPath }) { const combinedSwaggerOptions = { - url: resolve(reqPath, '/json'), + url: relativePath, dom_id: '#swagger-ui', ...swaggerOptions } @@ -84,7 +88,7 @@ export const swagger = async ( const scalarConfiguration: ReferenceConfiguration = { spec: { ...scalarConfig.spec, - url: resolve(reqPath, '/json') + url: relativePath }, ...scalarConfig, // so we can showcase the elysia theme @@ -108,7 +112,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[] From aa746ab22eddf900717b90014e7a57dd82496346 Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Sat, 19 Oct 2024 19:14:43 +0700 Subject: [PATCH 3/7] fix: add /json in documentation --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index ae0e972..8495f75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,7 +68,7 @@ export const swagger = async ( // 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) + const relativePath = join(extPrefix, prefixedPath, "json") const combinedSwaggerOptions = { url: relativePath, From 44f80b94b73fe68c97454cc6de6d2fc1b671743d Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Sat, 19 Oct 2024 19:55:43 +0700 Subject: [PATCH 4/7] fix: properly exclude docs path --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) ) From 192195db0c667066a069f489bff8cd3c324306ec Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Sat, 19 Oct 2024 20:08:39 +0700 Subject: [PATCH 5/7] fix: fix path in openapi schema --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8495f75..5b41b05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -137,7 +137,7 @@ export const swagger = async ( schema, hook: route.hooks, method, - path: route.path, + path: join(extPrefix, prefixedPath, route.path), // @ts-ignore models: app.definitions?.type, contentType: route.hooks.type @@ -150,7 +150,7 @@ export const swagger = async ( schema, hook: route.hooks, method: route.method, - path: route.path, + path: join(extPrefix, prefixedPath, route.path), // @ts-ignore models: app.definitions?.type, contentType: route.hooks.type From 41f996065f2536d588b40191d4d66cf33f01e1cf Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Sat, 19 Oct 2024 20:14:06 +0700 Subject: [PATCH 6/7] fix: should be appPrefix --- src/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5b41b05..ce35d27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,7 +62,8 @@ export const swagger = async ( } const app = new Elysia({ name: '@elysiajs/swagger' }) - const prefixedPath = join(app.config.prefix ?? "/", path) + 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 @@ -137,7 +138,7 @@ export const swagger = async ( schema, hook: route.hooks, method, - path: join(extPrefix, prefixedPath, route.path), + path: join(extPrefix, appPrefix, route.path), // @ts-ignore models: app.definitions?.type, contentType: route.hooks.type @@ -150,7 +151,7 @@ export const swagger = async ( schema, hook: route.hooks, method: route.method, - path: join(extPrefix, prefixedPath, route.path), + path: join(extPrefix, appPrefix, route.path), // @ts-ignore models: app.definitions?.type, contentType: route.hooks.type From 8665d6e9f552d1ed0b532c0dcf8e4dc0f13874ad Mon Sep 17 00:00:00 2001 From: Hazmi Alfarizqi Date: Sat, 30 Nov 2024 01:44:19 +0700 Subject: [PATCH 7/7] chore: lockfile --- bun.lockb | Bin 88783 -> 88783 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/bun.lockb b/bun.lockb index 21dca3102c9d18f3e57705f9623a303869017c09..244dba9d39102f1801aa6ab554bf38430a299a66 100755 GIT binary patch delta 599 zcmWmA$t%QR7{Kv&)Gt$%BFja{ePW7`nIYK@4pTUBP-NeYy(WLd@8~f0eH&x!+Z^o6 zSV|Rx#2UL3oZ=k&*ux!e@Qoj6p@RnPxW@yoaE&dz;{z_d;!Ox&u}--0f+y(Zw#Qn% zfhtECN>G7PG@%(as7DK0(TG}9q6!VDLp6#~Bquv{nhVN~102DDEc_w?iP*t5KCyvK z6d+%=IsMXOsc1wY4l#&8B-9Az%PzSOk&Rt^ArJ9LMGBJSU{^?3E|r5!nBW5&j&X)t WORlR$Qff7!kt($+!gBTOF8l)*{BD&1 delta 599 zcmWmA$t%QR7{Kv&)GvxsWVuX|yHJFTA=wTN3MUSV?CaP^P5y@8(P8ZSGGmNwtOxrt zmJ&%uiNQ=GOO|ZkIedD)Z|~uGd#z^4YL*Ncc1bUUP{>*KFlm_*rbfF(q3fc$(Su&} zp&tVn#1Mutf>DfN921y?0Y*&0glU*DgIUaB9t*Hw5le7CVMPcJu__xM)H-jDJ_uF_ z5r9Ah!4qD%;qDHvc*75V(Fq%_@r`$Uz=;jG@Prp+Lxn`BagS4+3E?L$hCv3qXxC8Lp?NTKqH#aj25(_4O+CzsSdTuPUYecpV-6}l97Z<9K(u3 z>|hr~D3;e8{+ZEK3}O+7ctju)QBd+_dCV_d;Rt&uL>khOf&^LV3<=Gr3XlUmd~u5t XoMX*Y;M7W9k>OF{?j