11import { HttpClient } from '@angular/common/http' ;
22import { inject , Injectable } from '@angular/core' ;
33
4- import { Observable } from 'rxjs' ;
5- import { map } from 'rxjs/operators' ;
4+ import { Observable , of } from 'rxjs' ;
5+ import { catchError , map } from 'rxjs/operators' ;
66import { Book } from './book.model' ;
77
8+ const FALLBACK_BOOKS : Book [ ] = [
9+ { id : '1' , volumeInfo : { title : 'Awakenings' , authors : [ 'Oliver Sacks' ] } } ,
10+ { id : '2' , volumeInfo : { title : 'The Man Who Mistook His Wife for a Hat' , authors : [ 'Oliver Sacks' ] } } ,
11+ { id : '3' , volumeInfo : { title : 'An Anthropologist on Mars' , authors : [ 'Oliver Sacks' ] } } ,
12+ { id : '4' , volumeInfo : { title : 'Musicophilia' , authors : [ 'Oliver Sacks' ] } } ,
13+ { id : '5' , volumeInfo : { title : 'The Island of the Colourblind' , authors : [ 'Oliver Sacks' ] } } ,
14+ ] ;
15+
816@Injectable ( { providedIn : 'root' } )
917export class GoogleBooksService {
1018 private readonly http = inject ( HttpClient ) ;
@@ -14,6 +22,9 @@ export class GoogleBooksService {
1422 . get < { items : Book [ ] } > (
1523 'https://www.googleapis.com/books/v1/volumes?maxResults=5&orderBy=relevance&q=oliver%20sacks'
1624 )
17- . pipe ( map ( ( books ) => books . items || [ ] ) ) ;
25+ . pipe (
26+ map ( ( books ) => books . items || [ ] ) ,
27+ catchError ( ( ) => of ( FALLBACK_BOOKS ) )
28+ ) ;
1829 }
1930}
0 commit comments