Below is a list of APIs I am working with:
1. https://www.something.com/?filter=something
Result:
{
id: 12
previous: null
next: https://www.something.com/?filter=something&page=2
data: ...
}
2. https://www.something.com/?filter=something&page=2
Result:
{
id: 13
previous: https://www.something.com/?filter=something
next: https://www.something.com/?filter=something&page=3
data: ...
}
3. https://www.something.com/?filter=something&page=3
Result:
{
id: 14
previous: https://www.something.com/?filter=something&page=2
next: null
data: ...
}
I am attempting to loop through all the URLs, gather and combine the data from each API call, and only continue if the 'next' variable is not null.
This is my current approach:
getByURL(url: string): any {
return this.apiService.get(url).pipe(map(data => data));
}
getData(): Observable<any> {
const url = 'https://www.something.com/?filter=something';
return this.getByURL(url).pipe(
expand(({ data }) => {
if (!data.next) return this.getByURL(data.next);
else return empty();
}),
concatMap(({ content }) => {
return of(content.data);
})
);
}
However, I am getting undefined results after trying for several days. Any help would be appreciated. Thanks!
UPDATED
I have shared my code on stackblitz: I am making 3 API calls :
http://5cf15627c936cb001450bd0a.mockapi.io/users
http://5cf15627c936cb001450bd0a.mockapi.io/users2
http://5cf15627c936cb001450bd0a.mockapi.io/users3
I am aiming to collect all the data from each request. Please check the console in the provided link to see the output. I keep getting undefined. Thank you
https://stackblitz.com/edit/angular-rai6az?file=src%2Fapp%2Fapp.component.ts