Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REQUEST object from core, used for Scope Request Providers is not available #1955

Closed
ackuser opened this issue Apr 8, 2019 · 9 comments
Closed

Comments

@ackuser
Copy link

ackuser commented Apr 8, 2019

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:

@ackuser
Copy link
Author

ackuser commented Apr 8, 2019

Is here

"import { REQUEST } from "@nestjs/core/router";"

instead of?

@prateekkathal
Copy link
Contributor

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 😞

@kamilmysliwiec
Copy link
Member

@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?

@prateekkathal
Copy link
Contributor

prateekkathal commented Apr 11, 2019

@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",
}

@kamilmysliwiec
Copy link
Member

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

@vkartaviy
Copy link

@kamilmysliwiec Could you please explain why it is not possible? Even based on the solution here #528 ?

@kamilmysliwiec
Copy link
Member

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.

@arjovanramshorst
Copy link

@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: #528 (comment)

@lock
Copy link

lock bot commented Sep 23, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants