Skip to content

Instantly share code, notes, and snippets.

@mitsuruog
Last active August 16, 2016 02:55
Show Gist options
  • Save mitsuruog/6f7f0ca3f546b245bccdd3ebc14375d8 to your computer and use it in GitHub Desktop.
Save mitsuruog/6f7f0ca3f546b245bccdd3ebc14375d8 to your computer and use it in GitHub Desktop.

Revisions

  1. mitsuruog revised this gist Apr 12, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Angular2 HttpInterceptor Needed.md
    Original 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(this.handleResponseError);
    return super.request(url, options).catch((res: Response) => this.handleResponseError(res));
    }

    get(url:string, options?:RequestOptionsArgs):Observable<Response> {
    return super.get(url, options).catch(this.handleResponseError);
    return super.get(url, options).catch((res: Response) => this.handleResponseError(res));
    }

    // ...長いので省略
  2. mitsuruog revised this gist Apr 12, 2016. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions custom-http.service.ts
    Original 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(this.handleResponseError);
    return super.request(url, options).catch((res: Response) => this.handleResponseError(res));
    }

    get(url:string, options?:RequestOptionsArgs):Observable<Response> {
    return super.get(url, options).catch(this.handleResponseError);
    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(this.handleResponseError)
    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(this.handleResponseError);
    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(this.handleResponseError);
    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(this.handleResponseError);
    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(this.handleResponseError);
    return super.head(url, options).catch((res: Response) => this.handleResponseError(res));
    }

    private handleResponseError(res:Response) {

    console.log(res);

    if (res.status === 401) {
    // do something
    // do something
    } else if (res.status === 500) {
    // do something
    // do something
    }
    return Observable.throw(res);

  3. mitsuruog revised this gist Apr 11, 2016. 1 changed file with 11 additions and 10 deletions.
    21 changes: 11 additions & 10 deletions Angular2 HttpInterceptor Needed.md
    Original file line number Diff line number Diff line change
    @@ -31,14 +31,6 @@ 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

    **custom-http.service.ts**
    @@ -87,13 +79,13 @@ export class CustomHttp extends Http {
    import 'rxjs/Rx';

    import {bootstrap} from 'angular2/platform/browser';
    import {Provider} from "angular2/core";
    import {provide} from "angular2/core";

    import {AppComponent} from './app.component';
    import {CustomHttp} from "./common/services/custom-http.service";

    bootstrap(AppComponent, [
    new Provider(Http, {
    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と近いことはできる。
  4. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion Angular2 HttpInterceptor Needed.md
    Original 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**
  5. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion Angular2 HttpInterceptor Needed.md
    Original 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)
  6. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 50 additions and 0 deletions.
    50 changes: 50 additions & 0 deletions custom-http.service.ts
    Original 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);

    }
    }
  7. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Angular2 HttpInterceptor Needed.md
    Original 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 Stuff.
    - Angular User Group Staff.

    ## HttpInterceptorとは

  8. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Angular2 HttpInterceptor Needed.md
    Original 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)を分離する
  9. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Angular2 HttpInterceptor Needed.md
    Original 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
  10. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions Angular2 HttpInterceptor Needed.md
    Original 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
    > :warning:
    Angular2は`RxJS`のAPIをWarpしているが、`RxJS`のすべてのAPIを提供していない。
    `RsJS`のAPIをフルで利用したい場合は、`main.ts``RxJS`を直接importする必要がある。
    > **main.ts**
    > ```ts
    import 'rxjs/Rx';
    ```
  11. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion Angular2 HttpInterceptor Needed.md
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,14 @@ following step
    1. 既存のHttpをOverrideするための`CustomHttp`サービスを作成する
    2. `bootstrap`で既存のHttpをOverideする

    :warning:
    > :warning:
    Angular2は`RxJS`のAPIをWarpしているが、`RxJS`のすべてのAPIを提供していない。
    `RsJS`のAPIをフルで利用したい場合は、`main.ts``RxJS`を直接importする必要がある。

    **main.ts**
    ```ts
    import 'rxjs/Rx';
    ```

    ### Create a `CustomHttp` service

  12. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions Angular2 HttpInterceptor Needed.md
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ export class CustomHttp extends Http {
    }
    ```

    ### Override `Http` at `bootstrap`
    ### Override `Http` to `CustomHttp` at `bootstrap`

    **main.ts**
    ```ts
    @@ -85,4 +85,7 @@ bootstrap(AppComponent, [
    ]);
    ```

    ##
    ## Conclusion

    - Angualr2でもAngular1のHttpInterceptorと近いことはできる。
    - ただ、少し面倒だ。
  13. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 66 additions and 5 deletions.
    71 changes: 66 additions & 5 deletions Angular2 HttpInterceptor Needed.md
    Original 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)

    ## Way of httpInterceptor in Angular 2
    ## How about a httpInterceptor in Angular 2 ?

    結論

    簡単にできない!!
    **簡単にできない!!**

    - この長いスレ
    - [RFC: Http interceptors and transformers · Issue #2684 · angular/angular](https://github.com/angular/angular/issues/2684)

    ## Angular2で
    ## 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]
    })
    ]);
    ```

    ##
  14. mitsuruog revised this gist Apr 10, 2016. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions Angular2 HttpInterceptor Needed.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,10 @@

    ## HttpInterceptorとは

    - XHRに関連する横断的機能(cross-cutting)を分離したい!
    - Global Error handling.
    - Angular1では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
  15. mitsuruog renamed this gist Apr 10, 2016. 1 changed file with 0 additions and 0 deletions.
  16. mitsuruog created this gist Apr 10, 2016.
    26 changes: 26 additions & 0 deletions Angular2 HttpInterceptor Needed
    Original 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
    ```