I have a service what can handle datas in the localstorage and I have an other service what handle the remote API querys. When I define a data model I give it the model is use the local storage or not.
Now I try to build a proxy service what can decide based on the model definition to ask datas from localstorage or from the remote API.
Right now I have this code:
export class ProxyService<T> {
public type: new () => T;
private emptyModel: T = new this.type();
private localstorageService: LocalstorageDataService<T> =
new LocalstorageDataService(this.type, this.httpClient);
private remoteDataService: RemoteDataService<T> = new RemoteDataService(this.type, this.httpClient);
constructor(
private httpClient: HttpClient,
) { }
getOne(id: number): Observable<T> {
if (this.emptyModel.use_localstorage) {
return of(this.localstorageService.findById(id));
} else {
return this.remoteDataService.getOne(id).pipe(map((result: T) => result));
}
}
// other proxy functions are here...
}
But I get this error:
Error: Uncaught (in promise): TypeError: this.type is not a constructor
TypeError: this.type is not a constructor
at new ProxyService (proxy.service.ts:19)
Both RemoteDataService & LocalstorageDataService use a model, so the ProxyService is the best place where I define one. Because the ProxyService is used in multiple place in the whole project with multiple models.
By the way the 19th line is this code from above:
private emptyModel: T = new this.type();
I tried to fix it in the constructor:
export class ProxyService<T> {
// ...
constructor(
public type: new () => T;
private httpClient: HttpClient,
) { }
// ...
But in this case I need to define a model when I call the ProxyService, and I don't want to define a model every case when I use the ProxyService. In this case I can't use the ProxyService like the next code, but this is the way what I want to use:
export class HelperService<T> {
constructor(
private proxy: ProxyService<T>,
) { }
Any idea how can I fix this issue?