Skip to content

Commit

Permalink
chore: Allow redirecting /docs to external docs solution (#16836)
Browse files Browse the repository at this point in the history
* chore: Allow redirecting /docs to external docs solution

* Added DOCS_URL to .env.example
  • Loading branch information
keithwillcode authored Sep 26, 2024
1 parent e4dc2d0 commit b6c6e41
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion apps/api/v2/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
IS_E2E=false
DOCS_URL=
3 changes: 3 additions & 0 deletions apps/api/v2/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -79,6 +80,8 @@ export class AppModule implements NestModule {
.forRoutes("*")
.apply(AppLoggerMiddleware)
.forRoutes("*")
.apply(RedirectsMiddleware)
.forRoutes("/")
.apply(RewriterMiddleware)
.forRoutes("/");
}
Expand Down
1 change: 1 addition & 0 deletions apps/api/v2/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] => {
Expand Down
11 changes: 7 additions & 4 deletions apps/api/v2/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
12 changes: 12 additions & 0 deletions apps/api/v2/src/middleware/app.redirects.middleware.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit b6c6e41

Please sign in to comment.