NestJS用于使用服务的工厂模式

huangapple go评论111阅读模式
英文:

NestJS factory pattern for use service

问题

I have 2 types of login. I need to pass the param in the request and use this param in auth service to choose what service for login I should use.

Body in POST request:

  1. {
  2. "type": "Customer",
  3. "store": "store24",
  4. "email": "test333333@email.com",
  5. "password": "testpass2"
  6. }

authFactory.ts:

  1. const loginFactoryTypes = {
  2. [LOGIN_TYPE.Customer]: CustomerAuthService,
  3. [LOGIN_TYPE.User]: UserAuthService,
  4. };
  5. export const createLoginFactory = (type: string) => {
  6. return loginFactoryTypes[type];
  7. };

auth.controller.ts:

  1. @Post('/signin')
  2. signInFactory(@Body() signInDto: SignInUserDto & SignInCustomerDto): Promise<unknown> {
  3. const { type } = signInDto;
  4. const loginFactory: AuthInterface = createLoginFactory(type);
  5. return loginFactory.signin(signInDto);
  6. }

If I pass param type - Customer then I get this service CustomerAuthService:

  1. async signin(signInDto: SignInCustomerDto) {
  2. const customer = await this.customersService.findCustomerByEmail(signInDto);
  3. const token = this.jwtService.sign({
  4. id: customer.id,
  5. email: customer.email,
  6. });
  7. return { token, customer };
  8. }

But the controller gets me an error on the string:
loginFactory.signin(signInDto);

I get the error loginFactory.signin is not a function.

英文:

I have 2 types of login. I need to pass the param in the request and use this param in auth service to choose what service for login I should use.
Does mb exist better way?

Body in POST request.

  1. {
  2. &quot;type&quot;: &quot;Customer&quot;,
  3. &quot;store&quot;: &quot;store24&quot;,
  4. &quot;email&quot;: &quot;test333333@email.com&quot;,
  5. &quot;password&quot;: &quot;testpass2&quot;
  6. }

authFactory.ts

  1. const loginFactoryTypes = {
  2. [LOGIN_TYPE.Customer]: CustomerAuthService,
  3. [LOGIN_TYPE.User]: UserAuthService,
  4. };
  5. export const createLoginFactory = (type: string) =&gt; {
  6. return loginFactoryTypes[type];
  7. };

auth.controller.ts

  1. @Post(&#39;/signin&#39;)
  2. signInFactory(@Body() signInDto: SignInUserDto &amp; SignInCustomerDto): Promise&lt;unknown&gt; {
  3. const { type } = signInDto;
  4. const loginFactory: AuthInterface = createLoginFactory(type);
  5. return loginFactory.signin(signInDto);
  6. }

If I pass param type - Customer then I get this service CustomerAuthService.

  1. async signin(signInDto: SignInCustomerDto) {
  2. const customer = await this.customersService.findCustomerByEmail(signInDto);
  3. const token = this.jwtService.sign({
  4. id: customer.id,
  5. email: customer.email,
  6. });
  7. return { token, customer };
  8. }

But the controller gets me an error on the string.
loginFactory.signin(signInDto);

I get the error loginFactory.signin is not a function.

答案1

得分: 0

loginFactoryTypes[type] 会返回一个类,而不是像你期望的那样返回一个实例。

createLoginFactory 的消费者端,你可以利用 moduleRef 来使用 Nest 的 DI 系统检索该类的实例。

英文:

as you have this:

  1. const loginFactoryTypes = {
  2. [LOGIN_TYPE.Customer]: CustomerAuthService,
  3. [LOGIN_TYPE.User]: UserAuthService,
  4. };

loginFactoryTypes[type] will return a class, not an instance like you've expected.

At the consumer side of createLoginFactory, you could leverage on moduleRef to retrieve an instance of that class using Nest's DI system.

huangapple
  • 本文由 发表于 2023年2月6日 20:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定