UNPKG

thrilled-be-core

Version:

Core Express backend package with middleware, logging, security, and base application setup

138 lines (137 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require("../"); // Example 1: Basic BaseApp usage console.log('=== Example 1: Basic BaseApp Usage ==='); new __1.BaseApp({ name: 'My API Server', port: 3000, environment: 'development', logging: { level: 'debug', dir: './logs/example', format: 'simple', }, cors: { origin: ['http://localhost:3000', 'http://localhost:3001'], credentials: true, }, rateLimit: { windowMs: 15 * 60 * 1000, // 15 minutes max: 100, // limit each IP to 100 requests per windowMs }, }); // Example 2: Creating a custom plugin console.log('\n=== Example 2: Custom Plugin ==='); class HealthCheckPlugin extends __1.BasePlugin { name = 'health-check'; version = '1.0.0'; setup() { this.logger.info('Setting up health check plugin'); } registerRoutes(app) { app.get('/health', (req, res) => { this.logger.debug('Health check requested'); res.json({ status: 'healthy', timestamp: new Date().toISOString(), uptime: process.uptime(), }); }); app.get('/ready', (req, res) => { this.logger.debug('Readiness check requested'); res.json({ status: 'ready', timestamp: new Date().toISOString(), }); }); } } // Example 3: Plugin with dependencies console.log('\n=== Example 3: Plugin with Dependencies ==='); class DatabasePlugin extends __1.BasePlugin { name = 'database'; version = '1.0.0'; async setup() { this.logger.info('Connecting to database...'); // Simulate database connection await new Promise((resolve) => setTimeout(resolve, 100)); this.logger.info('Database connected successfully'); } registerMiddleware(app) { app.use((req, res, next) => { // Add database connection to request req.db = { connected: true, }; next(); }); } } class UserServicePlugin extends __1.BasePlugin { name = 'user-service'; version = '1.0.0'; dependencies = ['database']; // Depends on database plugin setup() { this.logger.info('Setting up user service'); } registerRoutes(app) { app.get('/users', (req, res) => { this.logger.debug('Users endpoint called'); res.json({ users: [ { id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Smith' }, ], }); }); } } // Example 4: Using the application with plugins console.log('\n=== Example 4: Complete Application Setup ==='); async function createApplication() { const app = new __1.BaseApp({ name: 'Example API', port: 3001, environment: 'development', logging: { level: 'info', dir: './logs/api', format: 'json', }, }); // Register plugins app.use(new HealthCheckPlugin()); app.use(new DatabasePlugin()); app.use(new UserServicePlugin()); // Add a route directly to the Express app app.getApp().get('/', (req, res) => { res.json({ message: 'Welcome to the Example API', plugins: app.listPlugins(), }); }); console.log('Registered plugins:', app.listPlugins()); console.log('Plugin load order:', app.getPluginManager().getLoadOrder()); // In a real application, you would call app.start() here // await app.start(); return app; } // Example 5: Plugin management console.log('\n=== Example 5: Plugin Management ==='); // eslint-disable-next-line @typescript-eslint/no-unused-vars async function pluginManagement() { const app = await createApplication(); console.log('All plugins:', app.listPlugins()); // Disable a plugin app.disablePlugin('health-check'); console.log('After disabling health-check:', app.listPlugins()); // Re-enable the plugin app.enablePlugin('health-check'); console.log('After re-enabling health-check:', app.listPlugins()); } // Run examples (commented out to avoid execution during import) // createApplication().then(() => console.log('Application created successfully')); // pluginManagement().then(() => console.log('Plugin management completed')); console.log('\nBaseApp examples completed. Uncomment the last lines to run the examples.'); pluginManagement().then(() => console.log('Plugin management completed'));