Estoy intentando crear un servicio genérico que debería tomar una Entidad 'dada' para realizar operaciones CRUD básicas.
Hasta ahora, he intentado:
export type UserEntity<E> = { new (): E } | Function; class AuthenticationService<E> { private userEntity: UserEntity<E>; constructor(userEntity: UserEntity<E>) { this.userEntity = userEntity; } async login(email: string, password: string) { // const user = await getRepository(this.userEntity).findOneOrFail({ email }); const user = await this.userEntity?.findOneOrFail({ email }); const validPassword = argon2.verify(user?.password, password); if (!validPassword) { throw new ErrorHandler(401, ErrorMessage.INVALID_EMAIL_PASSWORD); } const accessToken = JWTHelpers.generateToken(user, { secret: config.ACCESS_TOKEN_SECRET, expiry: '300s', }); const refreshToken = JWTHelpers.generateToken(user, { secret: config.REFRESH_TOKEN_SECRET, expiry: '1y', }); user.tokens = user.tokens.concat(refreshToken); await user.save(); Reflect.deleteProperty(user, 'password'); return { accessToken, refreshToken }; }Este enfoque no parece funcionar y, como resultado, el compilador arroja varios tipos de errores, por ejemplo:
Property 'findOneOrFail' does not exist on type 'UserEntity<E>'. Property 'findOneOrFail' does not exist on type 'Function'.
Que se arroja de esta declaración: const user: E = await this.userEntity?.findOneOrFail({ email });
Property 'password' does not exist on type 'NonNullable<E>'. - arrojado a: const validPassword = argon2.verify(user?.password, password);
Estoy muy abierto a sugerencias sobre la mejor manera de abordar / solucionar estos problemas.
Gracias por adelantado.