Skip to main content

OpraHttpModule

NestJS dynamic module that wires OPRA's HTTP adapter into a NestJS application. Registers OPRA controllers as NestJS controllers, sets up middleware for context creation, and applies the exception filter.

import { OpraHttpModule } from '@opra/nestjs-http';

forRoot(options)​

Synchronous registration. Call from your root AppModule:

import { Module } from '@nestjs/common';
import { OpraHttpModule } from '@opra/nestjs-http';
import { Customer } from './models/customer.js';
import { CustomersController } from './controllers/customers.controller.js';

@Module({
imports: [
OpraHttpModule.forRoot({
name: 'MyApi',
basePath: '/api',
types: [Customer],
controllers: [CustomersController],
}),
],
})
export class AppModule {}

Options (OpraHttpModule.ModuleOptions)​

OptionTypeDescription
namestringAPI name included in the generated schema.
descriptionstringOptional API description.
basePathstringURL prefix for all OPRA routes.
scopestringValidation scope applied to every request and response.
typesType[]Data types to register on the ApiDocument.
referencesobjectType references from other documents.
infoobjectAPI info metadata (version, contact, license…).
controllersType[]NestJS / OPRA controller classes.
providersProvider[]Additional NestJS providers.
exportsany[]Providers to export from the module.
importsany[]Modules to import.
globalbooleanRegister the module globally.
tokenanyCustom injection token for the module.
schemaIsPublicbooleanExpose the $schema endpoint without authentication.
interceptors(InterceptorFunction | IHttpInterceptor | Type<IHttpInterceptor>)[]Interceptors applied to every request.
loggerLoggerCustom NestJS logger instance.

forRootAsync(options)​

Asynchronous registration — use when configuration depends on injected providers (e.g. ConfigService):

import { ConfigModule, ConfigService } from '@nestjs/config';

OpraHttpModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
name: 'MyApi',
basePath: config.get('API_BASE_PATH'),
types: [Customer],
controllers: [CustomersController],
}),
})

Options (OpraHttpModule.AsyncModuleOptions)​

Extends BaseModuleOptions (all non-API fields from ModuleOptions) and adds:

OptionTypeDescription
injectany[]Providers to inject into useFactory.
useFactory(...args) => ApiConfig | Promise<ApiConfig>Factory that returns the API configuration.

See also​