I'm trying to create a class for working with API on top of Angular2 HTTP
class. The main thing is that I need to add custom auth header which is available after user authorized.
The main problem is that the token is stored in Ionic Storage module from which it can be only be obtained asynchronously, but I need to return the specific type....
export class APIRequest extends Http {
private storage : Storage;
constructor (backend: XHRBackend, options: RequestOptions) {
super(backend, options);
this.storage = new Storage;
}
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
if (typeof url === 'string') {
url = AppConfig.API_SERVER + url
} else {
url.url = AppConfig.API_SERVER + url.url
}
this.storage.get('authToken').then(token => {
if (typeof url === 'string') {
if (!options) {
options = {headers: new Headers()};
}
options.headers.set('Authorization', `Bearer ${token}`);
} else {
url.headers.set('Authorization', `Bearer ${token}`);
}
// Here I need to return
}, err => {
// throw some error
})
return super.request(url, options).catch(this.catchAuthError(this));
}
}
So basically I need somehow to put return super.request(url, options)...
to the promise of the storage and return when i get that token.