如何将上下文注入到HttpModule

时间:2019-05-09 07:05:42

标签: nestjs

我想在注册时将context.heanders注入Httpmodule。如何?

nestjs 6.0

我创建一个如下所示的全局模块,我想在此位置添加context.header

@Global()
@Module({
  imports: [
    HttpModule.register({
      timeout: 20000,
     // (how to get context.header in this place )
      // headers: {
      //   'x-sec-profile': '281567',
      //   'accessToken': 'f647e353790fd5c54dbd8c06be4575a58f369476a569c71a',
      // },
    }),
  ],
})
export class AxiosModule {}

这是cats.resolver.ts

@Resolver(Cat)
export class CatsResolver {
  constructor(private readonly catsService: CatsService) {}
  @Query(returns => Cat,  { name: 'cat' })
  async findDetail(@Args('id') id: number, @Context('headers') headers) {
    console.log(arguments.length)
    return await this.catsService.findDetail(id)
  }
}

使用cats.service.ts

@Injectable()
export class CatsService {
  constructor(private readonly httpService: HttpService) { }

  async findDetail(id) {
    const a = await this.httpService.get('https://yuangong.com/api/contract-web/contract/contract-detail',
    {params: { contractId: 1000320 } }).toPromise()
    return {
      contractId: a.data.data.id,
    }
  }
}

0 个答案:

没有答案