typescriptaxiosopenapiopenapi-generator

How do I add an interceptor to a typescript-axios client generated by an OpenAPI generator?


I'm using the typescript-axios client generated by OpenAPI and I need to log the requests made by the client. I know how to use interceptors with axios, but how can I get at the underlying axios instance?


Solution

  • The code generated by https://github.com/OpenAPITools/openapi-generator has a protected axios property.

    The following [partially quoted] block comes from an OpenAPITools/openapi-generator issue.

    The DefaultApi has a protected axios property which can be accessed

    const finnhubClient = new DefaultApi({
      apiKey,
    })
    
    finnhubClient.axios.interceptors.response.use(
      response => response,
      error => {
        console.log('error: ', error)
        throw error
      }
    )