gpt4 book ai didi

单个序列中的 Angular、ngrx/store、store select 和异步调用

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:52 24 4
gpt4 key购买 nike

有谁知道是否有机会从商店获取值(value)然后调用 angular http 服务?请参见下面的示例。提前致谢。

我有一个异步方法的服务。在这种方法中,我调用了 angular http 服务,但在我调用 angular http 服务之前,我想从商店中检索一些值(oauth 访问 token )。问题是我在商店中存储了 oauth token ,我必须将 token 添加到请求中。如果服务器响应 401 错误,我刷新 token 并重复请求或使用新 token 的请求。

@Injectable()
export class Interceptor {
public get(request: Request): Observable<any> {
return this.store.select((s) => s.session.accessToken)
// tried .switchMap, it wasn't working for multiple requests
// .flatMap also is not working
// commented below is working but I don't think that it is right solution
// return Observable.of(request)
// .withLatestFrom(this.store.select((s) => {
// return s.session.accessToken
// }))
// .flatMap(([config, accessToken]) => { ... })
.flatMap((accessToken) => {
request.headers.set("Authorization", `Bearer ${accessToken}`);

return this.http.request(request);
})
.map((response) => {
return response.json();
})
.catch((response) => {
return this.handleError(response, request);
});
}

// handle errors
private handleError(response: Response, request: Request): Observable<Response> {
// handle outdated api tokens
if (response.status === HttpStatusesEnum.Unauthorized) {
const deferredRequest = new DeferredRequestModel({ request });

// appends HTTP request configuration object with deferred response
// attached to buffer
this.buffer(deferredRequest );

return deferredRequest.observable;
}

// handle server error
if (response.status === HttpStatusesEnum.InternalServerError) {
...
}

...

return Observable.throw(response);
}
}

export class DeferredRequestModel {
public observable: AsyncSubject<Response>;
public request: Request;

constructor(obj?: Partial<DeferredRequestModel>) {
this.observable = new AsyncSubject<Response>();
this.request = obj && obj.request || null;
}

public reject(error?: any): void {
this.end({ isValue: false, value: error });
}

public resolve(value?: any): void {
this.end({ isValue: true, value });
}

private end(result: any): void {
if (this.observable.isStopped) {
console.warn("Deferred already resloved/rejected.");
return;
}

if (result.isValue) {
this.observable.next(result.value);
this.observable.complete();
} else {
this.observable.error(result.error);
}
}
}

然后当我调用一些服务时,它会通过拦截器发送请求。

export class SomeEffect {
constructor(private someService: SomeService) {}

@Effect()
public someMethod(): Observable<Action> {
return this.actions$.ofType(SOME_ACTION_GET)
.map((action: SomeActions.Get) => action.payload)
.switchMap((someObj) => {
// someMethod call interceptor inside someService, it won't call angular
// http service directly
return this.someService.someMethod(someObj);
})
.map((someResult) => {
return new SomeActions.GetSuccess(someResult);
});
}
}

我试过在拦截器中使用 switchMap,但如果多次调用拦截器,它就不起作用。

最佳答案

如果您只对商店中包含的最后一个值感兴趣,您可以尝试以下操作:

public someMethod() {
return this.store.select(s => s.config)
.switchMap(config => {
return this.http.request(config);
});
}

虽然我会说只有在奇怪的情况下我才会将 Store 实例注入(inject)到服务中。也许您可以发布有关用例的更多信息

关于单个序列中的 Angular、ngrx/store、store select 和异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44439803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com