Last active
October 11, 2025 16:05
-
-
Save iTrooz/94b2254f808fbe6186b8b375eef0e3a5 to your computer and use it in GitHub Desktop.
Revisions
-
iTrooz revised this gist
Oct 11, 2025 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // Help: https://stackoverflow.com/questions/72852736/generating-swagger-json-file-without-running-nest-js-server // NestJS 11 import * as path from 'path'; import { writeFileSync } from 'fs'; import { DataType, newDb } from 'pg-mem'; -
iTrooz created this gist
Oct 11, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ // Help: https://stackoverflow.com/questions/72852736/generating-swagger-json-file-without-running-nest-js-server import * as path from 'path'; import { writeFileSync } from 'fs'; import { DataType, newDb } from 'pg-mem'; import { DataSource } from 'typeorm'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { Test } from '@nestjs/testing'; import { AppModule } from '../src/app.module'; async function dumpSwagger() { // Setup in-memory Postgres const db = newDb(); db.public.registerFunction({ name: 'current_database', args: [], returns: DataType.text, implementation: () => 'localdb', }); db.public.registerFunction({ name: 'version', args: [], returns: DataType.text, implementation: () => '1', }); const datasource = (await db.adapters.createTypeormDataSource({ type: 'postgres', autoLoadEntities: true, synchronize: true, })) as DataSource; await datasource.initialize(); await datasource.synchronize(); const moduleFixture = await Test.createTestingModule({ imports: [AppModule], }) .overrideProvider(DataSource) .useValue(datasource) .compile(); let app = moduleFixture.createNestApplication(); // Create Swagger const config = new DocumentBuilder() .setTitle('NestJS App') .setDescription('NestJS App description') .setVersion('1.0') .build(); const document = SwaggerModule.createDocument(app, config); await app.close(); return document; } dumpSwagger().then(document => { const jsonContent = JSON.stringify(document, null, 2); const outputFilePath = process.argv[2]; if (outputFilePath) { // Write to specified file const resolvedPath = path.resolve(process.cwd(), outputFilePath); writeFileSync(resolvedPath, jsonContent, 'utf8'); console.log(`Swagger JSON dumped to ${resolvedPath}`); } else { // Dump to stdout console.log(jsonContent); } }).catch(err => { console.error(err); process.exit(1); });