import { HttpClient , HttpHeaders , HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Observable } from 'rxjs' ; import { catchError , map } from 'rxjs/operators' ; import { Hero } from './hero' ; const httpOptions = { headers : new HttpHeaders ( { 'Content-Type' : 'application/json' } ) , } ; @ Injectable ( ) export class HeroService { readonly heroesUrl = 'api/heroes' ; constructor ( private http ) { } getHeroes ( ) { return this . http . get < Hero [ ] > ( this . heroesUrl ) . pipe ( catchError ( this . handleError ( 'getHeroes' ) ) ) as Observable < Hero [ ] > ; } getHero ( id ) { if ( typeof id === 'string' ) { id = parseInt ( id , 10 ) ; } const url = `${ this . heroesUrl }/?id=${ id }` ; return this . http . get < Hero [ ] > ( url ) . pipe ( map ( ( heroes ) => heroes [ 0 ] ) , catchError ( this . handleError < Hero > ( `getHero id=${ id }` ) ) ) ; } addHero ( hero ) { return this . http . post < Hero > ( this . heroesUrl , hero , httpOptions ) . pipe ( catchError ( this . handleError < Hero > ( 'addHero' ) ) ) ; } deleteHero ( hero ) { const id = typeof hero === 'number' ? hero : hero . id ; const url = `${ this . heroesUrl }/${ id }` ; return this . http . delete < Hero > ( url , httpOptions ) . pipe ( catchError ( this . handleError < Hero > ( 'deleteHero' ) ) ) ; } updateHero ( hero ) { return this . http . put ( this . heroesUrl , hero , httpOptions ) . pipe ( catchError ( this . handleError < unknown > ( 'updateHero' ) ) ) ; } private handleError < T > ( operation = 'operation' ) { return ( error ) : Observable < T > => { const message = error . error instanceof ErrorEvent ? error . error . message : `server returned code ${ error . status } with body "${ error . error }"` ; throw new Error ( `${ operation } failed: ${ message }` ) ; } ; } }