Can I use promise within HttpInterceptor? For example:
export class AuthInterceptor implements HttpInterceptor{
this.someService.someFunction()
My turn (Thanks to Jota.Toledo and Capripio) :
1) Switch "fromPromise" to "from" --> fromPromise does not exist on type Observable
2) Fix 'application/json' quote
import { Observable, from } from 'rxjs';
import { switchMap } from 'rxjs/operators';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService){}
intercept(req: HttpRequest, next: HttpHandler): Observable> {
return from(this.authService.getToken()).pipe(
switchMap(token => {
const headers = req.headers
.set('Authorization', 'Bearer ' + token)
.append('Content-Type', 'application/json');
const reqClone = req.clone({
headers
});
return next.handle(reqClone);
}));
}
}
My version of RxJs: "6.2.2" Hope that helped !