使用 aws-amplify 承诺 Observable

问题描述

我将 angular 11 与 Spring Boot RESTful 服务一起使用。我已经实现了一个角度应用程序。现在我需要将它迁移到 AWS。所以我使用 AWS 的 Amplify。当我们执行 get(),put(),post() 方法时,Amplify 总是返回 promise。

直到现在我都使用 Observable,而且它工作正常

getAccount(page: any,size: any,accountIds?: string[]): Observable<IPageble<IAccount>> {
    // parameter and headers
    
    return this.http.get<IPageble<IAccount>>(this.apiUrl + '/api/v1/accounts',{
      params: parameters,headers: this.httpOptions.headers,});
}

现在我使用像 API.get() 这样返回 Promise 的放大方法。所以我使用 from() 方法使其成为 Observable。

getAccount(page: any,accountIds?: string[]): Observable<IPageble<IAccount>> {  

    // config has parameters and headers
    return from(API.get('execute-api',this.apiUrl + '/api/v1/accounts',config))
}

这也工作正常。但是我的 HttpInteceptor 停止工作了。我的拦截

export class HttpConfigInterceptor implements HttpInterceptor {
  constructor() {}
  intercept( request: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {
    
    return from(
      Auth.currentSession()
        .then((data) =>  data.getAccesstoken().getJwtToken())
        .catch((err) => '')
    ).pipe(
      switchMap((token) => {        
        request = request.clone({
          headers: request.headers.set('accesstoken',token),});
        return next.handle(request).pipe(
          map((event: HttpEvent<any>) => {
            if (event instanceof HttpResponse) {
                // Few logic
            }
            return event;
          }),catchError((err: HttpErrorResponse) => {
            // Few logic
            );

            return throwError(err);
          }),finalize(() => {
           // Few logic
          })
        );
      })
    );
  }
}

这个拦截器不请求或接收任何东西。当我使用 from(API.get('execute-api',config)).subscribe(res=> console.log(res)) 时。

{
    config: {} // aws amplify configuration
    data: '' // this has my data
    headers: {content-type: "application/json"}
    request: ''
    status: 204
    statusText: ""
}

这个对象不同于 observable。有什么方法可以在不影响当前代码的情况下使用放大?或者有没有办法在不影响当前代码的情况下解决这个问题?

提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)