NestJS Integration
OPRA is designed to work alongside NestJS. Rather than replacing NestJS's module system, dependency injection, or lifecycle hooks, OPRA plugs into them — your controllers are registered as standard NestJS controllers, your services are injected as usual, and OPRA adds the contract layer on top.
NestJS is the recommended way to build OPRA applications.
How it fits together
NestJS AppModule
└── OpraHttpModule.forRoot(...)
├── types: [Customer, Order, ...] ← data model types
├── controllers: [CustomersController] ← OPRA controllers (NestJS controllers)
└── providers: [CustomersService] ← injected as usual
OPRA provides a dedicated module for each supported transport:
| Package | Transport |
|---|---|
@opra/nestjs-http | HTTP (Express) |
@opra/nestjs-socketio | Socket.IO |
@opra/nestjs-kafka | Kafka |
@opra/nestjs-rabbitmq | RabbitMQ |
Setup
import { Module } from '@nestjs/common';
import { OpraHttpModule } from '@opra/nestjs-http';
import { CustomersController } from './customers/customers.controller.js';
import { CustomersService } from './customers/customers.service.js';
import * as models from './models/index.js';
@Module({
imports: [
OpraHttpModule.forRoot({
name: 'MyApi',
info: { title: 'My API', version: '1.0' },
types: Object.values(models),
controllers: [CustomersController],
providers: [CustomersService],
}),
],
})
export class AppModule {}
Controllers declared in controllers are standard NestJS controllers — they receive injected dependencies through the constructor in the normal way:
@HttpController({ path: 'customers' })
export class CustomersController {
constructor(private readonly service: CustomersService) {}
@HttpOperation.Entity.FindMany(Customer)
async findMany(ctx: HttpContext) {
const { options } = await MongoAdapter.parseRequest(ctx);
return this.service.for(ctx).findMany(options);
}
}
What NestJS brings, what OPRA adds
| NestJS | OPRA |
|---|---|
| Module system & DI container | End-to-end request validation & encoding |
| Guards, interceptors, pipes | Data model type system |
| Lifecycle hooks | Multi-protocol transport (HTTP, MQ, WS) |
| Application structure | API schema generation & publication |
Neither framework interferes with the other. NestJS guards run before OPRA's request parsing; NestJS interceptors wrap the full OPRA execution. You can use both freely.
→ NestJS Setup · HTTP Module · Kafka Module · RabbitMQ Module · Socket.IO Module