I am looking to enhance my service class by creating a subclass where I can define functions with the same name but different implementations.
My desired structure is as follows:
httpWrapper.get //default is observables. returns observable
httpWrapper.promise.get //returns promise-variant
Here is a snippet of my current service:
export class HttpWrapperService {
constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
public get<T>(endpoint: string, options?: any): Observable<HttpEvent<T>> {
return this.http.get<T>(endpoint, options);
}
public post<T>(endpoint: string, data: any, options?: any): Observable<HttpEvent<T>> {
return this.http.post<T>(endpoint, data, options);
}
public delete<T>(endpoint: string, options?: any): Observable<HttpEvent<T>> {
return this.http.delete<T>(endpoint, options);
}
}
export namespace HttpWrapperService {
export class Promise {
constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
public get<T>(endpoint: string, options?: any) {
return this.http.get<T>(endpoint, options).toPromise();
}
public post<T>(endpoint: string, data: any, options?: any) {
return this.http.post<T>(endpoint, data, options).toPromise();
}
public delete<T>(endpoint: string, options?: any) {
return this.http.delete<T>(endpoint, options).toPromise();
}
}
}
However, currently when I type httpWrapper.
, only the observable variants are displayed and not the promise-variants.
How can I achieve this?
Essentially, I would like intellisense to present the following options after typing: httpWrapper.
:
httpWrapper.post
httpWrapper.get
httpWrapper.delete
httpWrapper.promise
And after selecting httpWrapper.promise.
:
httpWrapper.promise.get
httpWrapper.promise.post
httpWrapper.promise.delete