英文:
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.
{
"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
.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论