2016-11-30 18 views
2

私はデフォルトを拡張してカスタムAngular 2 httpリクエストを作成しようとしています。私は認証トークンを格納するためにIonic 2ローカルストレージを使用しています。 (将来はファイルシステムを使用する可能性が高い)。私の問題は、httpサービスから解決済みの約束を返す方法です。コンポーネント内のObservableを購読することができます。私はObservable.fromPromiseと他のバリエーションを無駄にしようとしました。返信を約束の中から見てください

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> { 

    // Get the token used for this request. 
    // * Need to return this promise resolved. 
    var promise = this.storage.get('token').then(token => { 

    if (typeof url === 'string') { // meaning we have to add the token to the options, not in url 
     if (!options) { 
     // let's make option object 
     options = {headers: new Headers()}; 
     } 
     options.headers.set('Authorization', 'Basic ' + token); 
    } else { 
    // we have to add the token to the url object 
     url.headers.set('Authorization', 'Basic ' + token); 
    } 

    return super.request(url, options).catch(this.catchAuthError(this)); 

    }).catch(error => { 
    console.log(error); 
    }); 

} 

アイデアはこのブログの投稿に基づいていますが、イオンストレージは約束を返します。 http://www.adonespitogo.com/articles/angular-2-extending-http-provider/

+0

「user」は誰ですか? –

+0

トークンが必要です。 –

+1

「then」の中で観測可能なものを扱うのは便利ではありません。私はそれが 'Observable.fromPromise(this.storage.get( 'token')))のようなものでなければならないと思います。 ..)) ' – estus

答えて

6

そのストレージがRxのと互換性のある約束を返すかどうかは知りませんが、それは、その後であれば解決策は次のようになります。

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> { 

    return Observable 
     .fromPromise(this.storage.get('token')) 
     .flatMap(token => { 

      if (typeof url === 'string') { // meaning we have to add the token to the options, not in url 
       if (!options) { 
        // let's make option object 
        options = {headers: new Headers()}; 
       } 
       options.headers.set('Authorization', 'Basic ' + token); 
      } else { 
       // we have to add the token to the url object 
       url.headers.set('Authorization', 'Basic ' + token); 
      } 

      return super.request(url, options).catch(this.catchAuthError(this)); 

     }); 
    }); 

} 

約束は観測との互換性がない場合は、まだありますそれはそれほどエレガントではありませんが、それを行う方法です。

request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> { 

    return Observable.create((observer: Observer) => { 

     this.storage.get('token').then(token => { 

      if (typeof url === 'string') { // meaning we have to add the token to the options, not in url 
       if (!options) { 
        // let's make option object 
        options = {headers: new Headers()}; 
       } 
       options.headers.set('Authorization', 'Basic ' + token); 
      } else { 
       // we have to add the token to the url object 
       url.headers.set('Authorization', 'Basic ' + token); 
      } 

      super.request(url, options).catch(this.catchAuthError(this)).subscribe(result => { 
       observer.next(result); 
       observer.complete(); 
      }); 

     }).catch(error => { 
      observer.error(error); 
     }); 

    }); 

} 
+0

最初のオプションは働きました、ありがとうございました!以前フラットマップを使っていなかった。また、インポート 'rxjs/add/operator/mergemap'を追加する必要があります。フラットマップを動作させる。 –

関連する問題