NestJS 初始化和传递请求上下文的最佳实践是什么

2024-04-25

我有一个全局拦截器,需要初始化我自己的请求上下文 DTO,并且我希望可以在处理当前请求的控制器中访问此 DTO。

到目前为止我找到的解决方案是创建 Request 范围内的可注入 RequestContext 类:

import {
    Injectable,
    Scope
} from '@nestjs/common';
import { Request } from 'express';
import { IncomingHttpHeaders } from 'http';

@Injectable({ scope: Scope.REQUEST })
export class RequestContext {
    public headers: IncomingHttpHeaders;
    ....

    initialize(request: Request) {
        this.headers = request.headers;
        .....
    }
}

并将此类注入拦截器:

import {
    NestInterceptor,
    ExecutionContext,
    CallHandler,
    Injectable,
    Inject
} from '@nestjs/common';
import { Request } from 'express';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { RequestContext } from '../dto';

@Injectable()
export class RequestContextInterceptor implements NestInterceptor {
    constructor(
        @Inject(RequestContext)
        protected requestContext: RequestContext
    ) { }

    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        const request = context.switchToHttp().getRequest<Request>();
        this.requestContext.initialize(request);

        return next.handle()
            .pipe(
                tap(() => {
                    // decorate response
                }));
    }
}

然后将这个RequestContext注入到每个控制器中......

import {
    Controller,
    UseInterceptors,
    Inject,
    Get
} from '@nestjs/common';
import { BaseMicroserviceController } from '../core/base/base-microservice.controller';
import { RequestContext } from '../dto';
import { DispatchService } from '../services';

@Controller('api/v1/example')
export class ExampleController extends BaseMicroserviceController {

    constructor (
        @Inject(RequestContext)
        protected requestContext: RequestContext,
        protected dispatcheService: DispatchService
    ) {
        super(dispatcheService);
    }

    @Get()
    test() {
        return 'test';
    }
}

恕我直言,有很多解决方法可以实现这个简单的功能 另外,我有这篇文章描述了为什么使用基于范围的注入不好:https://guxi.me/posts/why-you-should-avoid-using-request-scope-in​​jection-in-nest-js/ https://guxi.me/posts/why-you-should-avoid-using-request-scope-injection-in-nest-js/

我的服务将会非常庞大​​,有大量的控制器和大量的可注入服务。根据这篇文章 - 我的服务在性能和内存使用方面将无法扩展。

我的问题是如何在 NestJS 中实现我需要的功能以及最佳实践是什么? 另一个“额外问题” - RequestContext 类有initialize接收明确请求并解析它的方法。我不喜欢它,我希望此类的每个属性都是只读的,并通过调用构造函数以传统方式初始化此类request对象...我怎样才能实现它@Inject战略?


如果您想在不使用请求范围提供程序的情况下执行此操作,您可以通过使用附加数据丰富请求对象来简化很多工作。从技术上讲,请求对象始终可用于入站 HTTP 交互,无论您使用什么注入范围。你可以放弃RequestContext完全,只需将您想要的任何附加数据添加到拦截器内的请求对象中即可。

 intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const request = context.switchToHttp().getRequest<Request>();
    const customRequestContext = initialize(request); // whatever you need to do to build this

    request.customRequestContext = customRequestContext;

    return next.handle();
}

使用以下命令可以轻松地在任何控制器中访问该值定制装饰器 https://docs.nestjs.com/custom-decorators:

export const RequestContext = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    return request.customRequestContext;
  },
);

然后在任何控制器中,您可以使用它来访问该值:

@Get()
async findOne(@RequestContext() requestContext: RequestContextInterface) {
  // do whatever you need to do with it in your controllers
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NestJS 初始化和传递请求上下文的最佳实践是什么 的相关文章

随机推荐