首页 > 解决方案 > NestJS HttpService - 如何转换响应数据?

问题描述

所以我分别是 NestJS 和 RxJS 的新手,所以我知道这就是我苦苦挣扎的地方。我正在做一个附带项目,目的是了解更多关于两者的信息,所以我对在 HttpService 响应上调用 toPromise() 不感兴趣(我在相关数量的在线示例中看到)。相反,我希望看到如何使用 RxJS 来处理响应。

理想情况下,如果响应成功返回,我想从响应中提取有效负载,并在将其返回给调用者之前稍微修改它。

@Injectable
class TradierService {
    constructor(private readonly configService: ConfigService,
                private readonly httpService: HttpService) {}

    getQuote(symbol: string): Quote {
        const query = qs.stringify({ symbols: symbol });
        // Obviously has error below
        return this.httpService.get<QuotesWrapper>(`/markets/quotes?${query}`, this.getConfig());
    }

    private getConfig(): AxiosRequestConfig {
        return {
            baseURL: this.configService.get<string>(TRADIER_BASE_URL),
            headers: {
                Accept: 'application/json',
                Authorization: `Bearer ${this.configService.get<string>(TRADIER_API_KEY)}`
            }
        };
    }
}

标签: rxjsnestjs

解决方案


推荐阅读