How to create custom provider for third-party library in nestjs with access to request and response

牧云@^-^@ 提交于 2019-12-02 07:26:41

In this case I'd suggest to write a facade (wrapper) around the library. The facade does not need to be unit tested since it simply propagates the parameters and does not contain logic (you could write a "facade test" instead which essentially tests the library code, I'd say an integration test is a better fit):

@Injectable()
export class WebHookClientFacade {
  create(request, response) {
    return new dialogflow.WebhookClient({ request, response });
  }
}

And use the facade in your controller that is now testable:

constructor(private webHookClientBuilder: WebHookClientFacade) {}

@Post()
async fulfill(@Req() request: Request, @Res() response: Response) {
    const agent = this.webHookClientBuilder.create(request, response);

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!