I have a project where I make an API request and create an object by mapping data to a model (Parent). The object I create exists of a nested structure (Children) of objects. For each object a new API call needs to be made.
When I call my service that does all the above, I want to have one observable returned on which I can subscribe, but because the child structure is dynamic I cannot figure out how to get those requests combine.
// Parent
export interface Parent {
children: Child[];
}
// Child
export interface Child {
id: string;
infoFromApi: ApiInfo
children: Child[]
}
// Service
getAPIResponse(endpoint: string): Observable<Parent> {
return this.http.get(endpoint)
.pipe(
take(1),
catchError((error: HttpErrorResponse) => this.handleError(error, messageGroup)),
map((parent: RawParent) => {
return this.enrichChildren(parent.children);
})
);
}
enrichChildren(children: Child[]){
children.forEach((child) => {
child.infoFromApi = return this.http.get(endpoint);
this.enrichChildren(child.children);
})
}
This is a simplified mockup of my code please do not look at spelling mistakes.