Before each http call in my api service I want to check my local storage for an access token, then make the call once I have it. It looks like this
read(endpoint,params?) {
var url: string = this.authService.apiUrl + endpoint,
headers: Headers = new Headers(),
queryString: URLSearchParams = new URLSearchParams();
this.sessionService.getToken()
.then((value) => {
queryString.set('access_token', value);
headers.append('Content-Type', 'application/json; charset=utf-8');
return this.http.get(url,{
headers: headers,
search: queryString
})
.map(res => res.json())
});
}
And in my component I would have something like
getData() {
this.apiService.read('some endpoint')
.subscribe(
res => console.log(res),
error => this.logError(error)
)
}
This was working until I put the http call inside of the .then after checking the local storage. So I think it is now nested incorrectly.
What is the correct way to appraoch this? And is there perhaps a more efficent way to grab my token from local storage in this set up? NOTE: I am using Ionic 2 which has its own function for checking local storage which returns a promise.
Any advice would be great.
Thanks.
You will have to convert the http observable to a promise or convert promise to an observable.
Observable to promise:
To call use
Promise to Observable:
From RXJS 5.5 onwards:
Promise to Observable: