From 241b7e81652e1a7e9e408a0208a4c707906247fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Wed, 28 May 2025 19:14:55 +0200 Subject: [PATCH] chore: simplify serving of static openapi assets (#10046) Use existing public folder from frontend to serve opeanapi static assets. We also benefit from frontend image optimizations --- .dockerignore | 1 - .../public/openapi-static}/Beta.svg | 0 .../public/openapi-static}/Enterprise.svg | 0 package.json | 3 +-- src/lib/services/openapi-service.ts | 23 ++++--------------- 5 files changed, 6 insertions(+), 21 deletions(-) rename {openapi-static => frontend/public/openapi-static}/Beta.svg (100%) rename {openapi-static => frontend/public/openapi-static}/Enterprise.svg (100%) diff --git a/.dockerignore b/.dockerignore index 34eddb631b..201e064967 100644 --- a/.dockerignore +++ b/.dockerignore @@ -9,7 +9,6 @@ !LICENSE !README.md !frontend -!openapi-static frontend/node_modules !.yarn/** !.yarnrc.yml diff --git a/openapi-static/Beta.svg b/frontend/public/openapi-static/Beta.svg similarity index 100% rename from openapi-static/Beta.svg rename to frontend/public/openapi-static/Beta.svg diff --git a/openapi-static/Enterprise.svg b/frontend/public/openapi-static/Enterprise.svg similarity index 100% rename from openapi-static/Enterprise.svg rename to frontend/public/openapi-static/Enterprise.svg diff --git a/package.json b/package.json index 55c614729d..41c2ccd727 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ ], "files": [ "dist", - "openapi-static", "frontend/build", "frontend/build/*", "frontend/build/**/*", @@ -50,7 +49,7 @@ "db-migrate": "db-migrate --migrations-dir ./src/migrations", "lint": "biome check .", "lint:fix": "biome check . --write", - "local:package": "del-cli --force build && mkdir build && cp -r dist openapi-static CHANGELOG.md LICENSE README.md package.json build", + "local:package": "del-cli --force build && mkdir build && cp -r dist CHANGELOG.md LICENSE README.md package.json build", "build:watch": "tsc -w", "prepare": "husky && yarn --cwd ./frontend install && if [ ! -d ./dist ]; then yarn build; fi", "test": "PORT=4243 vitest run", diff --git a/src/lib/services/openapi-service.ts b/src/lib/services/openapi-service.ts index 36d2ee9aab..def7d8ed06 100644 --- a/src/lib/services/openapi-service.ts +++ b/src/lib/services/openapi-service.ts @@ -1,10 +1,5 @@ import openapi, { type IExpressOpenApi } from '@wesleytodd/openapi'; -import { - type Express, - type RequestHandler, - type Response, - static as expressStatic, -} from 'express'; +import type { Express, RequestHandler, Response } from 'express'; import type { IUnleashConfig } from '../types/option.js'; import { createOpenApiSchema, @@ -44,14 +39,15 @@ export class OpenApiService { validPath(op: ApiOperation): RequestHandler { const { beta, enterpriseOnly, ...rest } = op; - + const { baseUriPath = '' } = this.config.server ?? {}; + const openapiStaticAssets = `${baseUriPath}/openapi-static`; const betaBadge = beta - ? `![Beta](${this.docsStaticsPath()}/Beta.svg) This is a beta endpoint and it may change or be removed in the future. + ? `![Beta](${openapiStaticAssets}/Beta.svg) This is a beta endpoint and it may change or be removed in the future. ` : ''; const enterpriseBadge = enterpriseOnly - ? `![Unleash Enterprise](${this.docsStaticsPath()}/Enterprise.svg) **Enterprise feature** + ? `![Unleash Enterprise](${openapiStaticAssets}/Enterprise.svg) **Enterprise feature** ` : ''; @@ -68,15 +64,6 @@ export class OpenApiService { useDocs(app: Express): void { app.use(this.api); app.use(this.docsPath(), this.api.swaggerui()); - app.use( - this.docsStaticsPath(), - expressStatic('openapi-static', { index: false }), - ); - } - - docsStaticsPath(): string { - const { baseUriPath = '' } = this.config.server ?? {}; - return `${baseUriPath}/docs/static`; } docsPath(): string {