Class Mirror
import: @azure-net/kit
Utility class for working with class metadata and reflection (if we're talking about complex scenarios). If we're talking about simple scenarios, then the class copies and adds methods of the class passed in the constructor to the class that inherits ClassMirror.
When to use:
- You need to very quickly pass a repository through the service layer to the delivery layer
- You are sure that your service will never have scenario logic and is a direct proxy of the repository
Nevertheless, at a global level, it is not recommended to leave services in this form.
Methods copied by ClassMirror can be overridden, so you can quickly proxy a repository to work with methods and develop and implement method logic later, one by one, while having fully working proxied methods.
All methods must be declared. This prevents a high level of non-obviousness. The class will not automatically type the presence of methods.
typescript
import { ClassMirror } from '@azure-net/kit';
import { AuthRepository } from '${ContextName}/infrastructure/http/repositories';
// Pass repository as generic to ClassMirror
class AuthService extends ClassMirror<AuthRepository> {
constructor(private repository: AuthRepository) {
// At this stage the class creates copies of methods
super(repository);
}
// Declare methods that we proxy in the class
declare login: AuthRepository['login'];
declare logout: AuthRepository['logout'];
// If the repository has a current method, it will be proxied, but we can override it by removing it from the declaration
async current() {
return this.authRepository.current().catch(() => undefined);
}
}
// One important point for general understanding
const mirror = new AuthService(AuthRepository);
console.log(mirror.name); // 'AuthService'
console.log(mirror.methods); // ['constructor', 'login', 'logout', 'current']
console.log(mirror.properties); // ['repository']