我竭力要實現打字稿& RxJs一個函數,返回觀察數據的從後端。我需要根據用戶角色查詢不同的端點。我正在使用angular-JWT進行身份驗證。我有兩個問題:查詢不同的端點
目前代碼編譯很好,但第二對請求不斷取消。爲什麼?
有沒有比zip + throw + catch更好的方法呢?我一直在搜索rxjs文檔,但什麼都沒發現。
代碼:
public getData(): Observable<Data> {
return Observable.zip(
this.userService.getCurrentUser(),
this.authHttp.get(this.adminUrl),
(user, adminResponse) => {
if (user.role == 'admin') {
return adminResponse.json() as BackendResponse
} else {
throw "You are not an admin";
}
})
.catch(error => {
return Observable.zip(
this.userService.getCurrentUser(),
this.authHttp.get(this.normalUrl),
(user, normalResponse) => {
if (user.role == 'normal') {
return normalResponse.json() as BackendResponse
} else {
throw "You are not allowed to get this resource";
}
})
})
.catch(error => {
return Observable.throw(error)
});
}
管理端點返回兩個管理員和普通用戶(需要在我的項目)的一些數據,但如果他們詢問他們的特殊的API標準的用戶可以得到更多的數據。
服務器應該拒絕任何未經授權的請求。 –