|
| 1 | +/** |
| 2 | + * Standalone script that boots the NestJS app just long enough to generate |
| 3 | + * and write the OpenAPI JSON document to docs/openapi.json, then exits. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * npx ts-node -r tsconfig-paths/register scripts/export-openapi.ts |
| 7 | + * npm run openapi:export |
| 8 | + */ |
| 9 | + |
| 10 | +import { NestFactory } from "@nestjs/core"; |
| 11 | +import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; |
| 12 | +import { writeFileSync, mkdirSync } from "fs"; |
| 13 | +import { join } from "path"; |
| 14 | +import { AppModule } from "../src/app.module"; |
| 15 | + |
| 16 | +async function exportOpenApi() { |
| 17 | + // Silence NestJS bootstrap logs — we only want the artefact output |
| 18 | + const app = await NestFactory.create(AppModule, { logger: false, abortOnError: false }); |
| 19 | + |
| 20 | + const config = new DocumentBuilder() |
| 21 | + .setTitle("alian-structure Backend API") |
| 22 | + .setDescription( |
| 23 | + "Comprehensive API documentation for alian-structure backend services including " + |
| 24 | + "agent management, oracle submissions, compute operations, and audit trails.", |
| 25 | + ) |
| 26 | + .setVersion("1.0.0") |
| 27 | + .setContact("alian-structure Team", "https://alian-structure.com", "api@alian-structure.com") |
| 28 | + .setLicense("Apache 2.0", "https://www.apache.org/licenses/LICENSE-2.0") |
| 29 | + .addServer("http://localhost:3001", "Development Server") |
| 30 | + .addServer("https://api.alian-structure.com", "Production Server") |
| 31 | + .addBearerAuth( |
| 32 | + { type: "http", scheme: "bearer", bearerFormat: "JWT", name: "JWT", description: "Enter JWT token", in: "header" }, |
| 33 | + "JWT-auth", |
| 34 | + ) |
| 35 | + .addApiKey( |
| 36 | + { type: "apiKey", name: "X-API-Key", in: "header", description: "API key for service-to-service communication" }, |
| 37 | + "api-key", |
| 38 | + ) |
| 39 | + .addTag("Health", "Liveness, readiness, and startup probes for Kubernetes orchestration") |
| 40 | + .addTag("Authentication", "User authentication and authorization") |
| 41 | + .addTag("Enhanced Authentication & KYC", "Enhanced auth with 2FA and KYC flows") |
| 42 | + .addTag("Users", "User management operations") |
| 43 | + .addTag("Oracle", "Oracle data submissions and payload management") |
| 44 | + .addTag("Price Feed", "Aggregated on-chain price data") |
| 45 | + .addTag("Audit", "Audit trail and logging") |
| 46 | + .addTag("Profile", "User profile management") |
| 47 | + .addTag("Info", "API health and meta-information") |
| 48 | + .build(); |
| 49 | + |
| 50 | + const document = SwaggerModule.createDocument(app, config, { |
| 51 | + deepScanRoutes: true, |
| 52 | + operationIdFactory: (_controllerKey: string, methodKey: string) => methodKey, |
| 53 | + }); |
| 54 | + |
| 55 | + // Write JSON |
| 56 | + const outDir = join(__dirname, "..", "docs"); |
| 57 | + mkdirSync(outDir, { recursive: true }); |
| 58 | + |
| 59 | + const jsonPath = join(outDir, "openapi.json"); |
| 60 | + writeFileSync(jsonPath, JSON.stringify(document, null, 2), "utf8"); |
| 61 | + console.log(`✅ OpenAPI JSON written to ${jsonPath}`); |
| 62 | + |
| 63 | + await app.close(); |
| 64 | + process.exit(0); |
| 65 | +} |
| 66 | + |
| 67 | +exportOpenApi().catch((err) => { |
| 68 | + console.error("Failed to export OpenAPI spec:", err); |
| 69 | + process.exit(1); |
| 70 | +}); |
0 commit comments