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

How do I inject dependencies into my model? #72

Open
thalysonalexr opened this issue Mar 12, 2021 · 7 comments
Open

How do I inject dependencies into my model? #72

thalysonalexr opened this issue Mar 12, 2021 · 7 comments

Comments

@thalysonalexr
Copy link

How do I inject dependencies into my model? I tried it with injectable, but it looks like he totally ignores it when deco with his model decorator!??

@toondaey
Copy link
Owner

Hello @thalysonalexr , can you provide code snippets? This will better help proffer a solution.

@thalysonalexr
Copy link
Author

Okay, I have the following code:

@OAuth2Model()
@Injectable()
export class OAuth2ModelService implements RequestAuthenticationModel {
  constructor(private readonly configService: ConfigService) {}
}

as you can see, I'm passing the @ Injectable decorator so that the nest inject via constructor my dependency on ConfigService. I would like to use some of the environment with this module, instead of using directly with process.env as I did when implementing ... When trying to use this way, something simply causes nest to ignore ... Is there any solution for me to can use the decorator of OAuth2Model in a nest provider?

Thanks for the feedback!

@alitnk
Copy link

alitnk commented Oct 28, 2021

Hey!
having the same problem injecting Prisma service into the model service, any ideas?

@thalysonalexr
Copy link
Author

Hey guys,

I think I would have to revise the lib and implement the solution. In my case I needed to inject a typeorm repository and use configService. So manually I used process.env and getRepository method.

@helsonxiao
Copy link

Hey guys,

I think I would have to revise the lib and implement the solution. In my case I needed to inject a typeorm repository and use configService. So manually I used process.env and getRepository method.

The problem is caused by nested modules. @OAuth2Model provider is got by reflector inside ModelProviderModule, thus we cannot import modules for it. I will try to fix it in the next week.

@helsonxiao
Copy link

helsonxiao commented Nov 17, 2021

Update: After digging into the codebase, I'm using this workaround.

import * as OAuth2Server from 'oauth2-server';
import { ServerOptions } from 'oauth2-server';
import {
  OAuth2ServerTokenGuard,
  OAuth2ServerAuthorizationGuard,
  OAuth2ServerAuthenticationGuard,
} from '@t00nday/nestjs-oauth2-server/dist/guards';
import { IOAuth2ServerModuleOptions } from '@t00nday/nestjs-oauth2-server/dist/interfaces';
import {
  OAUTH2_SERVER,
  OAUTH2_SERVER_MODEL_PROVIDER,
  OAUTH2_SERVER_OPTIONS_TOKEN,
} from '@t00nday/nestjs-oauth2-server/dist/oauth2-server.constants';

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from 'src/user/entities/user.entity';
import { OAuth2User } from './entities/oauth2-user.entity';
import { OAuth2Controller } from './oauth2.controller';
import { OAuth2Model } from './oauth2.model';
import { OAuth2Service } from './oauth2.service';

@Module({
  imports: [
    TypeOrmModule.forFeature([
      OAuth2User,
      User,
    ]),
  ],
  providers: [
    OAuth2Service,
    {
      provide: OAUTH2_SERVER_OPTIONS_TOKEN,
      useValue: {},
    },
    {
      provide: OAUTH2_SERVER_MODEL_PROVIDER,
      useClass: OAuth2Model,
    },
    {
      provide: OAUTH2_SERVER,
      useFactory: (
        options: IOAuth2ServerModuleOptions,
        model: ServerOptions['model'],
      ): OAuth2Server =>
        new OAuth2Server(Object.assign({}, options, { model })),
      inject: [OAUTH2_SERVER_OPTIONS_TOKEN, OAUTH2_SERVER_MODEL_PROVIDER],
    },
    OAuth2ServerTokenGuard,
    OAuth2ServerAuthorizationGuard,
    OAuth2ServerAuthenticationGuard,
  ],
  controllers: [OAuth2Controller],
})
export class OAuth2Module {}

Now, OAuth2Service is injectable in OAuth2Model.

Also, OAuth2ServerModule is no more needed in your app.module.ts because providers have been already imported above.

// just remove this line!
import { OAuth2ServerModule } from '@t00nday/nestjs-oauth2-server';

@helsonxiao
Copy link

helsonxiao commented Nov 17, 2021

I'd like to propose a new but breaking API for this feature.

Update: This is available on npm now. Just install it by npm install @boyuai/nestjs-oauth2-server.

How do we use it?

import { Module } from '@nestjs/common';
import { OAuth2ServerModule } from '@boyuai/nestjs-oauth2-server';
import { OAuth2Controller } from './oauth2.controller';
import { OAuth2ServiceModule } from './oauth2-service.module';
import { OAuth2Model } from './oauth2.model';

@Module({
  imports: [
    OAuth2ServiceModule,
    OAuth2ServerModule.forRoot({
      imports: [OAuth2ServiceModule], // import anything you want to use in modelClass
      modelClass: OAuth2Model,
    }),
  ],
  controllers: [OAuth2Controller],
})
export class OAuth2Module {}

And you have to implement your own OAuth2Model.

@Injectable() // We don't use `@OAuth2Model()` any more.
export class OAuth2Model
  implements RequestAuthenticationModel, AuthorizationCodeModel
{
  private readonly logger = new Logger(OAuth2Model.name);

  constructor(
    private readonly redisService: RedisService,
    private readonly oauth2Service: OAuth2Service,
  ) {}

  // ...
  // methods for oauth2 server
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants