I am currently working with Angular 6 and RxJs 6. The backend api I am using sends the response with a generic class object, meaning that the response data model is not static. However, there is an indicator included in the response that tells me what model is being passed in. Here is some code to illustrate this:
interface orderSummary {...}
interface orderDetail {...}
interface IResponse {
objectType: string;
objectData: any
}
GetGenericDataModel() {
return this.httpClient.get<IResponse>(`../api/method`)
.map?flatmap?pipe?( -->what should I use here?
(response) => {
if (response.objectType === 'orderSummary')
return response.objectData.ToOrderSummary --> how to convert to orderSummary
else if (response.objectType === 'orderDetail')
return response.objectData.ToOrderDetail --> how to convert to orderDetail
}
);
}
When fetching results on the client side using http.get, I need to convert/transform the response data into my pre-defined interface (either orderSummary or orderDetail) based on the response indicator (objectType). How can I utilize rxjs to achieve this conversion/transformation of the response data into my pre-defined interface in Angular 6?