@opra/nestjs-http
Version:
Opra NestJS Http Module
44 lines (43 loc) • 1.43 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { Injectable } from '@nestjs/common';
import { HttpRequest, HttpResponse } from '@opra/http';
import { OpraHttpNestjsAdapter } from './opra-http-nestjs-adapter.js';
/**
* OpraMiddleware
*
* NestJS middleware that creates an OPRA context (HttpContext) for each HTTP request
* and adds it to the request.
*/
let OpraMiddleware = class OpraMiddleware {
opraAdapter;
constructor(opraAdapter) {
this.opraAdapter = opraAdapter;
}
/**
* Processes requests and creates the OPRA context.
*
* @param req - Express request object.
* @param res - Express response object.
* @param next - Function that calls the next middleware.
*/
use(req, res, next) {
const request = HttpRequest.create(req);
const response = HttpResponse.create(res);
this.opraAdapter
.createContext(request, response)
.then(async (context) => {
// @ts-ignore
context.platform = req.route ? 'express' : 'fastify';
req.opraContext = context;
await this.opraAdapter
.emitAsync('createContext', context)
.then(() => next());
})
.catch(next);
}
};
OpraMiddleware = __decorate([
Injectable(),
__metadata("design:paramtypes", [OpraHttpNestjsAdapter])
], OpraMiddleware);
export { OpraMiddleware };