My API function currently performs a post request and returns an Observable of ModelAResponse, which is an interface I have defined.
I now want to modify this function so that it can return an Observable of either ModelAResponse or ModelBResponse based on the status returned.
Here are the two response models I am working with:
export interface ModelAResponse {
res: ModelA;
}
export interface ModelBResponse {
res: ModelB
}
(ModelA and ModelB are interfaces that belong to another class)
Currently, my function only supports returning ModelAResponse:
public myApiFunc(req: MyRequestModel): Observable<ModelAResponse> {
...
this.http.post("my/api/path", req, {headers: myHeaders}),
(jsonReturned) => status === 200 ? {res: jsonReturned} : undefined);
...
}
I'm looking for the best practice way in TypeScript to modify this function so that it can return either ModelAResponse or ModelBResponse based on the status. How can I achieve this?