openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Support for using custom instance of axios

Open rchl opened this issue 3 years ago • 18 comments

When using the httpClient: 'axios' option, I would like to be able to also use a custom axios instance for making the requests.

The reason for that is that I would like to use an Axios instance configured to my liking (custom interceptors, different timeout value, etc...).

Looking at the code it seems that it would be pretty easy to support that and I could contribute if the feature request is accepted.

rchl avatar Jan 26 '22 14:01 rchl

How are you configuring Axios, globally or per instance? because you can already set global interceptors.

ferdikoomen avatar Jan 26 '22 14:01 ferdikoomen

I'm creating a custom Axios instance and configuring it.

rchl avatar Jan 26 '22 14:01 rchl

Hi there,

I may be interested in using a custom instance of Axios to override configuration of httpsAgent in order to get rid of the error ERR_CERT_AUTHORITY_INVALID when using auto-signed SSL certificates.

const httpsAgent = new Agent({
  rejectUnauthorized: false,
});

const instance = axios.create({ httpsAgent });

Not sure this is the best way but I guess it would be nice if we could provide a custom instance.

PS: Apologize if this is not the right place to put that message, as the title is a little bit generic, I wanted to add my use case.

Or maybe there is an existing way to do this in the parameters ?

Wish you a nice day 🌞

smarlhens avatar Jan 28 '22 15:01 smarlhens

You can specify your axios interceptors and defaults before axios is used. There isn't a way for different settings for different named clients so this won't work for some use cases.

https://share.busheezy.dev/bOEKbJ.png

busheezy avatar Feb 09 '22 14:02 busheezy

That applies globally. It's possible to apply custom settings to individual instances of axios. There would just need to be a way to provide a custom instance.

rchl avatar Feb 09 '22 14:02 rchl

I second @rchl, axios without a way to custom the current axiosInstance is not meaningful. But, I understand that the encapsulation HttpRequest doesn't want to carry custom behaviour from axios. So, what is the best approach to use AxiosInstance vs axios global?

IniterWorker avatar Apr 03 '22 21:04 IniterWorker

Having an option to use custom instances would be great. I actually have a problem where a can't use interceptors nor set language headers in a nuxt app because the framework encapsulates the options from the composables.

Unless I'm doing something very wrong, I can't have multiple configs working for all parts of my app without dropping this lib entirely

AllanPinheiroDeLima avatar May 30 '22 11:05 AllanPinheiroDeLima

I ran into this as well. In addition to the general reasons for supporting a custom axios instance mentioned above (i.e. don't mutate global state that can be used by any part of the program), having a custom axios instance is required if you want to automate JWT token refresh (using interceptors) in case the current token has expired. You can't have the global instance put your credentials on all requests and not all request are hitting the same server (and thus need different credentials).

tibbe avatar Aug 12 '22 11:08 tibbe

Makes sense to me as well. I think you also want the generated code to export the created axios instance so that your consumer code could set the default config for the instance via myAxiosInstance.default without messing with the generated code.

sam-mfb avatar Aug 12 '22 11:08 sam-mfb