Skip to content

Commit

Permalink
Add swagger spec file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Mar 5, 2024
1 parent 1d0fe38 commit b4858f2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
34 changes: 34 additions & 0 deletions api/src/create-swagger-specification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as fs from 'fs';
import * as crypto from 'crypto';
import * as process from 'process';

function hashContent(content: string): string {
return crypto.createHash('sha256').update(content).digest('hex');
}

export async function createOrUpdateSwaggerSpec(document: any): Promise<void> {
if (process.env.NODE_ENV !== 'development') {
console.log(
'Skipping Swagger spec update: Not in development environment.',
);
return;
}
const documentString: string = JSON.stringify(document);
const currentHash: string = hashContent(documentString);

const specPath: string = './swagger-spec.json';
if (fs.existsSync(specPath)) {
const existingSpec: string = fs.readFileSync(specPath, 'utf8');
const existingHash: string = hashContent(existingSpec);

if (currentHash !== existingHash) {
console.log('Swagger spec has changed. Updating...');
fs.writeFileSync(specPath, documentString);
} else {
console.log('No changes in Swagger spec.');
}
} else {
console.log('Swagger spec does not exist. Creating...');
fs.writeFileSync(specPath, documentString);
}
}
3 changes: 3 additions & 0 deletions api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as compression from 'compression';
import { JwtAuthGuard } from 'guards/jwt-auth.guard';
import { useContainer } from 'class-validator';
import { SensitiveInfoGuard } from 'guards/sensitive-info.guard';
import { createOrUpdateSwaggerSpec } from 'create-swagger-specification';

async function bootstrap(): Promise<void> {
const logger: Logger = new Logger('bootstrap');
Expand Down Expand Up @@ -35,6 +36,8 @@ async function bootstrap(): Promise<void> {
);
SwaggerModule.setup('/swagger', app, swaggerDocument);

await createOrUpdateSwaggerSpec(swaggerDocument);

app.useGlobalPipes(
new ValidationPipe({
forbidUnknownValues: true,
Expand Down
1 change: 1 addition & 0 deletions api/swagger-spec.json

Large diffs are not rendered by default.

0 comments on commit b4858f2

Please sign in to comment.