使用rxjs的angular2 http中间件

使用rxjs的angular2 http中间件,angular,rxjs,Angular,Rxjs,我正在使用angular2,并且有一个ApiService。。。下面是完整的类 从'@ngrx/Store'导入{Store}; 从“@angular/core”导入{Injectable}; 从'@angular/Http'导入{Headers,RequestOptions,Http}; 从“../app.store”导入{AppState}; 从“rxjs/Observable”导入{Observable}; 从“../actions/AuthActions”导入{AuthActions}

我正在使用angular2,并且有一个
ApiService
。。。下面是完整的类

从'@ngrx/Store'导入{Store};
从“@angular/core”导入{Injectable};
从'@angular/Http'导入{Headers,RequestOptions,Http};
从“../app.store”导入{AppState};
从“rxjs/Observable”导入{Observable};
从“../actions/AuthActions”导入{AuthActions};
@可注射()
出口级服务{
私有currentState:AppState;
建造师(
私家店,
专用http:http
) {
//获取配置一次。。。
store.subscribe((状态:AppState)=>{
this.currentState=状态;
});
}
//公开的
/*
--职位
*/
post(路径:字符串,有效负载:对象){
让request=this.http.post(
此.getEndpoint(路径),
stringify(有效载荷),
this.getHeaderOptions('post'))
);
//不起作用…触发http两次
//此.响应数据包(请求);
返回请求;
}
/*
--得到
*/
获取(路径:字符串){
让request=this.http.get(
此.getEndpoint(路径),
this.getHeaderOptions('get'))
);
返回请求;
}
/*
--删除
*/
删除(路径:字符串){
让请求=this.http.delete(
此.getEndpoint(路径),
this.getHeaderOptions('delete')
);
返回请求;
}
//处理所有寻找坏东西的请求??
//如何订阅而不触发http 2x??
//私有响应数据包(请求:可观察){
//请求。订阅(
//(res)=>{},
//(错误)=>{
////捕获错误
//如果(错误状态===401){
////非正统
//this.store.dispatch({type:AuthActions.AUTHENTICATE\u DELETE});
//       }
//     },
//     () => {}
//   );
// }
//从配置文件获取端点
私有getEndpoint(路径:字符串){
如果(!this.currentState.config.file.endpoint){
抛出新错误('=============未设置配置===========');
}
返回此.currentState.config.file.endpoint+路径;
}
//根据请求类型组合正确的头对象
私有getHeaderOptions(类型:string='get'){
让headersObj:any={
“接受”:“应用程序/json”,
“X-Lc-Svc-Channel”:“Lc web产品”
};
开关(类型){
案例“post”:
headersObj['Content-Type']='application/json';
打破
违约:
打破
}
让token=this.currentState.auth.token;
如果(令牌){
headersObj.Authorization=令牌;
}
返回新的RequestOptions({headers:newheaders(headersObj)});
}

}
对于任何感兴趣的人,我目前已经解决了这个购买问题,将一个
捕获链接到每个请求类型。我相信有更好的方法。。。如果你能想出一个,我很想知道,但这是目前的工作

get(path:string) {

  let request = this.http.get(
    this.getEndpoint(path),
    this.getHeaderOptions('get')
  ).catch((err:any) => {
    if(err.status === 401) {
      this.store.dispatch({type:AuthActions.AUTHENTICATE_DELETE});
      return Observable.of('Not authenticated');
    }
    return err;
  });

  return request;
}

它被调用了两次,因为有两个
subscribe
调用:一个由
post
调用方调用,另一个在
responseMiddleware
中调用。
responseMiddleware
应该在
请求
上合成一些内容,并返回合成的可观察内容-应该通过
post
返回。然而,在我的脑海中,我想不出你是如何在错误处理中写作的——这是你的中间件似乎正在做的事情。谢谢你的回复。。。我想是因为有两个订阅。我想我知道怎么做你说的。。。在内部映射中使用mergeMap&catch可能会出现错误吗?