Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 5940799

Browse files
committed
docs(http): remove status 200 guard and add RxJS operators individually
1 parent f5a9edc commit 5940799

File tree

12 files changed

+54
-61
lines changed

12 files changed

+54
-61
lines changed

public/docs/_examples/server-communication/dart/lib/toh/hero_service.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ class HeroService {
4747

4848
// #docregion extract-data
4949
dynamic _extractData(Response res) {
50-
if (res.statusCode < 200 || res.statusCode >= 300) {
51-
throw new Exception('Response status: ${res.statusCode}');
52-
}
5350
var body = JSON.decode(res.body);
5451
// TODO: once fixed, https://github.com/adaojunior/http-in-memory-web-api/issues/1
5552
// Drop the `?? body` term
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #docregion
2+
// import 'rxjs/Rx'; // adds ALL RxJS operators to Observable
3+
4+
// Just the Observable operators we need for THIS app.
5+
import 'rxjs/add/operator/catch';
6+
import 'rxjs/add/operator/debounceTime';
7+
import 'rxjs/add/operator/distinctUntilChanged';
8+
import 'rxjs/add/operator/map';
9+
import 'rxjs/add/operator/switchMap';

public/docs/_examples/server-communication/ts/app/hero-data.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
export class HeroData {
33
createDb() {
44
let heroes = [
5-
{ "id": "1", "name": "Windstorm" },
6-
{ "id": "2", "name": "Bombasto" },
7-
{ "id": "3", "name": "Magneta" },
8-
{ "id": "4", "name": "Tornado" }
5+
{ id: '1', name: 'Windstorm' },
6+
{ id: '2', name: 'Bombasto' },
7+
{ id: '3', name: 'Magneta' },
8+
{ id: '4', name: 'Tornado' }
99
];
1010
return {heroes};
1111
}

public/docs/_examples/server-communication/ts/app/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { HTTP_PROVIDERS } from '@angular/http';
66
// #enddocregion http-providers
77

88
// #docregion import-rxjs
9-
// Add all operators to Observable
10-
import 'rxjs/Rx';
9+
// Add the RxJS Observable operators we need in this app.
10+
import './add-rxjs-operators';
1111
// #enddocregion import-rxjs
1212

1313
import { TohComponent } from './toh/toh.component';

public/docs/_examples/server-communication/ts/app/toh/hero-list.component.1.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class HeroListComponent implements OnInit {
2828
}
2929

3030
addHero (name: string) {
31-
if (!name) {return;}
31+
if (!name) { return; }
3232
this.heroService.addHero(name)
3333
.then(
3434
hero => this.heroes.push(hero),

public/docs/_examples/server-communication/ts/app/toh/hero-list.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class HeroListComponent implements OnInit {
3131

3232
// #docregion addHero
3333
addHero (name: string) {
34-
if (!name) {return;}
34+
if (!name) { return; }
3535
this.heroService.addHero(name)
3636
.subscribe(
3737
hero => this.heroes.push(hero),

public/docs/_examples/server-communication/ts/app/toh/hero.service.1.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ export class HeroService {
3434
}
3535

3636
private extractData(res: Response) {
37-
if (res.status < 200 || res.status >= 300) {
38-
throw new Error('Bad response status: ' + res.status);
39-
}
4037
let body = res.json();
4138
return body.data || { };
4239
}
4340

4441
private handleError (error: any) {
45-
// In a real world app, we might send the error to remote logging infrastructure
46-
let errMsg = error.message || 'Server error';
42+
// In a real world app, we might use a remote logging infrastructure
43+
// We'd also dig deeper into the error to get a better message
44+
let errMsg = error.message || error.statusText || 'Server error';
4745
console.error(errMsg); // log to console instead
4846
return Promise.reject(errMsg);
4947
}

public/docs/_examples/server-communication/ts/app/toh/hero.service.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class HeroService {
2929
// #enddocregion error-handling, http-get, v1
3030

3131
// #docregion addhero, addhero-sig
32-
addHero (name: string): Observable<Hero> {
32+
addHero (name: string): Observable<Hero> {
3333
// #enddocregion addhero-sig
3434
let body = JSON.stringify({ name });
3535
let headers = new Headers({ 'Content-Type': 'application/json' });
@@ -43,9 +43,6 @@ export class HeroService {
4343

4444
// #docregion v1, extract-data
4545
private extractData(res: Response) {
46-
if (res.status < 200 || res.status >= 300) {
47-
throw new Error('Response status: ' + res.status);
48-
}
4946
let body = res.json();
5047
return body.data || { };
5148
}
@@ -54,7 +51,8 @@ export class HeroService {
5451
// #docregion error-handling
5552
private handleError (error: any) {
5653
// In a real world app, we might use a remote logging infrastructure
57-
let errMsg = error.message || 'Server error';
54+
// We'd also dig deeper into the error to get a better message
55+
let errMsg = error.message || error.statusText || 'Server error';
5856
console.error(errMsg); // log to console instead
5957
return Observable.throw(errMsg);
6058
}

public/docs/_examples/server-communication/ts/app/wiki/wiki-smart.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* tslint:disable:member-ordering */
12
// #docregion
23
import { Component } from '@angular/core';
34
import { JSONP_PROVIDERS } from '@angular/http';
@@ -20,7 +21,7 @@ import { WikipediaService } from './wikipedia.service';
2021
<li *ngFor="let item of items | async">{{item}}</li>
2122
</ul>
2223
`,
23-
providers:[JSONP_PROVIDERS, WikipediaService]
24+
providers: [JSONP_PROVIDERS, WikipediaService]
2425
})
2526
export class WikiSmartComponent {
2627

@@ -29,13 +30,13 @@ export class WikiSmartComponent {
2930
// #docregion subject
3031
private searchTermStream = new Subject<string>();
3132

32-
search(term:string) { this.searchTermStream.next(term); }
33+
search(term: string) { this.searchTermStream.next(term); }
3334
// #enddocregion subject
3435

3536
// #docregion observable-operators
36-
items:Observable<string[]> = this.searchTermStream
37+
items: Observable<string[]> = this.searchTermStream
3738
.debounceTime(300)
3839
.distinctUntilChanged()
39-
.switchMap((term:string) => this.wikipediaService.search(term));
40+
.switchMap((term: string) => this.wikipediaService.search(term));
4041
// #enddocregion observable-operators
4142
}

public/docs/_examples/server-communication/ts/app/wiki/wiki.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { WikipediaService } from './wikipedia.service';
1717
<li *ngFor="let item of items | async">{{item}}</li>
1818
</ul>
1919
`,
20-
providers:[JSONP_PROVIDERS, WikipediaService]
20+
providers: [JSONP_PROVIDERS, WikipediaService]
2121
})
2222
export class WikiComponent {
2323

0 commit comments

Comments
 (0)