Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Planet {
- name: string;
- }
- interface PlanetResult {
- count: number;
- next: string;
- results: Planet[];
- }
- @Injectable({
- providedIn: 'root'
- })
- export class PlanetService {
- constructor(private httpClient: HttpClient) {}
- getAllPlanets(): Observable<Planet[]> {
- return this.fetchPlanets().pipe(
- expand(result =>
- iif(() => !!result.next,
- this.fetchPlanets(result.next),
- EMPTY
- )
- ),
- pluck('results'),
- reduce((acc, planets) => acc.concat(planets), []),
- );
- }
- private fetchPlanets(nextUrl?: string): Observable<PlanetResult> {
- const startUrl = 'https://swapi.co/api/planets/';
- return this.httpClient.get<PlanetResult>(nextUrl || startUrl);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement