@kenniy/godeye-data-contracts
Version:
Enterprise-grade base repository architecture for GOD-EYE microservices with zero overhead and maximum code reuse
137 lines (136 loc) • 6.16 kB
JavaScript
;
/**
* GOD-EYE Application Bootstrap
* One-line setup for standardized NestJS microservice configuration
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.bootstrap = bootstrap;
exports.bootstrapGodsEyeApp = bootstrapGodsEyeApp;
const core_1 = require("@nestjs/core");
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const validation_1 = require("../core/validation");
/**
* Bootstrap a GOD-EYE microservice with standardized configuration
*
* @param appModule - The root application module
* @param config - Bootstrap configuration options
* @returns Promise<INestApplication> - The configured NestJS application
*/
async function bootstrap(appModule, config) {
const logger = new common_1.Logger('GOD-EYE Bootstrap');
try {
const app = await core_1.NestFactory.create(appModule);
// Set global prefix
if (config.globalPrefix) {
app.setGlobalPrefix(config.globalPrefix);
}
// Configure CORS
const corsEnabled = config.corsEnabled ?? config.cors?.enabled ?? true;
if (corsEnabled) {
const corsOptions = {
origin: config.cors?.origins || true,
credentials: config.cors?.credentials ?? true
};
app.enableCors(corsOptions);
}
// Configure global validation pipe
const validationOptions = {
whitelist: config.validation?.whitelist ?? true,
forbidNonWhitelisted: config.validation?.forbidNonWhitelisted ?? true,
transform: config.validation?.transform ?? true
};
app.useGlobalPipes(new validation_1.ValidationPipe(validationOptions));
// Configure Swagger documentation
const swaggerEnabled = config.enableSwagger ?? config.swagger?.enabled ?? true;
if (swaggerEnabled) {
let swaggerConfigBuilder = new swagger_1.DocumentBuilder()
.setTitle(config.swagger?.title || `${config.serviceName} API`)
.setDescription(config.swagger?.description ||
`${config.serviceName} microservice built with GOD-EYE Data Contracts`)
.setVersion(config.swagger?.version || '1.0.0')
.addBearerAuth();
// Add external documentation URL if provided
if (config.swagger?.docUrl) {
swaggerConfigBuilder = swaggerConfigBuilder.setExternalDoc('External Documentation', config.swagger.docUrl);
}
const swaggerConfig = swaggerConfigBuilder.build();
const document = swagger_1.SwaggerModule.createDocument(app, swaggerConfig);
const swaggerPath = config.swagger?.path || 'docs';
// Enhanced Swagger UI options with increased limits and better UX
const swaggerOptions = {
swaggerOptions: {
maxDisplayRequestSize: config.swagger?.maxDisplayRequestSize || 10000,
maxDisplayResponseSize: config.swagger?.maxDisplayResponseSize || 10000,
docExpansion: 'list', // Show endpoints collapsed
filter: true, // Enable search
showRequestHeaders: true, // Show request headers
deepLinking: true, // Enable deep linking
displayRequestDuration: true, // Show request duration
tryItOutEnabled: true, // Enable try it out by default
supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'], // Enable all methods
requestInterceptor: undefined, // Allow request interception
responseInterceptor: undefined, // Allow response interception
},
customCss: `
.swagger-ui .topbar { display: none; }
.swagger-ui .info { margin: 20px 0; }
.swagger-ui .scheme-container { margin: 20px 0; }
.swagger-ui .opblock .opblock-summary { cursor: pointer; }
.swagger-ui .parameters-col_description p,
.swagger-ui .parameters-col_description div {
max-height: 200px;
overflow-y: auto;
word-break: break-word;
}
.swagger-ui .response-col_description {
max-height: 300px;
overflow-y: auto;
}
.swagger-ui .model-box {
max-height: 400px;
overflow-y: auto;
}
`,
customSiteTitle: `${config.serviceName} API Documentation`,
...config.swagger?.customOptions
};
swagger_1.SwaggerModule.setup(swaggerPath, app, document, swaggerOptions);
logger.log(`📚 Swagger documentation available at: http://localhost:${config.port || 3000}/${swaggerPath}`);
if (config.swagger?.docUrl) {
logger.log(`📖 External documentation: ${config.swagger.docUrl}`);
}
}
// Execute custom beforeStart hook
if (config.beforeStart) {
await config.beforeStart(app);
}
// Start the application
const port = config.port ?? 3000;
await app.listen(port);
logger.log(`🚀 ${config.serviceName} service running on: http://localhost:${port}`);
if (config.globalPrefix) {
logger.log(`🌐 API available at: http://localhost:${port}/${config.globalPrefix}`);
}
return app;
}
catch (error) {
logger.error('❌ Bootstrap failed:', error);
process.exit(1);
}
}
/**
* Legacy bootstrap function for backward compatibility
* @deprecated Use bootstrap() instead
*/
async function bootstrapGodsEyeApp(options) {
const logger = new common_1.Logger('Bootstrap (Legacy)');
logger.warn('⚠️ bootstrapGodsEyeApp is deprecated. Use bootstrap() instead.');
await bootstrap(options.module, {
serviceName: options.appName,
port: options.port,
globalPrefix: options.globalPrefix,
enableSwagger: options.enableSwagger,
corsEnabled: options.enableCors
});
}