orval icon indicating copy to clipboard operation
orval copied to clipboard

Pass in custom axios instance

Open AleskiWeb opened this issue 3 years ago • 2 comments

Hey, first of all, this package seems really awesome and we currently have a very large manually maintained api package that has all of our typesafe code, which this package could completely make redundant, which would be a huge time saver!

I am struggling a bit with the docs here - If I wanted to have each of the endpoint-generated axios-functions to have a custom axios instance passed to it as a parameter, is that the job for a custom mutator?

What I am going for is something like this:

export const getPets = (axiosInstance: axiosInstance) => {
  /**
   * List all pets.
   * @summary GET pets
   */
  const petsIndex = <TData = AxiosResponse<PetsIndex200>>(
    params?: PetsIndexParams,
    options?: AxiosRequestConfig,
  ): Promise<TData> => {
    return axiosInstance.get(`/pets`, {
      ...options,
      params: { ...params, ...options?.params },
    });
  };
}

where axiosInstance is something I can pass into the request

Thanks

AleskiWeb avatar Nov 11 '22 16:11 AleskiWeb

Hello @AleskiWeb, yes it's the job of a mutator. You also have some examples here

anymaniax avatar Nov 14 '22 14:11 anymaniax

@anymaniax I know I'm a bit late on this but the solution provided by @AleskiWeb is way better considering it doesn't involve instantiating a global variable for the axios instance. That way you can have multiple clients with different authorization token for example.

jdrouet avatar Oct 02 '25 15:10 jdrouet