Skip to main content

OpraMiddleware

NestJS middleware that creates an OPRA HttpContext for each incoming HTTP request and attaches it to the request object as req.opraContext. Downstream guards, interceptors, and handlers can read this context to access OPRA's parsed parameters, typed body, and response utilities.

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

Implements: NestMiddleware

OpraHttpModule registers this middleware automatically on all routes — you do not need to apply it manually.


How it works​

For each request, OpraMiddleware:

  1. Creates an HttpRequest and HttpResponse wrapper around the raw Express/Fastify objects.
  2. Calls opraAdapter.createContext(request, response) to produce an HttpContext.
  3. Sets req.opraContext = context.
  4. Calls next() so the NestJS pipeline continues.

If context creation fails (e.g. the route is not an OPRA operation), req.opraContext remains unset and the request is handled by NestJS normally.


See also​