首页 > 解决方案 > AutoRest 的 TypeScript 客户端的 HttpInterceptor

问题描述

我对 TypeScript 和 Angular 还是很陌生。我有http拦截器:

export class HttpTokenInterceptor implements HttpInterceptor {
  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const headersConfig = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    };

    headersConfig['TEST'] = `TEST`;

    const request = req.clone({ setHeaders: headersConfig });
    return next.handle(request);
  }
}

当我通过 HttpClient 从 '@angular/common/http' 发送 GET 请求时,let resp = await this.http.get('http://localhost').toPromise();我可以在标题中看到我的 TEST 标题项。

我还使用 autorest 为我的休息服务生成 TypeScript 客户端(@microsoft.azure/autorest-core: 2.0.4302),并且当我调用生成的客户端自定义 TEST 标头的任何方法时,不会将其添加到查询中。
我认为 AutoRest 也通过相同的 HttpClient 生成 HTTP 查询,应该由HttpInterceptor......
也许我这样做完全错误,它应该以这种方式工作吗?或者有没有办法拦截由 AutoRest 生成的代码发出的 http 请求?

标签: angulartypescriptautorest

解决方案


推荐阅读