如何在TypeScript中使用Swagger UI的responseInterceptor?

问题描述 投票:1回答:1

我正在使用Swagger UI,以下代码在带有React的jsx中可以正常工作。

<SwaggerUI
    url={SPEC_FILE}
    responseInterceptor={(response: Response) => {
      //console.log("response ", response);
      if (response.url === REQUEST_URL && response.status === 200) {
        setData(response.data);
      }
    }}
  />

然而,当在React与TypeScript中使用以上代码时,在responseInterceptor处出现以下错误:

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<SwaggerUIProps>): SwaggerUI', gave the following error.
    Type '(response: Response) => void' is not assignable to type '(res: Response) => Response | Promise<Response>'.
      Types of parameters 'response' and 'res' are incompatible.
        Type 'Response' is missing the following properties from type 'Response': headers, ok, redirected, status, and 12 more.
  Overload 2 of 2, '(props: SwaggerUIProps, context?: any): SwaggerUI', gave the following error.
    Type '(response: Response) => void' is not assignable to type '(res: Response) => Response | Promise<Response>'.ts(2769)
index.d.ts(27, 5): The expected type comes from property 'responseInterceptor' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<SwaggerUI> & Readonly<SwaggerUIProps> & Readonly<...>'
index.d.ts(27, 5): The expected type comes from property 'responseInterceptor' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<SwaggerUI> & Readonly<SwaggerUIProps> & Readonly<...>'

任何人都有想法如何在TypeScript中使用Swagger UI的responseInterceptor

reactjs typescript swagger-ui
1个回答
0
投票

打字稿小组的好人帮助了我,所以我想在这里分享答案,我只需要返回答复。

<SwaggerUI
    url="https://api.mna.dev.spsapps.net/swagger/?format=openapi"
    responseInterceptor={response => {
      if (response.status === 200) {
        console.log(response.data);
      }
      return response; // this is what i needed to do.
    }}
  />
© www.soinside.com 2019 - 2024. All rights reserved.