From 3ba64f15f151e33a0c949f4af421f591a8e28250 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Mon, 25 Jan 2021 23:11:56 +0100 Subject: [PATCH] Swagger: Split public and private API Fixes #759 Signed-off-by: David Mehren --- src/main.ts | 18 ++++++------------ src/utils/swagger.ts | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/utils/swagger.ts diff --git a/src/main.ts b/src/main.ts index 355f60621..0af379f98 100644 --- a/src/main.ts +++ b/src/main.ts @@ -8,11 +8,11 @@ import { ValidationPipe } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { NestFactory } from '@nestjs/core'; import { NestExpressApplication } from '@nestjs/platform-express'; -import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from './app.module'; import { AppConfig } from './config/app.config'; -import { NestConsoleLoggerService } from './logger/nest-console-logger.service'; import { MediaConfig } from './config/media.config'; +import { NestConsoleLoggerService } from './logger/nest-console-logger.service'; +import { setupPrivateApiDocs, setupPublicApiDocs } from './utils/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule); @@ -23,16 +23,10 @@ async function bootstrap() { const appConfig = configService.get('appConfig'); const mediaConfig = configService.get('mediaConfig'); - const swaggerOptions = new DocumentBuilder() - .setTitle('HedgeDoc') - .setVersion('2.0-dev') - .addSecurity('token', { - type: 'http', - scheme: 'bearer', - }) - .build(); - const document = SwaggerModule.createDocument(app, swaggerOptions); - SwaggerModule.setup('apidoc', app, document); + setupPublicApiDocs(app); + if (process.env.NODE_ENV === 'development') { + setupPrivateApiDocs(app); + } app.useGlobalPipes( new ValidationPipe({ diff --git a/src/utils/swagger.ts b/src/utils/swagger.ts new file mode 100644 index 000000000..f0167992c --- /dev/null +++ b/src/utils/swagger.ts @@ -0,0 +1,39 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { INestApplication } from '@nestjs/common'; +import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import { PrivateApiModule } from '../api/private/private-api.module'; +import { PublicApiModule } from '../api/public/public-api.module'; + +export function setupPublicApiDocs(app: INestApplication) { + const publicApiOptions = new DocumentBuilder() + .setTitle('HedgeDoc Public API') + // TODO: Use real version + .setVersion('2.0-dev') + .addSecurity('token', { + type: 'http', + scheme: 'bearer', + }) + .build(); + const publicApi = SwaggerModule.createDocument(app, publicApiOptions, { + include: [PublicApiModule], + }); + SwaggerModule.setup('apidoc', app, publicApi); +} + +export function setupPrivateApiDocs(app: INestApplication) { + const privateApiOptions = new DocumentBuilder() + .setTitle('HedgeDoc Private API') + // TODO: Use real version + .setVersion('2.0-dev') + .build(); + + const privateApi = SwaggerModule.createDocument(app, privateApiOptions, { + include: [PrivateApiModule], + }); + SwaggerModule.setup('private/apidoc', app, privateApi); +}