Skip to content

Commit

Permalink
feat(cat-fostering-api): configure nestjs/swagger to auto generate …
Browse files Browse the repository at this point in the history
…OpenAPI specs
  • Loading branch information
getlarge committed May 28, 2024
1 parent ecc7949 commit 4c672d5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
8 changes: 8 additions & 0 deletions apps/cat-fostering-api/src/app/environment-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export class EnvironmentVariables {

@SecretValue()
ORY_ACTION_API_KEY: string;

/**
* @see tools/ory/mappings.ts
*/
@Expose()
@IsString()
@IsOptional()
ORY_SESSION_COOKIE_NAME?: string = 'ory_kratos_session';
}

export function validateEnvironmentVariables(
Expand Down
50 changes: 49 additions & 1 deletion apps/cat-fostering-api/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// trick static analyzers
import 'pg';

import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import {
ExpressAdapter,
NestExpressApplication,
} from '@nestjs/platform-express';
import {
DocumentBuilder,
SwaggerCustomOptions,
SwaggerModule,
} from '@nestjs/swagger';
import { Logger, LoggerErrorInterceptor } from 'nestjs-pino';
import { writeFile } from 'node:fs/promises';
import { join, resolve } from 'node:path';

import { AppModule } from './app/app.module';
import { EnvironmentVariables } from './app/environment-variables';

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(
Expand All @@ -28,7 +37,46 @@ async function bootstrap() {
app.useLogger(logger);
app.useGlobalInterceptors(new LoggerErrorInterceptor());

const port = process.env.PORT || 3000;
const configService =
app.get<ConfigService<EnvironmentVariables, true>>(ConfigService);

// Generate OpenAPI spec
const docBuilder = new DocumentBuilder()
.setTitle('Cat Fostering API')
.setDescription('Cat Fostering OpenAPI specifications')
.setVersion('1.0')
.addApiKey(
{
type: 'apiKey',
name: 'x-ory-api-key',
in: 'header',
},
'ory-action'
)
.addBearerAuth()
.addCookieAuth(configService.get('ORY_SESSION_COOKIE_NAME'))
.addTag('users')
.addTag('cat-profiles')
.addTag('fostering')
.build();

const document = SwaggerModule.createDocument(app, docBuilder);

await writeFile(
resolve(join('apps', 'cat-fostering-api', 'openapi.json')),
JSON.stringify(document, null, 2),
'utf-8'
);

// Swagger UI
const customOptions: SwaggerCustomOptions = {
swaggerOptions: {
persistAuthorization: true,
},
};
SwaggerModule.setup('open-api', app, document, customOptions);

const port = configService.get('PORT', { infer: true });
await app.listen(port);
logger.log(
`🚀 Application is running on: http://localhost:${port}/${globalPrefix}`
Expand Down
13 changes: 13 additions & 0 deletions apps/cat-fostering-api/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ module.exports = {
assets: ['./src/assets'],
optimization: false,
outputHashing: 'none',
transformers: [
{
name: '@nestjs/swagger/plugin',
options: {
classValidatorShim: true,
dtoFileNameSuffix: ['-entities.ts', '.dto.ts'],
controllerFileNameSuffix: ['.controller.ts'],
dtoKeyOfComment: 'description',
controllerKeyOfComment: 'description',
introspectComments: true,
},
},
],
}),
],
};

0 comments on commit 4c672d5

Please sign in to comment.