Last active
August 16, 2016 02:55
-
-
Save mitsuruog/6f7f0ca3f546b245bccdd3ebc14375d8 to your computer and use it in GitHub Desktop.
Revisions
-
mitsuruog revised this gist
Apr 12, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,11 +46,11 @@ export class CustomHttp extends Http { } request(url:string | Request, options?:RequestOptionsArgs):Observable<Response> { return super.request(url, options).catch((res: Response) => this.handleResponseError(res)); } get(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.get(url, options).catch((res: Response) => this.handleResponseError(res)); } // ...長いので省略 -
mitsuruog revised this gist
Apr 12, 2016 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,41 +8,41 @@ export class CustomHttp extends Http { } request(url:string | Request, options?:RequestOptionsArgs):Observable<Response> { return super.request(url, options).catch((res: Response) => this.handleResponseError(res)); } get(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.get(url, options).catch((res: Response) => this.handleResponseError(res)); } post(url:string, body:string, options?:RequestOptionsArgs):Observable<Response> { return super.post(url, body, options).catch((res: Response) => this.handleResponseError(res)) } put(url:string, body:string, options?:RequestOptionsArgs):Observable<Response> { return super.put(url, body, options).catch((res: Response) => this.handleResponseError(res)); } delete(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.delete(url, options).catch((res: Response) => this.handleResponseError(res)); } patch(url:string, body:string, options?:RequestOptionsArgs):Observable<Response> { return super.patch(url, body, options).catch((res: Response) => this.handleResponseError(res)); } head(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.head(url, options).catch((res: Response) => this.handleResponseError(res)); } private handleResponseError(res:Response) { console.log(res); if (res.status === 401) { // do something } else if (res.status === 500) { // do something } return Observable.throw(res); -
mitsuruog revised this gist
Apr 11, 2016 . 1 changed file with 11 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,14 +31,6 @@ following step 1. 既存のHttpをOverrideするための`CustomHttp`サービスを作成する 2. `bootstrap`で既存のHttpをOverideする ### Create a `CustomHttp` service **custom-http.service.ts** @@ -87,13 +79,13 @@ export class CustomHttp extends Http { import 'rxjs/Rx'; import {bootstrap} from 'angular2/platform/browser'; import {provide} from "angular2/core"; import {AppComponent} from './app.component'; import {CustomHttp} from "./common/services/custom-http.service"; bootstrap(AppComponent, [ provide(Http, { useFactory: (backend:XHRBackend, defaultOptions:RequestOptions) => { return new CustomHttp(backend, defaultOptions) }, @@ -102,6 +94,15 @@ bootstrap(AppComponent, [ ]); ``` > :warning: Angular2は`RxJS`のAPIをWarpしているが、`RxJS`のすべてのAPIを提供していない。 `RsJS`のAPIをフルで利用したい場合は、`main.ts`で`RxJS`を直接importする必要がある。 > **main.ts** > ```ts import 'rxjs/Rx'; ``` ## Conclusion - Angualr2でもAngular1のHttpInterceptorと近いことはできる。 -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,7 +61,7 @@ export class CustomHttp extends Http { return super.get(url, options).catch(this.handleResponseError); } // ...長いので省略 // // Here's to handle error stuff @@ -77,6 +77,9 @@ export class CustomHttp extends Http { } ``` 全体像はこちらを見てください。 [custom-http.service.ts](https://gist.github.com/mitsuruog/6f7f0ca3f546b245bccdd3ebc14375d8#file-custom-http-service-ts) ### Override `Http` to `CustomHttp` at `bootstrap` **main.ts** -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -102,4 +102,5 @@ bootstrap(AppComponent, [ ## Conclusion - Angualr2でもAngular1のHttpInterceptorと近いことはできる。 - ただ、少し面倒だ。 - 使えそうなもの置いておきます => [custom-http.service.ts](https://gist.github.com/mitsuruog/6f7f0ca3f546b245bccdd3ebc14375d8#file-custom-http-service-ts) -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 50 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ import {Http, ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response} from "angular2/http"; import {Observable} from "rxjs/Observable"; export class CustomHttp extends Http { constructor(backend:ConnectionBackend, defaultOptions:RequestOptions) { super(backend, defaultOptions) } request(url:string | Request, options?:RequestOptionsArgs):Observable<Response> { return super.request(url, options).catch(this.handleResponseError); } get(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.get(url, options).catch(this.handleResponseError); } post(url:string, body:string, options?:RequestOptionsArgs):Observable<Response> { return super.post(url, body, options).catch(this.handleResponseError) } put(url:string, body:string, options?:RequestOptionsArgs):Observable<Response> { return super.put(url, body, options).catch(this.handleResponseError); } delete(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.delete(url, options).catch(this.handleResponseError); } patch(url:string, body:string, options?:RequestOptionsArgs):Observable<Response> { return super.patch(url, body, options).catch(this.handleResponseError); } head(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.head(url, options).catch(this.handleResponseError); } private handleResponseError(res:Response) { console.log(res); if (res.status === 401) { // do something } else if (res.status === 500) { // do something } return Observable.throw(res); } } -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ - **小川充** - [@mitsuruog](https://github.com/mitsuruog) - Front-end Developer in Givery inc. - Angular User Group Staff. ## HttpInterceptorとは -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,11 @@ ## Who am I - **小川充** - [@mitsuruog](https://github.com/mitsuruog) - Front-end Developer in Givery inc. - Angular User Group Stuff. ## HttpInterceptorとは - XHRに関連する横断的機能(cross-cutting)を分離する -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -58,6 +58,9 @@ export class CustomHttp extends Http { // ... // // Here's to handle error stuff // private handleResponseError(res:Response) { if (res.status === 401) { // do something -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,12 +26,11 @@ following step 1. 既存のHttpをOverrideするための`CustomHttp`サービスを作成する 2. `bootstrap`で既存のHttpをOverideする > :warning: Angular2は`RxJS`のAPIをWarpしているが、`RxJS`のすべてのAPIを提供していない。 `RsJS`のAPIをフルで利用したい場合は、`main.ts`で`RxJS`を直接importする必要がある。 > **main.ts** > ```ts import 'rxjs/Rx'; ``` -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,14 @@ following step 1. 既存のHttpをOverrideするための`CustomHttp`サービスを作成する 2. `bootstrap`で既存のHttpをOverideする > :warning: Angular2は`RxJS`のAPIをWarpしているが、`RxJS`のすべてのAPIを提供していない。 `RsJS`のAPIをフルで利用したい場合は、`main.ts`で`RxJS`を直接importする必要がある。 **main.ts** ```ts import 'rxjs/Rx'; ``` ### Create a `CustomHttp` service -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 5 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -63,7 +63,7 @@ export class CustomHttp extends Http { } ``` ### Override `Http` to `CustomHttp` at `bootstrap` **main.ts** ```ts @@ -85,4 +85,7 @@ bootstrap(AppComponent, [ ]); ``` ## Conclusion - Angualr2でもAngular1のHttpInterceptorと近いことはできる。 - ただ、少し面倒だ。 -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 66 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,18 +10,79 @@ - Angular1ではHttpInterceptorを使うことで、これらを綺麗に分離できた - refs: [AngularJS: API: $http](https://docs.angularjs.org/api/ng/service/$http) ## How about a httpInterceptor in Angular 2 ? 結論 **簡単にできない!!** - この長いスレ - [RFC: Http interceptors and transformers · Issue #2684 · angular/angular](https://github.com/angular/angular/issues/2684) ## Way of httpInterceptor in Angular 2 following step 1. 既存のHttpをOverrideするための`CustomHttp`サービスを作成する 2. `bootstrap`で既存のHttpをOverideする :warning: ### Create a `CustomHttp` service **custom-http.service.ts** ```ts import {Http, ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response} from "angular2/http"; import {Observable} from "rxjs/Observable"; export class CustomHttp extends Http { constructor(backend:ConnectionBackend, defaultOptions:RequestOptions) { super(backend, defaultOptions) } request(url:string | Request, options?:RequestOptionsArgs):Observable<Response> { return super.request(url, options).catch(this.handleResponseError); } get(url:string, options?:RequestOptionsArgs):Observable<Response> { return super.get(url, options).catch(this.handleResponseError); } // ... private handleResponseError(res:Response) { if (res.status === 401) { // do something } else if (res.status === 500) { // do something } return Observable.throw(res); } } ``` ### Override `Http` at `bootstrap` **main.ts** ```ts import 'rxjs/Rx'; import {bootstrap} from 'angular2/platform/browser'; import {Provider} from "angular2/core"; import {AppComponent} from './app.component'; import {CustomHttp} from "./common/services/custom-http.service"; bootstrap(AppComponent, [ new Provider(Http, { useFactory: (backend:XHRBackend, defaultOptions:RequestOptions) => { return new CustomHttp(backend, defaultOptions) }, deps: [XHRBackend, RequestOptions] }) ]); ``` ## -
mitsuruog revised this gist
Apr 10, 2016 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,9 +4,10 @@ ## HttpInterceptorとは - XHRに関連する横断的機能(cross-cutting)を分離する - Global Error handling - Fail back to show notification - Angular1ではHttpInterceptorを使うことで、これらを綺麗に分離できた - refs: [AngularJS: API: $http](https://docs.angularjs.org/api/ng/service/$http) ## Way of httpInterceptor in Angular 2 -
mitsuruog renamed this gist
Apr 10, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
mitsuruog created this gist
Apr 10, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ # Angular2 HttpInterceptor Needed ## Who am I ## HttpInterceptorとは - XHRに関連する横断的機能(cross-cutting)を分離したい! - Global Error handling. - Angular1ではHttpInterceptorを使うことで綺麗に分離できた - refs: [AngularJS: API: $http](https://docs.angularjs.org/api/ng/service/$http) ## Way of httpInterceptor in Angular 2 結論 簡単にできない!! - この長いスレ - [RFC: Http interceptors and transformers · Issue #2684 · angular/angular](https://github.com/angular/angular/issues/2684) ## Angular2で ## ```ts ```