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

前端 未结 1 778
余生分开走
余生分开走 2021-01-26 19:25

I have a controller in nestjs as below

import * as dialogflow from \'dialogflow-fulfillment\';
import { Request, Response } from \'express\';

@Cont         


        
相关标签:
1条回答
  • 2021-01-26 19:51

    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);
    
    }
    
    0 讨论(0)
提交回复
热议问题