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

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

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:

{
    "type": "Customer",
    "store": "store24",
    "email": "test333333@email.com",
    "password": "testpass2"
}

authFactory.ts:

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

export const createLoginFactory = (type: string) => {
  return loginFactoryTypes[type];
};

auth.controller.ts:

@Post('/signin')
signInFactory(@Body() signInDto: SignInUserDto & SignInCustomerDto): Promise<unknown> {
  const { type } = signInDto;

  const loginFactory: AuthInterface = createLoginFactory(type);

  return loginFactory.signin(signInDto);
}

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

async signin(signInDto: SignInCustomerDto) {
  const customer = await this.customersService.findCustomerByEmail(signInDto);

  const token = this.jwtService.sign({
    id: customer.id,
    email: customer.email,
  });

  return { token, customer };
}

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.

{
    &quot;type&quot;: &quot;Customer&quot;,
    &quot;store&quot;: &quot;store24&quot;,
    &quot;email&quot;: &quot;test333333@email.com&quot;,
    &quot;password&quot;: &quot;testpass2&quot;
}

authFactory.ts

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

export const createLoginFactory = (type: string) =&gt; {
  return loginFactoryTypes[type];
};

auth.controller.ts

@Post(&#39;/signin&#39;)
signInFactory(@Body() signInDto: SignInUserDto &amp; SignInCustomerDto): Promise&lt;unknown&gt; {
  const { type } = signInDto;

  const loginFactory: AuthInterface = createLoginFactory(type);

  return loginFactory.signin(signInDto);
}

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

async signin(signInDto: SignInCustomerDto) {
  const customer = await this.customersService.findCustomerByEmail(signInDto);

  const token = this.jwtService.sign({
    id: customer.id,
    email: customer.email,
  });

  return { token, customer };
}

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:

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

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:

确定