Angular HttpClient имеет два таких интерфейса:
// type-1
get<T>(url: string, options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<T>;
// type-2
get<T>(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'response';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}): Observable<HttpResponse<T>>;
Они в основном такие же, за исключением поля observe
.
Вот текущая реализация метода оболочки:
myGet<T = any>(url: string, options?: {
headers?: HttpHeaders | {
[ header: string ]: string | string[];
};
observe?: "response" | "body";
params?: HttpParams | {
[ param: string ]: string | string[];
};
reportProgress?: boolean;
responseType?: "json";
withCredentials?: boolean;
},
): Observable<HttpResponse<Resp<T>> | Resp<T>> {
// how to solve the type casting make them into one return statement
if (options?.observe === "response") {
// return full http response
return this.http.get<Resp<T>>(`${ this.baseUrl }${ url }`, options as type-2)
.pipe(
catchError(this.handleHttpError),
tap(this.handleResp),
);
} else {
// return the response body
return this.http.get<Resp<T>>(`${ this.baseUrl }${ url }`, options as type-1)
.pipe(
catchError(this.handleHttpError),
tap(this.handleResp),
);
}
}