Skip to content

Scheduling infrastructure for absolutely everyone.

chore: Allow redirecting /docs to external docs solution (#16836)

Keith Williamsb6c6e41603d3
diff --git a/apps/api/v2/.env.example b/apps/api/v2/.env.example
index 1775086bb97..06283bdcb7b 100644
--- a/apps/api/v2/.env.example
+++ b/apps/api/v2/.env.example
@@ -26,4 +26,5 @@ WEB_APP_URL=http://localhost:3000/
CALCOM_LICENSE_KEY=
API_KEY_PREFIX=cal_
GET_LICENSE_KEY_URL="https://console.cal.com/api/license"
-IS_E2E=false
\ No newline at end of file
+IS_E2E=false
+DOCS_URL=
diff --git a/apps/api/v2/src/app.module.ts b/apps/api/v2/src/app.module.ts
index 16f55de93ad..77533de5cd8 100644
--- a/apps/api/v2/src/app.module.ts
+++ b/apps/api/v2/src/app.module.ts
@@ -1,6 +1,7 @@
import appConfig from "@/config/app";
import { CustomThrottlerGuard } from "@/lib/throttler-guard";
import { AppLoggerMiddleware } from "@/middleware/app.logger.middleware";
+import { RedirectsMiddleware } from "@/middleware/app.redirects.middleware";
import { RewriterMiddleware } from "@/middleware/app.rewrites.middleware";
import { JsonBodyMiddleware } from "@/middleware/body/json.body.middleware";
import { RawBodyMiddleware } from "@/middleware/body/raw.body.middleware";
@@ -79,6 +80,8 @@ export class AppModule implements NestModule {
.forRoutes("*")
.apply(AppLoggerMiddleware)
.forRoutes("*")
+ .apply(RedirectsMiddleware)
+ .forRoutes("/")
.apply(RewriterMiddleware)
.forRoutes("/");
}
diff --git a/apps/api/v2/src/env.ts b/apps/api/v2/src/env.ts
index 85bd516582d..e2431dd5496 100644
--- a/apps/api/v2/src/env.ts
+++ b/apps/api/v2/src/env.ts
@@ -19,6 +19,7 @@ export type Environment = {
CALCOM_LICENSE_KEY: string;
GET_LICENSE_KEY_URL: string;
API_KEY_PREFIX: string;
+ DOCS_URL: string;
};
export const getEnv = <K extends keyof Environment>(key: K, fallback?: Environment[K]): Environment[K] => {
diff --git a/apps/api/v2/src/main.ts b/apps/api/v2/src/main.ts
index 0f68d9cd4ff..8202d06592a 100644
--- a/apps/api/v2/src/main.ts
+++ b/apps/api/v2/src/main.ts
@@ -114,11 +114,14 @@ async function generateSwagger(app: NestExpressApplication<Server>) {
}
fs.writeFileSync(outputFile, JSON.stringify(document, null, 2), { encoding: "utf8" });
- SwaggerModule.setup("docs", app, document, {
- customCss: ".swagger-ui .topbar { display: none }",
- });
- logger.log(`Swagger documentation available in the "/docs" endpoint\n`);
+ if (!process.env.DOCS_URL) {
+ SwaggerModule.setup("docs", app, document, {
+ customCss: ".swagger-ui .topbar { display: none }",
+ });
+
+ logger.log(`Swagger documentation available in the "/docs" endpoint\n`);
+ }
}
run().catch((error: Error) => {
diff --git a/apps/api/v2/src/middleware/app.redirects.middleware.ts b/apps/api/v2/src/middleware/app.redirects.middleware.ts
new file mode 100644
index 00000000000..a4b12cdc520
--- /dev/null
+++ b/apps/api/v2/src/middleware/app.redirects.middleware.ts
@@ -0,0 +1,12 @@
+import { Injectable, NestMiddleware } from "@nestjs/common";
+import { Request, Response } from "express";
+
+@Injectable()
+export class RedirectsMiddleware implements NestMiddleware {
+ use(req: Request, res: Response, next: () => void) {
+ if (process.env.DOCS_URL && (req.url.startsWith("/v2/docs") || req.url.startsWith("/docs"))) {
+ return res.redirect(process.env.DOCS_URL);
+ }
+ next();
+ }
+}