I need to fetch entity details, lets say book from REST endpoint. The book object might look like this
{
title: XYZ,
published: XYZ
author: URL_TO_ENDPOINT
publisher: URL_TO_ENDPOINT
}
Now I need to chain promises so that I get the author and publisher data as well. I have simple ApiService that implements getResourceByURL method, and I created new service DetailsService that calls my ApiService and chains promices This is easy as long as I know exactly my object:
return this.apiService.getResourceByURL(book.url).flatMap((entity) => {
return Observable.forkJoin(
Observable.of(entity),
this.apiService.getResourceByURL(entity.author)
this.apiService.getResourceByURL(entity.publisher)
).map(entityFullDetails => {
let entity = entityFullDetails[0];
let author = entityFullDetails[1];
let publisher = entityFullDetails[2];
entity.author = author;
entity.publisher = publisher;
return entity;
});
});
problem is that I wanted to use the same method of DetailsService to be able to fetch details for authors/publishers as well, meaning that it would need to iterate over all keys of entity object and call apiService.getResourceByURL if value of that key is an URL (lets say it starts with http).
Can that be easily done? or should I lower the abstraction level and prepare separate methods to get BookDetail, AuthorDetail, etc.
I am interested only in one level nesting. meaning that if I fetch book's author I do not need to get all books written by that author meaning I am fine getting something like
{
title: XYZ,
published: XYZ
author: {name: XXX, country: XXX, books: [URL, URL, URL], ...}
...
}
to prevent looping