Nest: REQUEST object from core, used for Scope Request Providers is not available

Created on 8 Apr 2019  路  9Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I am trying to implement a functionality with some request providers, I am following this example in the doc https://docs.nestjs.com/fundamentals/injection-scopes#request-provider

import { Injectable, Scope, Inject } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';

@Injectable({ scope: Scope.REQUEST })
export class CatsService {
  constructor(@Inject(REQUEST) private readonly request: Request) {}
}

However, I cannot import the REQUEST object. It seems like it does not exist in the core of library

image2

Expected behavior


The REQUEST object should be imported correctly

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: 6.0.5

"dependencies": {
    "@nestjs/common": "^6.0.5",
    "@nestjs/core": "^6.0.5",



For Tooling issues:
- Node version:  8.15.1  
- Platform:  Mac 

Others:

needs clarification

All 9 comments

Is here
"import { REQUEST } from "@nestjs/core/router";"
instead of?

I tried using the same in my Validator Constraint but it didn't work.

I wish there was a way to pass Request Params to the Validator as Constraints. I am unable to find one... had to use a work around 馃槥

@ackuser It is there:
https://github.com/nestjs/nest/blob/master/packages/core/router/index.ts

Are you sure that your @nestjs/x packages were updated to 6.x.x?

@kamilmysliwiec

I am unable to get this to work. Is this the correct way?

This is UserEmailAlreadyExists.ts

@ValidatorConstraint({ name: 'userEmailAlreadyExist', async: true })
@Injectable({ scope: Scope.REQUEST })
export class UserEmailAlreadyExists implements ValidatorConstraintInterface {
  constructor(
    private readonly userRepository: UsersRepository,
    @Inject(REQUEST) private readonly request: Request,
  ) {}

  async validate(email: string, args: ValidationArguments): Promise<boolean> {
    console.log(this.userRepository); // undefined
    console.log(this.request); // undefined
  }
}

This is users.module.ts

@Module({
  imports: [
    MongooseModule.forFeature([...]),
    forwardRef(() => AuthModule),
  ],
  controllers: [UsersController],
  providers: [UsersRepository, UsersService, UserEmailAlreadyExists],
  exports: [UsersRepository, UsersService],
})

In package.json

"dependencies": {
  "@nestjs/common": "^6.0.0",
  "@nestjs/core": "^6.0.0",
  "@nestjs/jwt": "^6.0.0",
  "@nestjs/mongoose": "^6.0.0",
  "@nestjs/passport": "^6.0.0",
  "@nestjs/platform-express": "^6.0.0",
  "@nestjs/typeorm": "^6.0.0",
  "class-transformer": "^0.2.0",
  "class-validator": "^0.9.1",
}

You cannot use request scoped providers with 3rd-party libraries (like class-validator). This is a framework feature exclusively.

@kamilmysliwiec Could you please explain why it is not possible? Even based on the solution here https://github.com/nestjs/nest/issues/528 ?

Because it's a framework feature :) 3rd-party libraries may use different DI systems that have nothing to do with Nest DI (e.g. class-validator). We can work around it for simple use-cases, not for more sophisticated ones (as request-scoped providers) unfortunately.

@vkartaviy I thought of a workaround for cases where you need specific stuff from the request inside your validation components, see my comment on this issue: https://github.com/nestjs/nest/issues/528#issuecomment-497020970

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

menme95 picture menme95  路  3Comments

rlesniak picture rlesniak  路  3Comments

cojack picture cojack  路  3Comments

janckerchen picture janckerchen  路  3Comments

anyx picture anyx  路  3Comments