orval icon indicating copy to clipboard operation
orval copied to clipboard

Pass an additional option object in fetcher function when using custom axios instance

Open julianobrasil opened this issue 1 year ago • 5 comments

What are the steps to reproduce this issue?

  1. Follow de docs to create an axios custom instance, with an optional 2nd parameter
  2. for the client, choose swr

What happens?

The second optional parameter is not added to the generated fetchers.

orval.config.js

module.exports = {
  test: {
    output: {
      ....
      client: 'swr',
      override: {
        mutator: {
          path: './custom-instance.ts',
         name: 'customInstance',
       },
       ...
      },
    },
  }
}

The custom instance:

import Axios, { AxiosRequestConfig } from 'axios';
 
 export const AXIOS_INSTANCE = Axios.create(); 
 
 export const customInstance = <T>(
   config: AxiosRequestConfig,
   options?: AxiosRequestConfig,
 ): Promise<T> => {
 // ...
}

The generated fecher:

export const getMyEndpointMutationFetcher = ( ) => {
  return (_: string, { arg }: { arg: Arguments }): Promise<MyEndpointResponse> => {
    return myEndpoint(arg as MyEndpointRequest);
  }
}

What were you expecting to happen?

The generated fetcher should allow for optional options and the options should be passed as a second parameter of the returning function call:

export const getMyEndpointMutationFetcher = (options?: AxiosRequestConfig) => {
  return (_: string, { arg }: { arg: Arguments }): Promise<MyEndpointResponse> => {
    return myEndpoint(arg as MyEndpointRequest, options);
  }
}

Any logs, error output, etc?

No

Any other comments?

What versions are you using?

Operating System: MacOS 14.2.1 Package Version: 6.25.0 Browser Version: The problem is not the browser, but... Chrome Version 121.0.6167.184 (Official Build) (arm64)

julianobrasil avatar Feb 22 '24 18:02 julianobrasil

This issue is similar to #379, but while that one was just a matter of lacking documentation, this one is a real bug.

julianobrasil avatar Feb 22 '24 18:02 julianobrasil

The problem seems to be that the library assumes that if the mutator is overridden, it doesn't use Axios, therefore it doesn't add the arguments or the imports:

Here:

https://github.com/anymaniax/orval/blob/15e903320963c06218566c24818c7a6a40e26c82/packages/swr/src/index.ts#L695-L696

And here (my use case uses split-tags, so that's how I got here):

https://github.com/anymaniax/orval/blob/15e903320963c06218566c24818c7a6a40e26c82/packages/core/src/writers/split-tags-mode.ts#L71

Maybe in more places too. One possible solution would be to add a isAxios option to the override.mutator attribute:

module.exports = {
  test: {
    output: {
      ....
      client: 'swr',
      override: {
        mutator: {
          path: './custom-instance.ts',
          name: 'customInstance',
          isAxios: true // 👈 This is a fix, not sure if it's acceptable
       },
       ...
      },
    },
  }
}

Then we could (pseudo code):

const swrMutationFetcherOptions = 
   (!mutator || (mutator && override?.mutator?.isAxios)) && isRequestOptions ? 'options?: AxiosRequestConfig' : ''; 
  hasGlobalMutator: !!output.override.mutator && !output.override.mutator.isAxios, 

julianobrasil avatar Feb 22 '24 20:02 julianobrasil

I’ve encountered the same issue, and for now, I’ve downgraded to version 6.23.0.

Junlol avatar Mar 05 '24 02:03 Junlol

Thank you for made this report. I understood that when using a custom instance of Axios, the settings of Axios cannot be passed to the custom instance via hooks. I think a good solution is to specify a custom instance using Axios via the orvrride property and i prefer to use isAxios: true over clinet: 'axios' because the custom instance may have additional clients in the future. You can submit a PR and I'll submit in turn.

soartec-lab avatar Mar 05 '24 09:03 soartec-lab

Is this the same as #959 ???

melloware avatar May 16 '24 00:05 melloware