import { Injectable } from '@angular/core' ; import { HttpClient , HttpErrorResponse } from '@angular/common/http' ; import { throwError } from 'rxjs' ; import { map , catchError } from 'rxjs/operators' ; import { Sorter } from './sorter' ; @ Injectable ( ) export class DataService { url = 'https://swapi.dev/api/' ; constructor ( private http , private sorter ) { } getFilms ( ) { return this . http . get ( this . url + 'films/' ) . pipe ( map ( ( data ) => { let films = data [ 'results' ] ; this . sorter . sort ( films , 'episode_id' ) ; return films ; } ) , catchError ( this . handleError ) ) ; } private handleError ( error ) { console . error ( 'server error:' , error ) ; if ( error . error instanceof Error ) { let errMessage = error . error . message ; return throwError ( errMessage ) ; } return throwError ( error || 'Server error' ) ; } }