| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  | // ToH Promise Version
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							| 
									
										
										
										
											2016-04-27 11:28:22 -07:00
										 |  |  | import {Injectable}     from '@angular/core'; | 
					
						
							|  |  |  | import {Http, Response} from '@angular/http'; | 
					
						
							|  |  |  | import {Headers, RequestOptions} from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  | import {Hero}           from './hero'; | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Injectable() | 
					
						
							|  |  |  | export class HeroService { | 
					
						
							|  |  |  |   constructor (private http: Http) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 18:13:57 +01:00
										 |  |  |   // URL to web api
 | 
					
						
							|  |  |  |   private _heroesUrl = 'app/heroes.json'; | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   // #docregion methods
 | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |   getHeroes (): Promise<Hero[]> { | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |     return this.http.get(this._heroesUrl) | 
					
						
							|  |  |  |                     .toPromise() | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |                     .then(this.extractData) | 
					
						
							|  |  |  |                     .catch(this.handleError); | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |   addHero (name: string): Promise<Hero> { | 
					
						
							| 
									
										
										
										
											2016-02-01 19:52:14 -08:00
										 |  |  |     let body = JSON.stringify({ name }); | 
					
						
							|  |  |  |     let headers = new Headers({ 'Content-Type': 'application/json' }); | 
					
						
							|  |  |  |     let options = new RequestOptions({ headers: headers }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.http.post(this._heroesUrl, body, options) | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |                .toPromise() | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |                .then(this.extractData) | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |                .catch(this.handleError); | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private extractData(res: Response) { | 
					
						
							|  |  |  |     if (res.status < 200 || res.status >= 300) { | 
					
						
							|  |  |  |       throw new Error('Bad response status: ' + res.status); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     let body = res.json(); | 
					
						
							|  |  |  |     return body.data || { }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   private handleError (error: any) { | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |     // In a real world app, we might send the error to remote logging infrastructure
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     let errMsg = error.message || 'Server error'; | 
					
						
							| 
									
										
										
										
											2016-04-13 08:21:32 -07:00
										 |  |  |     console.error(errMsg); // log to console instead
 | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  |     return Promise.reject(errMsg); | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-10 15:04:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 10:22:59 +01:00
										 |  |  | // #enddocregion methods
 | 
					
						
							| 
									
										
										
										
											2015-12-01 12:15:14 +01:00
										 |  |  | } | 
					
						
							|  |  |  | // #enddocregion
 |