NestJS-Flex is a modular library designed to enhance NestJS applications by providing a foundation for state management, performance optimization, and flexibility while seamlessly integrating with other tools. It brings to the NestJS ecosystem principles inspired by efficient development practices, ensuring scalable and maintainable applications.
- State Management: Effortlessly manage your application's state with a centralized service.
- WebSocket Gateway: Enhance real-time communication with built-in WebSocket functionality.
- Middleware Support: Apply cross-cutting concerns globally with the FlexibilityMiddleware.
- Performance Monitoring: Measure request duration for better performance insights using the PerformanceInterceptor.
- Modular Architecture: Easily integrate and extend NestJS-Flex across your projects.
Install the nestjs-flex
package:
npm install nestjs-flex
Here's a quick example to get you started:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
console.log('Starting the application...');
await app.listen(3000);
}
bootstrap();
Add the FlexModule to your application:
import { Module } from '@nestjs/common';
import { FlexModule } from 'nestjs-flex';
@Module({
imports: [FlexModule],
})
export class AppModule {}
Use StateService to manage application state:
POST /flex/state
Body: { "key": "exampleKey", "value": "exampleValue" }
GET /flex/state?key=exampleKey
Response: { "key": "exampleKey", "value": "exampleValue" }
Send a WebSocket message and receive a response:
-
Example WebSocket Endpoint:
ws://localhost:3000/ws
-
Message Response:
Echo: {your_message}
- FlexibilityMiddleware: Logs requests and adds custom headers.
- PerformanceInterceptor: Logs request durations to optimize performance.
This project is licensed under the MIT License. See the LICENSE file for more details.