Skip to content

Commit

Permalink
refactor: change swagger operationId output
Browse files Browse the repository at this point in the history
.
  • Loading branch information
compojoom committed Nov 22, 2024
1 parent a4cc91f commit 1c7f19e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/app.provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { INestApplication } from '@nestjs/common';
import { VersioningType } from '@nestjs/common';
import type { SwaggerDocumentOptions } from '@nestjs/swagger';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { NestFactory } from '@nestjs/core';
import { IConfigurationService } from '@/config/configuration.service.interface';
Expand All @@ -26,7 +27,28 @@ function configureSwagger(app: INestApplication): void {
.setVersion(configurationService.get('about.version') ?? '')
.build();

const document = SwaggerModule.createDocument(app, config);
const options: SwaggerDocumentOptions = {
operationIdFactory: (
controllerKey: string,
methodKey: string,
version: string | undefined,
) => {
const capitalize = (str: string): string =>
str ? str[0].toUpperCase() + str.slice(1) : '';
const decapitalize = (str: string): string =>
str ? str[0].toLowerCase() + str.slice(1) : '';
const versionPart = version ? capitalize(version) : '';
const controllerPart = decapitalize(
controllerKey
.replace('Controller', '')
.replace(new RegExp(versionPart, 'i'), ''),
);
const methodPart = capitalize(methodKey);
return `${controllerPart}${methodPart}${versionPart}`;
},
};

const document = SwaggerModule.createDocument(app, config, options);
SwaggerModule.setup('api', app, document, {
customfavIcon: '/favicon.png',
customSiteTitle: 'Safe Client Gateway',
Expand Down

0 comments on commit 1c7f19e

Please sign in to comment.