I want to create a subclass in my service class where I can declare functions with the same name but with different usages.
I want to be able to write:
httpWrapper.get //default is observables. returns observable
httpWrapper.promise.get //returns promise-variant
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, when I write httpWrapper. I only get the observable variants. I can't choose the promise-variants.
How can I do this?
I basically want intellisense to show me when I type: httpWrapper.:
httpWrapper.post
httpWrapper.get
httpWrapper.delete
httpWrapper.promise
And when I've selected httpWrapper.promise.:
httpWrapper.promise.get
httpWrapper.promise.post
httpWrapper.promise.delete
httpWrapper.getPromiseinstead ofhttpWrapper.promise.get?httpWrapper.promise.getless repetitive than doinghttpWrapper.get.toPromise()?optionsparameter or one that doesnt have the keysobserve: "events"andreportProgress: true, the return type is simplyObservable<T>observe: 'response'then the return type will beObservable<HttpResponse<T>>1&2: readability and preferability (also, i think it is easier to refractor if functionality in the wrapper changes).3: Not sure what you mean by this. The options param is optional?3: what do you mean by signature parameters?4: You lost me :/. If you have ways to help me with any problems that might occur with my current solution, I will be happy to approve it as the answer.