Skip to main content

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.

tip

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:

PackageTransport
@opra/nestjs-httpHTTP (Express)
@opra/nestjs-socketioSocket.IO
@opra/nestjs-kafkaKafka
@opra/nestjs-rabbitmqRabbitMQ

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

NestJSOPRA
Module system & DI containerEnd-to-end request validation & encoding
Guards, interceptors, pipesData model type system
Lifecycle hooksMulti-protocol transport (HTTP, MQ, WS)
Application structureAPI 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